Only use SHA256 feature when git >= 2.42 (#28466)

And fix some comments
This commit is contained in:
wxiaoguang 2023-12-14 16:51:05 +08:00 committed by GitHub
parent 52046b934d
commit 9947af639c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 12 deletions

View File

@ -33,8 +33,8 @@ var (
// DefaultContext is the default context to run git commands in, must be initialized by git.InitXxx // DefaultContext is the default context to run git commands in, must be initialized by git.InitXxx
DefaultContext context.Context DefaultContext context.Context
// SupportProcReceive version >= 2.29.0 SupportProcReceive bool // >= 2.29
SupportProcReceive bool SupportHashSha256 bool // >= 2.42, SHA-256 repositories no longer an experimental curiosity
gitVersion *version.Version gitVersion *version.Version
) )
@ -189,7 +189,7 @@ func InitFull(ctx context.Context) (err error) {
globalCommandArgs = append(globalCommandArgs, "-c", "credential.helper=") globalCommandArgs = append(globalCommandArgs, "-c", "credential.helper=")
} }
SupportProcReceive = CheckGitVersionAtLeast("2.29") == nil SupportProcReceive = CheckGitVersionAtLeast("2.29") == nil
SupportHashSha256 = CheckGitVersionAtLeast("2.42") == nil
if setting.LFS.StartServer { if setting.LFS.StartServer {
if CheckGitVersionAtLeast("2.1.2") != nil { if CheckGitVersionAtLeast("2.1.2") != nil {
return errors.New("LFS server support requires Git >= 2.1.2") return errors.New("LFS server support requires Git >= 2.1.2")

View File

@ -40,7 +40,6 @@ type ObjectFormat interface {
NewHasher() HasherInterface NewHasher() HasherInterface
} }
/* SHA1 Type */
type Sha1ObjectFormat struct{} type Sha1ObjectFormat struct{}
func (*Sha1ObjectFormat) ID() ObjectFormatID { return Sha1 } func (*Sha1ObjectFormat) ID() ObjectFormatID { return Sha1 }
@ -83,7 +82,6 @@ func (h *Sha1ObjectFormat) NewHasher() HasherInterface {
return &Sha1Hasher{sha1.New()} return &Sha1Hasher{sha1.New()}
} }
// utils
func ObjectFormatFromID(id ObjectFormatID) ObjectFormat { func ObjectFormatFromID(id ObjectFormatID) ObjectFormat {
switch id { switch id {
case Sha1: case Sha1:

View File

@ -20,7 +20,6 @@ type ObjectID interface {
Type() ObjectFormat Type() ObjectFormat
} }
/* SHA1 */
type Sha1Hash [20]byte type Sha1Hash [20]byte
func (h *Sha1Hash) String() string { func (h *Sha1Hash) String() string {
@ -38,7 +37,7 @@ func NewSha1() *Sha1Hash {
return &Sha1Hash{} return &Sha1Hash{}
} }
// generic implementations // NewHash is for generic implementations
func NewHash(hash string) (ObjectID, error) { func NewHash(hash string) (ObjectID, error) {
hash = strings.ToLower(hash) hash = strings.ToLower(hash)
switch hash { switch hash {
@ -73,7 +72,6 @@ func genericIDFromString(h ObjectFormat, s string) (ObjectID, error) {
return h.NewID(b) return h.NewID(b)
} }
// utils
func IDFromString(hexHash string) (ObjectID, error) { func IDFromString(hexHash string) (ObjectID, error) {
switch len(hexHash) { switch len(hexHash) {
case 40: case 40:
@ -101,7 +99,7 @@ func IsEmptyCommitID(commitID string) bool {
return id.IsZero() return id.IsZero()
} }
// HashInterface is a struct that will generate a Hash // HasherInterface is a struct that will generate a Hash
type HasherInterface interface { type HasherInterface interface {
hash.Hash hash.Hash
@ -127,7 +125,7 @@ func ComputeHash(hashType ObjectFormat, t ObjectType, content []byte) ObjectID {
return h.HashSum() return h.HashSum()
} }
// Sum generates a SHA1 for the provided hash // HashSum generates a SHA1 for the provided hash
func (h *Sha1Hasher) HashSum() ObjectID { func (h *Sha1Hasher) HashSum() ObjectID {
var sha1 Sha1Hash var sha1 Sha1Hash
copy(sha1[:], h.Hash.Sum(nil)) copy(sha1[:], h.Hash.Sum(nil))

View File

@ -63,7 +63,7 @@ func IsRepoURLAccessible(ctx context.Context, url string) bool {
return err == nil return err == nil
} }
// GetObjectFormatOfRepo returns the hash type of a repository at a given path // GetObjectFormatOfRepo returns the hash type of repository at a given path
func GetObjectFormatOfRepo(ctx context.Context, repoPath string) (ObjectFormat, error) { func GetObjectFormatOfRepo(ctx context.Context, repoPath string) (ObjectFormat, error) {
var stdout, stderr strings.Builder var stdout, stderr strings.Builder
@ -96,7 +96,10 @@ func InitRepository(ctx context.Context, repoPath string, bare bool, objectForma
return err return err
} }
cmd := NewCommand(ctx, "init", "--object-format").AddDynamicArguments(objectFormat.String()) cmd := NewCommand(ctx, "init")
if SupportHashSha256 {
cmd.AddOptionValues("--object-format", objectFormat.String())
}
if bare { if bare {
cmd.AddArguments("--bare") cmd.AddArguments("--bare")
} }