From 0bc77aa30c2ea5bc25a82463fee7f54504e1dc6d Mon Sep 17 00:00:00 2001 From: Matt Burke Date: Thu, 27 Jan 2011 14:58:50 -0500 Subject: [PATCH] Add GitTfsException for showing short error messages. --- GitTfs/Commands/CheckinTool.cs | 3 +- GitTfs/Core/GitTfsException.cs | 35 ++++++++++++++++++++++++ GitTfs/Core/TfsWorkspace.cs | 44 ++++++++++++------------------ GitTfs/GitTfs.csproj | 1 + GitTfs/Program.cs | 14 ++++++++++ GitTfs/Util/GitTfsCommandRunner.cs | 29 ++++++++++++++------ 6 files changed, 88 insertions(+), 38 deletions(-) create mode 100644 GitTfs/Core/GitTfsException.cs diff --git a/GitTfs/Commands/CheckinTool.cs b/GitTfs/Commands/CheckinTool.cs index dd5ac332..03ce5eba 100644 --- a/GitTfs/Commands/CheckinTool.cs +++ b/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 @@ 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; diff --git a/GitTfs/Core/GitTfsException.cs b/GitTfs/Core/GitTfsException.cs new file mode 100644 index 00000000..d5ce39b6 --- /dev/null +++ b/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 RecommendedSolutions { get; set; } + + public GitTfsException(string message, IEnumerable solutions, Exception e) + : base(message, e) + { + RecommendedSolutions = solutions; + } + + public GitTfsException(string message, Exception e) + : base(message, e) + {} + + public GitTfsException(string message, IEnumerable solutions) + : base(message) + { + RecommendedSolutions = solutions; + } + + public GitTfsException(string message) + : base(message) + {} + + public Exception ToRethrowable() + { + return new GitTfsException(Message, RecommendedSolutions, this); + } + } +} \ No newline at end of file diff --git a/GitTfs/Core/TfsWorkspace.cs b/GitTfs/Core/TfsWorkspace.cs index 2528c5b7..98d26f25 100644 --- a/GitTfs/Core/TfsWorkspace.cs +++ b/GitTfs/Core/TfsWorkspace.cs @@ -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 { diff --git a/GitTfs/GitTfs.csproj b/GitTfs/GitTfs.csproj index fc518e91..257f4544 100644 --- a/GitTfs/GitTfs.csproj +++ b/GitTfs/GitTfs.csproj @@ -141,6 +141,7 @@ + diff --git a/GitTfs/Program.cs b/GitTfs/Program.cs index 09dab920..9ecd850f 100644 --- a/GitTfs/Program.cs +++ b/GitTfs/Program.cs @@ -21,6 +21,20 @@ namespace Sep.Git.Tfs var container = Initialize(); container.GetInstance().Run(new List(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); diff --git a/GitTfs/Util/GitTfsCommandRunner.cs b/GitTfs/Util/GitTfsCommandRunner.cs index cf8b4eb1..6f9c8edd 100644 --- a/GitTfs/Util/GitTfsCommandRunner.cs +++ b/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 @@ namespace Sep.Git.Tfs.Util public int Run(GitTfsCommand command, IList 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; + } } } }