Abstract hash function usage (#28138)

Refactor Hash interfaces and centralize hash function. This will allow
easier introduction of different hash function later on.

This forms the "no-op" part of the SHA256 enablement patch.
This commit is contained in:
Adam Majer 2023-12-13 21:02:00 +00:00 committed by GitHub
parent 064f05204c
commit cbf923e87b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
122 changed files with 947 additions and 594 deletions

View file

@ -7,6 +7,7 @@ package git
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/url"
@ -62,14 +63,40 @@ func IsRepoURLAccessible(ctx context.Context, url string) bool {
return err == nil
}
// GetObjectFormatOfRepo returns the hash type of a repository at a given path
func GetObjectFormatOfRepo(ctx context.Context, repoPath string) (ObjectFormat, error) {
var stdout, stderr strings.Builder
err := NewCommand(ctx, "hash-object", "--stdin").Run(&RunOpts{
Dir: repoPath,
Stdout: &stdout,
Stderr: &stderr,
Stdin: &strings.Reader{},
})
if err != nil {
return nil, err
}
if stderr.Len() > 0 {
return nil, errors.New(stderr.String())
}
h, err := IDFromString(strings.TrimRight(stdout.String(), "\n"))
if err != nil {
return nil, err
}
return h.Type(), nil
}
// InitRepository initializes a new Git repository.
func InitRepository(ctx context.Context, repoPath string, bare bool) error {
func InitRepository(ctx context.Context, repoPath string, bare bool, objectFormat ObjectFormat) error {
err := os.MkdirAll(repoPath, os.ModePerm)
if err != nil {
return err
}
cmd := NewCommand(ctx, "init")
cmd := NewCommand(ctx, "init", "--object-format").AddDynamicArguments(objectFormat.String())
if bare {
cmd.AddArguments("--bare")
}