From f79dd99dc152d4a4de38162d4708a0bc8ea0d59f Mon Sep 17 00:00:00 2001 From: Philippe Miossec Date: Sun, 20 Apr 2014 02:38:27 +0200 Subject: [PATCH] AutoSelect remote only for command that need it --- GitTfs/GitTfs.cs | 39 ++---------------------------------- GitTfs/Globals.cs | 51 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 39 deletions(-) diff --git a/GitTfs/GitTfs.cs b/GitTfs/GitTfs.cs index 74f76bb1..e9e40150 100644 --- a/GitTfs/GitTfs.cs +++ b/GitTfs/GitTfs.cs @@ -47,45 +47,9 @@ namespace Sep.Git.Tfs var unparsedArgs = ParseOptions(command, args); Trace.WriteLine("Command run:" + commandLineRun); ParseAuthors(); - AutoDetectRemoteToUse(); return Main(command, unparsedArgs); } - private void AutoDetectRemoteToUse() - { - if (string.IsNullOrEmpty(_globals.UserSpecifiedRemoteId)) - { - if (_globals.AutoFindRemote) - _stdout.WriteLine("info: Option '-I' is now the default behavior and is no more needed. It will be removed in the next version."); - - var changesetsWithRemote = _globals.Repository.GetLastParentTfsCommits("HEAD"); - if (!changesetsWithRemote.Any()) - { - var allRemotes = _globals.Repository.ReadAllTfsRemotes(); - if (!allRemotes.Any()) - throw new Exception("error: no tfs remotes defined in this repository!"); - - if (allRemotes.Count() == 1) - { - _globals.UserSpecifiedRemoteId = allRemotes.First().Id; - _stdout.WriteLine("Working with tfs remote: " + _globals.RemoteId); - return; - } - throw new Exception("error: can't find a tfs remote to use\n No TFS parents found and more than one tfs remote defined in the repository!" - + "\n Use '-i' option to define which one to use."); - } - var foundRemote = changesetsWithRemote.First().Remote; - if (foundRemote.IsDerived) - { - _stdout.WriteLine("Bootstraping tfs remote..."); - foundRemote = _bootstrapper.CreateRemote(changesetsWithRemote.First()); - } - - _globals.UserSpecifiedRemoteId = foundRemote.Id; - _stdout.WriteLine("Working with tfs remote: " + _globals.RemoteId); - } - } - public int Main(GitTfsCommand command, IList unparsedArgs) { Trace.WriteLine(_gitTfsVersionProvider.GetVersionString()); @@ -151,7 +115,8 @@ namespace Sep.Git.Tfs { _globals.GitDir = ".git"; } - _globals.RemoteId = GitTfsConstants.DefaultRepositoryId; + _globals.Stdout = _stdout; + _globals.Bootstrapper = _bootstrapper; } public void AssertValidGitRepository() diff --git a/GitTfs/Globals.cs b/GitTfs/Globals.cs index c46a8e12..de5ceab4 100644 --- a/GitTfs/Globals.cs +++ b/GitTfs/Globals.cs @@ -2,6 +2,7 @@ using System; using System.ComponentModel; using System.Diagnostics; using System.IO; +using System.Linq; using NDesk.Options; using Sep.Git.Tfs.Core; using Sep.Git.Tfs.Util; @@ -63,13 +64,55 @@ namespace Sep.Git.Tfs public string UserSpecifiedRemoteId { get { return _userSpecifiedRemoteId; } - set { RemoteId = _userSpecifiedRemoteId = value; } + set { _userSpecifiedRemoteId = value; } } private string _userSpecifiedRemoteId; + private string _activeRemoteId = null; + public string ActiveRemoteId + { + get + { + if (!string.IsNullOrEmpty(_activeRemoteId)) + return _activeRemoteId; + + if (!string.IsNullOrEmpty(UserSpecifiedRemoteId)) + return UserSpecifiedRemoteId; + //if (AutoFindRemote) + // Stdout.WriteLine("info: Option '-I' is now the default behavior and is no more needed. It will be removed in the next version."); + + var changesetsWithRemote = Repository.GetLastParentTfsCommits("HEAD"); + if (!changesetsWithRemote.Any()) + { + var allRemotes = Repository.ReadAllTfsRemotes(); + if (!allRemotes.Any()) + throw new Exception("error: no tfs remotes defined in this repository!"); + + if (allRemotes.Count() == 1) + { + _activeRemoteId = allRemotes.First().Id; + Stdout.WriteLine("Working with tfs remote: " + _activeRemoteId); + return _activeRemoteId; + } + throw new Exception("error: can't find a tfs remote to use\n No TFS parents found and more than one tfs remote defined in the repository!" + + "\n Use '-i' option to define which one to use."); + } + var foundRemote = changesetsWithRemote.First().Remote; + if (foundRemote.IsDerived) + { + Stdout.WriteLine("Bootstraping tfs remote..."); + foundRemote = Bootstrapper.CreateRemote(changesetsWithRemote.First()); + } + + _activeRemoteId = foundRemote.Id; + Stdout.WriteLine("Working with tfs remote: " + _activeRemoteId); + return _activeRemoteId; + } + } + public bool AutoFindRemote { get; set; } - public string RemoteId { get; set; } + public string RemoteId { get{ return ActiveRemoteId; } } public string GitDir { @@ -110,5 +153,9 @@ For more information, see https://github.com/git-tfs/git-tfs/issues/448 "); { get { return 200; } } + + public TextWriter Stdout { get; set; } + + public Bootstrapper Bootstrapper { get; set; } } }