Skip to content

Commit 795cb75

Browse files
dahlbyknulltoken
authored andcommitted
Add Submodule bindings
1 parent 2764e9d commit 795cb75

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
@@ -1014,6 +1014,71 @@ internal delegate int git_status_cb(
10141014
[DllImport(libgit2)]
10151015
internal static extern int git_status_foreach(RepositorySafeHandle repo, git_status_cb cb, IntPtr payload);
10161016

1017+
[DllImport(libgit2)]
1018+
internal static extern int git_submodule_lookup(
1019+
out SubmoduleSafeHandle reference,
1020+
RepositorySafeHandle repo,
1021+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string name);
1022+
1023+
internal delegate int submodule_callback(
1024+
IntPtr sm,
1025+
IntPtr name,
1026+
IntPtr payload);
1027+
1028+
[DllImport(libgit2)]
1029+
internal static extern int git_submodule_foreach(
1030+
RepositorySafeHandle repo,
1031+
submodule_callback callback,
1032+
IntPtr payload);
1033+
1034+
[DllImport(libgit2)]
1035+
internal static extern int git_submodule_add_to_index(
1036+
SubmoduleSafeHandle submodule,
1037+
bool write_index);
1038+
1039+
[DllImport(libgit2)]
1040+
internal static extern int git_submodule_save(
1041+
SubmoduleSafeHandle submodule);
1042+
1043+
[DllImport(libgit2)]
1044+
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8NoCleanupMarshaler))]
1045+
internal static extern string git_submodule_path(
1046+
SubmoduleSafeHandle submodule);
1047+
1048+
[DllImport(libgit2)]
1049+
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8NoCleanupMarshaler))]
1050+
internal static extern string git_submodule_url(
1051+
SubmoduleSafeHandle submodule);
1052+
1053+
[DllImport(libgit2)]
1054+
internal static extern OidSafeHandle git_submodule_index_id(
1055+
SubmoduleSafeHandle submodule);
1056+
1057+
[DllImport(libgit2)]
1058+
internal static extern OidSafeHandle git_submodule_head_id(
1059+
SubmoduleSafeHandle submodule);
1060+
1061+
[DllImport(libgit2)]
1062+
internal static extern OidSafeHandle git_submodule_wd_id(
1063+
SubmoduleSafeHandle submodule);
1064+
1065+
[DllImport(libgit2)]
1066+
internal static extern SubmoduleIgnore git_submodule_ignore(
1067+
SubmoduleSafeHandle submodule);
1068+
1069+
[DllImport(libgit2)]
1070+
internal static extern SubmoduleUpdate git_submodule_update(
1071+
SubmoduleSafeHandle submodule);
1072+
1073+
[DllImport(libgit2)]
1074+
internal static extern bool git_submodule_fetch_recurse_submodules(
1075+
SubmoduleSafeHandle submodule);
1076+
1077+
[DllImport(libgit2)]
1078+
internal static extern int git_submodule_status(
1079+
out SubmoduleStatus status,
1080+
SubmoduleSafeHandle submodule);
1081+
10171082
[DllImport(libgit2)]
10181083
internal static extern int git_tag_create(
10191084
out GitOid oid,

LibGit2Sharp/Core/Proxy.cs

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

19131913
#endregion
19141914

1915+
#region git_submodule_
1916+
1917+
/// <summary>
1918+
/// Returns a handle to the corresponding submodule,
1919+
/// or an invalid handle if a submodule is not found.
1920+
/// </summary>
1921+
public static SubmoduleSafeHandle git_submodule_lookup(RepositorySafeHandle repo, string name)
1922+
{
1923+
using (ThreadAffinity())
1924+
{
1925+
SubmoduleSafeHandle reference;
1926+
var res = NativeMethods.git_submodule_lookup(out reference, repo, name);
1927+
1928+
switch (res)
1929+
{
1930+
case (int)GitErrorCode.NotFound:
1931+
case (int)GitErrorCode.Exists:
1932+
case (int)GitErrorCode.OrphanedHead:
1933+
return null;
1934+
1935+
default:
1936+
Ensure.ZeroResult(res);
1937+
return reference;
1938+
}
1939+
}
1940+
}
1941+
1942+
public static ICollection<TResult> git_submodule_foreach<TResult>(RepositorySafeHandle repo, Func<IntPtr, IntPtr, TResult> resultSelector)
1943+
{
1944+
return git_foreach(resultSelector, c => NativeMethods.git_submodule_foreach(repo, (x, y, p) => c(x, y, p), IntPtr.Zero));
1945+
}
1946+
1947+
public static void git_submodule_add_to_index(SubmoduleSafeHandle submodule, bool write_index)
1948+
{
1949+
using (ThreadAffinity())
1950+
{
1951+
var res = NativeMethods.git_submodule_add_to_index(submodule, write_index);
1952+
Ensure.ZeroResult(res);
1953+
}
1954+
}
1955+
1956+
public static void git_submodule_save(SubmoduleSafeHandle submodule)
1957+
{
1958+
using (ThreadAffinity())
1959+
{
1960+
var res = NativeMethods.git_submodule_save(submodule);
1961+
Ensure.ZeroResult(res);
1962+
}
1963+
}
1964+
1965+
public static string git_submodule_path(SubmoduleSafeHandle submodule)
1966+
{
1967+
return NativeMethods.git_submodule_path(submodule);
1968+
}
1969+
1970+
public static string git_submodule_url(SubmoduleSafeHandle submodule)
1971+
{
1972+
return NativeMethods.git_submodule_url(submodule);
1973+
}
1974+
1975+
public static ObjectId git_submodule_index_id(SubmoduleSafeHandle submodule)
1976+
{
1977+
return NativeMethods.git_submodule_index_id(submodule).MarshalAsObjectId();
1978+
}
1979+
1980+
public static ObjectId git_submodule_head_id(SubmoduleSafeHandle submodule)
1981+
{
1982+
return NativeMethods.git_submodule_head_id(submodule).MarshalAsObjectId();
1983+
}
1984+
1985+
public static ObjectId git_submodule_wd_id(SubmoduleSafeHandle submodule)
1986+
{
1987+
return NativeMethods.git_submodule_wd_id(submodule).MarshalAsObjectId();
1988+
}
1989+
1990+
public static SubmoduleIgnore git_submodule_ignore(SubmoduleSafeHandle submodule)
1991+
{
1992+
return NativeMethods.git_submodule_ignore(submodule);
1993+
}
1994+
1995+
public static SubmoduleUpdate git_submodule_update(SubmoduleSafeHandle submodule)
1996+
{
1997+
return NativeMethods.git_submodule_update(submodule);
1998+
}
1999+
2000+
public static bool git_submodule_fetch_recurse_submodules(SubmoduleSafeHandle submodule)
2001+
{
2002+
return NativeMethods.git_submodule_fetch_recurse_submodules(submodule);
2003+
}
2004+
2005+
public static SubmoduleStatus git_submodule_status(SubmoduleSafeHandle submodule)
2006+
{
2007+
using (ThreadAffinity())
2008+
{
2009+
SubmoduleStatus status;
2010+
var res = NativeMethods.git_submodule_status(out status, submodule);
2011+
Ensure.ZeroResult(res);
2012+
return status;
2013+
}
2014+
}
2015+
2016+
#endregion
2017+
19152018
#region git_tag_
19162019

19172020
public static ObjectId git_tag_create(

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
<Compile Include="ReflogCollection.cs" />
7777
<Compile Include="Core\PathCase.cs" />
7878
<Compile Include="MatchedPathsAggregator.cs" />
79+
<Compile Include="Core\Handles\SubmoduleSafeHandle.cs" />
7980
<Compile Include="Network.cs" />
8081
<Compile Include="Core\GitRemoteHead.cs" />
8182
<Compile Include="ReflogEntry.cs" />
@@ -84,6 +85,10 @@
8485
<Compile Include="OrphanedHeadException.cs" />
8586
<Compile Include="StashCollection.cs" />
8687
<Compile Include="ExplicitPathsOptions.cs" />
88+
<Compile Include="SubmoduleExtensions.cs" />
89+
<Compile Include="SubmoduleIgnore.cs" />
90+
<Compile Include="SubmoduleStatus.cs" />
91+
<Compile Include="SubmoduleUpdate.cs" />
8792
<Compile Include="UnmergedIndexEntriesException.cs" />
8893
<Compile Include="Commit.cs" />
8994
<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)