Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 47b7c58

Browse files
author
Edward Thomson
committed
Introduce FileStatus.Conflicted
Introduce conflict data to status and diff information. Introduce staging of conflicts.
1 parent 02940ce commit 47b7c58

File tree

6 files changed

+70
-3
lines changed

6 files changed

+70
-3
lines changed

LibGit2Sharp/ChangeKind.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,10 @@ public enum ChangeKind
5555
/// Entry is unreadable.
5656
/// </summary>
5757
Unreadable = 9,
58+
59+
/// <summary>
60+
/// Entry is currently in conflict.
61+
/// </summary>
62+
Conflicted = 10,
5863
}
5964
}

LibGit2Sharp/Core/GitDiff.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ internal enum GitDiffFlags
231231
GIT_DIFF_FLAG_BINARY = (1 << 0),
232232
GIT_DIFF_FLAG_NOT_BINARY = (1 << 1),
233233
GIT_DIFF_FLAG_VALID_ID = (1 << 2),
234+
GIT_DIFF_FLAG_EXISTS = (1 << 3),
234235
}
235236

236237
[StructLayout(LayoutKind.Sequential)]

LibGit2Sharp/FileStatus.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,10 @@ public enum FileStatus
125125
/// The file is <see cref="NewInWorkdir"/> but its name and/or path matches an exclude pattern in a <c>gitignore</c> file.
126126
/// </summary>
127127
Ignored = (1 << 14), /* GIT_STATUS_IGNORED */
128+
129+
/// <summary>
130+
/// The file is <see cref="Conflicted"/> due to a merge.
131+
/// </summary>
132+
Conflicted = (1 << 15), /* GIT_STATUS_CONFLICTED */
128133
}
129134
}

LibGit2Sharp/Repository.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,6 +1656,7 @@ public void Stage(IEnumerable<string> paths, StageOptions stageOptions)
16561656
.Where(
16571657
tec => tec.Status != ChangeKind.Added &&
16581658
tec.Status != ChangeKind.Modified &&
1659+
tec.Status != ChangeKind.Conflicted &&
16591660
tec.Status != ChangeKind.Unmodified &&
16601661
tec.Status != ChangeKind.Deleted).ToList();
16611662

@@ -1667,10 +1668,25 @@ public void Stage(IEnumerable<string> paths, StageOptions stageOptions)
16671668
unexpectedTypesOfChanges[0].Path, unexpectedTypesOfChanges[0].Status));
16681669
}
16691670

1670-
foreach (TreeEntryChanges treeEntryChanges in changes
1671-
.Where(tec => tec.Status == ChangeKind.Deleted))
1671+
/* Remove files from the index that don't exist on disk */
1672+
foreach (TreeEntryChanges treeEntryChanges in changes)
16721673
{
1673-
RemoveFromIndex(treeEntryChanges.Path);
1674+
switch (treeEntryChanges.Status)
1675+
{
1676+
case ChangeKind.Conflicted:
1677+
if (!treeEntryChanges.Exists)
1678+
{
1679+
RemoveFromIndex(treeEntryChanges.Path);
1680+
}
1681+
break;
1682+
1683+
case ChangeKind.Deleted:
1684+
RemoveFromIndex(treeEntryChanges.Path);
1685+
break;
1686+
1687+
default:
1688+
continue;
1689+
}
16741690
}
16751691

16761692
foreach (TreeEntryChanges treeEntryChanges in changes)
@@ -1682,6 +1698,13 @@ public void Stage(IEnumerable<string> paths, StageOptions stageOptions)
16821698
AddToIndex(treeEntryChanges.Path);
16831699
break;
16841700

1701+
case ChangeKind.Conflicted:
1702+
if (treeEntryChanges.Exists)
1703+
{
1704+
AddToIndex(treeEntryChanges.Path);
1705+
}
1706+
break;
1707+
16851708
default:
16861709
continue;
16871710
}

