Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add GitTfsException for showing short error messages.

  • Loading branch information...
commit 0bc77aa30c2ea5bc25a82463fee7f54504e1dc6d 1 parent ff77e67
Matt Burke authored
View
3  GitTfs/Commands/CheckinTool.cs
@@ -1,6 +1,5 @@
using System.Collections.Generic;
using System.ComponentModel;
-using System.Linq;
using CommandLine.OptParse;
using Sep.Git.Tfs.Core;
using Sep.Git.Tfs.Util;
@@ -38,7 +37,7 @@ public int Run(string refToCheckin)
if (changeset.Remote.Tfs.CanShowCheckinDialog)
changeset.Remote.CheckinTool(refToCheckin, changeset);
else
- throw new Exception(
+ throw new GitTfsException(
"checkintool does not work with this TFS version (" + changeset.Remote.Tfs.TfsClientLibraryVersion + ").",
new[] {"Try installing the VS2010 edition of Team Explorer."});
return GitTfsExitCodes.OK;
View
35 GitTfs/Core/GitTfsException.cs
@@ -0,0 +1,35 @@
+using System;
+using System.Collections.Generic;
+
+namespace Sep.Git.Tfs.Core
+{
+ public class GitTfsException : Exception
+ {
+ public IEnumerable<string> RecommendedSolutions { get; set; }
+
+ public GitTfsException(string message, IEnumerable<string> solutions, Exception e)
+ : base(message, e)
+ {
+ RecommendedSolutions = solutions;
+ }
+
+ public GitTfsException(string message, Exception e)
+ : base(message, e)
+ {}
+
+ public GitTfsException(string message, IEnumerable<string> solutions)
+ : base(message)
+ {
+ RecommendedSolutions = solutions;
+ }
+
+ public GitTfsException(string message)
+ : base(message)
+ {}
+
+ public Exception ToRethrowable()
+ {
+ return new GitTfsException(Message, RecommendedSolutions, this);
+ }
+ }
+}
View
44 GitTfs/Core/TfsWorkspace.cs
@@ -34,24 +34,20 @@ public void Shelve(string shelvesetName, bool evaluateCheckinPolicies)
{
var pendingChanges = _workspace.GetPendingChanges();
- if (pendingChanges.Count() == 0)
- {
- _stdout.WriteLine(" nothing to shelve");
- }
- else
+ if (pendingChanges.IsEmpty())
+ throw new GitTfsException("Nothing to shelve!");
+
+ var shelveset = _tfsHelper.CreateShelveset(_workspace, shelvesetName);
+ shelveset.Comment = _checkinOptions.CheckinComment;
+ shelveset.WorkItemInfo = GetWorkItemInfos().ToArray();
+ if(evaluateCheckinPolicies)
{
- var shelveset = _tfsHelper.CreateShelveset(_workspace, shelvesetName);
- shelveset.Comment = _checkinOptions.CheckinComment;
- shelveset.WorkItemInfo = GetWorkItemInfos().ToArray();
- if(evaluateCheckinPolicies)
+ foreach(var message in _policyEvaluator.EvaluateCheckin(_workspace, pendingChanges, shelveset.Comment, shelveset.WorkItemInfo))
{
- foreach(var message in _policyEvaluator.EvaluateCheckin(_workspace, pendingChanges, shelveset.Comment, shelveset.WorkItemInfo))
- {
- _stdout.WriteLine("[Checkin Policy] " + message);
- }
+ _stdout.WriteLine("[Checkin Policy] " + message);
}
- _workspace.Shelve(shelveset, pendingChanges, _checkinOptions.Force ? TfsShelvingOptions.Replace : TfsShelvingOptions.None);
}
+ _workspace.Shelve(shelveset, pendingChanges, _checkinOptions.Force ? TfsShelvingOptions.Replace : TfsShelvingOptions.None);
}
public void CheckinTool()
@@ -59,17 +55,11 @@ public void CheckinTool()
var pendingChanges = _workspace.GetPendingChanges();
if (pendingChanges.IsEmpty())
- throw new Exception("Nothing to checkin");
- if (pendingChanges.Any())
- {
- if (!_tfsHelper.ShowCheckinDialog(_workspace, pendingChanges, GetWorkItemCheckedInfos(), _checkinOptions.CheckinComment))
- {
- _stdout.WriteLine(" cancelled.");
- }
- }
- else
+ throw new GitTfsException("Nothing to checkin!");
+
+ if (!_tfsHelper.ShowCheckinDialog(_workspace, pendingChanges, GetWorkItemCheckedInfos(), _checkinOptions.CheckinComment))
{
- throw new Exception("Nothing to checkin.");
+ throw new GitTfsException("Checkin cancelled!");
}
}
@@ -78,7 +68,7 @@ public long Checkin()
var pendingChanges = _workspace.GetPendingChanges();
if(pendingChanges.IsEmpty())
- throw new Exception("Nothing to shelve");
+ throw new GitTfsException("Nothing to checkin!");
var workItemInfos = GetWorkItemInfos();
var checkinProblems = _policyEvaluator.EvaluateCheckin(_workspace, pendingChanges, _checkinOptions.CheckinComment, workItemInfos);
@@ -88,14 +78,14 @@ public long Checkin()
{
_stdout.WriteLine("[ERROR] " + message);
}
- throw new Exception("No changes checked in.");
+ throw new GitTfsException("No changes checked in.");
}
else
{
var newChangeset = _workspace.Checkin(pendingChanges, _checkinOptions.CheckinComment, null, workItemInfos);
if(newChangeset == 0)
{
- throw new Exception("Checkin failed!");
+ throw new GitTfsException("Checkin failed!");
}
else
{
View
1  GitTfs/GitTfs.csproj
@@ -141,6 +141,7 @@
<Compile Include="Core\TfsInterop\IChangeset.cs" />
<Compile Include="Core\TfsWorkspace.cs" />
<Compile Include="Core\TfsWriter.cs" />
+ <Compile Include="Core\GitTfsException.cs" />
<Compile Include="Util\GitTfsCommandFactory.cs" />
<Compile Include="Util\GitTfsCommandRunner.cs" />
<Compile Include="Util\PluggableWithAliases.cs" />
View
14 GitTfs/Program.cs
@@ -21,6 +21,20 @@ public static void Main(string [] args)
var container = Initialize();
container.GetInstance<GitTfs>().Run(new List<string>(args));
}
+ catch(GitTfsException e)
+ {
+ Trace.WriteLine(e);
+ Console.WriteLine(e.Message);
+ if (!e.RecommendedSolutions.IsEmpty())
+ {
+ Console.WriteLine("You may be able to resolve this problem.");
+ foreach (var solution in e.RecommendedSolutions)
+ {
+ Console.WriteLine("- " + solution);
+ }
+ }
+ Environment.ExitCode = -1;
+ }
catch (Exception e)
{
Trace.WriteLine(e);
View
29 GitTfs/Util/GitTfsCommandRunner.cs
@@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.Linq;
+using System.Reflection;
using Sep.Git.Tfs.Commands;
+using Sep.Git.Tfs.Core;
namespace Sep.Git.Tfs.Util
{
@@ -15,15 +17,24 @@ public GitTfsCommandRunner(IHelpHelper help)
public int Run(GitTfsCommand command, IList<string> args)
{
- var runMethods = command.GetType().GetMethods().Where(m => m.Name == "Run" && m.ReturnType == typeof(int)).Select(m => new { Method = m, Parameters = m.GetParameters() });
- var splitRunMethods = runMethods.Where(m => m.Parameters.All(p => p.ParameterType == typeof (string)));
- var exactMatchingMethod = splitRunMethods.SingleOrDefault(m => m.Parameters.Length == args.Count);
- if (exactMatchingMethod != null)
- return (int) exactMatchingMethod.Method.Invoke(command, args.ToArray());
- var defaultRunMethod = runMethods.FirstOrDefault(m => m.Parameters.Length == 1 && m.Parameters[0].ParameterType.IsAssignableFrom(args.GetType()));
- if (defaultRunMethod != null)
- return (int) defaultRunMethod.Method.Invoke(command, new object[] {args});
- return _help.ShowHelpForInvalidArguments(command);
+ try
+ {
+ var runMethods = command.GetType().GetMethods().Where(m => m.Name == "Run" && m.ReturnType == typeof(int)).Select(m => new { Method = m, Parameters = m.GetParameters() });
+ var splitRunMethods = runMethods.Where(m => m.Parameters.All(p => p.ParameterType == typeof(string)));
+ var exactMatchingMethod = splitRunMethods.SingleOrDefault(m => m.Parameters.Length == args.Count);
+ if (exactMatchingMethod != null)
+ return (int)exactMatchingMethod.Method.Invoke(command, args.ToArray());
+ var defaultRunMethod = runMethods.FirstOrDefault(m => m.Parameters.Length == 1 && m.Parameters[0].ParameterType.IsAssignableFrom(args.GetType()));
+ if (defaultRunMethod != null)
+ return (int)defaultRunMethod.Method.Invoke(command, new object[] { args });
+ return _help.ShowHelpForInvalidArguments(command);
+ }
+ catch (TargetInvocationException e)
+ {
+ if (e.InnerException is GitTfsException)
+ throw ((GitTfsException) e.InnerException).ToRethrowable();
+ throw;
+ }
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.