| 1 | package delta |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | // A rename is a deletion and an addition with one hash, and a change of |
| 9 | // mode alone, a symlink's target and a file replacing a symlink are each |
| 10 | // one line, read from trees git itself built. |
| 11 | func TestBetweenReadsTreesAsWholeFiles(t *testing.T) { |
| 12 | r := newRepo(t) |
| 13 | pre := r.tree([]file{ |
| 14 | {"old name.txt", ModeFile, []byte("moved\n")}, |
| 15 | {"run.sh", ModeFile, []byte("#!/bin/sh\n")}, |
| 16 | {"link", ModeSymlink, []byte("old name.txt")}, |
| 17 | {"was-link", ModeSymlink, []byte("run.sh")}, |
| 18 | {"same/deep/file", ModeFile, []byte("untouched\n")}, |
| 19 | }) |
| 20 | post := r.tree([]file{ |
| 21 | {"new name.txt", ModeFile, []byte("moved\n")}, |
| 22 | {"run.sh", ModeExec, []byte("#!/bin/sh\n")}, |
| 23 | {"link", ModeSymlink, []byte("new name.txt")}, |
| 24 | {"was-link", ModeFile, []byte("run.sh")}, |
| 25 | {"same/deep/file", ModeFile, []byte("untouched\n")}, |
| 26 | }) |
| 27 | d, err := Between(r.repo(), pre, post) |
| 28 | if err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | var got []string |
| 32 | for _, c := range d { |
| 33 | got = append(got, c.Path+": "+c.Pre.Mode+" -> "+c.Post.Mode) |
| 34 | } |
| 35 | want := []string{ |
| 36 | "link: 120000 -> 120000", |
| 37 | "new name.txt: -> 100644", |
| 38 | "old name.txt: 100644 -> ", |
| 39 | "run.sh: 100644 -> 100755", |
| 40 | "was-link: 120000 -> 100644", |
| 41 | } |
| 42 | if strings.Join(got, "\n") != strings.Join(want, "\n") { |
| 43 | t.Fatalf("the delta is\n%s", strings.Join(got, "\n")) |
| 44 | } |
| 45 | if d[1].Post.Sum != d[2].Pre.Sum { |
| 46 | t.Fatal("a rename's two lines do not carry the same hash") |
| 47 | } |
| 48 | if d[3].Pre.Sum != d[3].Post.Sum || d[4].Pre.Sum != d[4].Post.Sum { |
| 49 | t.Fatal("a change of mode alone changed the hash") |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // Trees a checkout on case-sensitive Linux could not hold faithfully are |
| 54 | // refused on either side, and so is one whose entries git would not read |
| 55 | // as written. An alias deep in a directory both trees share is refused |
| 56 | // too: each tree is read whole. |
| 57 | func TestTreesACheckoutCannotHoldAreRefused(t *testing.T) { |
| 58 | r := newRepo(t) |
| 59 | blob := r.blob([]byte("x\n")) |
| 60 | sub := r.tree([]file{{"inner", ModeFile, []byte("x\n")}}) |
| 61 | empty := r.git(nil, "mktree") |
| 62 | good := r.tree([]file{{"ok", ModeFile, []byte("x\n")}}) |
| 63 | cases := []struct { |
| 64 | name, says string |
| 65 | tree string |
| 66 | }{ |
| 67 | {"a file and a directory at one path", "named twice", |
| 68 | r.rawTree(rawEntry{"100644", "a", blob}, rawEntry{"40000", "a", sub})}, |
| 69 | {"one name twice", "named twice", |
| 70 | r.rawTree(rawEntry{"100644", "a", blob}, rawEntry{"100644", "a", blob})}, |
| 71 | {"a submodule", "submodule", |
| 72 | r.rawTree(rawEntry{"160000", "mod", strings.Repeat("ab", 20)})}, |
| 73 | {"a mode git does not write", "mode \"100664\"", |
| 74 | r.rawTree(rawEntry{"100664", "a", blob})}, |
| 75 | {"a zero-padded directory mode", "mode \"040000\"", |
| 76 | r.rawTree(rawEntry{"040000", "a", sub})}, |
| 77 | {"a name holding a slash", "slash", |
| 78 | r.rawTree(rawEntry{"100644", "a/b", blob})}, |
| 79 | {"the name ..", "component ..", |
| 80 | r.rawTree(rawEntry{"100644", "..", blob})}, |
| 81 | {"the name .git", "git keeps for itself", |
| 82 | r.rawTree(rawEntry{"40000", ".git", sub})}, |
| 83 | {"the name .GIT", "git keeps for itself", |
| 84 | r.rawTree(rawEntry{"40000", "d", r.rawTree(rawEntry{"100644", ".GIT", blob})})}, |
| 85 | {"an empty directory", "empty directory", |
| 86 | r.rawTree(rawEntry{"40000", "hollow", empty})}, |
| 87 | {"entries out of order", "out of git's order", |
| 88 | r.rawTree(rawEntry{"100644", "b", blob}, rawEntry{"100644", "a", blob})}, |
| 89 | {"a directory sorted as a file", "out of git's order", |
| 90 | r.rawTree(rawEntry{"40000", "a", sub}, rawEntry{"100644", "a.txt", blob})}, |
| 91 | {"a file whose object is a tree", "no blob", |
| 92 | r.rawTree(rawEntry{"100644", "a", sub})}, |
| 93 | {"a directory whose object is a blob", "is a blob", |
| 94 | r.rawTree(rawEntry{"40000", "a", blob})}, |
| 95 | {"an alias in a shared directory", "named twice", |
| 96 | r.rawTree(rawEntry{"40000", "shared", |
| 97 | r.rawTree(rawEntry{"100644", "a", blob}, rawEntry{"40000", "a", sub})})}, |
| 98 | } |
| 99 | for _, c := range cases { |
| 100 | t.Run(c.name, func(t *testing.T) { |
| 101 | for _, pair := range [][2]string{{c.tree, good}, {good, c.tree}, {c.tree, c.tree}} { |
| 102 | _, err := Between(r.repo(), pair[0], pair[1]) |
| 103 | if err == nil { |
| 104 | t.Fatalf("Between(%s, %s) accepted it", pair[0], pair[1]) |
| 105 | } |
| 106 | if !strings.Contains(err.Error(), c.says) { |
| 107 | t.Fatalf("the refusal does not say %q: %v", c.says, err) |
| 108 | } |
| 109 | } |
| 110 | }) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // A file and a directory at one path in two different trees is no alias: |
| 115 | // the file is absent after and the directory's files absent before. |
| 116 | func TestAFileMayBecomeADirectory(t *testing.T) { |
| 117 | r := newRepo(t) |
| 118 | pre := r.tree([]file{{"a", ModeFile, []byte("flat\n")}}) |
| 119 | post := r.tree([]file{{"a/b", ModeFile, []byte("nested\n")}}) |
| 120 | d, err := Between(r.repo(), pre, post) |
| 121 | if err != nil { |
| 122 | t.Fatal(err) |
| 123 | } |
| 124 | if len(d) != 2 || d[0].Path != "a" || !d[0].Post.Absent() || d[1].Path != "a/b" || !d[1].Pre.Absent() { |
| 125 | t.Fatalf("the delta is %q", d.Encode()) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // Only a full object ID that names a tree or a commit is read. |
| 130 | func TestBetweenTakesTreesAndCommitsByID(t *testing.T) { |
| 131 | r := newRepo(t) |
| 132 | tree := r.tree([]file{{"a", ModeFile, []byte("a\n")}}) |
| 133 | blob := r.blob([]byte("a\n")) |
| 134 | for _, bad := range []string{"HEAD", tree[:12], blob, strings.Repeat("0", 40)} { |
| 135 | if _, err := Between(r.repo(), bad, tree); err == nil { |
| 136 | t.Errorf("Between took %q", bad) |
| 137 | } |
| 138 | } |
| 139 | } |