forked from microsoft/azure-pipelines-tasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitUtil.cs
198 lines (170 loc) · 6.35 KB
/
GitUtil.cs
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System.Text.RegularExpressions;
namespace BuildConfigGen
{
internal static class GitUtil
{
public static void GetUntrackedFiles(string taskTarget, out IEnumerable<string> toAdd, out IEnumerable<string> toRemove)
{
if (!Directory.Exists(taskTarget))
{
toAdd = new string[0];
toRemove = new string[0];
return;
}
// get directory prefix
int exitCode3;
string[] revParseOut;
string revParsePrefix;
if ((exitCode3 = ProcessUtil.RunCommandWithExitCode("git", "rev-parse --show-prefix", taskTarget, out revParseOut)) == 0)
{
if (revParseOut.Length < 1)
{
throw new Exception("revParseOut.Length<1");
}
revParsePrefix = revParseOut[0];
}
else
{
throw new Exception("non-zero exit code");
}
string[] untrackedOutput;
int exitCode2;
const string gitaddDryRun = "add . --dry-run";
if ((exitCode2 = ProcessUtil.RunCommandWithExitCode("git", gitaddDryRun, taskTarget, out untrackedOutput)) == 0)
{
}
else
{
throw new Exception("non-zero exit code");
}
List<string> untrackedOuput2 = new();
List<string> untrackedOuputRemove = new();
const string fileNameGroup = "fileNameGroup";
const string action = "action";
const string addPattern = $@"^(?<{action}>add|remove) '(?<{fileNameGroup}>.*)'$";
Regex re = new Regex(addPattern, RegexOptions.Singleline);
foreach (var o in untrackedOutput)
{
Match? m = null;
if (null != (m = re.Match(o)))
{
if (m.Success)
{
List<string> target;
if (m.Groups[action].Value == "remove")
{
target = untrackedOuputRemove;
}
else
{
if (m.Groups[action].Value != "add")
{
throw new Exception($"expected add or remove in {o}");
}
target = untrackedOuput2;
}
string path = m.Groups[fileNameGroup].Value;
if (!path.StartsWith(revParsePrefix))
{
throw new Exception($"expected {path} to start with ${revParsePrefix}");
}
path = path.Remove(0, revParsePrefix.Length);
target.Add(FixupPath(path));
}
else
{
throw new Exception($"'{o}' did not match expected output");
}
}
else
{
throw new Exception($"unexpected line from output didn't match regex. {nameof(o)}={o} {nameof(gitaddDryRun)}={gitaddDryRun} {nameof(addPattern)}={addPattern}");
}
}
toAdd = untrackedOuput2;
toRemove = untrackedOuputRemove;
}
internal static string GetGitRootPath(string currentDir)
{
const string args = "rev-parse --git-dir";
string path = RunGitCommandScalar(currentDir, args);
path = FixupPath(path);
const string gitDir = ".git";
if (path.EndsWith(gitDir))
{
path = path.Substring(0, path.Length - gitDir.Length);
if (path == "")
{
return currentDir;
}
return path;
}
else
{
throw new Exception($"expected git {args} to return ");
}
}
internal static IEnumerable<string> GetNonIgnoredFileListFromPath(string taskTarget)
{
if(!Directory.Exists(taskTarget))
{
throw new Exception($"{nameof(taskTarget)}=={taskTarget} doesn't exist");
}
var output = GitLsFiles(taskTarget).Select(FixupPath);
IEnumerable<string> untrackedOuput2;
IEnumerable<string> untrackedToRemove;
GitUtil.GetUntrackedFiles(taskTarget, out untrackedOuput2, out untrackedToRemove);
var paths = output.Union(untrackedOuput2);
paths = paths.Except(untrackedToRemove);
return paths;
}
private static IEnumerable<string> GitLsFiles(string taskTarget)
{
if (!Directory.Exists(taskTarget))
{
throw new Exception($"{nameof(taskTarget)}=={taskTarget} doesn't exist");
//return new string[0];
}
int exitCode;
string[] output;
if ((exitCode = ProcessUtil.RunCommandWithExitCode("git", "ls-files", taskTarget, out output)) == 0)
{
}
else
{
throw new Exception("non-zero exit code");
}
return output;
}
private static string RunGitCommandScalar(string currentDir, string args)
{
if (!Directory.Exists(currentDir))
{
throw new Exception($"{nameof(currentDir)}=={currentDir} doesn't exist");
//return new string[0];
}
string[] output;
int exitCode;
if (0 != (exitCode = ProcessUtil.RunCommandWithExitCode("git", args, currentDir, out output)))
{
throw new Exception($"non-zero from git {exitCode}");
}
if (output.Length > 0)
{
return output[0];
}
else
{
throw new Exception($"no output from {args}");
}
}
public static string FixupPath(string s)
{
if (Path.DirectorySeparatorChar == '\\')
{
return s.Replace("/", "\\");
}
return s;
}
}
}