8000 Introduce Index.Conflicts.ResolvedConflicts · apfunk/libgit2sharp@86e561d · GitHub
[go: up one dir, main page]

Skip to content

Commit 86e561d

Browse files
Edward Thomsonnulltoken
Edward Thomson
authored andcommitted
Introduce Index.Conflicts.ResolvedConflicts
1 parent 5151c43 commit 86e561d

9 files changed

+324
-2
lines changed

LibGit2Sharp.Tests/ConflictFixture.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,17 @@ public void CanResolveConflictsByRemovingFromTheIndex(
6161

6262
Assert.Equal(existsBeforeRemove, File.Exists(fullpath));
6363
Assert.NotNull(repo.Index.Conflicts[filename]);
64+
Assert.Equal(0, repo.Index.Conflicts.ResolvedConflicts.Count());
6465

6566
repo.Index.Remove(filename, removeFromWorkdir);
6667

6768
Assert.Null(repo.Index.Conflicts[filename]);
6869
Assert.Equal(count - removedIndexEntries, repo.Index.Count);
6970
Assert.Equal(existsAfterRemove, File.Exists(fullpath));
7071
Assert.Equal(lastStatus, repo.Index.RetrieveStatus(filename));
72+
73+
Assert.Equal(1, repo.Index.Conflicts.ResolvedConflicts.Count());
74+
Assert.NotNull(repo.Index.Conflicts.ResolvedConflicts[filename]);
7175
}
7276
}
7377

LibGit2Sharp/ConflictCollection.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,19 @@ public virtual Conflict this[string path]
4040
}
4141
}
4242

43-
#region IEnumerable<IndexEntry> Members
43+
/// <summary>
44+
/// Get the <see cref="IndexReucEntryCollection"/> that contains
45+
/// the list of conflicts that have been resolved.
46+
/// </summary>
47+
public virtual IndexReucEntryCollection ResolvedConflicts
48+
{
49+
get
50+
{
51+
return new IndexReucEntryCollection(repo);
52+
}
53+
}
54+
55+
#region IEnumerable<Conflict> Members
4456

4557
private List<Conflict> AllConflicts()
4658
{
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace LibGit2Sharp.Core
5+
{
6+
[StructLayout(LayoutKind.Sequential)]
7+
internal class GitIndexReucEntry
8+
{
9+
public uint AncestorMode;
10+
public uint OurMode;
11+
public uint TheirMode;
12+
public GitOid AncestorId;
13+
public GitOid OurId;
14+
public GitOid TheirId;
15+
public IntPtr Path;
16+
}
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Runtime.InteropServices;
2+
3+
namespace LibGit2Sharp.Core.Handles
4+
{
5+
internal class IndexReucEntrySafeHandle : NotOwnedSafeHandleBase
6+
{
7+
public GitIndexReucEntry MarshalAsGitIndexReucEntry()
8+
{
9+
return handle.MarshalAs<GitIndexReucEntry>();
10+
}
11+
}
12+
}

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,18 @@ internal static extern int git_index_remove_bypath(
585585
IndexSafeHandle index,
586586
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath path);
587587

588+
589+
[DllImport(libgit2)]
590+
internal static extern uint git_index_reuc_entrycount(IndexSafeHandle handle);
591+
592+
[DllImport(libgit2)]
593+
internal static extern IndexReucEntrySafeHandle git_index_reuc_get_byindex(IndexSafeHandle handle, UIntPtr n);
594+
595+
[DllImport(libgit2)]
596+
internal static extern IndexReucEntrySafeHandle git_index_reuc_get_bypath(
597+
IndexSafeHandle handle,
598+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath path);
599+
588600
[DllImport(libgit2)]
589601
internal static extern int git_index_write(IndexSafeHandle index);
590602

LibGit2Sharp/Core/Proxy.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,26 @@ public static void git_index_remove_bypath(IndexSafeHandle index, FilePath path)
928928
}
929929
}
930930

