Navigate to snapshots from the transcript
This commit is contained in:
+13
-6
@@ -18,20 +18,27 @@ var statuses = map[byte]api.FileChangeStatus{
|
||||
}
|
||||
|
||||
func Changes(dir, defaultBranch, since string) (api.TaskChanges, error) {
|
||||
tree, err := Snapshot(dir)
|
||||
if err != nil {
|
||||
return api.TaskChanges{}, err
|
||||
}
|
||||
return Compare(dir, defaultBranch, "HEAD", tree, since)
|
||||
}
|
||||
|
||||
func Parent(dir, commit string) (string, error) {
|
||||
return run(dir, "rev-parse", "--verify", commit+"^")
|
||||
}
|
||||
|
||||
func Compare(dir, defaultBranch, head, tree, since string) (api.TaskChanges, error) {
|
||||
base := since
|
||||
if base == "" {
|
||||
mb, err := run(dir, "merge-base", "HEAD", defaultBranch)
|
||||
mb, err := run(dir, "merge-base", head, defaultBranch)
|
||||
if err != nil {
|
||||
return api.TaskChanges{}, err
|
||||
}
|
||||
base = mb
|
||||
}
|
||||
|
||||
tree, err := Snapshot(dir)
|
||||
if err != nil {
|
||||
return api.TaskChanges{}, err
|
||||
}
|
||||
|
||||
files, err := stats(dir, base, tree)
|
||||
if err != nil {
|
||||
return api.TaskChanges{}, err
|
||||
|
||||
@@ -142,3 +142,87 @@ func TestChanges(t *testing.T) {
|
||||
t.Fatalf("CommitAll on a clean tree: %q %v", again, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompareSnapshots(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
git := func(args ...string) string {
|
||||
t.Helper()
|
||||
out, err := run(dir, args...)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return out
|
||||
}
|
||||
write := func(name, content string) {
|
||||
t.Helper()
|
||||
err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0o644)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
snap := func(n int) (string, string) {
|
||||
t.Helper()
|
||||
tree, err := Snapshot(dir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
commit, err := CommitSnapshot(dir, tree, SnapshotRef("t1", n), "snap", nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return commit, tree
|
||||
}
|
||||
paths := func(c api.TaskChanges) string {
|
||||
names := []string{}
|
||||
for _, f := range c.Files {
|
||||
names = append(names, f.Path)
|
||||
}
|
||||
return strings.Join(names, ",")
|
||||
}
|
||||
|
||||
git("init", "--quiet", "--initial-branch=main")
|
||||
git("config", "user.email", "test@example.com")
|
||||
git("config", "user.name", "test")
|
||||
write("a.txt", "a\n")
|
||||
git("add", ".")
|
||||
git("commit", "--quiet", "-m", "base")
|
||||
git("switch", "--quiet", "-c", "task")
|
||||
|
||||
write("b.txt", "b\n")
|
||||
commit1, tree1 := snap(1)
|
||||
write("c.txt", "c\n")
|
||||
commit2, tree2 := snap(2)
|
||||
write("d.txt", "d\n")
|
||||
|
||||
sinceMain, err := Compare(dir, "main", commit2, tree2, "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := paths(sinceMain); got != "b.txt,c.txt" {
|
||||
t.Fatalf("snapshot 2 since main: got %s", got)
|
||||
}
|
||||
|
||||
step, err := Compare(dir, "main", commit2, tree2, tree1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := paths(step); got != "c.txt" {
|
||||
t.Fatalf("snapshot 2 step: got %s", got)
|
||||
}
|
||||
patch, err := FileDiff(dir, step.Base, step.Tree, "c.txt", "")
|
||||
if err != nil || !strings.Contains(patch, "+c") {
|
||||
t.Fatalf("snapshot 2 step patch: %q %v", patch, err)
|
||||
}
|
||||
|
||||
parent, err := Parent(dir, commit1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
first, err := Compare(dir, "main", commit1, tree1, parent)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := paths(first); got != "b.txt" {
|
||||
t.Fatalf("snapshot 1 step: got %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user