Skip to content

Commit

Permalink
ReferenceRepository: Write git index (#10699)
Browse files Browse the repository at this point in the history
  • Loading branch information
mstv committed Mar 6, 2023
1 parent 5d87bdf commit 04ec0e2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,23 +201,29 @@ public void PreserveCommitMessageOnReopenFromAmendCommit()
[Test]
public void SelectMessageFromHistory()
{
const string lastCommitMessage = "last commit message";
AppSettings.LastCommitMessage = lastCommitMessage;

RunFormTest(form =>
{
var commitMessageToolStripMenuItem = form.GetTestAccessor().CommitMessageToolStripMenuItem;
// Verify the message appears correctly
commitMessageToolStripMenuItem.ShowDropDown();
Assert.AreEqual("A commit message", commitMessageToolStripMenuItem.DropDownItems[0].Text);
commitMessageToolStripMenuItem.DropDownItems[0].Text.Should().Be(lastCommitMessage);
// Verify the message is selected correctly
commitMessageToolStripMenuItem.DropDownItems[0].PerformClick();
Assert.AreEqual("A commit message", form.GetTestAccessor().Message.Text);
form.GetTestAccessor().Message.Text.Should().Be(lastCommitMessage);
});
}

[Test]
public void Should_handle_well_commit_message_in_commit_message_menu()
{
const string lastCommitMessage = "last commit message";
AppSettings.LastCommitMessage = lastCommitMessage;

_referenceRepository.CreateCommit("Only first line\n\nof a multi-line commit message\nmust be displayed in the menu");
_referenceRepository.CreateCommit("Too long commit message that should be shorten because first line of a commit message is only 50 chars long");
RunFormTest(form =>
Expand All @@ -226,8 +232,9 @@ public void Should_handle_well_commit_message_in_commit_message_menu()
// Verify the message appears correctly
commitMessageToolStripMenuItem.ShowDropDown();
Assert.AreEqual("Too long commit message that should be shorten because first line of ...", commitMessageToolStripMenuItem.DropDownItems[0].Text);
Assert.AreEqual("Only first line", commitMessageToolStripMenuItem.DropDownItems[1].Text);
commitMessageToolStripMenuItem.DropDownItems[0].Text.Should().Be(lastCommitMessage);
commitMessageToolStripMenuItem.DropDownItems[1].Text.Should().Be("Too long commit message that should be shorten because first line of ...");
commitMessageToolStripMenuItem.DropDownItems[2].Text.Should().Be("Only first line");
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ public void View_reflects_reset_branch_filter()
[Test]
public void ToggleBetweenArtificialAndHeadCommits_no_empty([Values(false, true)] bool showGitStatusForArtificialCommits)
{
File.WriteAllText(Path.Combine(_referenceRepository.Module.WorkingDir, "new.txt"), "new");
File.WriteAllText(Path.Combine(_referenceRepository.Module.WorkingDir, "stage.txt"), "staged");
_referenceRepository.Module.StageFile("stage.txt");

RunToggleBetweenArtificialAndHeadCommitsTest(
showGitStatusForArtificialCommits,
revisionGridControl =>
Expand Down Expand Up @@ -231,7 +235,8 @@ public void ToggleBetweenArtificialAndHeadCommits_no_empty([Values(false, true)]
[Test]
public void ToggleBetweenArtificialAndHeadCommits_no_workdir_change([Values(false, true)] bool showGitStatusForArtificialCommits)
{
File.Delete(Path.Combine(_referenceRepository.Module.WorkingDir, "A.txt"));
File.WriteAllText(Path.Combine(_referenceRepository.Module.WorkingDir, "stage.txt"), "staged");
_referenceRepository.Module.StageFile("stage.txt");

RunToggleBetweenArtificialAndHeadCommitsTest(
showGitStatusForArtificialCommits,
Expand Down Expand Up @@ -267,7 +272,6 @@ public void ToggleBetweenArtificialAndHeadCommits_no_workdir_change([Values(fals
[Test]
public void ToggleBetweenArtificialAndHeadCommits_no_index_change([Values(false, true)] bool showGitStatusForArtificialCommits)
{
_referenceRepository.Module.Reset(ResetMode.Hard);
File.WriteAllText(Path.Combine(_referenceRepository.Module.WorkingDir, "new.txt"), "new");

RunToggleBetweenArtificialAndHeadCommitsTest(
Expand Down Expand Up @@ -303,8 +307,6 @@ public void ToggleBetweenArtificialAndHeadCommits_no_index_change([Values(false,
[Test]
public void ToggleBetweenArtificialAndHeadCommits_no_change([Values(false, true)] bool showGitStatusForArtificialCommits)
{
_referenceRepository.Module.Reset(ResetMode.Hard);

RunToggleBetweenArtificialAndHeadCommitsTest(
showGitStatusForArtificialCommits,
revisionGridControl =>
Expand Down
18 changes: 13 additions & 5 deletions UnitTests/CommonTestUtils/ReferenceRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public static void ResetRepo(ref ReferenceRepository? refRepo)
catch (LibGit2Sharp.LockedFileException)
{
// the index is locked; this might be due to a concurrent or crashed process
refRepo.Dispose();
refRepo = new ReferenceRepository();
Trace.WriteLine("Repo is locked, creating new");
}
Expand All @@ -46,12 +47,19 @@ public static void ResetRepo(ref ReferenceRepository? refRepo)

private const string _fileName = "A.txt";

private void IndexAdd(Repository repository, string fileName)
{
repository.Index.Add(fileName);
repository.Index.Write();
}

private string Commit(Repository repository, string commitMessage)
{
LibGit2Sharp.Signature author = new("GitUITests", "unittests@gitextensions.com", DateTimeOffset.Now);
var committer = author;
LibGit2Sharp.CommitOptions options = new() { PrettifyMessage = false };
var commit = repository.Commit(commitMessage, author, committer, options);
repository.Index.Write();
return commit.Id.Sha;
}

Expand All @@ -66,7 +74,7 @@ public string CreateCommit(string commitMessage, string content = null)
{
using LibGit2Sharp.Repository repository = new(_moduleTestHelper.Module.WorkingDir);
_moduleTestHelper.CreateRepoFile(_fileName, content ?? commitMessage);
repository.Index.Add(_fileName);
IndexAdd(repository, _fileName);

CommitHash = Commit(repository, commitMessage);
Console.WriteLine($"Created commit: {CommitHash}, message: {commitMessage}");
Expand All @@ -77,11 +85,11 @@ public string CreateCommit(string commitMessage, string content1, string fileNam
{
using LibGit2Sharp.Repository repository = new(_moduleTestHelper.Module.WorkingDir);
_moduleTestHelper.CreateRepoFile(fileName1, content1);
repository.Index.Add(fileName1);
IndexAdd(repository, fileName1);
if (content2 != null && fileName2 != null)
{
_moduleTestHelper.CreateRepoFile(fileName2, content2);
repository.Index.Add(fileName2);
IndexAdd(repository, fileName2);
}

CommitHash = Commit(repository, commitMessage);
Expand All @@ -95,7 +103,7 @@ public string CreateCommitRelative(string fileRelativePath, string fileName, str
{
using Repository repository = new(_moduleTestHelper.Module.WorkingDir);
_moduleTestHelper.CreateRepoFile(fileRelativePath, fileName, content ?? commitMessage);
repository.Index.Add(Path.Combine(fileRelativePath, fileName));
IndexAdd(repository, Path.Combine(fileRelativePath, fileName));

CommitHash = Commit(repository, commitMessage);
Console.WriteLine($"Created commit: {CommitHash}, message: {commitMessage}");
Expand Down Expand Up @@ -177,7 +185,7 @@ public void Stash(string stashMessage, string content = null)
{
using LibGit2Sharp.Repository repository = new(_moduleTestHelper.Module.WorkingDir);
_moduleTestHelper.CreateRepoFile(_fileName, content ?? stashMessage);
repository.Index.Add(_fileName);
IndexAdd(repository, _fileName);

LibGit2Sharp.Signature author = new("GitUITests", "unittests@gitextensions.com", DateTimeOffset.Now);
Stash stash = repository.Stashes.Add(author, stashMessage);
Expand Down

0 comments on commit 04ec0e2

Please sign in to comment.