-
Notifications
You must be signed in to change notification settings - Fork 400
Expand file tree
/
Copy pathChange.cs
More file actions
165 lines (151 loc) · 4.92 KB
/
Change.cs
File metadata and controls
165 lines (151 loc) · 4.92 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
namespace SourceGit.Models
{
public enum ChangeViewMode
{
List,
Grid,
Tree,
}
public enum ChangeSortMode
{
Path,
Status,
}
public enum ChangeState
{
None,
Modified,
TypeChanged,
Added,
Deleted,
Renamed,
Copied,
Untracked,
Conflicted,
}
public enum ConflictReason
{
None,
BothDeleted,
AddedByUs,
DeletedByThem,
AddedByThem,
DeletedByUs,
BothAdded,
BothModified,
}
public class ChangeDataForAmend
{
public string FileMode { get; set; } = "";
public string ObjectHash { get; set; } = "";
public string ParentSHA { get; set; } = "";
}
public class Change
{
public ChangeState Index { get; set; } = ChangeState.None;
public ChangeState WorkTree { get; set; } = ChangeState.None;
public string Path { get; set; } = "";
public string OriginalPath { get; set; } = "";
public ChangeDataForAmend DataForAmend { get; set; } = null;
public ConflictReason ConflictReason { get; set; } = ConflictReason.None;
public bool IsConflicted => WorkTree == ChangeState.Conflicted;
public string ConflictMarker => CONFLICT_MARKERS[(int)ConflictReason];
public string ConflictDesc => CONFLICT_DESCS[(int)ConflictReason];
public string WorkTreeDesc => TYPE_DESCS[(int)WorkTree];
public string IndexDesc => TYPE_DESCS[(int)Index];
public void Set(ChangeState index, ChangeState workTree = ChangeState.None)
{
Index = index;
WorkTree = workTree;
if (index == ChangeState.Renamed || workTree == ChangeState.Renamed)
{
var parts = Path.Split('\t', 2);
if (parts.Length < 2)
parts = Path.Split(" -> ", 2);
if (parts.Length == 2)
{
OriginalPath = parts[0];
Path = parts[1];
}
}
if (Path[0] == '"')
Path = Path.Substring(1, Path.Length - 2);
if (!string.IsNullOrEmpty(OriginalPath) && OriginalPath[0] == '"')
OriginalPath = OriginalPath.Substring(1, OriginalPath.Length - 2);
}
/// <summary>
/// Gets the sort priority for a change based on its status, used for sorting changes by status.
/// Lower numbers indicate higher priority (appear first in sorted lists).
/// </summary>
/// <param name="change">The change object to get priority for</param>
/// <param name="isUnstagedContext">True if sorting in unstaged context, false for staged context</param>
/// <returns>Priority value where lower numbers appear first</returns>
public static int GetStatusSortPriority(Change change, bool isUnstagedContext)
{
if (change == null) return int.MaxValue;
if (isUnstagedContext)
{
// For unstaged context, only consider WorkTree state
return change.WorkTree switch
{
ChangeState.Conflicted => 1, // Conflicts first - most urgent
ChangeState.Modified => 2,
ChangeState.TypeChanged => 3,
ChangeState.Deleted => 4, // Missing files
ChangeState.Renamed => 5,
ChangeState.Copied => 6,
ChangeState.Untracked => 7, // New files last
_ => 10
};
}
else
{
// For staged context, only consider Index state
return change.Index switch
{
ChangeState.Modified => 1,
ChangeState.TypeChanged => 2,
ChangeState.Renamed => 3,
ChangeState.Copied => 4,
ChangeState.Added => 5,
ChangeState.Deleted => 6,
_ => 10
};
}
}
private static readonly string[] TYPE_DESCS =
[
"Unknown",
"Modified",
"Type Changed",
"Added",
"Deleted",
"Renamed",
"Copied",
"Untracked",
"Conflict"
];
private static readonly string[] CONFLICT_MARKERS =
[
string.Empty,
"DD",
"AU",
"UD",
"UA",
"DU",
"AA",
"UU"
];
private static readonly string[] CONFLICT_DESCS =
[
string.Empty,
"Both deleted",
"Added by us",
"Deleted by them",
"Added by them",
"Deleted by us",
"Both added",
"Both modified"
];
}
}