Skip to content

Commit

Permalink
Merge pull request #44 from twpol/feature/always-list-files
Browse files Browse the repository at this point in the history
feat: List all files in a pull request before attempting merge
  • Loading branch information
twpol authored Nov 21, 2024
2 parents 00c7c79 + 72f587d commit 79c68e1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 30 deletions.
58 changes: 32 additions & 26 deletions Git/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Open_Rails_Code_Bot.Git
{
public class Project
{
string GitPath;
readonly string GitPath;

public Project(string gitPath)
{
Expand All @@ -18,13 +18,11 @@ public Project(string gitPath)

public void Init(string repository)
{
if (!Directory.Exists(GitPath))
{
Directory.CreateDirectory(GitPath);
RunCommand($"init");
RunCommand($"remote add origin {repository}");
RunCommand($"config remote.origin.fetch +refs/*:refs/*");
}
if (!Directory.Exists(GitPath)) Directory.CreateDirectory(GitPath);
if (!File.Exists(Path.Join(GitPath, ".git", "config"))) RunCommand($"init");

RunCommand($"config remove-section remote.origin");
RunCommand($"remote add origin --mirror=fetch {repository}");
}

public void Fetch()
Expand Down Expand Up @@ -52,6 +50,16 @@ public void Clean()
RunCommand("clean --force -d -x");
}

public void DiffStat(string reference1, string reference2)
{
foreach (var line in GetCommandOutput($"diff --numstat {reference1}...{reference2}"))
{
var parts = line.Split('\t');
if (parts.Length == 3 && int.TryParse(parts[0], out var added) && int.TryParse(parts[1], out var deleted))
Console.WriteLine(" {2} {0:+#,##0} {1:-#,##0}", added, deleted, parts[2]);
}
}

public void Merge(string reference)
{
RunCommand($"merge --quiet --no-edit --no-ff -Xignore-space-change {reference}");
Expand Down Expand Up @@ -129,45 +137,43 @@ public void SetBranchRef(string branch, string reference)
RunCommand($"branch -f {branch} {reference}");
}

void RunCommand(string command)
void RunCommand(string arguments)
{
foreach (var line in GetCommandOutput(command, true))
foreach (var line in GetCommandOutput(arguments, true))
{
}
}

IEnumerable<string> GetCommandOutput(string command, bool printOutput = false)
IEnumerable<string> GetCommandOutput(string arguments, bool printOutput = false)
{
var args = $"--no-pager {command}";
arguments = $"--no-pager {arguments}";
if (printOutput)
Console.WriteLine($" > git {args}");
Console.WriteLine($" > git {arguments}");
var lines = new List<string>();
var git = new Process();
git.StartInfo.WorkingDirectory = GitPath;
git.StartInfo.FileName = "git";
git.StartInfo.Arguments = args;
git.StartInfo.Arguments = arguments;
git.StartInfo.UseShellExecute = false;
git.StartInfo.RedirectStandardOutput = true;
git.StartInfo.RedirectStandardError = true;
git.StartInfo.StandardOutputEncoding = Encoding.UTF8;
git.StartInfo.StandardErrorEncoding = Encoding.UTF8;
git.ErrorDataReceived += (sender, e) =>
{
if (e.Data?.Length > 0)
Console.Error.WriteLine($" ! {e.Data}");
};
git.OutputDataReceived += (sender, e) => lines.Add($" < {e.Data}");
git.ErrorDataReceived += (sender, e) => lines.Add($" ! {e.Data}");
git.Start();
git.BeginOutputReadLine();
git.BeginErrorReadLine();
while (!git.StandardOutput.EndOfStream)
git.WaitForExit();
foreach (var line in lines)
{
if (printOutput)
Console.WriteLine($" < {git.StandardOutput.ReadLine()}");
else
yield return git.StandardOutput.ReadLine();
if (printOutput && line.Length > 4)
Console.WriteLine(line);
yield return line[4..];
}
git.WaitForExit();
if (git.ExitCode != 0)
{
throw new ApplicationException($"git {command} failed: {git.ExitCode}");
throw new ApplicationException($"git {arguments} failed: {git.ExitCode}");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion GitHub/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Query

readonly string Token;

HttpClient Client = new HttpClient();
readonly HttpClient Client = new();

public Query(string token)
{
Expand Down
9 changes: 6 additions & 3 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,17 @@ static async Task AsyncMain(IConfigurationRoot config)
var mergeBranchTree = git.ParseRef($"{mergeBranchCommit}^{{tree}}");
git.CheckoutDetached(baseBranchCommit);
var baseBranchVersion = String.Format(gitHubConfig["versionFormat"] ?? "{0}", git.Describe(gitHubConfig["versionDescribeOptions"] ?? ""));
var mergeBranchParents = new List<string>();
mergeBranchParents.Add(mergeBranchCommit);
mergeBranchParents.Add(baseBranchCommit);
var mergeBranchParents = new List<string>
{
mergeBranchCommit,
baseBranchCommit
};
var autoMergePullRequestsSuccess = new List<GraphPullRequest>();
var autoMergePullRequestsFailure = new List<GraphPullRequest>();
foreach (var pullRequest in autoMergePullRequests)
{
Console.WriteLine($"Merging #{pullRequest.Number} {pullRequest.Title}...");
git.DiffStat(baseBranchCommit, $"pull/{pullRequest.Number}/head");
var mergeCommit = git.ParseRef($"pull/{pullRequest.Number}/head");
try
{
Expand Down

0 comments on commit 79c68e1

Please sign in to comment.