From 9947af639c24d185ace096d3f25098ff94c0790e Mon Sep 17 00:00:00 2001
From: wxiaoguang <wxiaoguang@gmail.com>
Date: Thu, 14 Dec 2023 16:51:05 +0800
Subject: [PATCH] Only use SHA256 feature when git >= 2.42 (#28466)

And fix some comments
---
 modules/git/git.go           | 6 +++---
 modules/git/object_format.go | 2 --
 modules/git/object_id.go     | 8 +++-----
 modules/git/repo.go          | 7 +++++--
 4 files changed, 11 insertions(+), 12 deletions(-)

diff --git a/modules/git/git.go b/modules/git/git.go
index 12d2f94e51..166655eb73 100644
--- a/modules/git/git.go
+++ b/modules/git/git.go
@@ -33,8 +33,8 @@ var (
 	// DefaultContext is the default context to run git commands in, must be initialized by git.InitXxx
 	DefaultContext context.Context
 
-	// SupportProcReceive version >= 2.29.0
-	SupportProcReceive bool
+	SupportProcReceive bool // >= 2.29
+	SupportHashSha256  bool // >= 2.42, SHA-256 repositories no longer an ‘experimental curiosity’
 
 	gitVersion *version.Version
 )
@@ -189,7 +189,7 @@ func InitFull(ctx context.Context) (err error) {
 		globalCommandArgs = append(globalCommandArgs, "-c", "credential.helper=")
 	}
 	SupportProcReceive = CheckGitVersionAtLeast("2.29") == nil
-
+	SupportHashSha256 = CheckGitVersionAtLeast("2.42") == nil
 	if setting.LFS.StartServer {
 		if CheckGitVersionAtLeast("2.1.2") != nil {
 			return errors.New("LFS server support requires Git >= 2.1.2")
diff --git a/modules/git/object_format.go b/modules/git/object_format.go
index 7f5d09170c..3c52de772b 100644
--- a/modules/git/object_format.go
+++ b/modules/git/object_format.go
@@ -40,7 +40,6 @@ type ObjectFormat interface {
 	NewHasher() HasherInterface
 }
 
-/* SHA1 Type */
 type Sha1ObjectFormat struct{}
 
 func (*Sha1ObjectFormat) ID() ObjectFormatID { return Sha1 }
@@ -83,7 +82,6 @@ func (h *Sha1ObjectFormat) NewHasher() HasherInterface {
 	return &Sha1Hasher{sha1.New()}
 }
 
-// utils
 func ObjectFormatFromID(id ObjectFormatID) ObjectFormat {
 	switch id {
 	case Sha1:
diff --git a/modules/git/object_id.go b/modules/git/object_id.go
index 3cba6d4f72..21e1c67c64 100644
--- a/modules/git/object_id.go
+++ b/modules/git/object_id.go
@@ -20,7 +20,6 @@ type ObjectID interface {
 	Type() ObjectFormat
 }
 
-/* SHA1 */
 type Sha1Hash [20]byte
 
 func (h *Sha1Hash) String() string {
@@ -38,7 +37,7 @@ func NewSha1() *Sha1Hash {
 	return &Sha1Hash{}
 }
 
-// generic implementations
+// NewHash is for generic implementations
 func NewHash(hash string) (ObjectID, error) {
 	hash = strings.ToLower(hash)
 	switch hash {
@@ -73,7 +72,6 @@ func genericIDFromString(h ObjectFormat, s string) (ObjectID, error) {
 	return h.NewID(b)
 }
 
-// utils
 func IDFromString(hexHash string) (ObjectID, error) {
 	switch len(hexHash) {
 	case 40:
@@ -101,7 +99,7 @@ func IsEmptyCommitID(commitID string) bool {
 	return id.IsZero()
 }
 
-// HashInterface is a struct that will generate a Hash
+// HasherInterface is a struct that will generate a Hash
 type HasherInterface interface {
 	hash.Hash
 
@@ -127,7 +125,7 @@ func ComputeHash(hashType ObjectFormat, t ObjectType, content []byte) ObjectID {
 	return h.HashSum()
 }
 
-// Sum generates a SHA1 for the provided hash
+// HashSum generates a SHA1 for the provided hash
 func (h *Sha1Hasher) HashSum() ObjectID {
 	var sha1 Sha1Hash
 	copy(sha1[:], h.Hash.Sum(nil))
diff --git a/modules/git/repo.go b/modules/git/repo.go
index 871d267a5b..c036a217eb 100644
--- a/modules/git/repo.go
+++ b/modules/git/repo.go
@@ -63,7 +63,7 @@ func IsRepoURLAccessible(ctx context.Context, url string) bool {
 	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) {
 	var stdout, stderr strings.Builder
 
@@ -96,7 +96,10 @@ func InitRepository(ctx context.Context, repoPath string, bare bool, objectForma
 		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 {
 		cmd.AddArguments("--bare")
 	}