Skip to content

Commit 4ef08c2

Browse files
committed
Add Submodule bindings
1 parent ae3ad37 commit 4ef08c2

File tree

8 files changed

+355
-0
lines changed

8 files changed

+355
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace LibGit2Sharp.Core.Handles
2+
{
3+
internal class SubmoduleSafeHandle : NotOwnedSafeHandleBase
4+
{
5+
}
6+
}

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,71 @@ internal delegate int git_status_cb(
970970
[DllImport(libgit2)]
971971
internal static extern int git_status_foreach(RepositorySafeHandle repo, git_status_cb cb, IntPtr payload);
972972

973+
[DllImport(libgit2)]
974+
internal static extern int git_submodule_lookup(
975+
out SubmoduleSafeHandle reference,
976+
RepositorySafeHandle repo,
977+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string name);
978+
979+
internal delegate int submodule_callback(
980+
IntPtr sm,
981+
IntPtr name,
982+
IntPtr payload);
983+
984+
[DllImport(libgit2)]
985+
internal static extern int git_submodule_foreach(
986+
RepositorySafeHandle repo,
987+
submodule_callback callback,
988+
IntPtr payload);
989+
990+
[DllImport(libgit2)]
991+
internal static extern int git_submodule_add_to_index(
992+
SubmoduleSafeHandle submodule,
993+
bool write_index);
994+
995+
[DllImport(libgit2)]
996+
internal static extern int git_submodule_save(
997+
SubmoduleSafeHandle submodule);
998+
999+
[DllImport(libgit2)]
1000+
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8NoCleanupMarshaler))]
1001+
internal static extern string git_submodule_path(
1002+
SubmoduleSafeHandle submodule);
1003+
1004+
[DllImport(libgit2)]
1005+
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8NoCleanupMarshaler))]
1006+
internal static extern string git_submodule_url(
1007+
SubmoduleSafeHandle submodule);
1008+
1009+
[DllImport(libgit2)]
1010+
internal static extern OidSafeHandle git_submodule_index_id(
1011+
SubmoduleSafeHandle submodule);
1012+
1013+
[DllImport(libgit2)]
1014+
internal static extern OidSafeHandle git_submodule_head_id(
1015+
SubmoduleSafeHandle submodule);
1016+
1017+
[DllImport(libgit2)]
1018+
internal static extern OidSafeHandle git_submodule_wd_id(
1019+
SubmoduleSafeHandle submodule);
1020+
1021+
[DllImport(libgit2)]
1022+
internal static extern SubmoduleIgnore git_submodule_ignore(
1023+
SubmoduleSafeHandle submodule);
1024+
1025+
[DllImport(libgit2)]
1026+
internal static extern SubmoduleUpdate git_submodule_update(
1027+
SubmoduleSafeHandle submodule);
1028+
1029+
[DllImport(libgit2)]
1030+
internal static extern bool git_submodule_fetch_recurse_submodules(
1031+
SubmoduleSafeHandle submodule);
1032+
1033+
[DllImport(libgit2)]
1034+
internal static extern int git_submodule_status(
1035+
out SubmoduleStatus status,
1036+
SubmoduleSafeHandle submodule);
1037+
9731038
[DllImport(libgit2)]
9741039
internal static extern int git_tag_create(
9751040
out GitOid oid,

LibGit2Sharp/Core/Proxy.cs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,6 +1845,109 @@ public static ICollection<TResult> git_status_foreach<TResult>(RepositorySafeHan
18451845

18461846
#endregion
18471847

1848+
#region git_submodule_
1849+
1850+
/// <summary>
1851+
/// Returns a handle to the corresponding submodule,
1852+
/// or an invalid handle if a submodule is not found.
1853+
/// </summary>
1854+
public static SubmoduleSafeHandle git_submodule_lookup(RepositorySafeHandle repo, string name)
1855+
{
1856+
using (ThreadAffinity())
1857+
{
1858+
SubmoduleSafeHandle reference;
1859+
var res = NativeMethods.git_submodule_lookup(out reference, repo, name);
1860+
1861+
switch (res)
1862+
{
1863+
case (int)GitErrorCode.NotFound:
1864+
case (int)GitErrorCode.Exists:
1865+
case (int)GitErrorCode.OrphanedHead:
1866+
return null;
1867+
1868+
default:
1869+
Ensure.ZeroResult(res);
1870+
return reference;
1871+
}
1872+
}
1873+
}
1874+
1875+
public static ICollection<TResult> git_submodule_foreach<TResult>(RepositorySafeHandle repo, Func<IntPtr, IntPtr, TResult> resultSelector)
1876+
{
1877+
return git_foreach(resultSelector, c => NativeMethods.git_submodule_foreach(repo, (x, y, p) => c(x, y, p), IntPtr.Zero));
1878+
}
1879+
1880+
public static void git_submodule_add_to_index(SubmoduleSafeHandle submodule, bool write_index)
1881+
{
1882+
using (ThreadAffinity())
1883+
{
1884+
var res = NativeMethods.git_submodule_add_to_index(submodule, write_index);
1885+
Ensure.ZeroResult(res);
1886+
}
1887+
}
1888+
1889+
public static void git_submodule_save(SubmoduleSafeHandle submodule)
1890+
{
1891+
using (ThreadAffinity())
1892+
{
1893+
var res = NativeMethods.git_submodule_save(submodule);
1894+
Ensure.ZeroResult(res);
1895+
}
1896+
}
1897+
1898+
public static string git_submodule_path(SubmoduleSafeHandle submodule)
1899+
{
1900+
return NativeMethods.git_submodule_path(submodule);
1901+
}
1902+
1903+
public static string git_submodule_url(SubmoduleSafeHandle submodule)
1904+
{
1905+
return NativeMethods.git_submodule_url(submodule);
1906+
}
1907+
1908+
public static ObjectId git_submodule_index_id(SubmoduleSafeHandle submodule)
1909+
{
1910+
return NativeMethods.git_submodule_index_id(submodule).MarshalAsObjectId();
1911+
}
1912+
1913+
public static ObjectId git_submodule_head_id(SubmoduleSafeHandle submodule)
1914+
{
1915+
return NativeMethods.git_submodule_head_id(submodule).MarshalAsObjectId();
1916+
}
1917+
1918+
public static ObjectId git_submodule_wd_id(SubmoduleSafeHandle submodule)
1919+
{
1920+
return NativeMethods.git_submodule_wd_id(submodule).MarshalAsObjectId();
1921+
}
1922+
1923+
public static SubmoduleIgnore git_submodule_ignore(SubmoduleSafeHandle submodule)
1924+
{
1925+
return NativeMethods.git_submodule_ignore(submodule);
1926+
}
1927+
1928+
public static SubmoduleUpdate git_submodule_update(SubmoduleSafeHandle submodule)
1929+
{
1930+
return NativeMethods.git_submodule_update(submodule);
1931+
}
1932+
1933+
public static bool git_submodule_fetch_recurse_submodules(SubmoduleSafeHandle submodule)
1934+
{
1935+
return NativeMethods.git_submodule_fetch_recurse_submodules(submodule);
1936+
}
1937+
1938+
public static SubmoduleStatus git_submodule_status(SubmoduleSafeHandle submodule)
1939+
{
1940+
using (ThreadAffinity())
1941+
{
1942+
SubmoduleStatus status;
1943+
var res = NativeMethods.git_submodule_status(out status, submodule);
1944+
Ensure.ZeroResult(res);
1945+
return status;
1946+
}
1947+
}
1948+
1949+
#endregion
1950+
18481951
#region git_tag_
18491952

18501953
public static ObjectId git_tag_create(

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,17 @@
7171
<Compile Include="ConflictCollection.cs" />
7272
<Compile Include="Core\Handles\GitFetchSpecHandle.cs" />
7373
<Compile Include="Core\PathCase.cs" />
74+
<Compile Include="Core\Handles\SubmoduleSafeHandle.cs" />
7475
<Compile Include="Network.cs" />
7576
<Compile Include="Core\GitRemoteHead.cs" />
7677
<Compile Include="Stash.cs" />
7778
<Compile Include="StashOptions.cs" />
7879
<Compile Include="OrphanedHeadException.cs" />
7980
<Compile Include="StashCollection.cs" />
81+
<Compile Include="SubmoduleExtensions.cs" />
82+
<Compile Include="SubmoduleIgnore.cs" />
83+
<Compile Include="SubmoduleStatus.cs" />
84+
<Compile Include="SubmoduleUpdate.cs" />
8085
<Compile Include="UnmergedIndexEntriesException.cs" />
8186
<Compile Include="Commit.cs" />
8287
<Compile Include="CommitLog.cs" />

LibGit2Sharp/SubmoduleExtensions.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace LibGit2Sharp
2+
{
3+
/// <summary>
4+
/// Extensions related to submodules
5+
/// </summary>
6+
public static class SubmoduleExtensions
7+
{
8+
private const SubmoduleStatus UnmodifiedMask = ~(SubmoduleStatus.InConfig | SubmoduleStatus.InHead | SubmoduleStatus.InIndex | SubmoduleStatus.InWorkDir);
9+
private const SubmoduleStatus WorkDirDirtyMask = SubmoduleStatus.WorkDirFilesIndexDirty | SubmoduleStatus.WorkDirFilesModified | SubmoduleStatus.WorkDirFilesUntracked;
10+
11+
/// <summary>
12+
/// The submodule is unmodified.
13+
/// </summary>
14+
public static bool IsUnmodified(this SubmoduleStatus @this)
15+
{
16+
return (@this & UnmodifiedMask) == SubmoduleStatus.Unmodified;
17+
}
18+
19+
/// <summary>
20+
/// The submodule working directory is dirty.
21+
/// </summary>
22+
public static bool IsWorkingDirectoryDirty(this SubmoduleStatus @this)
23+
{
24+
return (@this & WorkDirDirtyMask) != SubmoduleStatus.Unmodified;
25+
}
26+
}
27+
}

LibGit2Sharp/SubmoduleIgnore.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace LibGit2Sharp
2+
{
3+
/// <summary>
4+
/// Values that could be specified for how closely to examine the
5+
/// working directory when getting submodule status.
6+
/// </summary>
7+
public enum SubmoduleIgnore
8+
{
9+
/// <summary>
10+
/// Reset to the last saved ignore rule.
11+
/// </summary>
12+
Default = -1,
13+
/// <summary>
14+
/// Any change or untracked == dirty
15+
/// </summary>
16+
None = 0,
17+
/// <summary>
18+
/// Dirty if tracked files change
19+
/// </summary>
20+
Untracked = 1,
21+
/// <summary>
22+
/// Only dirty if HEAD moved
23+
/// </summary>
24+
Dirty = 2,
25+
/// <summary>
26+
/// Never dirty
27+
/// </summary>
28+
All = 3,
29+
}
30+
}

LibGit2Sharp/SubmoduleStatus.cs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using System;
2+
3+
namespace LibGit2Sharp
4+
{
5+
/// <summary>
6+
/// Calculated status of a submodule in the working directory considering the current <see cref = "Repository.Index" /> and the <see cref="Repository.Head" />.
7+
/// </summary>
8+
[Flags]
9+
public enum SubmoduleStatus
10+
{
11+
/// <summary>
12+
/// No submodule changes detected.
13+
/// </summary>
14+
Unmodified = 0,
15+
16+
/// <summary>
17+
/// Superproject head contains submodule.
18+
/// </summary>
19+
/// <remarks>Can be returned even if ignore is set to "ALL".</remarks>
20+
InHead = (1 << 0),
21+
/// <summary>
22+
/// Superproject index contains submodule.
23+
/// </summary>
24+
/// <remarks>Can be returned even if ignore is set to "ALL".</remarks>
25+
InIndex = (1 << 1),
26+
/// <summary>
27+
/// Superproject gitmodules has submodule.
28+
/// </summary>
29+
/// <remarks>Can be returned even if ignore is set to "ALL".</remarks>
30+
InConfig = (1 << 2),
31+
/// <summary>
32+
/// Superproject working directory has submodule.
33+
/// </summary>
34+
/// <remarks>Can be returned even if ignore is set to "ALL".</remarks>
35+
InWorkDir = (1 << 3),
36+
37+
/// <summary>
38+
/// Submodule is in index, but not in head.
39+
/// </summary>
40+
/// <remarks>Can be returned unless ignore is set to "ALL".</remarks>
41+
IndexAdded = (1 << 4),
42+
/// <summary>
43+
/// Submodule is in head, but not in index.
44+
/// </summary>
45+
/// <remarks>Can be returned unless ignore is set to "ALL".</remarks>
46+
IndexDeleted = (1 << 5),
47+
/// <summary>
48+
/// Submodule in index and head don't match.
49+
/// </summary>
50+
/// <remarks>Can be returned unless ignore is set to "ALL".</remarks>
51+
IndexModified = (1 << 6),
52+
/// <summary>
53+
/// Submodule in working directory is not initialized.
54+
/// </summary>
55+
/// <remarks>Can be returned unless ignore is set to "ALL".</remarks>
56+
WorkDirUninitialized = (1 << 7),
57+
/// <summary>
58+
/// Submodule is in working directory, but not index.
59+
/// </summary>
60+
/// <remarks>Can be returned unless ignore is set to "ALL".</remarks>
61+
WorkDirAdded = (1 << 8),
62+
/// <summary>
63+
/// Submodule is in index, but not working directory.
64+
/// </summary>
65+
/// <remarks>Can be returned unless ignore is set to "ALL".</remarks>
66+
WorkDirDeleted = (1 << 9),
67+
/// <summary>
68+
/// Submodule in index and working directory head don't match.
69+
/// </summary>
70+
/// <remarks>Can be returned unless ignore is set to "ALL".</remarks>
71+
WorkDirModified = (1 << 10),
72+
73+
/// <summary>
74+
/// Submodule working directory index is dirty.
75+
/// </summary>
76+
/// <remarks>Can only be returned if ignore is "NONE" or "UNTRACKED".</remarks>
77+
WorkDirFilesIndexDirty = (1 << 11),
78+
/// <summary>
79+
/// Submodule working directory has modified files.
80+
/// </summary>
81+
/// <remarks>Can only be returned if ignore is "NONE" or "UNTRACKED".</remarks>
82+
WorkDirFilesModified = (1 << 12),
83+
84+
/// <summary>
85+
/// Working directory contains untracked files.
86+
/// </summary>
87+
/// <remarks>Can only be returned if ignore is "NONE".</remarks>
88+
WorkDirFilesUntracked = (1 << 13),
89+
}
90+
}

LibGit2Sharp/SubmoduleUpdate.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace LibGit2Sharp
2+
{
3+
/// <summary>
4+
/// Submodule update rule options.
5+
/// </summary>
6+
public enum SubmoduleUpdate
7+
{
8+
/// <summary>
9+
/// Reset to the last saved update rule.
10+
/// </summary>
11+
Default = -1,
12+
/// <summary>
13+
/// Checkout the commit recorded in the superproject.
14+
/// </summary>
15+
Checkout = 0,
16+
/// <summary>
17+
/// Rebase the current branch of the submodule onto the commit recorded in the superproject.
18+
/// </summary>
19+
Rebase = 1,
20+
/// <summary>
21+
/// Merge the commit recorded in the superproject into the current branch of the submodule.
22+
/// </summary>
23+
Merge = 2,
24+
/// <summary>
25+
/// Do not update the submodule.
26+
/// </summary>
27+
None = 3,
28+
}
29+
}

0 commit comments

Comments
 (0)