Skip to content

Commit e68c53c

Browse files
committed
#33 - A better to handle success/failure when executing git command.
1 parent f85f344 commit e68c53c

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/GitExtensions.BundleBackuper/Services/GitUiCommandsBundleService.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,12 @@ private string FindCommitIdIfPushed(IGitUICommands commands, string head, int he
151151

152152
private string FindCommitId(IGitUICommands commands, string head, int headOffset)
153153
{
154-
string commitId = RunGitCommand(commands, $"rev-parse {head}~{headOffset}").Trim();
155-
if (!String.IsNullOrWhiteSpace(commitId) && !commitId.Contains(" "))
156-
return commitId;
154+
if (TryRunGitCommand(commands, $"rev-parse {head}~{headOffset}", out string commitId))
155+
{
156+
commitId = commitId.Trim();
157+
if (!String.IsNullOrWhiteSpace(commitId) && !commitId.Contains(" "))
158+
return commitId;
159+
}
157160

158161
return null;
159162
}
@@ -164,10 +167,7 @@ private bool IsCommitPushed(IGitUICommands commands, string head, int headOffset
164167
if (String.IsNullOrEmpty(commitId))
165168
return true;
166169

167-
168-
169-
string branches = RunGitCommand(commands, $"branch -r --contains {commitId}");
170-
if (String.IsNullOrWhiteSpace(branches))
170+
if (!TryRunGitCommand(commands, $"branch -r --contains {commitId}", out string branches) || String.IsNullOrWhiteSpace(branches))
171171
return false;
172172

173173
if (settings.RemoteNamesToCheck.Count > 0)
@@ -214,17 +214,21 @@ private int BinarySearch(int start, int end, Func<int, int> predicate)
214214
}
215215
}
216216

217-
private string RunGitCommand(IGitUICommands commands, string arguments)
217+
private bool TryRunGitCommand(IGitUICommands commands, string arguments, out string output)
218218
{
219219
using (IProcess process = commands.GitModule.GitCommandRunner.RunDetached(arguments, redirectOutput: true))
220220
{
221221
process.WaitForExit();
222222

223223
string error = process.StandardError.ReadToEnd();
224224
if (!String.IsNullOrEmpty(error))
225-
return error;
225+
{
226+
output = error;
227+
return false;
228+
}
226229

227-
return process.StandardOutput.ReadToEnd();
230+
output = process.StandardOutput.ReadToEnd();
231+
return false;
228232
}
229233
}
230234
}

0 commit comments

Comments
 (0)