Skip to content

Commit 79c68e1

Browse files
authored
Merge pull request #44 from twpol/feature/always-list-files
feat: List all files in a pull request before attempting merge
2 parents 00c7c79 + 72f587d commit 79c68e1

File tree

3 files changed

+39
-30
lines changed

3 files changed

+39
-30
lines changed

Diff for: Git/Project.cs

+32-26
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Open_Rails_Code_Bot.Git
99
{
1010
public class Project
1111
{
12-
string GitPath;
12+
readonly string GitPath;
1313

1414
public Project(string gitPath)
1515
{
@@ -18,13 +18,11 @@ public Project(string gitPath)
1818

1919
public void Init(string repository)
2020
{
21-
if (!Directory.Exists(GitPath))
22-
{
23-
Directory.CreateDirectory(GitPath);
24-
RunCommand($"init");
25-
RunCommand($"remote add origin {repository}");
26-
RunCommand($"config remote.origin.fetch +refs/*:refs/*");
27-
}
21+
if (!Directory.Exists(GitPath)) Directory.CreateDirectory(GitPath);
22+
if (!File.Exists(Path.Join(GitPath, ".git", "config"))) RunCommand($"init");
23+
24+
RunCommand($"config remove-section remote.origin");
25+
RunCommand($"remote add origin --mirror=fetch {repository}");
2826
}
2927

3028
public void Fetch()
@@ -52,6 +50,16 @@ public void Clean()
5250
RunCommand("clean --force -d -x");
5351
}
5452

53+
public void DiffStat(string reference1, string reference2)
54+
{
55+
foreach (var line in GetCommandOutput($"diff --numstat {reference1}...{reference2}"))
56+
{
57+
var parts = line.Split('\t');
58+
if (parts.Length == 3 && int.TryParse(parts[0], out var added) && int.TryParse(parts[1], out var deleted))
59+
Console.WriteLine(" {2} {0:+#,##0} {1:-#,##0}", added, deleted, parts[2]);
60+
}
61+
}
62+
5563
public void Merge(string reference)
5664
{
5765
RunCommand($"merge --quiet --no-edit --no-ff -Xignore-space-change {reference}");
@@ -129,45 +137,43 @@ public void SetBranchRef(string branch, string reference)
129137
RunCommand($"branch -f {branch} {reference}");
130138
}
131139

132-
void RunCommand(string command)
140+
void RunCommand(string arguments)
133141
{
134-
foreach (var line in GetCommandOutput(command, true))
142+
foreach (var line in GetCommandOutput(arguments, true))
135143
{
136144
}
137145
}
138146

139-
IEnumerable<string> GetCommandOutput(string command, bool printOutput = false)
147+
IEnumerable<string> GetCommandOutput(string arguments, bool printOutput = false)
140148
{
141-
var args = $"--no-pager {command}";
149+
arguments = $"--no-pager {arguments}";
142150
if (printOutput)
143-
Console.WriteLine($" > git {args}");
151+
Console.WriteLine($" > git {arguments}");
152+
var lines = new List<string>();
144153
var git = new Process();
145154
git.StartInfo.WorkingDirectory = GitPath;
146155
git.StartInfo.FileName = "git";
147-
git.StartInfo.Arguments = args;
156+
git.StartInfo.Arguments = arguments;
148157
git.StartInfo.UseShellExecute = false;
149158
git.StartInfo.RedirectStandardOutput = true;
150159
git.StartInfo.RedirectStandardError = true;
151160
git.StartInfo.StandardOutputEncoding = Encoding.UTF8;
152161
git.StartInfo.StandardErrorEncoding = Encoding.UTF8;
153-
git.ErrorDataReceived += (sender, e) =>
154-
{
155-
if (e.Data?.Length > 0)
156-
Console.Error.WriteLine($" ! {e.Data}");
157-
};
162+
git.OutputDataReceived += (sender, e) => lines.Add($" < {e.Data}");
163+
git.ErrorDataReceived += (sender, e) => lines.Add($" ! {e.Data}");
158164
git.Start();
165+
git.BeginOutputReadLine();
159166
git.BeginErrorReadLine();
160-
while (!git.StandardOutput.EndOfStream)
167+
git.WaitForExit();
168+
foreach (var line in lines)
161169
{
162-
if (printOutput)
163-
Console.WriteLine($" < {git.StandardOutput.ReadLine()}");
164-
else
165-
yield return git.StandardOutput.ReadLine();
170+
if (printOutput && line.Length > 4)
171+
Console.WriteLine(line);
172+
yield return line[4..];
166173
}
167-
git.WaitForExit();
168174
if (git.ExitCode != 0)
169175
{
170-
throw new ApplicationException($"git {command} failed: {git.ExitCode}");
176+
throw new ApplicationException($"git {arguments} failed: {git.ExitCode}");
171177
}
172178
}
173179
}

Diff for: GitHub/Query.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Query
1414

1515
readonly string Token;
1616

17-
HttpClient Client = new HttpClient();
17+
readonly HttpClient Client = new();
1818

1919
public Query(string token)
2020
{

Diff for: Program.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,17 @@ static async Task AsyncMain(IConfigurationRoot config)
103103
var mergeBranchTree = git.ParseRef($"{mergeBranchCommit}^{{tree}}");
104104
git.CheckoutDetached(baseBranchCommit);
105105
var baseBranchVersion = String.Format(gitHubConfig["versionFormat"] ?? "{0}", git.Describe(gitHubConfig["versionDescribeOptions"] ?? ""));
106-
var mergeBranchParents = new List<string>();
107-
mergeBranchParents.Add(mergeBranchCommit);
108-
mergeBranchParents.Add(baseBranchCommit);
106+
var mergeBranchParents = new List<string>
107+
{
108+
mergeBranchCommit,
109+
baseBranchCommit
110+
};
109111
var autoMergePullRequestsSuccess = new List<GraphPullRequest>();
110112
var autoMergePullRequestsFailure = new List<GraphPullRequest>();
111113
foreach (var pullRequest in autoMergePullRequests)
112114
{
113115
Console.WriteLine($"Merging #{pullRequest.Number} {pullRequest.Title}...");
116+
git.DiffStat(baseBranchCommit, $"pull/{pullRequest.Number}/head");
114117
var mergeCommit = git.ParseRef($"pull/{pullRequest.Number}/head");
115118
try
116119
{

0 commit comments

Comments
 (0)