|
| 1 | +package git |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +func allReflogEntries(t *testing.T, repo *Repository, refName string) (entries []*RefLogEntry) { |
| 11 | + rl, err := repo.ReadRefLog(refName) |
| 12 | + checkFatal(t, err) |
| 13 | + defer rl.Free() |
| 14 | + |
| 15 | + for i := uint(0); i < rl.EntryCount(); i++ { |
| 16 | + entries = append(entries, rl.EntryByIndex(i)) |
| 17 | + } |
| 18 | + return entries |
| 19 | +} |
| 20 | + |
| 21 | +// assertEntriesEqual will assert that the reflogs match with the exception of |
| 22 | +// the signature time (it is not reliably deterministic to predict the |
| 23 | +// signature time during many reference updates) |
| 24 | +func assertEntriesEqual(t *testing.T, got, want []*RefLogEntry) { |
| 25 | + if len(got) != len(want) { |
| 26 | + t.Fatalf("got %d length, wanted %d length", len(got), len(want)) |
| 27 | + } |
| 28 | + |
| 29 | + for i := 0; i < len(got); i++ { |
| 30 | + gi := got[i] |
| 31 | + wi := want[i] |
| 32 | + // remove the signature time to make the results deterministic |
| 33 | + gi.Committer.When = time.Time{} |
| 34 | + wi.Committer.When = time.Time{} |
| 35 | + // check committer separately to print results clearly |
| 36 | + if !reflect.DeepEqual(gi.Committer, wi.Committer) { |
| 37 | + t.Fatalf("got committer %v, want committer %v", |
| 38 | + gi.Committer, wi.Committer) |
| 39 | + } |
| 40 | + if !reflect.DeepEqual(gi, wi) { |
| 41 | + t.Fatalf("got %v, want %v", gi, wi) |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestReflog(t *testing.T) { |
| 47 | + t.Parallel() |
| 48 | + repo := createTestRepo(t) |
| 49 | + defer cleanupTestRepo(t, repo) |
| 50 | + |
| 51 | + commitID, treeId := seedTestRepo(t, repo) |
| 52 | + |
| 53 | + testRefName := "refs/heads/test" |
| 54 | + |
| 55 | + // configure committer for deterministic reflog entries |
| 56 | + cfg, err := repo.Config() |
| 57 | + checkFatal(t, err) |
| 58 | + |
| 59 | + sig := &Signature{ |
| 60 | + Name: "Rand Om Hacker", |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + checkFatal(t, cfg.SetString("user.name", sig.Name)) |
| 65 | + checkFatal(t, cfg.SetString("user.email", sig.Email)) |
| 66 | + |
| 67 | + checkFatal(t, repo.References.EnsureLog(testRefName)) |
| 68 | + _, err = repo.References.Create(testRefName, commitID, true, "first update") |
| 69 | + checkFatal(t, err) |
| 70 | + got := allReflogEntries(t, repo, testRefName) |
| 71 | + want := []*RefLogEntry{ |
| 72 | + &RefLogEntry{ |
| 73 | + New: commitID, |
| 74 | + Old: &Oid{}, |
| 75 | + Committer: sig, |
| 76 | + Message: "first update", |
| 77 | + }, |
| 78 | + } |
| 79 | + |
| 80 | + // create additional commits and verify they are added to reflog |
| 81 | + tree, err := repo.LookupTree(treeId) |
| 82 | + checkFatal(t, err) |
| 83 | + for i := 0; i < 10; i++ { |
| 84 | + nextEntry := &RefLogEntry{ |
| 85 | + Old: commitID, |
| 86 | + Committer: sig, |
| 87 | + Message: fmt.Sprintf("commit: %d", i), |
| 88 | + } |
| 89 | + |
| 90 | + commit, err := repo.LookupCommit(commitID) |
| 91 | + checkFatal(t, err) |
| 92 | + |
| 93 | + commitID, err = repo.CreateCommit(testRefName, sig, sig, fmt.Sprint(i), tree, commit) |
| 94 | + checkFatal(t, err) |
| 95 | + |
| 96 | + nextEntry.New = commitID |
| 97 | + |
| 98 | + want = append([]*RefLogEntry{nextEntry}, want...) |
| 99 | + } |
| 100 | + got = allReflogEntries(t, repo, testRefName) |
| 101 | + |
| 102 | + t.Run("ReadReflog", func(t *testing.T) { |
| 103 | + assertEntriesEqual(t, got, want) |
| 104 | + }) |
| 105 | + |
| 106 | + t.Run("DropEntry", func(t *testing.T) { |
| 107 | + rl, err := repo.ReadRefLog(testRefName) |
| 108 | + checkFatal(t, err) |
| 109 | + defer rl.Free() |
| 110 | + |
| 111 | + gotBefore := allReflogEntries(t, repo, testRefName) |
| 112 | + |
| 113 | + checkFatal(t, rl.DropEntry(0, false)) |
| 114 | + checkFatal(t, rl.Write()) |
| 115 | + |
| 116 | + gotAfter := allReflogEntries(t, repo, testRefName) |
| 117 | + |
| 118 | + assertEntriesEqual(t, gotAfter, gotBefore[1:]) |
| 119 | + }) |
| 120 | + |
| 121 | + t.Run("AppendEntry", func(t *testing.T) { |
| 122 | + logs := allReflogEntries(t, repo, testRefName) |
| 123 | + |
| 124 | + rl, err := repo.ReadRefLog(testRefName) |
| 125 | + checkFatal(t, err) |
| 126 | + defer rl.Free() |
| 127 | + |
| 128 | + newOID := NewOidFromBytes([]byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}) |
| 129 | + checkFatal(t, rl.AppendEntry(newOID, sig, "synthetic")) |
| 130 | + checkFatal(t, rl.Write()) |
| 131 | + |
| 132 | + want := append([]*RefLogEntry{ |
| 133 | + &RefLogEntry{ |
| 134 | + New: newOID, |
| 135 | + Old: logs[0].New, |
| 136 | + Committer: sig, |
| 137 | + Message: "synthetic", |
| 138 | + }, |
| 139 | + }, logs...) |
| 140 | + got := allReflogEntries(t, repo, testRefName) |
| 141 | + assertEntriesEqual(t, got, want) |
| 142 | + }) |
| 143 | + |
| 144 | + t.Run("RenameReflog", func(t *testing.T) { |
| 145 | + logs := allReflogEntries(t, repo, testRefName) |
| 146 | + newRefName := "refs/heads/new" |
| 147 | + |
| 148 | + checkFatal(t, repo.RenameReflog(testRefName, newRefName)) |
| 149 | + assertEntriesEqual(t, allReflogEntries(t, repo, testRefName), nil) |
| 150 | + assertEntriesEqual(t, allReflogEntries(t, repo, newRefName), logs) |
| 151 | + |
| 152 | + checkFatal(t, repo.RenameReflog(newRefName, testRefName)) |
| 153 | + assertEntriesEqual(t, allReflogEntries(t, repo, testRefName), logs) |
| 154 | + assertEntriesEqual(t, allReflogEntries(t, repo, newRefName), nil) |
| 155 | + }) |
| 156 | + |
| 157 | + t.Run("DeleteReflog", func(t *testing.T) { |
| 158 | + checkFatal(t, repo.DeleteReflog(testRefName)) |
| 159 | + assertEntriesEqual(t, allReflogEntries(t, repo, testRefName), nil) |
| 160 | + }) |
| 161 | + |
| 162 | +} |
0 commit comments