From d3366ad155f644d5f9a5b5d49a2578e6ab89736f Mon Sep 17 00:00:00 2001 From: Philippe Miossec Date: Fri, 22 Dec 2017 21:08:53 +0100 Subject: [PATCH 1/2] Fix failing copy of author file when repository still not initialized (`clone` and `init` commands) We now try to copy the file after the command is run. + refactor AuthorFile.cs Should fix #1094 --- src/GitTfs/Commands/Rcheckin.cs | 2 +- src/GitTfs/GitTfs.cs | 36 ++++++++------ src/GitTfs/Util/AuthorsFile.cs | 88 ++++++++++++++++++++------------- 3 files changed, 76 insertions(+), 50 deletions(-) diff --git a/src/GitTfs/Commands/Rcheckin.cs b/src/GitTfs/Commands/Rcheckin.cs index 64fd9d5c8..f0caaf1b7 100644 --- a/src/GitTfs/Commands/Rcheckin.cs +++ b/src/GitTfs/Commands/Rcheckin.cs @@ -62,7 +62,7 @@ public int Run(string localBranch) if (!_globals.Repository.IsBare) throw new GitTfsException("error: This syntax with one parameter is only allowed in bare repository."); - _authors.Parse(null, _globals.GitDir); + _authors.LoadAuthorsFromSavedFile(_globals.GitDir); return _writer.Write(GitRepository.ShortToLocalName(localBranch), PerformRCheckin); } diff --git a/src/GitTfs/GitTfs.cs b/src/GitTfs/GitTfs.cs index 5a60d9476..03dd8becb 100644 --- a/src/GitTfs/GitTfs.cs +++ b/src/GitTfs/GitTfs.cs @@ -42,8 +42,14 @@ public int Run(IList args) UpdateLoggerOnDebugging(); Trace.WriteLine("Command run:" + _globals.CommandLineRun); if (RequiresValidGitRepository(command)) AssertValidGitRepository(); - ParseAuthors(); - return Main(command, unparsedArgs); + bool willCreateRepository = command.GetType() == typeof(Clone) || command.GetType() == typeof(Init); + ParseAuthorsAndSave(!willCreateRepository); + var exitCode = Main(command, unparsedArgs); + if (willCreateRepository) + { + SaveAuthorFileInRepository(); + } + return exitCode; } private void UpdateLoggerOnDebugging() @@ -64,22 +70,19 @@ public int Main(GitTfsCommand command, IList unparsedArgs) { return _help.ShowHelp(command); } - else if (_globals.ShowVersion) + if (_globals.ShowVersion) { Trace.TraceInformation(_gitTfsVersionProvider.GetVersionString()); Trace.TraceInformation(GitTfsConstants.MessageForceVersion); return GitTfsExitCodes.OK; } - else + try { - try - { - return _runner.Run(command, unparsedArgs); - } - finally - { - _container.GetInstance().Dispose(); - } + return _runner.Run(command, unparsedArgs); + } + finally + { + _container.GetInstance().Dispose(); } } @@ -88,11 +91,11 @@ public bool RequiresValidGitRepository(GitTfsCommand command) return !command.GetType().GetCustomAttributes(typeof(RequiresValidGitRepositoryAttribute), false).IsEmpty(); } - private void ParseAuthors() + private void ParseAuthorsAndSave(bool couldSaveAuthorFile) { try { - _container.GetInstance().Parse(_globals.AuthorsFilePath, _globals.GitDir); + _container.GetInstance().Parse(_globals.AuthorsFilePath, _globals.GitDir, couldSaveAuthorFile); } catch (Exception ex) { @@ -104,6 +107,11 @@ private void ParseAuthors() } } + private void SaveAuthorFileInRepository() + { + _container.GetInstance().SaveAuthorFileInRepository(_globals.AuthorsFilePath, _globals.GitDir); + } + public void InitializeGlobals() { var git = _container.GetInstance(); diff --git a/src/GitTfs/Util/AuthorsFile.cs b/src/GitTfs/Util/AuthorsFile.cs index 0ccca9b85..f74a1b3b9 100644 --- a/src/GitTfs/Util/AuthorsFile.cs +++ b/src/GitTfs/Util/AuthorsFile.cs @@ -146,47 +146,65 @@ public bool Parse(TextReader authorsFileStream) return true; } - public bool Parse(string authorsFilePath, string gitDir) + private string GetSavedAuthorFilePath(string gitDir) { - var savedAuthorFile = Path.Combine(gitDir, GitTfsCachedAuthorsFileName); - if (!string.IsNullOrWhiteSpace(authorsFilePath)) + return Path.Combine(gitDir, GitTfsCachedAuthorsFileName); + } + + public bool Parse(string authorsFilePath, string gitDir, bool couldSaveAuthorFile) + { + if (string.IsNullOrWhiteSpace(authorsFilePath)) { - if (!File.Exists(authorsFilePath)) - { - throw new GitTfsException("Authors file cannot be found: '" + authorsFilePath + "'"); - } - else - { - Trace.WriteLine("Reading authors file : " + authorsFilePath); - bool parseSuccess; - using (StreamReader sr = new StreamReader(authorsFilePath)) - { - parseSuccess = Parse(sr); - } - try - { - File.Copy(authorsFilePath, savedAuthorFile, true); - return parseSuccess; - } - catch (Exception) - { - Trace.TraceWarning("Failed to copy authors file from \"" + authorsFilePath + "\" to \"" + savedAuthorFile + "\"."); - } - } + return LoadAuthorsFromSavedFile(gitDir); } - else if (File.Exists(savedAuthorFile)) + + if (!File.Exists(authorsFilePath)) { - if (Authors.Count != 0) - return true; - Trace.WriteLine("Reading cached authors file (" + savedAuthorFile + ")..."); - using (StreamReader sr = new StreamReader(savedAuthorFile)) - { - return Parse(sr); - } + throw new GitTfsException("Authors file cannot be found: '" + authorsFilePath + "'"); + } + + if (couldSaveAuthorFile) + { + SaveAuthorFileInRepository(authorsFilePath, gitDir); } - else + + Trace.WriteLine("Reading authors file : " + authorsFilePath); + using (StreamReader sr = new StreamReader(authorsFilePath)) + { + return Parse(sr); + } + } + + public void SaveAuthorFileInRepository(string authorsFilePath, string gitDir) + { + var savedAuthorFile = GetSavedAuthorFilePath(gitDir); + try + { + File.Copy(authorsFilePath, savedAuthorFile, true); + } + catch (Exception) + { + Trace.TraceWarning("Failed to copy authors file from \"" + authorsFilePath + "\" to \"" + + savedAuthorFile + "\"."); + } + } + + public bool LoadAuthorsFromSavedFile(string gitDir) + { + var savedAuthorFile = GetSavedAuthorFilePath(gitDir); + if (!File.Exists(savedAuthorFile)) + { Trace.WriteLine("No authors file used."); - return false; + return false; + } + + if (Authors.Count != 0) + return true; + Trace.WriteLine("Reading cached authors file (" + savedAuthorFile + ")..."); + using (StreamReader sr = new StreamReader(savedAuthorFile)) + { + return Parse(sr); + } } } } From c6c6e551bdf1301f987dd5cb6aaf397daa32df64 Mon Sep 17 00:00:00 2001 From: Philippe Miossec Date: Fri, 22 Dec 2017 21:17:06 +0100 Subject: [PATCH 2/2] Better copy the author file just after the creation of the repository --- src/GitTfs/Commands/Init.cs | 11 ++++++++++- src/GitTfs/GitTfs.cs | 11 ++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/GitTfs/Commands/Init.cs b/src/GitTfs/Commands/Init.cs index 0d20b7533..3cd2e39fe 100644 --- a/src/GitTfs/Commands/Init.cs +++ b/src/GitTfs/Commands/Init.cs @@ -6,6 +6,7 @@ using System.Text.RegularExpressions; using NDesk.Options; using GitTfs.Core; +using GitTfs.Util; using StructureMap; namespace GitTfs.Commands @@ -18,11 +19,13 @@ public class Init : GitTfsCommand private readonly RemoteOptions _remoteOptions; private readonly Globals _globals; private readonly IGitHelpers _gitHelper; + private readonly AuthorsFile _authorsFileHelper; - public Init(RemoteOptions remoteOptions, InitOptions initOptions, Globals globals, IGitHelpers gitHelper) + public Init(RemoteOptions remoteOptions, InitOptions initOptions, Globals globals, IGitHelpers gitHelper, AuthorsFile authorsFileHelper) { _remoteOptions = remoteOptions; _gitHelper = gitHelper; + _authorsFileHelper = authorsFileHelper; _globals = globals; _initOptions = initOptions; } @@ -47,6 +50,7 @@ public int Run(string tfsUrl, string tfsRepositoryPath) tfsRepositoryPath.AssertValidTfsPathOrRoot(); DoGitInitDb(); VerifyGitUserConfig(); + SaveAuthorFileInRepository(); CommitTheGitIgnoreFile(_initOptions.GitIgnorePath); GitTfsInit(tfsUrl, tfsRepositoryPath); return 0; @@ -67,6 +71,11 @@ private void VerifyGitUserConfig() } } + private void SaveAuthorFileInRepository() + { + _authorsFileHelper.SaveAuthorFileInRepository(_globals.AuthorsFilePath, _globals.GitDir); + } + private void CommitTheGitIgnoreFile(string pathToGitIgnoreFile) { if (string.IsNullOrWhiteSpace(pathToGitIgnoreFile)) diff --git a/src/GitTfs/GitTfs.cs b/src/GitTfs/GitTfs.cs index 03dd8becb..6f2f08a83 100644 --- a/src/GitTfs/GitTfs.cs +++ b/src/GitTfs/GitTfs.cs @@ -20,9 +20,10 @@ public class GitTfs private readonly GitTfsCommandRunner _runner; private readonly Globals _globals; private readonly Bootstrapper _bootstrapper; + private readonly AuthorsFile _authorsFileHelper; public GitTfs(GitTfsCommandFactory commandFactory, IHelpHelper help, IContainer container, - IGitTfsVersionProvider gitTfsVersionProvider, GitTfsCommandRunner runner, Globals globals, Bootstrapper bootstrapper) + IGitTfsVersionProvider gitTfsVersionProvider, GitTfsCommandRunner runner, Globals globals, Bootstrapper bootstrapper, AuthorsFile authorsFileHelper) { _commandFactory = commandFactory; _help = help; @@ -31,6 +32,7 @@ public class GitTfs _runner = runner; _globals = globals; _bootstrapper = bootstrapper; + _authorsFileHelper = authorsFileHelper; } public int Run(IList args) @@ -47,7 +49,7 @@ public int Run(IList args) var exitCode = Main(command, unparsedArgs); if (willCreateRepository) { - SaveAuthorFileInRepository(); + _authorsFileHelper.SaveAuthorFileInRepository(_globals.AuthorsFilePath, _globals.GitDir); } return exitCode; } @@ -107,11 +109,6 @@ private void ParseAuthorsAndSave(bool couldSaveAuthorFile) } } - private void SaveAuthorFileInRepository() - { - _container.GetInstance().SaveAuthorFileInRepository(_globals.AuthorsFilePath, _globals.GitDir); - } - public void InitializeGlobals() { var git = _container.GetInstance();