Skip to content

Commit

Permalink
Store the commit message in the correct file
Browse files Browse the repository at this point in the history
and
Replace commit message in an undo-able way
Disable change of message and amend unless Normal
  • Loading branch information
mstv committed May 18, 2019
1 parent fdb6f7e commit 82f125d
Show file tree
Hide file tree
Showing 9 changed files with 447 additions and 121 deletions.
77 changes: 0 additions & 77 deletions GitCommands/CommitHelper.cs

This file was deleted.

124 changes: 124 additions & 0 deletions GitCommands/CommitMessageManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System.IO.Abstractions;
using System.Text;

namespace GitCommands
{
public interface ICommitMessageManager
{
/// <summary>
/// Reads/stores whether the previous commit shall be amended (if AppSettings.RememberAmendCommitState).
/// </summary>
bool AmendState { get; set; }

/// <summary>
/// The pathname of the file where a prepared (non-merge) commit message is stored.
/// </summary>
string CommitMessagePath { get; }

/// <summary>
/// Returns whether .git/MERGE_MSG exists.
/// </summary>
bool IsMergeCommit { get; }

/// <summary>
/// Reads/stores the prepared commit message from/in .git/MERGE_MSG if it exists or else in .git/COMMITMESSAGE.
/// </summary>
string MergeOrCommitMessage { get; set; }

/// <summary>
/// Deletes .git/COMMITMESSAGE and the file with the AmendState.
/// </summary>
void ResetCommitMessage();
}

public sealed class CommitMessageManager : ICommitMessageManager
{
private readonly string _amendSaveStatePath;
private readonly string _commitMessagePath;
private readonly string _mergeMessagePath;

private Encoding _commitEncoding;
private IFileSystem _fileSystem;

public CommitMessageManager(string workingDirGitDir, Encoding commitEncoding)
: this(workingDirGitDir, commitEncoding, new FileSystem())
{
}

private CommitMessageManager(string workingDirGitDir, Encoding commitEncoding, IFileSystem fileSystem)
{
_fileSystem = fileSystem;
_commitEncoding = commitEncoding;
_amendSaveStatePath = GetFilePath(workingDirGitDir, "GitExtensions.amend");
_commitMessagePath = GetFilePath(workingDirGitDir, "COMMITMESSAGE");
_mergeMessagePath = GetFilePath(workingDirGitDir, "MERGE_MSG");
}

public bool AmendState
{
get
{
bool amendState = false;

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

return amendState;
}
set
{
if (AppSettings.RememberAmendCommitState && value)
{
_fileSystem.File.WriteAllText(_amendSaveStatePath, true.ToString());
}
else
{
_fileSystem.File.Delete(_amendSaveStatePath);
}
}
}

public string CommitMessagePath => _commitMessagePath;

public bool IsMergeCommit => _fileSystem.File.Exists(_mergeMessagePath);

public string MergeOrCommitMessage
{
get
{
var (file, exists) = GetMergeOrCommitMessagePath();
return exists ? _fileSystem.File.ReadAllText(file, _commitEncoding) : string.Empty;
}
set
{
_fileSystem.File.WriteAllText(GetMergeOrCommitMessagePath().FilePath, value ?? string.Empty, _commitEncoding);
}
}

public void ResetCommitMessage()
{
_fileSystem.File.Delete(_commitMessagePath);
_fileSystem.File.Delete(_amendSaveStatePath);
}

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

private (string FilePath, bool FileExists) GetMergeOrCommitMessagePath()
{
if (IsMergeCommit)
{
return (_mergeMessagePath, FileExists: true);
}

return (_commitMessagePath, _fileSystem.File.Exists(_commitMessagePath));
}

internal class TestAccessor
{
internal static CommitMessageManager Construct(string workingDirGitDir, Encoding commitEncoding, IFileSystem fileSystem)
=> new CommitMessageManager(workingDirGitDir, commitEncoding, fileSystem);
}
}
}
10 changes: 0 additions & 10 deletions GitCommands/Git/GitModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -868,16 +868,6 @@ public string GetCommitCountString(string from, string to)
(added > 0 ? ("+" + added) : "");
}

public string GetMergeMessage()
{
var file = GetGitDirectory() + "MERGE_MSG";

return
File.Exists(file)
? File.ReadAllText(file)
: "";
}

public void RunGitK()
{
if (EnvUtils.RunningOnUnix())
Expand Down
2 changes: 1 addition & 1 deletion GitCommands/GitCommands.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
<Compile Include="Git\GitSubmoduleStatus.cs" />
<Compile Include="LockableNotifier.cs" />
<Compile Include="Logging\CommandLog.cs" />
<Compile Include="CommitHelper.cs" />
<Compile Include="CommitMessageManager.cs" />
<Compile Include="Config\ConfigFile.cs" />
<Compile Include="Config\ConfigSection.cs" />
<Compile Include="Git\CommandCache.cs" />
Expand Down
Loading

0 comments on commit 82f125d

Please sign in to comment.