Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Feb 3, 2023. It is now read-only.

Commit d8ac9e4

Browse files
mumitrollernulltoken
authored andcommitted
Fix Commit() so that it always updates the HEAD
Fix libgit2#692
1 parent 5a00322 commit d8ac9e4

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

LibGit2Sharp.Tests/RepositoryOptionsFixture.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Linq;
34
using System.Text;
45
using LibGit2Sharp.Tests.TestHelpers;
56
using Xunit;
@@ -174,5 +175,31 @@ public void CanProvideDifferentConfigurationFilesToARepository()
174175

175176
AssertValueInConfigFile(systemLocation, "xpaulbettsx");
176177
}
178+
179+
[Fact]
180+
public void CanCommitOnBareRepository()
181+
{
182+
string repoPath = InitNewRepository(true);
183+
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
184+
string workPath = Path.Combine(scd.RootedDirectoryPath, "work");
185+
Directory.CreateDirectory(workPath);
186+
187+
var repositoryOptions = new RepositoryOptions
188+
{
189+
WorkingDirectoryPath = workPath,
190+
IndexPath = Path.Combine(scd.RootedDirectoryPath, "index")
191+
};
192+
193+
using (var repo = new Repository(repoPath, repositoryOptions))
194+
{
195+
const string relativeFilepath = "test.txt";
196+
Touch(repo.Info.WorkingDirectory, relativeFilepath, "test\n");
197+
repo.Index.Stage(relativeFilepath);
198+
199+
Assert.NotNull(repo.Commit("Initial commit", Constants.Signature, Constants.Signature));
200+
Assert.Equal(1, repo.Head.Commits.Count());
201+
Assert.Equal(1, repo.Commits.Count());
202+
}
203+
}
177204
}
178205
}

LibGit2Sharp/Repository.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -894,14 +894,8 @@ public Commit Commit(string message, Signature author, Signature committer, Comm
894894

895895
Proxy.git_repository_state_cleanup(handle);
896896

897-
var logAllRefUpdates = Config.GetValueOrDefault<bool>("core.logAllRefUpdates", false);
898-
if (!logAllRefUpdates)
899-
{
900-
return result;
901-
}
902-
903897
var logMessage = BuildCommitLogMessage(result, options.AmendPreviousCommit, isHeadOrphaned, parents.Count > 1);
904-
LogCommit(result, logMessage);
898+
UpdateHeadAndTerminalReference(result, logMessage);
905899

906900
return result;
907901
}
@@ -925,7 +919,7 @@ private static string BuildCommitLogMessage(Commit commit, bool amendPreviousCom
925919
return string.Format(CultureInfo.InvariantCulture, "commit{0}: {1}", kind, commit.MessageShort);
926920
}
927921

928-
private void LogCommit(Commit commit, string reflogMessage)
922+
private void UpdateHeadAndTerminalReference(Commit commit, string reflogMessage)
929923
{
930924
Reference reference = Refs.Head;
931925

0 commit comments

Comments
 (0)