| 1 | package githistory |
| 2 | |
| 3 | import ( |
| 4 | "encoding/hex" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "github.com/git-lfs/gitobj/v2" |
| 14 | "github.com/stretchr/testify/assert" |
| 15 | ) |
| 16 | |
| 17 | // DatabaseFromFixture returns a *gitobj.ObjectDatabase instance that is safely |
| 18 | // mutable and created from a template equivalent to the fixture that you |
| 19 | // provided it. |
| 20 | // |
| 21 | // If any error was encountered, it will call t.Fatalf() immediately. |
| 22 | func DatabaseFromFixture(t *testing.T, name string) *gitobj.ObjectDatabase { |
| 23 | path, err := copyToTmp(filepath.Join("fixtures", name)) |
| 24 | if err != nil { |
| 25 | t.Fatalf("gitobj: could not copy fixture %s: %v", name, err) |
| 26 | } |
| 27 | |
| 28 | db, err := gitobj.FromFilesystem(filepath.Join(path, "objects"), "") |
| 29 | if err != nil { |
| 30 | t.Fatalf("gitobj: could not create object database: %v", err) |
| 31 | } |
| 32 | return db |
| 33 | } |
| 34 | |
| 35 | // AssertBlobContents asserts that the blob contents given by loading the path |
| 36 | // starting from the root tree "tree" has the given "contents". |
| 37 | func AssertBlobContents(t *testing.T, db *gitobj.ObjectDatabase, tree, path, contents string) { |
| 38 | // First, load the root tree. |
| 39 | root, err := db.Tree(HexDecode(t, tree)) |
| 40 | if err != nil { |
| 41 | t.Fatalf("gitobj: cannot load tree: %s: %s", tree, err) |
| 42 | } |
| 43 | |
| 44 | // Then, iterating through each part of the filepath (i.e., a/b/c.txt -> |
| 45 | // []string{"a", "b", "c.txt"}). |
| 46 | parts := strings.Split(path, "/") |
| 47 | for i := 0; i < len(parts)-1; i++ { |
| 48 | part := parts[i] |
| 49 | |
| 50 | // Load the subtree given by that name. |
| 51 | var subtree *gitobj.Tree |
| 52 | for _, entry := range root.Entries { |
| 53 | if entry.Name != part { |
| 54 | continue |
| 55 | } |
| 56 | |
| 57 | subtree, err = db.Tree(entry.Oid) |
| 58 | if err != nil { |
| 59 | t.Fatalf("gitobj: cannot load subtree %s: %s", filepath.Join(parts[:i]...), err) |
| 60 | } |
| 61 | break |
| 62 | } |
| 63 | |
| 64 | if subtree == nil { |
| 65 | t.Fatalf("gitobj: subtree %s does not exist", path) |
| 66 | } |
| 67 | |
| 68 | // And re-assign it to root, creating a sort of pseudo-recursion. |
| 69 | root = subtree |
| 70 | } |
| 71 | |
| 72 | filename := parts[len(parts)-1] |
| 73 | |
| 74 | // Find the blob given by the last entry in parts (the filename). |
| 75 | var blob *gitobj.Blob |
| 76 | for _, entry := range root.Entries { |
| 77 | if entry.Name == filename { |
| 78 | blob, err = db.Blob(entry.Oid) |
| 79 | if err != nil { |
| 80 | t.Fatalf("gitobj: cannot load blob %x: %s", entry.Oid, err) |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // If we couldn't find the blob, fail immediately. |
| 86 | if blob == nil { |
| 87 | t.Fatalf("gitobj: blob at %s in %s does not exist", path, tree) |
| 88 | } |
| 89 | |
| 90 | // Perform an assertion on the blob's contents. |
| 91 | got, err := io.ReadAll(blob.Contents) |
| 92 | if err != nil { |
| 93 | t.Fatalf("gitobj: cannot read contents from blob %s: %s", path, err) |
| 94 | } |
| 95 | |
| 96 | assert.Equal(t, contents, string(got)) |
| 97 | } |
| 98 | |
| 99 | // AssertCommitParent asserts that the given commit has a parent equivalent to |
| 100 | // the one provided. |
| 101 | func AssertCommitParent(t *testing.T, db *gitobj.ObjectDatabase, sha, parent string) { |
| 102 | commit, err := db.Commit(HexDecode(t, sha)) |
| 103 | if err != nil { |
| 104 | t.Fatalf("gitobj: expected to read commit: %s, couldn't: %v", sha, err) |
| 105 | } |
| 106 | |
| 107 | decoded, err := hex.DecodeString(parent) |
| 108 | if err != nil { |
| 109 | t.Fatalf("gitobj: expected to decode parent SHA: %s, couldn't: %v", parent, err) |
| 110 | } |
| 111 | |
| 112 | assert.Contains(t, commit.ParentIDs, decoded, |
| 113 | "gitobj: expected parents of commit: %s to contain: %s", sha, parent) |
| 114 | } |
| 115 | |
| 116 | // AssertCommitTree asserts that the given commit has a tree equivalent to the |
| 117 | // one provided. |
| 118 | func AssertCommitTree(t *testing.T, db *gitobj.ObjectDatabase, sha, tree string) { |
| 119 | commit, err := db.Commit(HexDecode(t, sha)) |
| 120 | if err != nil { |
| 121 | t.Fatalf("gitobj: expected to read commit: %s, couldn't: %v", sha, err) |
| 122 | } |
| 123 | |
| 124 | decoded, err := hex.DecodeString(tree) |
| 125 | if err != nil { |
| 126 | t.Fatalf("gitobj: expected to decode tree SHA: %s, couldn't: %v", tree, err) |
| 127 | } |
| 128 | |
| 129 | assert.Equal(t, decoded, commit.TreeID, "gitobj: expected tree ID: %s (got: %x)", tree, commit.TreeID) |
| 130 | } |
| 131 | |
| 132 | // AssertRef asserts that a given refname points at the expected commit. |
| 133 | func AssertRef(t *testing.T, db *gitobj.ObjectDatabase, ref string, expected []byte) { |
| 134 | root, ok := db.Root() |
| 135 | assert.True(t, ok, "gitobj: expected *odb.ObjectDatabase to have Root()") |
| 136 | |
| 137 | cmd := exec.Command("git", "rev-parse", ref) |
| 138 | cmd.Dir = root |
| 139 | out, err := cmd.Output() |
| 140 | |
| 141 | assert.Nil(t, err) |
| 142 | |
| 143 | assert.Equal(t, hex.EncodeToString(expected), strings.TrimSpace(string(out))) |
| 144 | } |
| 145 | |
| 146 | // HexDecode decodes the given ASCII hex-encoded string into []byte's, or fails |
| 147 | // the test immediately if the given "sha" wasn't a valid hex-encoded sequence. |
| 148 | func HexDecode(t *testing.T, sha string) []byte { |
| 149 | b, err := hex.DecodeString(sha) |
| 150 | if err != nil { |
| 151 | t.Fatalf("gitobj: could not decode string: %q, %v", sha, err) |
| 152 | } |
| 153 | |
| 154 | return b |
| 155 | } |
| 156 | |
| 157 | // copyToTmp copies the given fixture to a folder in /tmp. |
| 158 | func copyToTmp(fixture string) (string, error) { |
| 159 | p, err := os.MkdirTemp("", fmt.Sprintf("git-lfs-fixture-%s", filepath.Dir(fixture))) |
| 160 | if err != nil { |
| 161 | return "", err |
| 162 | } |
| 163 | |
| 164 | if err = copyDir(fixture, p); err != nil { |
| 165 | return "", err |
| 166 | } |
| 167 | return p, nil |
| 168 | } |
| 169 | |
| 170 | // copyDir copies a directory (and recursively all files and subdirectories) |
| 171 | // from "from" to "to" preserving permissions and ownership. |
| 172 | func copyDir(from, to string) error { |
| 173 | stat, err := os.Stat(from) |
| 174 | if err != nil { |
| 175 | return err |
| 176 | } |
| 177 | |
| 178 | if err := os.MkdirAll(to, stat.Mode()); err != nil { |
| 179 | return err |
| 180 | } |
| 181 | |
| 182 | entries, err := os.ReadDir(from) |
| 183 | if err != nil { |
| 184 | return err |
| 185 | } |
| 186 | |
| 187 | for _, entry := range entries { |
| 188 | sp := filepath.Join(from, entry.Name()) |
| 189 | dp := filepath.Join(to, entry.Name()) |
| 190 | |
| 191 | if entry.IsDir() { |
| 192 | err = copyDir(sp, dp) |
| 193 | } else { |
| 194 | err = copyFile(sp, dp) |
| 195 | } |
| 196 | |
| 197 | if err != nil { |
| 198 | return err |
| 199 | } |
| 200 | } |
| 201 | return nil |
| 202 | } |
| 203 | |
| 204 | // copyFile copies a file from "from" to "to" preserving permissions and |
| 205 | // ownership. |
| 206 | func copyFile(from, to string) error { |
| 207 | src, err := os.Open(from) |
| 208 | if err != nil { |
| 209 | return err |
| 210 | } |
| 211 | defer src.Close() |
| 212 | |
| 213 | dst, err := os.Create(to) |
| 214 | if err != nil { |
| 215 | return err |
| 216 | } |
| 217 | defer dst.Close() |
| 218 | |
| 219 | if _, err = io.Copy(dst, src); err != nil { |
| 220 | return err |
| 221 | } |
| 222 | |
| 223 | stat, err := os.Stat(from) |
| 224 | if err != nil { |
| 225 | return err |
| 226 | } |
| 227 | |
| 228 | return os.Chmod(to, stat.Mode()) |
| 229 | } |