Skip to content

Commit 9947af6

Browse files
authored
Only use SHA256 feature when git >= 2.42 (#28466)
And fix some comments
1 parent 52046b9 commit 9947af6

File tree

4 files changed

+11
-12
lines changed

4 files changed

+11
-12
lines changed

modules/git/git.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ var (
3333
// DefaultContext is the default context to run git commands in, must be initialized by git.InitXxx
3434
DefaultContext context.Context
3535

36-
// SupportProcReceive version >= 2.29.0
37-
SupportProcReceive bool
36+
SupportProcReceive bool // >= 2.29
37+
SupportHashSha256 bool // >= 2.42, SHA-256 repositories no longer an ‘experimental curiosity’
3838

3939
gitVersion *version.Version
4040
)
@@ -189,7 +189,7 @@ func InitFull(ctx context.Context) (err error) {
189189
globalCommandArgs = append(globalCommandArgs, "-c", "credential.helper=")
190190
}
191191
SupportProcReceive = CheckGitVersionAtLeast("2.29") == nil
192-
192+
SupportHashSha256 = CheckGitVersionAtLeast("2.42") == nil
193193
if setting.LFS.StartServer {
194194
if CheckGitVersionAtLeast("2.1.2") != nil {
195195
return errors.New("LFS server support requires Git >= 2.1.2")

modules/git/object_format.go

-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ type ObjectFormat interface {
4040
NewHasher() HasherInterface
4141
}
4242

43-
/* SHA1 Type */
4443
type Sha1ObjectFormat struct{}
4544

4645
func (*Sha1ObjectFormat) ID() ObjectFormatID { return Sha1 }
@@ -83,7 +82,6 @@ func (h *Sha1ObjectFormat) NewHasher() HasherInterface {
8382
return &Sha1Hasher{sha1.New()}
8483
}
8584

86-
// utils
8785
func ObjectFormatFromID(id ObjectFormatID) ObjectFormat {
8886
switch id {
8987
case Sha1:

modules/git/object_id.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ type ObjectID interface {
2020
Type() ObjectFormat
2121
}
2222

23-
/* SHA1 */
2423
type Sha1Hash [20]byte
2524

2625
func (h *Sha1Hash) String() string {
@@ -38,7 +37,7 @@ func NewSha1() *Sha1Hash {
3837
return &Sha1Hash{}
3938
}
4039

41-
// generic implementations
40+
// NewHash is for generic implementations
4241
func NewHash(hash string) (ObjectID, error) {
4342
hash = strings.ToLower(hash)
4443
switch hash {
@@ -73,7 +72,6 @@ func genericIDFromString(h ObjectFormat, s string) (ObjectID, error) {
7372
return h.NewID(b)
7473
}
7574

76-
// utils
7775
func IDFromString(hexHash string) (ObjectID, error) {
7876
switch len(hexHash) {
7977
case 40:
@@ -101,7 +99,7 @@ func IsEmptyCommitID(commitID string) bool {
10199
return id.IsZero()
102100
}
103101

104-
// HashInterface is a struct that will generate a Hash
102+
// HasherInterface is a struct that will generate a Hash
105103
type HasherInterface interface {
106104
hash.Hash
107105

@@ -127,7 +125,7 @@ func ComputeHash(hashType ObjectFormat, t ObjectType, content []byte) ObjectID {
127125
return h.HashSum()
128126
}
129127

130-
// Sum generates a SHA1 for the provided hash
128+
// HashSum generates a SHA1 for the provided hash
131129
func (h *Sha1Hasher) HashSum() ObjectID {
132130
var sha1 Sha1Hash
133131
copy(sha1[:], h.Hash.Sum(nil))

modules/git/repo.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func IsRepoURLAccessible(ctx context.Context, url string) bool {
6363
return err == nil
6464
}
6565

66-
// GetObjectFormatOfRepo returns the hash type of a repository at a given path
66+
// GetObjectFormatOfRepo returns the hash type of repository at a given path
6767
func GetObjectFormatOfRepo(ctx context.Context, repoPath string) (ObjectFormat, error) {
6868
var stdout, stderr strings.Builder
6969

@@ -96,7 +96,10 @@ func InitRepository(ctx context.Context, repoPath string, bare bool, objectForma
9696
return err
9797
}
9898

99-
cmd := NewCommand(ctx, "init", "--object-format").AddDynamicArguments(objectFormat.String())
99+
cmd := NewCommand(ctx, "init")
100+
if SupportHashSha256 {
101+
cmd.AddOptionValues("--object-format", objectFormat.String())
102+
}
100103
if bare {
101104
cmd.AddArguments("--bare")
102105
}

0 commit comments

Comments
 (0)