LibGit2Sharp/TreeChanges.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class TreeChanges : IEnumerable<TreeEntryChanges>
2525
private readonly List<TreeEntryChanges> unmodified = new List<TreeEntryChanges>();
2626
private readonly List<TreeEntryChanges> renamed = new List<TreeEntryChanges>();
2727
private readonly List<TreeEntryChanges> copied = new List<TreeEntryChanges>();
28+
private readonly List<TreeEntryChanges> conflicted = new List<TreeEntryChanges>();
2829

2930
private readonly IDictionary<ChangeKind, Action<TreeChanges, TreeEntryChanges>> fileDispatcher = Build();
3031

@@ -39,6 +40,7 @@ private static IDictionary<ChangeKind, Action<TreeChanges, TreeEntryChanges>> Bu
3940
{ ChangeKind.Unmodified, (de, d) => de.unmodified.Add(d) },
4041
{ ChangeKind.Renamed, (de, d) => de.renamed.Add(d) },
4142
{ ChangeKind.Copied, (de, d) => de.copied.Add(d) },
43+
{ ChangeKind.Conflicted, (de, d) => de.conflicted.Add(d) },
4244
};
4345
}
4446

@@ -146,6 +148,14 @@ public virtual IEnumerable<TreeEntryChanges> Unmodified
146148
get { return unmodified; }
147149
}
148150

151+
/// <summary>
152+
/// List of <see cref="TreeEntryChanges"/> which are conflicted
153+
/// </summary>
154+
public virtual IEnumerable<TreeEntryChanges> Conflicted
155+
{
156+
get { return conflicted; }
157+
}
158+
149159
private string DebuggerDisplay
150160
{
151161
get

LibGit2Sharp/TreeEntryChanges.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ internal TreeEntryChanges(GitDiffDelta delta)
2525
OldMode = (Mode)delta.OldFile.Mode;
2626
Oid = delta.NewFile.Id;
2727
OldOid = delta.OldFile.Id;
28+
Exists = (delta.NewFile.Flags & GitDiffFlags.GIT_DIFF_FLAG_EXISTS) != 0;
29+
OldExists = (delta.OldFile.Flags & GitDiffFlags.GIT_DIFF_FLAG_EXISTS) != 0;
2830

2931
Status = (delta.Status == ChangeKind.Untracked || delta.Status == ChangeKind.Ignored)
3032
? ChangeKind.Added
@@ -46,6 +48,17 @@ internal TreeEntryChanges(GitDiffDelta delta)
4648
/// </summary>
4749
public virtual ObjectId Oid { get; private set; }
4850

51+
/// <summary>
52+
/// The file exists in the new side of the diff.
53+
/// This is useful in determining if you have content in
54+
/// the ours or theirs side of a conflict. This will
55+
/// be false during a conflict that deletes both the
56+
/// "ours" and "theirs" sides, or when the diff is a
57+
/// delete and the status is
58+
/// <see cref="ChangeType.Deleted"/>.
59+
/// </summary>
60+
public virtual bool Exists { get; private set; }
61+
4962
/// <summary>
5063
/// The kind of change that has been done (added, deleted, modified ...).
5164
/// </summary>
@@ -66,6 +79,16 @@ internal TreeEntryChanges(GitDiffDelta delta)
6679
/// </summary>
6780
public virtual ObjectId OldOid { get; private set; }
6881

82+
/// <summary>
83+
/// The file exists in the old side of the diff.
84+
/// This is useful in determining if you have an ancestor
85+
/// side to a conflict. This will be false during a
86+
/// conflict that involves both the "ours" and "theirs"
87+
/// side being added, or when the diff is an add and the
88+
/// status is <see cref="ChangeType.Added"/>.
89+
/// </summary>
90+
public virtual bool OldExists { get; private set; }
91+
6992
private string DebuggerDisplay
7093
{
7194
get

0 commit comments

Comments
 (0)