Skip to content

Commit bfe1c5c

Browse files
Mpdreamzreakaleek
andauthored
Only emit one error if capturing stdout goes wrong during assembler checkout (#1225)
* Only emit one error if capturing stdout goes wrong during assembler checkout * Update src/tooling/docs-assembler/Sourcing/RepositorySourcesFetcher.cs Co-authored-by: Jan Calanog <[email protected]> --------- Co-authored-by: Jan Calanog <[email protected]>
1 parent 8e12e2a commit bfe1c5c

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/tooling/docs-assembler/Sourcing/RepositorySourcesFetcher.cs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,6 @@ private bool TryUpdateSource(string name, string branch, string relativePath, ID
125125
_logger.LogInformation("Pull: {Name}\t{Branch}\t{RelativePath}", name, branch, relativePath);
126126
// --allow-unrelated-histories due to shallow clones not finding a common ancestor
127127
ExecIn(checkoutFolder, "git", "pull", "--depth", "1", "--allow-unrelated-histories", "--no-ff");
128-
head = Capture(checkoutFolder, "git", "rev-parse", "HEAD");
129-
return true;
130128
}
131129
catch (Exception e)
132130
{
@@ -136,9 +134,12 @@ private bool TryUpdateSource(string name, string branch, string relativePath, ID
136134
checkoutFolder.Delete(true);
137135
checkoutFolder.Refresh();
138136
}
137+
return false;
139138
}
140139

141-
return false;
140+
head = Capture(checkoutFolder, "git", "rev-parse", "HEAD");
141+
142+
return true;
142143
}
143144

144145
private string CheckoutFromScratch(Repository repository, string name, string branch, string relativePath,
@@ -183,19 +184,26 @@ private void ExecIn(IDirectoryInfo? workingDirectory, string binary, params stri
183184
// ReSharper disable once UnusedMember.Local
184185
private string Capture(IDirectoryInfo? workingDirectory, string binary, params string[] args)
185186
{
186-
// Try 10 times to capture the output of the command, if it fails we'll throw an exception on the last try
187-
for (var i = 0; i < 9; i++)
187+
// Try 10 times to capture the output of the command, if it fails, we'll throw an exception on the last try
188+
Exception? e = null;
189+
for (var i = 0; i <= 9; i++)
188190
{
189191
try
190192
{
191193
return CaptureOutput();
192194
}
193-
catch
195+
catch (Exception ex)
194196
{
195-
// ignored
197+
if (ex is not null)
198+
e = ex;
196199
}
197200
}
198-
return CaptureOutput();
201+
202+
if (e is not null)
203+
collector.EmitError("", "failure capturing stdout", e);
204+
205+
206+
return string.Empty;
199207

200208
string CaptureOutput()
201209
{
@@ -208,9 +216,9 @@ string CaptureOutput()
208216
ConsoleOutWriter = NoopConsoleWriter.Instance
209217
};
210218
var result = Proc.Start(arguments);
211-
if (result.ExitCode != 0)
212-
collector.EmitError("", $"Exit code: {result.ExitCode} while executing {binary} {string.Join(" ", args)} in {workingDirectory}");
213-
var line = result.ConsoleOut.FirstOrDefault()?.Line ?? throw new Exception($"No output captured for {binary}: {workingDirectory}");
219+
var line = result.ExitCode != 0
220+
? throw new Exception($"Exit code is not 0. Received {result.ExitCode} from {binary}: {workingDirectory}")
221+
: result.ConsoleOut.FirstOrDefault()?.Line ?? throw new Exception($"No output captured for {binary}: {workingDirectory}");
214222
return line;
215223
}
216224
}

0 commit comments

Comments
 (0)