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

Skip to content

Additional tests for Reflog #390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 11, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions LibGit2Sharp.Tests/CommitFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -623,14 +623,19 @@ public void CanCommitALittleBit()
Assert.Equal(0, commit.Parents.Count());
Assert.False(repo.Info.IsHeadOrphaned);

// Assert a reflog entry is created
// Assert a reflog entry is created on HEAD
Assert.Equal(1, repo.Refs.Log("HEAD").Count());
var reflogEntry = repo.Refs.Log("HEAD").First();
Assert.Equal(author, reflogEntry.Commiter);
Assert.Equal(commit.Id, reflogEntry.To);
Assert.Equal(ObjectId.Zero, reflogEntry.From);
Assert.Equal(string.Format("commit (initial): {0}", commitMessage), reflogEntry.Message);

// Assert a reflog entry is created on HEAD target
var targetCanonicalName = repo.Refs.Head.TargetIdentifier;
Assert.Equal(1, repo.Refs.Log(targetCanonicalName).Count());
Assert.Equal(commit.Id, repo.Refs.Log(targetCanonicalName).First().To);

File.WriteAllText(filePath, "nulltoken commits!\n");
repo.Index.Stage(relativeFilepath);

Expand Down Expand Up @@ -783,7 +788,7 @@ public void CanNotAmendAnEmptyRepository()

using (Repository repo = Repository.Init(scd.DirectoryPath))
{
Assert.Throws<LibGit2SharpException>(() => repo.Commit("I can not amend anything !:(", DummySignature, DummySignature, true));
Assert.Throws<OrphanedHeadException>(() => repo.Commit("I can not amend anything !:(", DummySignature, DummySignature, true));
}
}

Expand Down
31 changes: 31 additions & 0 deletions LibGit2Sharp.Tests/ReflogFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,36 @@ public void CommitOnUnbornReferenceShouldCreateReflogEntryWithInitialTag()
Assert.Equal(string.Format("commit (initial): {0}", commitMessage), repo.Refs.Log("HEAD").First().Message);
}
}

[Fact]
public void CommitOnDetachedHeadShouldInsertReflogEntry()
{
string repoPath = CloneStandardTestRepo();

using (var repo = new Repository(repoPath))
{
Assert.False(repo.Info.IsHeadDetached);

var parentCommit = repo.Head.Tip.Parents.First();
repo.Checkout(parentCommit.Sha);
Assert.True(repo.Info.IsHeadDetached);

const string relativeFilepath = "new.txt";
string filePath = Path.Combine(repo.Info.WorkingDirectory, relativeFilepath);

File.WriteAllText(filePath, "content\n");
repo.Index.Stage(relativeFilepath);

var author = DummySignature;
const string commitMessage = "Commit on detached head";
var commit = repo.Commit(commitMessage, author, author);

// Assert a reflog entry is created on HEAD
var reflogEntry = repo.Refs.Log("HEAD").First();
Assert.Equal(author, reflogEntry.Commiter);
Assert.Equal(commit.Id, reflogEntry.To);
Assert.Equal(string.Format("commit: {0}", commitMessage), repo.Refs.Log("HEAD").First().Message);
}
}
}
}
12 changes: 7 additions & 5 deletions LibGit2Sharp/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -670,9 +670,11 @@ public void Reset(Commit commit, IEnumerable<string> paths = null, ExplicitPaths
/// <returns>The generated <see cref = "Commit" />.</returns>
public Commit Commit(string message, Signature author, Signature committer, bool amendPreviousCommit = false)
{
if (amendPreviousCommit && Info.IsHeadOrphaned)
bool isHeadOrphaned = Info.IsHeadOrphaned;

if (amendPreviousCommit && isHeadOrphaned)
{
throw new LibGit2SharpException("Can not amend anything. The Head doesn't point at any commit.");
throw new OrphanedHeadException("Can not amend anything. The Head doesn't point at any commit.");
}

var treeId = Proxy.git_tree_create_fromindex(Index);
Expand All @@ -685,16 +687,16 @@ public Commit Commit(string message, Signature author, Signature committer, bool
Proxy.git_repository_merge_cleanup(handle);

// Insert reflog entry
LogCommit(result, amendPreviousCommit, parents.Count() == 0);
LogCommit(result, amendPreviousCommit, isHeadOrphaned);

return result;
}

private void LogCommit(Commit commit, bool amendPreviousCommit, bool isInitialCommit)
private void LogCommit(Commit commit, bool amendPreviousCommit, bool isHeadOrphaned)
{
// Compute reflog message
string reflogMessage = "commit";
if (isInitialCommit)
if (isHeadOrphaned)
{
reflogMessage += " (initial)";
}
Expand Down