Skip to content

Commit 324ffe0

Browse files
committed
When there is an unexpected exception dump the commit graph for debugging purposes
1 parent 48e7905 commit 324ffe0

File tree

3 files changed

+47
-30
lines changed

3 files changed

+47
-30
lines changed

GitVersionCore.Tests/Helpers/GitTestExtensions.cs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,13 @@
11
using System;
2-
using System.Diagnostics;
32
using System.IO;
43
using System.Linq;
5-
using System.Text;
64
using GitVersion;
7-
using GitVersion.Helpers;
85
using LibGit2Sharp;
96

107
public static class GitTestExtensions
118
{
129
static int pad = 1;
1310

14-
public static void DumpGraph(this IRepository repository)
15-
{
16-
var output = new StringBuilder();
17-
18-
try
19-
{
20-
ProcessHelper.Run(
21-
o => output.AppendLine(o),
22-
e => output.AppendLineFormat("ERROR: {0}", e),
23-
null,
24-
"git",
25-
@"log --graph --abbrev-commit --decorate --date=relative --all --remotes=*",
26-
repository.Info.Path);
27-
}
28-
catch (FileNotFoundException exception)
29-
{
30-
if (exception.FileName != "git")
31-
throw;
32-
33-
output.AppendLine("Could not execute 'git log' due to the following error:");
34-
output.AppendLine(exception.ToString());
35-
}
36-
37-
Trace.Write(output.ToString());
38-
}
39-
4011
public static Commit MakeACommit(this IRepository repository)
4112
{
4213
return CreateFileAndCommit(repository, Guid.NewGuid().ToString());

GitVersionCore/LibGitExtensions.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ namespace GitVersion
22
{
33
using System;
44
using System.Collections.Generic;
5+
using System.Diagnostics;
56
using System.IO;
67
using System.Linq;
8+
using System.Text;
9+
using GitVersion.Helpers;
710
using LibGit2Sharp;
811

912
static class LibGitExtensions
@@ -174,5 +177,40 @@ public static void CheckoutFilesIfExist(this IRepository repository, params stri
174177
}
175178
}
176179
}
180+
181+
public static void DumpGraph(this IRepository repository, Action<string> writer = null, int? maxCommits = null)
182+
{
183+
DumpGraph(repository.Info.Path, writer, maxCommits);
184+
}
185+
186+
public static void DumpGraph(string workingDirectory, Action<string> writer = null, int? maxCommits = null)
187+
{
188+
var output = new StringBuilder();
189+
190+
try
191+
{
192+
ProcessHelper.Run(
193+
o => output.AppendLine(o),
194+
e => output.AppendLineFormat("ERROR: {0}", e),
195+
null,
196+
"git",
197+
@"log --graph --format=""%h %cr %d"" --decorate --date=relative --all --remotes=*" + (maxCommits != null ? string.Format(" -n {0}", maxCommits) : null),
198+
//@"log --graph --abbrev-commit --decorate --date=relative --all --remotes=*",
199+
workingDirectory);
200+
}
201+
catch (FileNotFoundException exception)
202+
{
203+
if (exception.FileName != "git")
204+
{
205+
throw;
206+
}
207+
208+
output.AppendLine("Could not execute 'git log' due to the following error:");
209+
output.AppendLine(exception.ToString());
210+
}
211+
212+
if (writer != null) writer(output.ToString());
213+
else Trace.Write(output.ToString());
214+
}
177215
}
178216
}

GitVersionExe/Program.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ static void Main()
3232

3333
static int VerifyArgumentsAndRun()
3434
{
35+
Arguments arguments = null;
3536
try
3637
{
3738
var fileSystem = new FileSystem();
3839

39-
Arguments arguments;
4040
var argumentsWithoutExeName = GetArgumentsWithoutExeName();
4141
try
4242
{
@@ -99,6 +99,14 @@ static int VerifyArgumentsAndRun()
9999
{
100100
var error = string.Format("An unexpected error occurred:\r\n{0}", exception);
101101
Logger.WriteError(error);
102+
103+
if (arguments != null)
104+
{
105+
Logger.WriteInfo(string.Empty);
106+
Logger.WriteInfo("Here is the current git graph (please include in issue): ");
107+
Logger.WriteInfo("Showing max of 100 commits");
108+
LibGitExtensions.DumpGraph(arguments.TargetPath, Logger.WriteInfo, 100);
109+
}
102110
return 1;
103111
}
104112

0 commit comments

Comments
 (0)