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 26, 2021
1 parent c9b8c73 commit 04e20d1
Showing 1 changed file with 45 additions and 41 deletions.
86 changes: 45 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,44 @@ 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
return;
}

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

0 comments on commit 04e20d1

Please sign in to comment.