|
1 | | -using LibGit2Sharp.Tests.TestHelpers; |
| 1 | +using System.IO; |
| 2 | +using LibGit2Sharp.Tests.TestHelpers; |
2 | 3 | using System.Linq; |
3 | 4 | using Xunit; |
4 | 5 |
|
@@ -37,5 +38,69 @@ public void CannotReadReflogOnUnknownReference() |
37 | 38 | Assert.Throws<InvalidSpecificationException>(() => repo.Refs.Log("toto").Count()); |
38 | 39 | } |
39 | 40 | } |
| 41 | + |
| 42 | + [Fact] |
| 43 | + public void CommitShouldCreateReflogEntryOnHeadandOnTargetedDirectReference() |
| 44 | + { |
| 45 | + SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); |
| 46 | + |
| 47 | + using (var repo = Repository.Init(scd.DirectoryPath)) |
| 48 | + { |
| 49 | + // setup refs as HEAD => unit_test => master |
| 50 | + var newRef = repo.Refs.Add("refs/heads/unit_test", "refs/heads/master"); |
| 51 | + Assert.NotNull(newRef); |
| 52 | + repo.Refs.UpdateTarget(repo.Refs.Head, newRef); |
| 53 | + |
| 54 | + const string relativeFilepath = "new.txt"; |
| 55 | + string filePath = Path.Combine(repo.Info.WorkingDirectory, relativeFilepath); |
| 56 | + |
| 57 | + File.WriteAllText(filePath, "content\n"); |
| 58 | + repo.Index.Stage(relativeFilepath); |
| 59 | + |
| 60 | + var author = DummySignature; |
| 61 | + const string commitMessage = "Hope reflog behaves as it should"; |
| 62 | + Commit commit = repo.Commit(commitMessage, author, author); |
| 63 | + |
| 64 | + // Assert a reflog entry is created on HEAD |
| 65 | + Assert.Equal(1, repo.Refs.Log("HEAD").Count()); |
| 66 | + var reflogEntry = repo.Refs.Log("HEAD").First(); |
| 67 | + Assert.Equal(author, reflogEntry.Commiter); |
| 68 | + Assert.Equal(commit.Id, reflogEntry.To); |
| 69 | + Assert.Equal(ObjectId.Zero, reflogEntry.From); |
| 70 | + |
| 71 | + // Assert the same reflog entry is created on refs/heads/master |
| 72 | + Assert.Equal(1, repo.Refs.Log("refs/heads/master").Count()); |
| 73 | + reflogEntry = repo.Refs.Log("HEAD").First(); |
| 74 | + Assert.Equal(author, reflogEntry.Commiter); |
| 75 | + Assert.Equal(commit.Id, reflogEntry.To); |
| 76 | + Assert.Equal(ObjectId.Zero, reflogEntry.From); |
| 77 | + |
| 78 | + // Assert no reflog entry is created on refs/heads/unit_test |
| 79 | + Assert.Equal(0, repo.Refs.Log("refs/heads/unit_test").Count()); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + [Fact] |
| 84 | + public void CommitOnUnbornReferenceShouldCreateReflogEntryWithInitialTag() |
| 85 | + { |
| 86 | + SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); |
| 87 | + |
| 88 | + using (var repo = Repository.Init(scd.DirectoryPath)) |
| 89 | + { |
| 90 | + const string relativeFilepath = "new.txt"; |
| 91 | + string filePath = Path.Combine(repo.Info.WorkingDirectory, relativeFilepath); |
| 92 | + |
| 93 | + File.WriteAllText(filePath, "content\n"); |
| 94 | + repo.Index.Stage(relativeFilepath); |
| 95 | + |
| 96 | + var author = DummySignature; |
| 97 | + const string commitMessage = "First commit should be logged as initial"; |
| 98 | + repo.Commit(commitMessage, author, author); |
| 99 | + |
| 100 | + // Assert the reflog entry message is correct |
| 101 | + Assert.Equal(1, repo.Refs.Log("HEAD").Count()); |
| 102 | + Assert.Equal(string.Format("commit (initial): {0}", commitMessage), repo.Refs.Log("HEAD").First().Message); |
| 103 | + } |
| 104 | + } |
40 | 105 | } |
41 | 106 | } |
0 commit comments