Skip to content

Commit

Permalink
Fix failing copy of author file when repository still not initialized
Browse files Browse the repository at this point in the history
(`clone` and `init` commands)

We now try to copy the file after the command is run.

+ refactor AuthorFile.cs

Should fix #1094
  • Loading branch information
pmiossec committed Jan 5, 2018
1 parent f0bb594 commit bbb9352
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 50 deletions.
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
36 changes: 22 additions & 14 deletions src/GitTfs/GitTfs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,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)
{
SaveAuthorFileInRepository();
}
return exitCode;
}

private void UpdateLoggerOnDebugging()
Expand All @@ -64,22 +70,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 +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<AuthorsFile>().Parse(_globals.AuthorsFilePath, _globals.GitDir);
_container.GetInstance<AuthorsFile>().Parse(_globals.AuthorsFilePath, _globals.GitDir, couldSaveAuthorFile);
}
catch (Exception ex)
{
Expand All @@ -104,6 +107,11 @@ private void ParseAuthors()
}
}

private void SaveAuthorFileInRepository()
{
_container.GetInstance<AuthorsFile>().SaveAuthorFileInRepository(_globals.AuthorsFilePath, _globals.GitDir);
}

public void InitializeGlobals()
{
var git = _container.GetInstance<IGitHelpers>();
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);
}
}
}
}

0 comments on commit bbb9352

Please sign in to comment.