Add GitTfsException for showing short error messages.

This commit is contained in:
Matt Burke
2011-01-27 14:58:50 -05:00
parent ff77e67cf7
commit 0bc77aa30c
6 changed files with 88 additions and 38 deletions
+1 -2
View File
@@ -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 @@ namespace Sep.Git.Tfs.Commands
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;
+35
View File
@@ -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);
}
}
}
+17 -27
View File
@@ -34,24 +34,20 @@ namespace Sep.Git.Tfs.Core
{
var pendingChanges = _workspace.GetPendingChanges();
if (pendingChanges.Count() == 0)
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)
{
_stdout.WriteLine(" nothing to shelve");
}
else
{
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 @@ namespace Sep.Git.Tfs.Core
var pendingChanges = _workspace.GetPendingChanges();
if (pendingChanges.IsEmpty())
throw new Exception("Nothing to checkin");
if (pendingChanges.Any())
throw new GitTfsException("Nothing to checkin!");
if (!_tfsHelper.ShowCheckinDialog(_workspace, pendingChanges, GetWorkItemCheckedInfos(), _checkinOptions.CheckinComment))
{
if (!_tfsHelper.ShowCheckinDialog(_workspace, pendingChanges, GetWorkItemCheckedInfos(), _checkinOptions.CheckinComment))
{
_stdout.WriteLine(" cancelled.");
}
}
else
{
throw new Exception("Nothing to checkin.");
throw new GitTfsException("Checkin cancelled!");
}
}
@@ -78,7 +68,7 @@ namespace Sep.Git.Tfs.Core
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 @@ namespace Sep.Git.Tfs.Core
{
_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
{
+1
View File
@@ -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" />
+14
View File
@@ -21,6 +21,20 @@ namespace Sep.Git.Tfs
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);
+20 -9
View File
@@ -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 @@ namespace Sep.Git.Tfs.Util
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;
}
}
}
}