forked from libgit2/libgit2sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConflictCollection.cs
More file actions
116 lines (100 loc) · 3.62 KB
/
ConflictCollection.cs
File metadata and controls
116 lines (100 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using LibGit2Sharp.Core;
namespace LibGit2Sharp
{
/// <summary>
/// The collection of <see cref="LibGit2Sharp.Conflict"/>s in a
/// <see cref="LibGit2Sharp.Repository"/> index due to a
/// previously performed merge operation.
/// </summary>
public class ConflictCollection : IEnumerable<Conflict>
{
private readonly Repository repo;
/// <summary>
/// Needed for mocking purposes.
/// </summary>
protected ConflictCollection()
{ }
internal ConflictCollection(Repository repo)
{
this.repo = repo;
}
/// <summary>
/// Gets the <see cref="LibGit2Sharp.Conflict" /> for the
/// specified relative path.
/// </summary>
/// <param name="path">The relative path to query</param>
/// <returns>A <see cref="Conflict"/> that represents the conflict for this file.</returns>
public virtual Conflict this[string path]
{
get
{
return Proxy.git_index_conflict_get(repo.Index.Handle, repo, path);
}
}
#region IEnumerable<IndexEntry> Members
private List<Conflict> AllConflicts()
{
var list = new List<Conflict>();
IndexEntry ancestor = null, ours = null, theirs = null;
string currentPath = null;
foreach (IndexEntry entry in repo.Index)
{
if (entry.StageLevel == StageLevel.Staged)
{
continue;
}
if (currentPath != null && !entry.Path.Equals(currentPath, StringComparison.Ordinal))
{
list.Add(new Conflict(ancestor, ours, theirs));
ancestor = null;
ours = null;
theirs = null;
}
currentPath = entry.Path;
switch (entry.StageLevel)
{
case StageLevel.Ancestor:
ancestor = entry;
break;
case StageLevel.Ours:
ours = entry;
break;
case StageLevel.Theirs:
theirs = entry;
break;
default:
throw new InvalidOperationException(string.Format(
CultureInfo.InvariantCulture,
"Entry '{0}' bears an unexpected StageLevel '{1}'",
entry.Path, entry.StageLevel));
}
}
if (currentPath != null)
{
list.Add(new Conflict(ancestor, ours, theirs));
}
return list;
}
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>An <see cref = "IEnumerator{T}" /> object that can be used to iterate through the collection.</returns>
public virtual IEnumerator<Conflict> GetEnumerator()
{
return AllConflicts().GetEnumerator();
}
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>An <see cref = "IEnumerator" /> object that can be used to iterate through the collection.</returns>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
}
}