diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-07-18 13:49:20 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2022-07-20 07:39:35 +0300 |
commit | 65a8b26c762023c57ae70221c4811bcaa5569765 (patch) | |
tree | 2d4a1e4495113555f9daeb9827ba04cc33dfc837 | |
parent | 06c3ce783ee54e061e9464237beb4fdc651893b0 (diff) |
gittest: Introduce default object hash
The gittest package is currently re-exporting the functionality provided
by the `ObjectHash` structure. Depending on whether we are testing with
SHA1 or SHA256, we export either the implementation details of the
`ObjectHashSHA1` or `ObjectHashSHA256` structure.
Now that we have the hash-specific information neatly encapsulated in
its own structure there is no need to re-export the separate details of
that structure anymore. Instead, refactor the gittest package to only
export a single `DefaultObjectHash` variable that points to one of the
above structures, depending on what we're currently testing.
-rw-r--r-- | internal/git/gittest/commit.go | 4 | ||||
-rw-r--r-- | internal/git/gittest/objects.go | 2 | ||||
-rw-r--r-- | internal/git/gittest/ref.go | 2 | ||||
-rw-r--r-- | internal/git/gittest/sha1.go | 15 | ||||
-rw-r--r-- | internal/git/gittest/sha256.go | 18 | ||||
-rw-r--r-- | internal/git/gittest/tag.go | 2 | ||||
-rw-r--r-- | internal/git/gittest/tree.go | 4 |
7 files changed, 17 insertions, 30 deletions
diff --git a/internal/git/gittest/commit.go b/internal/git/gittest/commit.go index 7b4b14440..f8902ffe4 100644 --- a/internal/git/gittest/commit.go +++ b/internal/git/gittest/commit.go @@ -150,7 +150,7 @@ func WriteCommit(t testing.TB, cfg config.Cfg, repoPath string, opts ...WriteCom tree = writeCommitConfig.treeID.String() } else if len(parents) == 0 { // If there are no parents, then we set the root tree to the empty tree. - tree = EmptyTreeOID.String() + tree = DefaultObjectHash.EmptyTreeOID.String() } else { tree = parents[0].String() + "^{tree}" } @@ -210,7 +210,7 @@ func WriteCommit(t testing.TB, cfg config.Cfg, repoPath string, opts ...WriteCom Stdin: stdin, Env: env, }, commitArgs...) - oid, err := NewObjectIDFromHex(text.ChompBytes(stdout)) + oid, err := DefaultObjectHash.FromHex(text.ChompBytes(stdout)) require.NoError(t, err) if writeCommitConfig.branch != "" { diff --git a/internal/git/gittest/objects.go b/internal/git/gittest/objects.go index f6c8b4c64..aafca61b2 100644 --- a/internal/git/gittest/objects.go +++ b/internal/git/gittest/objects.go @@ -77,7 +77,7 @@ func WriteBlob(t testing.TB, cfg config.Cfg, testRepoPath string, contents []byt hex := text.ChompBytes(ExecOpts(t, cfg, ExecConfig{Stdin: bytes.NewReader(contents)}, "-C", testRepoPath, "hash-object", "-w", "--stdin", )) - oid, err := NewObjectIDFromHex(hex) + oid, err := DefaultObjectHash.FromHex(hex) require.NoError(t, err) return oid } diff --git a/internal/git/gittest/ref.go b/internal/git/gittest/ref.go index e413ed9f5..9d9687089 100644 --- a/internal/git/gittest/ref.go +++ b/internal/git/gittest/ref.go @@ -18,7 +18,7 @@ func WriteRef(t testing.TB, cfg config.Cfg, repoPath string, ref git.ReferenceNa func ResolveRevision(t testing.TB, cfg config.Cfg, repoPath string, revision string) git.ObjectID { t.Helper() output := Exec(t, cfg, "-C", repoPath, "rev-parse", "--verify", revision) - objectID, err := NewObjectIDFromHex(text.ChompBytes(output)) + objectID, err := DefaultObjectHash.FromHex(text.ChompBytes(output)) require.NoError(t, err) return objectID } diff --git a/internal/git/gittest/sha1.go b/internal/git/gittest/sha1.go index 3cfb3dc26..4a5d8de33 100644 --- a/internal/git/gittest/sha1.go +++ b/internal/git/gittest/sha1.go @@ -4,14 +4,9 @@ package gittest import "gitlab.com/gitlab-org/gitaly/v15/internal/git" -// EmptyTreeOID is the Git tree object hash that corresponds to an empty tree (directory) -var EmptyTreeOID = git.ObjectHashSHA1.EmptyTreeOID +var ( + // DefaultObjectHash is the default hash used for running tests. + DefaultObjectHash = git.ObjectHashSHA1 -var initRepoExtraArgs = []string{} - -// NewObjectIDFromHex constructs a new ObjectID from the given hex -// representation of the object ID. Returns ErrInvalidObjectID if the given -// OID is not valid. -func NewObjectIDFromHex(hex string) (git.ObjectID, error) { - return git.ObjectHashSHA1.FromHex(hex) -} + initRepoExtraArgs = []string{} +) diff --git a/internal/git/gittest/sha256.go b/internal/git/gittest/sha256.go index ad69cd981..d571657a9 100644 --- a/internal/git/gittest/sha256.go +++ b/internal/git/gittest/sha256.go @@ -4,19 +4,11 @@ package gittest import ( "gitlab.com/gitlab-org/gitaly/v15/internal/git" - "gitlab.com/gitlab-org/gitaly/v15/internal/git/sha256" ) -const ( - // EmptyTreeOID is the Git tree object hash that corresponds to an empty tree (directory) - EmptyTreeOID = sha256.EmptyTreeOID -) - -var initRepoExtraArgs = []string{"--object-format=sha256"} +var ( + // DefaultObjectHash is the default hash used for running tests. + DefaultObjectHash = git.ObjectHashSHA256 -// NewObjectIDFromHex constructs a new ObjectID from the given hex -// representation of the object ID. Returns ErrInvalidObjectID if the given -// OID is not valid. -func NewObjectIDFromHex(hex string) (git.ObjectID, error) { - return sha256.NewObjectIDFromHex(hex) -} + initRepoExtraArgs = []string{"--object-format=sha256"} +) diff --git a/internal/git/gittest/tag.go b/internal/git/gittest/tag.go index bc03b17b4..09cfa2f5d 100644 --- a/internal/git/gittest/tag.go +++ b/internal/git/gittest/tag.go @@ -64,7 +64,7 @@ func WriteTag( tagID := Exec(t, cfg, "-C", repoPath, "show-ref", "-s", tagName) - objectID, err := NewObjectIDFromHex(text.ChompBytes(tagID)) + objectID, err := DefaultObjectHash.FromHex(text.ChompBytes(tagID)) require.NoError(t, err) return objectID diff --git a/internal/git/gittest/tree.go b/internal/git/gittest/tree.go index 8ff19779a..7b93c996f 100644 --- a/internal/git/gittest/tree.go +++ b/internal/git/gittest/tree.go @@ -55,7 +55,7 @@ func RequireTree(t testing.TB, cfg config.Cfg, repoPath, treeish string, expecte path := string(tabSplit[1]) - objectID, err := NewObjectIDFromHex(string(spaceSplit[2])) + objectID, err := DefaultObjectHash.FromHex(string(spaceSplit[2])) require.NoError(t, err) actualEntries = append(actualEntries, TreeEntry{ @@ -107,7 +107,7 @@ func WriteTree(t testing.TB, cfg config.Cfg, repoPath string, entries []TreeEntr stdout := ExecOpts(t, cfg, ExecConfig{Stdin: &tree}, "-C", repoPath, "mktree", "-z", "--missing", ) - treeOID, err := NewObjectIDFromHex(text.ChompBytes(stdout)) + treeOID, err := DefaultObjectHash.FromHex(text.ChompBytes(stdout)) require.NoError(t, err) return treeOID |