Skip to content

Commit 619483a

Browse files
committed
Use ObjectFormatName in repository struct
1 parent 3e3902a commit 619483a

File tree

22 files changed

+84
-79
lines changed

22 files changed

+84
-79
lines changed

models/git/branch_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ func TestAddDeletedBranch(t *testing.T) {
2929
secondBranch := unittest.AssertExistsAndLoadBean(t, &git_model.Branch{RepoID: repo.ID, Name: "branch2"})
3030
assert.True(t, secondBranch.IsDeleted)
3131

32+
objectFormat := git.ObjectFormatFromName(repo.ObjectFormatName)
3233
commit := &git.Commit{
33-
ID: repo.ObjectFormat.MustIDFromString(secondBranch.CommitID),
34+
ID: objectFormat.MustIDFromString(secondBranch.CommitID),
3435
CommitMessage: secondBranch.CommitMessage,
3536
Committer: &git.Signature{
3637
When: secondBranch.CommitTime.AsLocalTime(),

models/repo/repo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ type Repository struct {
180180
IsFsckEnabled bool `xorm:"NOT NULL DEFAULT true"`
181181
CloseIssuesViaCommitInAnyBranch bool `xorm:"NOT NULL DEFAULT false"`
182182
Topics []string `xorm:"TEXT JSON"`
183-
ObjectFormat git.ObjectFormat `xorm:"-"`
183+
ObjectFormatName string `xorm:"-"`
184184

185185
TrustModel TrustModelType
186186

@@ -277,7 +277,7 @@ func (repo *Repository) AfterLoad() {
277277
repo.NumOpenProjects = repo.NumProjects - repo.NumClosedProjects
278278
repo.NumOpenActionRuns = repo.NumActionRuns - repo.NumClosedActionRuns
279279

280-
repo.ObjectFormat = git.Sha1ObjectFormat{}
280+
repo.ObjectFormatName = "sha1"
281281
}
282282

283283
// LoadAttributes loads attributes of the repository.

modules/git/object_format.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
// sha1Pattern can be used to determine if a string is an valid sha
1212
var sha1Pattern = regexp.MustCompile(`^[0-9a-f]{4,40}$`)
1313

14+
const Sha1ObjectFormatName = "sha1"
15+
1416
type ObjectFormat interface {
1517
String() string
1618

@@ -33,7 +35,7 @@ type ObjectFormat interface {
3335

3436
type Sha1ObjectFormat struct{}
3537

36-
func (Sha1ObjectFormat) String() string { return "sha1" }
38+
func (Sha1ObjectFormat) String() string { return Sha1ObjectFormatName }
3739
func (Sha1ObjectFormat) Empty() ObjectID { return &Sha1Hash{} }
3840
func (Sha1ObjectFormat) EmptyTree() ObjectID {
3941
return &Sha1Hash{
@@ -71,3 +73,12 @@ func (Sha1ObjectFormat) NewEmptyID() ObjectID {
7173
func (h Sha1ObjectFormat) NewHasher() HasherInterface {
7274
return &Sha1Hasher{sha1.New()}
7375
}
76+
77+
func ObjectFormatFromName(name string) ObjectFormat {
78+
switch name {
79+
case "sha1":
80+
return Sha1ObjectFormat{}
81+
default:
82+
return nil
83+
}
84+
}

modules/git/repo.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,18 @@ func GetObjectFormatOfRepo(ctx context.Context, repoPath string) (ObjectFormat,
9090
}
9191

9292
// InitRepository initializes a new Git repository.
93-
func InitRepository(ctx context.Context, repoPath string, bare bool, objectFormat ObjectFormat) error {
93+
func InitRepository(ctx context.Context, repoPath string, bare bool, objectFormatName string) error {
9494
err := os.MkdirAll(repoPath, os.ModePerm)
9595
if err != nil {
9696
return err
9797
}
9898

9999
cmd := NewCommand(ctx, "init")
100100
if SupportHashSha256 {
101-
cmd.AddOptionValues("--object-format", objectFormat.String())
101+
if objectFormatName == "" {
102+
objectFormatName = "sha1"
103+
}
104+
cmd.AddOptionValues("--object-format", objectFormatName)
102105
}
103106
if bare {
104107
cmd.AddArguments("--bare")

modules/repository/generate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func generateRepoCommit(ctx context.Context, repo, templateRepo, generateRepo *r
224224
}
225225

226226
// FIXME: fix the hash
227-
if err := git.InitRepository(ctx, tmpDir, false, git.Sha1ObjectFormat{}); err != nil {
227+
if err := git.InitRepository(ctx, tmpDir, false, git.Sha1ObjectFormat{}.String()); err != nil {
228228
return err
229229
}
230230

@@ -358,7 +358,7 @@ func GenerateRepository(ctx context.Context, doer, owner *user_model.User, templ
358358
}
359359

360360
// FIXME - fix the hash
361-
if err = CheckInitRepository(ctx, owner.Name, generateRepo.Name, git.Sha1ObjectFormat{}); err != nil {
361+
if err = CheckInitRepository(ctx, owner.Name, generateRepo.Name, git.Sha1ObjectFormat{}.String()); err != nil {
362362
return generateRepo, err
363363
}
364364

modules/repository/init.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func InitRepoCommit(ctx context.Context, tmpPath string, repo *repo_model.Reposi
188188
return nil
189189
}
190190

191-
func CheckInitRepository(ctx context.Context, owner, name string, objectFormat git.ObjectFormat) (err error) {
191+
func CheckInitRepository(ctx context.Context, owner, name, objectFormatName string) (err error) {
192192
// Somehow the directory could exist.
193193
repoPath := repo_model.RepoPath(owner, name)
194194
isExist, err := util.IsExist(repoPath)
@@ -204,7 +204,7 @@ func CheckInitRepository(ctx context.Context, owner, name string, objectFormat g
204204
}
205205

206206
// Init git bare new repository.
207-
if err = git.InitRepository(ctx, repoPath, true, objectFormat); err != nil {
207+
if err = git.InitRepository(ctx, repoPath, true, objectFormatName); err != nil {
208208
return fmt.Errorf("git.InitRepository: %w", err)
209209
} else if err = CreateDelegateHooks(repoPath); err != nil {
210210
return fmt.Errorf("createDelegateHooks: %w", err)

routers/api/v1/repo/repo.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -242,18 +242,18 @@ func CreateUserRepo(ctx *context.APIContext, owner *user_model.User, opt api.Cre
242242
}
243243

244244
repo, err := repo_service.CreateRepository(ctx, ctx.Doer, owner, repo_service.CreateRepoOptions{
245-
Name: opt.Name,
246-
Description: opt.Description,
247-
IssueLabels: opt.IssueLabels,
248-
Gitignores: opt.Gitignores,
249-
License: opt.License,
250-
Readme: opt.Readme,
251-
IsPrivate: opt.Private,
252-
AutoInit: opt.AutoInit,
253-
DefaultBranch: opt.DefaultBranch,
254-
TrustModel: repo_model.ToTrustModel(opt.TrustModel),
255-
IsTemplate: opt.Template,
256-
ObjectFormat: git.Sha1ObjectFormat{},
245+
Name: opt.Name,
246+
Description: opt.Description,
247+
IssueLabels: opt.IssueLabels,
248+
Gitignores: opt.Gitignores,
249+
License: opt.License,
250+
Readme: opt.Readme,
251+
IsPrivate: opt.Private,
252+
AutoInit: opt.AutoInit,
253+
DefaultBranch: opt.DefaultBranch,
254+
TrustModel: repo_model.ToTrustModel(opt.TrustModel),
255+
IsTemplate: opt.Template,
256+
ObjectFormatName: git.Sha1ObjectFormatName,
257257
})
258258
if err != nil {
259259
if repo_model.IsErrRepoAlreadyExist(err) {

routers/web/repo/githttp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ func dummyInfoRefs(ctx *context.Context) {
329329
}
330330
}()
331331

332-
if err := git.InitRepository(ctx, tmpDir, true, git.Sha1ObjectFormat{}); err != nil {
332+
if err := git.InitRepository(ctx, tmpDir, true, git.Sha1ObjectFormatName); err != nil {
333333
log.Error("Failed to init bare repo for git-receive-pack cache: %v", err)
334334
return
335335
}

routers/web/repo/repo.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -278,18 +278,18 @@ func CreatePost(ctx *context.Context) {
278278
}
279279
} else {
280280
repo, err = repo_service.CreateRepository(ctx, ctx.Doer, ctxUser, repo_service.CreateRepoOptions{
281-
Name: form.RepoName,
282-
Description: form.Description,
283-
Gitignores: form.Gitignores,
284-
IssueLabels: form.IssueLabels,
285-
License: form.License,
286-
Readme: form.Readme,
287-
IsPrivate: form.Private || setting.Repository.ForcePrivate,
288-
DefaultBranch: form.DefaultBranch,
289-
AutoInit: form.AutoInit,
290-
IsTemplate: form.Template,
291-
TrustModel: repo_model.ToTrustModel(form.TrustModel),
292-
ObjectFormat: form.ObjectFormat,
281+
Name: form.RepoName,
282+
Description: form.Description,
283+
Gitignores: form.Gitignores,
284+
IssueLabels: form.IssueLabels,
285+
License: form.License,
286+
Readme: form.Readme,
287+
IsPrivate: form.Private || setting.Repository.ForcePrivate,
288+
DefaultBranch: form.DefaultBranch,
289+
AutoInit: form.AutoInit,
290+
IsTemplate: form.Template,
291+
TrustModel: repo_model.ToTrustModel(form.TrustModel),
292+
ObjectFormatName: form.ObjectFormatName,
293293
})
294294
if err == nil {
295295
log.Trace("Repository created [%d]: %s/%s", repo.ID, ctxUser.Name, repo.Name)

services/forms/repo_form.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
issues_model "code.gitea.io/gitea/models/issues"
1414
project_model "code.gitea.io/gitea/models/project"
1515
"code.gitea.io/gitea/modules/context"
16-
"code.gitea.io/gitea/modules/git"
1716
"code.gitea.io/gitea/modules/setting"
1817
"code.gitea.io/gitea/modules/structs"
1918
"code.gitea.io/gitea/modules/web/middleware"
@@ -54,7 +53,7 @@ type CreateRepoForm struct {
5453
TrustModel string
5554

5655
ForkSingleBranch string
57-
ObjectFormat git.ObjectFormat
56+
ObjectFormatName string
5857
}
5958

6059
// Validate validates the fields

0 commit comments

Comments
 (0)