Skip to content

Commit

Permalink
Handle errors accessing the amend state file
Browse files Browse the repository at this point in the history
the same way as for the commit message
  • Loading branch information
mstv committed Mar 28, 2021
1 parent ea24f08 commit 52c5c72
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 44 deletions.
87 changes: 46 additions & 41 deletions GitCommands/CommitMessageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public sealed class CommitMessageManager : ICommitMessageManager
{
private string CannotReadCommitMessage => "Cannot read commit message";
private string CannotSaveCommitMessage => "Cannot save commit message";
private string CannotReadAmendState => "Cannot read amend state";
private string CannotSaveAmendState => "Cannot save amend state";
private string CannotAccessFile => "Exception: \"{0}\" when accessing {1}";

private readonly string _amendSaveStatePath;
Expand Down Expand Up @@ -91,9 +93,9 @@ public bool AmendState
{
bool amendState = false;

if (AppSettings.RememberAmendCommitState && _fileSystem.File.Exists(_amendSaveStatePath))
if (AppSettings.RememberAmendCommitState)
{
bool.TryParse(_fileSystem.File.ReadAllText(_amendSaveStatePath), out amendState);
bool.TryParse(ReadFile(_amendSaveStatePath, CannotReadAmendState), out amendState);
}

return amendState;
Expand All @@ -102,9 +104,11 @@ public bool AmendState
{
if (AppSettings.RememberAmendCommitState && value)
{
_fileSystem.File.WriteAllText(_amendSaveStatePath, true.ToString());
WriteFile(_amendSaveStatePath, CannotSaveAmendState, true.ToString());
return;
}
else

if (_fileSystem.File.Exists(_amendSaveStatePath))
{
_fileSystem.File.Delete(_amendSaveStatePath);
}
Expand All @@ -130,21 +134,7 @@ public string MergeOrCommitMessage
return _overriddenCommitMessage;
}

var (file, exists) = GetMergeOrCommitMessagePath();
string result;
try
{
result = exists ? _fileSystem.File.ReadAllText(file, _commitEncoding) : string.Empty;
}
catch (Exception ex)
{
MessageBox.Show(null, string.Format(CannotAccessFile, ex.Message, file),
CannotReadCommitMessage, MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
result = string.Empty;
}

return result;
return ReadFile(GetMergeOrCommitMessagePath(), CannotReadCommitMessage, _commitEncoding);
}
set
{
Expand All @@ -153,23 +143,7 @@ public string MergeOrCommitMessage
// do not remember commit message when they have been specified by the command line
if (content != _overriddenCommitMessage)
{
var path = GetMergeOrCommitMessagePath().filePath;
if (!_fileSystem.Directory.Exists(Path.GetDirectoryName(path)))
{
// The repo no longer exists
return;
}

try
{
_fileSystem.File.WriteAllText(path, content, _commitEncoding);
}
catch (Exception ex)
{
MessageBox.Show(null, string.Format(CannotAccessFile, ex.Message, path),
CannotSaveCommitMessage, MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
WriteFile(GetMergeOrCommitMessagePath(), CannotSaveCommitMessage, content, _commitEncoding);
}
}
}
Expand All @@ -188,7 +162,7 @@ public void WriteCommitMessageToFile(string commitMessage, CommitMessageType mes
var formattedCommitMessage = FormatCommitMessage(commitMessage, usingCommitTemplate, ensureCommitMessageSecondLineEmpty);

string path = messageType == CommitMessageType.Normal ? CommitMessagePath : MergeMessagePath;
File.WriteAllText(path, formattedCommitMessage, _commitEncoding);
WriteFile(path, CannotSaveCommitMessage, formattedCommitMessage, _commitEncoding);
}

internal static string FormatCommitMessage(string commitMessage, bool usingCommitTemplate, bool ensureCommitMessageSecondLineEmpty)
Expand Down Expand Up @@ -227,14 +201,45 @@ internal static string FormatCommitMessage(string commitMessage, bool usingCommi

private string GetFilePath(string workingDirGitDir, string fileName) => _fileSystem.Path.Combine(workingDirGitDir, fileName);

private (string filePath, bool fileExists) GetMergeOrCommitMessagePath()
private string GetMergeOrCommitMessagePath() => IsMergeCommit ? MergeMessagePath : CommitMessagePath;

private string ReadFile(string filePath, string errorTitle, Encoding? encoding = null)
{
try
{
if (!_fileSystem.File.Exists(filePath))
{
return string.Empty;
}

return _fileSystem.File.ReadAllText(filePath, encoding ?? Encoding.Default);
}
catch (Exception ex)
{
MessageBox.Show(null, string.Format(CannotAccessFile, ex.Message, filePath), errorTitle,
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return string.Empty;
}
}

private void WriteFile(string filePath, string errorTitle, string content, Encoding? encoding = null)
{
if (IsMergeCommit)
if (!_fileSystem.Directory.Exists(Path.GetDirectoryName(filePath)))
{
return (MergeMessagePath, fileExists: true);
// The repo no longer exists - ignore silently
return;
}

return (CommitMessagePath, _fileSystem.File.Exists(CommitMessagePath));
try
{
_fileSystem.File.WriteAllText(filePath, content, encoding ?? Encoding.Default);
}
catch (Exception ex)
{
// No need to cancel the other operations in FormCommit - just let the user know that something went wrong
MessageBox.Show(null, string.Format(CannotAccessFile, ex.Message, filePath), errorTitle,
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
}
9 changes: 6 additions & 3 deletions UnitTests/GitCommands.Tests/CommitMessageManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public void AmendState_should_be_false_if_not_RememberAmendCommitState()
public void AmendState_should_be_false_if_file_contains(string amendText)
{
_file.Exists(_amendSaveStatePath).Returns(true);
_file.ReadAllText(_amendSaveStatePath).Returns(amendText);
_file.ReadAllText(_amendSaveStatePath, Encoding.Default).Returns(amendText);

AppSettings.RememberAmendCommitState = true;
_manager.AmendState.Should().BeFalse();
Expand All @@ -148,7 +148,7 @@ public void AmendState_should_be_false_if_file_contains(string amendText)
public void AmendState_should_be_true_if_file_contains(string amendText)
{
_file.Exists(_amendSaveStatePath).Returns(true);
_file.ReadAllText(_amendSaveStatePath).Returns(amendText);
_file.ReadAllText(_amendSaveStatePath, Encoding.Default).Returns(amendText);

AppSettings.RememberAmendCommitState = true;
_manager.AmendState.Should().BeTrue();
Expand All @@ -158,7 +158,8 @@ public void AmendState_should_be_true_if_file_contains(string amendText)
public void AmendState_true_should_write_true_to_file()
{
bool correctlyWritten = false;
_file.When(x => x.WriteAllText(_amendSaveStatePath, true.ToString())).Do(_ => correctlyWritten = true);
_file.When(x => x.WriteAllText(_amendSaveStatePath, true.ToString(), Encoding.Default)).Do(_ => correctlyWritten = true);
_directory.Exists(Path.GetDirectoryName(_amendSaveStatePath)).Returns(true);

AppSettings.RememberAmendCommitState = true;
_manager.AmendState = true;
Expand All @@ -170,6 +171,7 @@ public void AmendState_true_should_write_true_to_file()
public void AmendState_true_should_delete_file_if_not_RememberAmendCommitState()
{
bool correctlyDeleted = false;
_file.Exists(_amendSaveStatePath).Returns(true);
_file.When(x => x.Delete(_amendSaveStatePath)).Do(_ => correctlyDeleted = true);

AppSettings.RememberAmendCommitState = false;
Expand All @@ -184,6 +186,7 @@ public void AmendState_false_should_delete_file(bool rememberAmendCommitState)
{
bool correctlyDeleted = false;
_file.When(x => x.Delete(_amendSaveStatePath)).Do(_ => correctlyDeleted = true);
_file.Exists(_amendSaveStatePath).Returns(true);

AppSettings.RememberAmendCommitState = rememberAmendCommitState;
_manager.AmendState = false;
Expand Down

0 comments on commit 52c5c72

Please sign in to comment.