Skip to content

Only use SHA256 feature when git >= 2.42 #28466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions modules/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 0 additions & 2 deletions modules/git/object_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ type ObjectFormat interface {
NewHasher() HasherInterface
}

/* SHA1 Type */
type Sha1ObjectFormat struct{}

func (*Sha1ObjectFormat) ID() ObjectFormatID { return Sha1 }
Expand Down Expand Up @@ -83,7 +82,6 @@ func (h *Sha1ObjectFormat) NewHasher() HasherInterface {
return &Sha1Hasher{sha1.New()}
}

// utils
func ObjectFormatFromID(id ObjectFormatID) ObjectFormat {
switch id {
case Sha1:
Expand Down
8 changes: 3 additions & 5 deletions modules/git/object_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ type ObjectID interface {
Type() ObjectFormat
}

/* SHA1 */
type Sha1Hash [20]byte

func (h *Sha1Hash) String() string {
Expand All @@ -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 {
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand All @@ -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))
Expand Down
7 changes: 5 additions & 2 deletions modules/git/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")
}
Expand Down