Skip to content

Commit 410b0aa

Browse files
authored
Merge pull request #10 from openrails/feature/git-stdout
feat: More detailed git logging
2 parents e26290d + b4ac822 commit 410b0aa

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

Git/Project.cs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ namespace Open_Rails_Code_Bot.Git
1010
public class Project
1111
{
1212
string GitPath;
13-
bool Verbose;
1413

15-
public Project(string gitPath, bool verbose)
14+
public Project(string gitPath)
1615
{
1716
GitPath = gitPath;
18-
Verbose = verbose;
1917
}
2018

2119
public void Init(string repository)
@@ -56,7 +54,7 @@ public void Clean()
5654

5755
public void Merge(string reference)
5856
{
59-
RunCommand($"merge --no-edit --no-ff {reference}");
57+
RunCommand($"merge --quiet --no-edit --no-ff {reference}");
6058
}
6159

6260
public void Push(string reference)
@@ -132,40 +130,44 @@ public void SetBranchRef(string branch, string reference)
132130

133131
void RunCommand(string command)
134132
{
135-
foreach (var line in GetCommandOutput(command))
133+
foreach (var line in GetCommandOutput(command, true))
136134
{
137135
}
138136
}
139137

140-
IEnumerable<string> GetCommandOutput(string command)
138+
IEnumerable<string> GetCommandOutput(string command, bool printOutput = false)
141139
{
142140
var args = $"--no-pager {command}";
143-
if (Verbose)
141+
if (printOutput)
142+
Console.WriteLine($" > git {args}");
143+
var git = new Process();
144+
git.StartInfo.WorkingDirectory = GitPath;
145+
git.StartInfo.FileName = "git";
146+
git.StartInfo.Arguments = args;
147+
git.StartInfo.UseShellExecute = false;
148+
git.StartInfo.RedirectStandardOutput = true;
149+
git.StartInfo.RedirectStandardError = true;
150+
git.StartInfo.StandardOutputEncoding = Encoding.UTF8;
151+
git.StartInfo.StandardErrorEncoding = Encoding.UTF8;
152+
git.ErrorDataReceived += (sender, e) =>
144153
{
145-
Console.WriteLine("```shell");
146-
Console.WriteLine($"{GitPath}> git {args}");
147-
}
148-
var git = Process.Start(new ProcessStartInfo()
149-
{
150-
WorkingDirectory = GitPath,
151-
FileName = "git",
152-
Arguments = args,
153-
StandardOutputEncoding = Encoding.UTF8,
154-
RedirectStandardOutput = true,
155-
});
154+
if (e.Data?.Length > 0)
155+
Console.Error.WriteLine($" ! {e.Data}");
156+
};
157+
git.Start();
158+
git.BeginErrorReadLine();
156159
while (!git.StandardOutput.EndOfStream)
157160
{
158-
yield return git.StandardOutput.ReadLine();
161+
if (printOutput)
162+
Console.WriteLine($" < {git.StandardOutput.ReadLine()}");
163+
else
164+
yield return git.StandardOutput.ReadLine();
159165
}
160166
git.WaitForExit();
161167
if (git.ExitCode != 0)
162168
{
163169
throw new ApplicationException($"git {command} failed: {git.ExitCode}");
164170
}
165-
if (Verbose)
166-
{
167-
Console.WriteLine("```");
168-
}
169171
}
170172
}
171173
}

Program.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
@@ -82,7 +82,8 @@ static async Task AsyncMain(IConfigurationRoot config)
8282
Console.WriteLine($" #{pullRequest.Number} {pullRequest.Title}");
8383
}
8484

85-
var git = new Git.Project(GetGitPath(), false);
85+
Console.WriteLine("Preparing repository...");
86+
var git = new Git.Project(GetGitPath());
8687
git.Init($"https://github.com/{gitHubConfig["organization"]}/{gitHubConfig["repository"]}.git");
8788
git.Fetch();
8889
git.ResetHard();
@@ -99,7 +100,7 @@ static async Task AsyncMain(IConfigurationRoot config)
99100
var autoMergePullRequestsFailure = new List<GraphPullRequest>();
100101
foreach (var pullRequest in autoMergePullRequests)
101102
{
102-
Console.WriteLine($"Merging #{pullRequest.Number} {pullRequest.Title}");
103+
Console.WriteLine($"Merging #{pullRequest.Number} {pullRequest.Title}...");
103104
var mergeCommit = git.ParseRef($"pull/{pullRequest.Number}/head");
104105
try
105106
{
@@ -112,7 +113,7 @@ static async Task AsyncMain(IConfigurationRoot config)
112113
autoMergePullRequestsFailure.Add(pullRequest);
113114
git.ResetHard();
114115
git.Clean();
115-
Console.WriteLine($"Error: {error.Message}");
116+
Console.WriteLine($" Error: {error.Message}");
116117
}
117118
}
118119
var autoMergeCommit = git.ParseRef("HEAD");
@@ -137,6 +138,7 @@ static async Task AsyncMain(IConfigurationRoot config)
137138
}
138139
else
139140
{
141+
Console.WriteLine("Creating merge commit...");
140142
var newMergeBranchMessage = String.Format(gitHubConfig["mergeMessageFormat"],
141143
baseBranchVersion,
142144
autoMergePullRequestsSuccess.Count,

0 commit comments

Comments
 (0)