Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix author file copy on init repo #1148

Merged
merged 2 commits into from
Jan 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/GitTfs/Commands/Init.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Text.RegularExpressions;
using NDesk.Options;
using GitTfs.Core;
using GitTfs.Util;
using StructureMap;

namespace GitTfs.Commands
Expand All @@ -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;
}
Expand All @@ -47,6 +50,7 @@ public int Run(string tfsUrl, string tfsRepositoryPath)
tfsRepositoryPath.AssertValidTfsPathOrRoot();
DoGitInitDb();
VerifyGitUserConfig();
SaveAuthorFileInRepository();
CommitTheGitIgnoreFile(_initOptions.GitIgnorePath);
GitTfsInit(tfsUrl, tfsRepositoryPath);
return 0;
Expand All @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion src/GitTfs/Commands/Rcheckin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
35 changes: 20 additions & 15 deletions src/GitTfs/GitTfs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +32,7 @@ public class GitTfs
_runner = runner;
_globals = globals;
_bootstrapper = bootstrapper;
_authorsFileHelper = authorsFileHelper;
}

public int Run(IList<string> args)
Expand All @@ -42,8 +44,14 @@ public int Run(IList<string> 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)
{
_authorsFileHelper.SaveAuthorFileInRepository(_globals.AuthorsFilePath, _globals.GitDir);
}
return exitCode;
}

private void UpdateLoggerOnDebugging()
Expand All @@ -64,22 +72,19 @@ public int Main(GitTfsCommand command, IList<string> 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<Janitor>().Dispose();
}
return _runner.Run(command, unparsedArgs);
}
finally
{
_container.GetInstance<Janitor>().Dispose();
}
}

Expand All @@ -88,11 +93,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<AuthorsFile>().Parse(_globals.AuthorsFilePath, _globals.GitDir);
_container.GetInstance<AuthorsFile>().Parse(_globals.AuthorsFilePath, _globals.GitDir, couldSaveAuthorFile);
}
catch (Exception ex)
{
Expand Down
88 changes: 53 additions & 35 deletions src/GitTfs/Util/AuthorsFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}