Permalink
Browse files

Handle hooks errors (refactor for better handling of bash errors/outp…

…uts)
  • Loading branch information...
1 parent bf94ea6 commit c71799ebcbd4df2f292e4d19d46086799fe855d0 @jacargentina jacargentina committed Sep 14, 2012
@@ -46,10 +46,11 @@ public IEnumerable<GitTreeObject> Children
try
{
- var tree = GitBash.Run("ls-tree -z \"" + Id + "\"", this.Repository);
- children = tree.Split(new char[] { '\0', '\n' })
- .Select(t=>ParseString(t))
- .OfType<GitTreeObject>();
+ var result = GitBash.Run("ls-tree -z \"" + Id + "\"", this.Repository);
+ if (!result.HasError)
+ children = result.Output.Split(new char[] { '\0', '\n' })
+ .Select(t => ParseString(t))
+ .OfType<GitTreeObject>();
}
catch (Exception ex)
{
@@ -7,7 +7,7 @@ namespace GitScc.DataServices
{
public class RepositoryGraph
{
- private const int CommitsToLoad = 200;
+ private const int CommitsToLoad = 200;
private const string LogFormat = "--pretty=format:%H%n%P%n%cr%n%cn%n%ce%n%ci%n%T%n%s%n%b";
private string workingDirectory;
@@ -29,16 +29,16 @@ public IEnumerable<Commit> Commits
{
if (commits == null)
{
- var output = GitBash.Run(string.Format("log -n {0} --date-order --all --boundary -z {1} HEAD",
- CommitsToLoad, LogFormat),
+ var result = GitBash.Run(string.Format("log -n {0} --date-order --all --boundary -z {1} HEAD",
+ CommitsToLoad, LogFormat),
this.workingDirectory);
- if (string.IsNullOrEmpty(output) || output.Contains("fatal:"))
+ if (result.HasError || string.IsNullOrEmpty(result.Output) || result.Output.Contains("fatal:"))
{
return new List<Commit>();
}
- var logs = output.Split('\0');
+ var logs = result.Output.Split('\0');
commits = logs.Select(log => ParseCommit(log)).ToList();
commits.ToList().ForEach(
commit => commit.ChildIds =
@@ -73,14 +73,15 @@ public IList<Ref> Refs
{
if (refs == null)
{
- var output = GitBash.Run("show-ref --head --dereference", this.workingDirectory);
- refs = (from t in output.Split('\n')
- where !string.IsNullOrWhiteSpace(t)
- select new Ref
- {
- Id = t.Substring(0, 40),
- RefName = t.Substring(41)
- }).ToList();
+ var result = GitBash.Run("show-ref --head --dereference", this.workingDirectory);
+ if (!result.HasError)
+ refs = (from t in result.Output.Split('\n')
+ where !string.IsNullOrWhiteSpace(t)
+ select new Ref
+ {
+ Id = t.Substring(0, 40),
+ RefName = t.Substring(41)
+ }).ToList();
}
return refs;
@@ -132,12 +133,12 @@ private void GenerateGraph(IEnumerable<Commit> commits)
select r;
var children = (from c in commits
- where c.ParentIds.Contains(id)
- select c).ToList();
+ where c.ParentIds.Contains(id)
+ select c).ToList();
var parents = (from c in commits
- where c.ChildIds.Contains(id)
- select c).ToList();
+ where c.ChildIds.Contains(id)
+ select c).ToList();
var lane = lanes.IndexOf(id);
if (lane < 0)
@@ -147,7 +148,7 @@ where c.ChildIds.Contains(id)
}
int m = parents.Count() - 1;
- for (int n = m; n>=0; n--)
+ for (int n = m; n >= 0; n--)
{
if (lanes.IndexOf(parents[n].Id) <= 0)
{
@@ -199,8 +200,8 @@ private IEnumerable<Commit> GetSimplifiedCommits()
{
foreach (var commit in Commits)
{
- if (commit.ParentIds.Count() == 1 && commit.ChildIds.Count() == 1 && !this.Refs.Any(r=>r.Id==commit.Id))
- {
+ if (commit.ParentIds.Count() == 1 && commit.ChildIds.Count() == 1 && !this.Refs.Any(r => r.Id == commit.Id))
+ {
var cid = commit.ChildIds[0];
var pid = commit.ParentIds[0];
@@ -230,10 +231,11 @@ private IEnumerable<Commit> GetSimplifiedCommits()
private int GetLane(string id)
{
- return Nodes.Where(n=>n.Id == id).Select(n=>n.X).FirstOrDefault();
+ return Nodes.Where(n => n.Id == id).Select(n => n.X).FirstOrDefault();
}
- public bool IsSimplified {
+ public bool IsSimplified
+ {
get { return isSimplified; }
set { isSimplified = value; commits = null; nodes = null; links = null; }
}
@@ -242,10 +244,10 @@ public Commit GetCommit(string commitId)
{
try
{
- var output = GitBash.Run(string.Format("log -1 {0} {1}", LogFormat, commitId),
+ var result = GitBash.Run(string.Format("log -1 {0} {1}", LogFormat, commitId),
this.workingDirectory);
- return ParseCommit(output);
+ return ParseCommit(result.Output);
}
catch (Exception ex)
{
@@ -259,10 +261,13 @@ public GitTreeObject GetTree(string commitId)
var commit = GetCommit(commitId);
if (commit == null) return null;
- return new GitTreeObject
- {
- Id = commitId, Name = "", FullName = "",
- Type="tree", IsExpanded= true,
+ return new GitTreeObject
+ {
+ Id = commitId,
+ Name = "",
+ FullName = "",
+ Type = "tree",
+ IsExpanded = true,
Repository = this.workingDirectory,
};
}
@@ -278,13 +283,13 @@ public IEnumerable<Change> GetChanges(string fromCommitId, string toCommitId)
try
{
- var output = GitBash.Run(string.Format("diff -M -C --name-status -z {0} {1}", fromCommitId, toCommitId), this.workingDirectory);
+ var result = GitBash.Run(string.Format("diff -M -C --name-status -z {0} {1}", fromCommitId, toCommitId), this.workingDirectory);
- if (!string.IsNullOrWhiteSpace(output))
+ if (!string.IsNullOrWhiteSpace(result.Output))
{
//from gitextensions GitCommandHelper.cs
var nl = new char[] { '\n', '\r' };
- string trimmedStatus = output.Trim(nl);
+ string trimmedStatus = result.Output.Trim(nl);
int lastNewLinePos = trimmedStatus.LastIndexOfAny(nl);
if (lastNewLinePos > 0)
{
@@ -331,7 +336,7 @@ public IEnumerable<Change> GetChanges(string fromCommitId, string toCommitId)
}
catch (Exception ex)
{
- Log.WriteLine("Repository.GetChanges: {0} - {1}\r\n{2}", fromCommitId, toCommitId , ex.ToString());
+ Log.WriteLine("Repository.GetChanges: {0} - {1}\r\n{2}", fromCommitId, toCommitId, ex.ToString());
}
return changes;
@@ -46,6 +46,8 @@
<Compile Include="DataServices\Ref.cs" />
<Compile Include="DataServices\RepositoryGraph.cs" />
<Compile Include="GitBash.cs" />
+ <Compile Include="GitBashResult.cs" />
+ <Compile Include="GitCommitResult.cs" />
<Compile Include="GitFileStatus.cs" />
<Compile Include="GitFileStatusTracker.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
View
@@ -29,11 +29,13 @@ public static string GitExePath
public static bool Exists { get { return !string.IsNullOrWhiteSpace(gitExePath) &&
File.Exists(gitExePath); } }
- public static string Run(string args, string workingDirectory)
+ public static GitBashResult Run(string args, string workingDirectory)
{
if (string.IsNullOrWhiteSpace(gitExePath) || !File.Exists(gitExePath))
throw new Exception("Git Executable not found");
+ GitBashResult result = new GitBashResult();
+
//Debug.WriteLine(string.Format("{2}>{0} {1}", gitExePath, args, workingDirectory));
var pinfo = new ProcessStartInfo(gitExePath)
@@ -60,14 +62,11 @@ public static string Run(string args, string workingDirectory)
//Debug.WriteLine(output);
- if (!string.IsNullOrEmpty(error))
- {
- //Debug.WriteLine("STDERR: " + error);
- //throw new Exception(error);
+ result.HasError = process.ExitCode != 0;
+ result.Output = output;
+ result.Error = error;
- output += Environment.NewLine + error;
- }
- return output;
+ return result;
}
}
@@ -0,0 +1,16 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace GitScc
+{
+ public class GitBashResult
+ {
+ public bool HasError { get; set; }
+
+ public string Error { get; set; }
+
+ public string Output { get; set; }
+ }
+}
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace GitScc
+{
+ public class GitCommitResult
+ {
+ public bool IsSha1 { get; set; }
+
+ public string Message { get; set; }
+ }
+}
Oops, something went wrong.

0 comments on commit c71799e

Please sign in to comment.