931+
public static int git_index_reuc_entrycount(IndexSafeHandle index)
932+
{
933+
uint count = NativeMethods.git_index_reuc_entrycount(index);
934+
if ((long)count > int.MaxValue)
935+
{
936+
throw new LibGit2SharpException("Index REUC entry count exceeds size of int");
937+
}
938+
return (int)count;
939+
}
940+
941+
public static IndexReucEntrySafeHandle git_index_reuc_get_byindex(IndexSafeHandle index, UIntPtr n)
942+
{
943+
return NativeMethods.git_index_reuc_get_byindex(index, n);
944+
}
945+
946+
public static IndexReucEntrySafeHandle git_index_reuc_get_bypath(IndexSafeHandle index, string path)
947+
{
948+
return NativeMethods.git_index_reuc_get_bypath(index, path);
949+
}
950+
931951
public static void git_index_write(IndexSafeHandle index)
932952
{
933953
using (ThreadAffinity())

LibGit2Sharp/IndexReucEntry.cs

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Globalization;
4+
using LibGit2Sharp.Core;
5+
using LibGit2Sharp.Core.Handles;
6+
7+
namespace LibGit2Sharp
8+
{
9+
/// <summary>
10+
/// A reference to a resolved <see cref="Conflict"/>,
11+
/// known by the <see cref="Index"/>.
12+
/// </summary>
13+
[DebuggerDisplay("{DebuggerDisplay,nq}")]
14+
public class IndexReucEntry : IEquatable<IndexReucEntry>
15+
{
16+
private static readonly LambdaEqualityHelper<IndexReucEntry> equalityHelper =
17+
new LambdaEqualityHelper<IndexReucEntry>(x => x.Path,
18+
x => x.AncestorId, x => x.AncestorMode,
19+
x => x.OurId, x => x.OurMode,
20+
x => x.TheirId, x => x.TheirMode);
21+
22+
/// <summary>
23+
/// Needed for mocking purposes.
24+
/// </summary>
25+
protected IndexReucEntry()
26+
{ }
27+
28+
internal static IndexReucEntry BuildFromPtr(IndexReucEntrySafeHandle handle)
29+
{
30+
if (handle == null || handle.IsZero)
31+
{
32+
return null;
33+
}
34+
35+
GitIndexReucEntry entry = handle.MarshalAsGitIndexReucEntry();
36+
37+
FilePath path = LaxFilePathMarshaler.FromNative(entry.Path);
38+
39+
return new IndexReucEntry
40+
{
41+
Path = path.Native,
42+
AncestorId = entry.AncestorId,
43+
AncestorMode = (Mode)entry.AncestorMode,
44+
OurId = entry.OurId,
45+
OurMode = (Mode)entry.OurMode,
46+
TheirId = entry.TheirId,
47+
TheirMode = (Mode)entry.TheirMode,
48+
};
49+
}
50+
51+
/// <summary>
52+
/// Gets the path of this conflict.
53+
/// </summary>
54+
public virtual string Path { get; private set; }
55+
56+
/// <summary>
57+
/// Gets the <see cref="ObjectId"/> that was the ancestor of this
58+
/// conflict.
59+
/// </summary>
60+
public virtual ObjectId AncestorId { get; private set; }
61+
62+
/// <summary>
63+
/// Gets the <see cref="Mode"/> of the file that was the ancestor of
64+
/// conflict.
65+
/// </summary>
66+
public virtual Mode AncestorMode { get; private set; }
67+
68+
/// <summary>
69+
/// Gets the <see cref="ObjectId"/> that was "our" side of this
70+
/// conflict.
71+
/// </summary>
72+
public virtual ObjectId OurId { get; private set; }
73+
74+
/// <summary>
75+
/// Gets the <see cref="Mode"/> of the file that was "our" side of
76+
/// the conflict.
77+
/// </summary>
78+
public virtual Mode OurMode { get; private set; }
79+
80+
/// <summary>
81+
/// Gets the <see cref="ObjectId"/> that was "their" side of this
82+
/// conflict.
83+
/// </summary>
84+
public virtual ObjectId TheirId { get; private set; }
85+
86+
/// <summary>
87+
/// Gets the <see cref="Mode"/> of the file that was "their" side of
88+
/// the conflict.
89+
/// </summary>
90+
public virtual Mode TheirMode { get; private set; }
91+
92+
/// <summary>
93+
/// Determines whether the specified <see cref="Object"/> is equal to the current <see cref="IndexReucEntry"/>.
94+
/// </summary>
95+
/// <param name="obj">The <see cref="Object"/> to compare with the current <see cref="IndexReucEntry"/>.</param>
96+
/// <returns>True if the specified <see cref="Object"/> is equal to the current <see cref="IndexReucEntry"/>; otherwise, false.</returns>
97+
public override bool Equals(object obj)
98+
{
99+
return Equals(obj as IndexReucEntry);
100+
}
101+
102+
/// <summary>
103+
/// Determines whether the specified <see cref="IndexReucEntry"/> is equal to the current <see cref="IndexReucEntry"/>.
104+
/// </summary>
105+
/// <param name="other">The <see cref="IndexReucEntry"/> to compare with the current <see cref="IndexReucEntry"/>.</param>
106+
/// <returns>True if the specified <see cref="IndexReucEntry"/> is equal to the current <see cref="IndexReucEntry"/>; otherwise, false.</returns>
107+
public bool Equals(IndexReucEntry other)
108+
{
109+
return equalityHelper.Equals(this, other);
110+
}
111+
112+
/// <summary>
113+
/// Returns the hash code for this instance.
114+
/// </summary>
115+
/// <returns>A 32-bit signed integer hash code.</returns>
116+
public override int GetHashCode()
117+
{
118+
return equalityHelper.GetHashCode(this);
119+
}
120+
121+
/// <summary>
122+
/// Tests if two <see cref="IndexReucEntry"/> are equal.
123+
/// </summary>
124+
/// <param name="left">First <see cref="IndexReucEntry"/> to compare.</param>
125+
/// <param name="right">Second <see cref="IndexReucEntry"/> to compare.</param>
126+
/// <returns>True if the two objects are equal; false otherwise.</returns>
127+
public static bool operator ==(IndexReucEntry left, IndexReucEntry right)
128+
{
129+
return Equals(left, right);
130+
}
131+
132+
/// <summary>
133+
/// Tests if two <see cref="IndexReucEntry"/> are different.
134+
/// </summary>
135+
/// <param name="left">First <see cref="IndexReucEntry"/> to compare.</param>
136+
/// <param name="right">Second <see cref="IndexReucEntry"/> to compare.</param>
137+
/// <returns>True if the two objects are different; false otherwise.</returns>
138+
public static bool operator !=(IndexReucEntry left, IndexReucEntry right)
139+
{
140+
return !Equals(left, right);
141+
}
142+
143+
private string DebuggerDisplay
144+
{
145+
get
146+
{
147+
return string.Format(CultureInfo.InvariantCulture,
148+
"{0}: {1} {2} {3}", Path, AncestorId, OurId, TheirId);
149+
}
150+
}
151+
}
152+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Globalization;
5+
using LibGit2Sharp.Core;
6+
using LibGit2Sharp.Core.Handles;
7+
8+
namespace LibGit2Sharp
9+
{
10+
/// <summary>
11+
/// The collection of <see cref="LibGit2Sharp.IndexReucEntry"/>s in a
12+
/// <see cref="LibGit2Sharp.Repository"/> index that reflect the
13+
/// resolved conflicts.
14+
/// </summary>
15+
public class IndexReucEntryCollection : IEnumerable<IndexReucEntry>
16+
{
17+
private readonly Repository repo;
18+
19+
/// <summary>
20+
/// Needed for mocking purposes.
21+
/// </summary>
22+
protected IndexReucEntryCollection()
23+
{ }
24+
25+
internal IndexReucEntryCollection(Repository repo)
26+
{
27+
this.repo = repo;
28+
}
29+
30+
/// <summary>
31+
/// Gets the <see cref="IndexReucEntry"/> with the specified relative path.
32+
/// </summary>
33+
public virtual IndexReucEntry this[string path]
34+
{
35+
get
36+
{
37+
Ensure.ArgumentNotNullOrEmptyString(path, "path");
38+
39+
IndexReucEntrySafeHandle entryHandle = Proxy.git_index_reuc_get_bypath(repo.Index.Handle, path);
40+
return IndexReucEntry.BuildFromPtr(entryHandle);
41+
}
42+
}
43+
44+
private IndexReucEntry this[int index]
45+
{
46+
get
47+
{
48+
IndexReucEntrySafeHandle entryHandle = Proxy.git_index_reuc_get_byindex(repo.Index.Handle, (UIntPtr)index);
49+
return IndexReucEntry.BuildFromPtr(entryHandle);
50+
}
51+
}
52+
53+
#region IEnumerable<IndexReucEntry> Members
54+
55+
private List<IndexReucEntry> AllIndexReucs()
56+
{
57+
var list = new List<IndexReucEntry>();
58+
59+
int count = Proxy.git_index_reuc_entrycount(repo.Index.Handle);
60+
61+
for (int i = 0; i < count; i++)
62+
{
63+
list.Add(this[i]);
64+
}
65+
66+
return list;
67+
}
68+
69+
/// <summary>
70+
/// Returns an enumerator that iterates through the collection.
71+
/// </summary>
72+
/// <returns>An <see cref="IEnumerator{T}"/> object that can be used to iterate through the collection.</returns>
73+
public virtual IEnumerator<IndexReucEntry> GetEnumerator()
74+
{
75+
return AllIndexReucs().GetEnumerator();
76+
}
77+
78+
/// <summary>
79+
/// Returns an enumerator that iterates through the collection.
80+
/// </summary>
81+
/// <returns>An <see cref="IEnumerator"/> object that can be used to iterate through the collection.</returns>
82+
IEnumerator IEnumerable.GetEnumerator()
83+
{
84+
return GetEnumerator();
85+
}
86+
87+
#endregion
88+
}
89+
}

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 5 additions & 1 deletion
< 48DA td data-grid-cell-id="diff-7d975dfc1bdb41f7ef721689ba023e4af74858fd04f82cba4908b4567bfd290e-84-86-1" data-selected="false" role="gridcell" style="background-color:var(--bgColor-default);text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative diff-line-number-neutral left-side">86
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@
8080
<Compile Include="CommitOptions.cs" />
8181
<Compile Include="CommitSortStrategies.cs" />
8282
<Compile Include="CompareOptions.cs" />
83+
<Compile Include="Core\GitIndexReucEntry.cs" />
84+
<Compile Include="Core\Handles\IndexReucEntrySafeHandle.cs" />
8385
<Compile Include="ContentChangeStats.cs" />
84
<Compile Include="BuiltInFeatures.cs" />
8587
<Compile Include="Core\GitCheckoutOptsWrapper.cs" />
@@ -93,6 +95,8 @@
9395
<Compile Include="EmptyCommitException.cs" />
9496
<Compile Include="FetchOptions.cs" />
9597
<Compile Include="GlobalSettings.cs" />
98+
<Compile Include="IndexReucEntry.cs" />
99+
<Compile Include="IndexReucEntryCollection.cs" />
96100
<Compile Include="MergeOptions.cs" />
97101
<Compile Include="MergeResult.cs" />
98102
<Compile Include="PatchEntryChanges.cs" />
@@ -343,4 +347,4 @@
343347
</CreateItem>
344348
<Copy SourceFiles="@(NativeBinaries)" DestinationFiles="@(NativeBinaries->'$(OutputPath)NativeBinaries\%(RecursiveDir)%(Filename)%(Extension)')" SkipUnchangedFiles="true" />
345349
</Target>
346-
</Project>
350+
</Project>

0 commit comments

Comments
 (0)
0