diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini index 8d10d5adce3..a0019b2d5a4 100644 --- a/custom/conf/app.example.ini +++ b/custom/conf/app.example.ini @@ -1112,6 +1112,9 @@ LEVEL = Info ;; The default branch name of new repositories ;DEFAULT_BRANCH = main ;; +;; The default Git object format of new repositories. Available values: sha1, sha256. +;DEFAULT_OBJECT_FORMAT = sha1 +;; ;; Allow adoption of unadopted repositories ;ALLOW_ADOPTION_OF_UNADOPTED_REPOSITORIES = false ;; diff --git a/modules/git/object_format.go b/modules/git/object_format.go index 7c340d14733..9e0ab9ec6a9 100644 --- a/modules/git/object_format.go +++ b/modules/git/object_format.go @@ -115,9 +115,42 @@ func (h Sha256ObjectFormatImpl) ComputeHash(t ObjectType, content []byte) Object return h.MustID(hasher.Sum(nil)) } +type invalidObjectFormatImpl struct{} + +var emptyInvalidObjectID = &Sha1Hash{} + +func (h invalidObjectFormatImpl) Name() string { + return "invalid-object-format" +} + +func (h invalidObjectFormatImpl) EmptyObjectID() ObjectID { + return emptyInvalidObjectID +} + +func (h invalidObjectFormatImpl) EmptyTree() ObjectID { + return emptyInvalidObjectID +} + +func (h invalidObjectFormatImpl) FullLength() int { + return len(emptyInvalidObjectID) * 2 +} + +func (h invalidObjectFormatImpl) IsValid(input string) bool { + return false +} + +func (h invalidObjectFormatImpl) MustID(b []byte) ObjectID { + return emptyInvalidObjectID +} + +func (h invalidObjectFormatImpl) ComputeHash(t ObjectType, content []byte) ObjectID { + return emptyInvalidObjectID +} + var ( - Sha1ObjectFormat ObjectFormat = Sha1ObjectFormatImpl{} - Sha256ObjectFormat ObjectFormat = Sha256ObjectFormatImpl{} + Sha1ObjectFormat ObjectFormat = Sha1ObjectFormatImpl{} + Sha256ObjectFormat ObjectFormat = Sha256ObjectFormatImpl{} + invalidObjectFormat ObjectFormat = invalidObjectFormatImpl{} ) func ObjectFormatFromName(name string) ObjectFormat { @@ -126,9 +159,9 @@ func ObjectFormatFromName(name string) ObjectFormat { return objectFormat } } - return nil + return invalidObjectFormat } func IsValidObjectFormat(name string) bool { - return ObjectFormatFromName(name) != nil + return ObjectFormatFromName(name) != invalidObjectFormat } diff --git a/modules/git/object_id_test.go b/modules/git/object_id_test.go index 213a0cd341d..64b0c70303e 100644 --- a/modules/git/object_id_test.go +++ b/modules/git/object_id_test.go @@ -10,14 +10,13 @@ import ( ) func TestIsValidSHAPattern(t *testing.T) { - h := Sha1ObjectFormat - assert.True(t, h.IsValid("fee1")) - assert.True(t, h.IsValid("abc000")) - assert.True(t, h.IsValid("9023902390239023902390239023902390239023")) - assert.False(t, h.IsValid("90239023902390239023902390239023902390239023")) - assert.False(t, h.IsValid("abc")) - assert.False(t, h.IsValid("123g")) - assert.False(t, h.IsValid("some random text")) + assert.True(t, Sha1ObjectFormat.IsValid("fee1")) + assert.True(t, Sha1ObjectFormat.IsValid("abc000")) + assert.True(t, Sha1ObjectFormat.IsValid("9023902390239023902390239023902390239023")) + assert.False(t, Sha1ObjectFormat.IsValid("90239023902390239023902390239023902390239023")) + assert.False(t, Sha1ObjectFormat.IsValid("abc")) + assert.False(t, Sha1ObjectFormat.IsValid("123g")) + assert.False(t, Sha1ObjectFormat.IsValid("some random text")) assert.Equal(t, "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", ComputeBlobHash(Sha1ObjectFormat, nil).String()) assert.Equal(t, "2e65efe2a145dda7ee51d1741299f848e5bf752e", ComputeBlobHash(Sha1ObjectFormat, []byte("a")).String()) assert.Equal(t, "473a0f4c3be8a93681a267e3b1e9a7dcda1185436fe141f7749120a303721813", ComputeBlobHash(Sha256ObjectFormat, nil).String()) @@ -25,3 +24,9 @@ func TestIsValidSHAPattern(t *testing.T) { assert.True(t, IsEmptyCommitID("")) assert.True(t, IsEmptyCommitID("0000000000000000000000000000000000000000")) } + +func TestInvalidObjectFormat(t *testing.T) { + of := ObjectFormatFromName("no-such") + assert.NotNil(t, of) + assert.False(t, IsValidObjectFormat("no-such")) +} diff --git a/modules/setting/repository.go b/modules/setting/repository.go index 8f3fa049a33..82b6d93a0a1 100644 --- a/modules/setting/repository.go +++ b/modules/setting/repository.go @@ -57,6 +57,7 @@ var ( DisableMigrations bool DisableStars bool `ini:"DISABLE_STARS"` DefaultBranch string + DefaultObjectFormat string AllowAdoptionOfUnadoptedRepositories bool AllowDeleteOfUnadoptedRepositories bool DisableDownloadSourceArchives bool @@ -186,6 +187,7 @@ var ( DisableMigrations: false, DisableStars: false, DefaultBranch: "main", + DefaultObjectFormat: "sha1", AllowForkWithoutMaximumLimit: true, StreamArchives: true, diff --git a/routers/web/repo/repo.go b/routers/web/repo/repo.go index 4ad6ce21002..c235f687cb1 100644 --- a/routers/web/repo/repo.go +++ b/routers/web/repo/repo.go @@ -135,7 +135,7 @@ func createCommon(ctx *context.Context) { ctx.Data["CanCreateRepoInDoer"] = ctx.Doer.CanCreateRepoIn(ctx.Doer) ctx.Data["MaxCreationLimitOfDoer"] = ctx.Doer.MaxCreationLimit() ctx.Data["SupportedObjectFormats"] = git.DefaultFeatures().SupportedObjectFormats - ctx.Data["DefaultObjectFormat"] = git.Sha1ObjectFormat + ctx.Data["DefaultObjectFormat"] = git.ObjectFormatFromName(setting.Repository.DefaultObjectFormat) } // Create render creating repository page diff --git a/templates/repo/create.tmpl b/templates/repo/create.tmpl index 85090928943..35dec3e3099 100644 --- a/templates/repo/create.tmpl +++ b/templates/repo/create.tmpl @@ -185,12 +185,12 @@