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

+2-1
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

+2-2
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

+12-1
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

+5-2
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

+2-2
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

+2-2
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

+12-12
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

+1-1
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

+12-12
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

+1-2
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

services/migrations/gitea_uploader_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func TestGiteaUploadUpdateGitForPullRequest(t *testing.T) {
232232
//
233233
fromRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
234234
baseRef := "master"
235-
assert.NoError(t, git.InitRepository(git.DefaultContext, fromRepo.RepoPath(), false, fromRepo.ObjectFormat))
235+
assert.NoError(t, git.InitRepository(git.DefaultContext, fromRepo.RepoPath(), false, fromRepo.ObjectFormatName))
236236
err := git.NewCommand(git.DefaultContext, "symbolic-ref").AddDynamicArguments("HEAD", git.BranchPrefix+baseRef).Run(&git.RunOpts{Dir: fromRepo.RepoPath()})
237237
assert.NoError(t, err)
238238
assert.NoError(t, os.WriteFile(filepath.Join(fromRepo.RepoPath(), "README.md"), []byte(fmt.Sprintf("# Testing Repository\n\nOriginally created in: %s", fromRepo.RepoPath())), 0o644))

services/packages/cargo/index.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ func alterRepositoryContent(ctx context.Context, doer *user_model.User, repo *re
271271
if !git.IsErrBranchNotExist(err) || !repo.IsEmpty {
272272
return err
273273
}
274-
if err := t.Init(repo.ObjectFormat); err != nil {
274+
if err := t.Init(repo.ObjectFormatName); err != nil {
275275
return err
276276
}
277277
} else {

services/pull/temp_repo.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,8 @@ func createTemporaryRepoForPR(ctx context.Context, pr *issues_model.PullRequest)
9393

9494
baseRepoPath := pr.BaseRepo.RepoPath()
9595
headRepoPath := pr.HeadRepo.RepoPath()
96-
objectFormat, err := git.GetObjectFormatOfRepo(ctx, baseRepoPath)
97-
if err != nil {
98-
log.Error("Unable to fetch ObjectFormat of repository %s: %v", baseRepoPath, err)
99-
cancel()
100-
return nil, nil, err
101-
}
10296

103-
if err := git.InitRepository(ctx, tmpBasePath, false, objectFormat); err != nil {
97+
if err := git.InitRepository(ctx, tmpBasePath, false, pr.BaseRepo.ObjectFormatName); err != nil {
10498
log.Error("Unable to init tmpBasePath for %-v: %v", pr, err)
10599
cancel()
106100
return nil, nil, err
@@ -174,6 +168,7 @@ func createTemporaryRepoForPR(ctx context.Context, pr *issues_model.PullRequest)
174168
}
175169

176170
trackingBranch := "tracking"
171+
objectFormat := git.ObjectFormatFromName(pr.BaseRepo.ObjectFormatName)
177172
// Fetch head branch
178173
var headBranch string
179174
if pr.Flow == issues_model.PullRequestFlowGithub {

services/repository/check.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func ReinitMissingRepositories(ctx context.Context) error {
192192
default:
193193
}
194194
log.Trace("Initializing %d/%d...", repo.OwnerID, repo.ID)
195-
if err := git.InitRepository(ctx, repo.RepoPath(), true, repo.ObjectFormat); err != nil {
195+
if err := git.InitRepository(ctx, repo.RepoPath(), true, repo.ObjectFormatName); err != nil {
196196
log.Error("Unable (re)initialize repository %d at %s. Error: %v", repo.ID, repo.RepoPath(), err)
197197
if err2 := system_model.CreateRepositoryNotice("InitRepository [%d]: %v", repo.ID, err); err2 != nil {
198198
log.Error("CreateRepositoryNotice: %v", err2)

services/repository/create.go

+19-23
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,23 @@ import (
2727

2828
// CreateRepoOptions contains the create repository options
2929
type CreateRepoOptions struct {
30-
Name string
31-
Description string
32-
OriginalURL string
33-
GitServiceType api.GitServiceType
34-
Gitignores string
35-
IssueLabels string
36-
License string
37-
Readme string
38-
DefaultBranch string
39-
IsPrivate bool
40-
IsMirror bool
41-
IsTemplate bool
42-
AutoInit bool
43-
Status repo_model.RepositoryStatus
44-
TrustModel repo_model.TrustModelType
45-
MirrorInterval string
46-
ObjectFormat git.ObjectFormat
30+
Name string
31+
Description string
32+
OriginalURL string
33+
GitServiceType api.GitServiceType
34+
Gitignores string
35+
IssueLabels string
36+
License string
37+
Readme string
38+
DefaultBranch string
39+
IsPrivate bool
40+
IsMirror bool
41+
IsTemplate bool
42+
AutoInit bool
43+
Status repo_model.RepositoryStatus
44+
TrustModel repo_model.TrustModelType
45+
MirrorInterval string
46+
ObjectFormatName string
4747
}
4848

4949
func prepareRepoCommit(ctx context.Context, repo *repo_model.Repository, tmpDir, repoPath string, opts CreateRepoOptions) error {
@@ -135,7 +135,7 @@ func prepareRepoCommit(ctx context.Context, repo *repo_model.Repository, tmpDir,
135135

136136
// InitRepository initializes README and .gitignore if needed.
137137
func initRepository(ctx context.Context, repoPath string, u *user_model.User, repo *repo_model.Repository, opts CreateRepoOptions) (err error) {
138-
if err = repo_module.CheckInitRepository(ctx, repo.OwnerName, repo.Name, opts.ObjectFormat); err != nil {
138+
if err = repo_module.CheckInitRepository(ctx, repo.OwnerName, repo.Name, opts.ObjectFormatName); err != nil {
139139
return err
140140
}
141141

@@ -210,10 +210,6 @@ func CreateRepositoryDirectly(ctx context.Context, doer, u *user_model.User, opt
210210
opts.DefaultBranch = setting.Repository.DefaultBranch
211211
}
212212

213-
if opts.ObjectFormat == nil {
214-
opts.ObjectFormat = git.Sha1ObjectFormat{}
215-
}
216-
217213
// Check if label template exist
218214
if len(opts.IssueLabels) > 0 {
219215
if _, err := repo_module.LoadTemplateLabelsByDisplayName(opts.IssueLabels); err != nil {
@@ -239,7 +235,7 @@ func CreateRepositoryDirectly(ctx context.Context, doer, u *user_model.User, opt
239235
TrustModel: opts.TrustModel,
240236
IsMirror: opts.IsMirror,
241237
DefaultBranch: opts.DefaultBranch,
242-
ObjectFormat: opts.ObjectFormat,
238+
ObjectFormatName: opts.ObjectFormatName,
243239
}
244240

245241
var rollbackRepo *repo_model.Repository

services/repository/files/cherry_pick.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"code.gitea.io/gitea/models"
1212
repo_model "code.gitea.io/gitea/models/repo"
1313
user_model "code.gitea.io/gitea/models/user"
14+
"code.gitea.io/gitea/modules/git"
1415
"code.gitea.io/gitea/modules/log"
1516
"code.gitea.io/gitea/modules/structs"
1617
"code.gitea.io/gitea/services/pull"
@@ -66,7 +67,7 @@ func CherryPick(ctx context.Context, repo *repo_model.Repository, doer *user_mod
6667
}
6768
parent, err := commit.ParentID(0)
6869
if err != nil {
69-
parent = repo.ObjectFormat.EmptyTree()
70+
parent = git.ObjectFormatFromName(repo.ObjectFormatName).EmptyTree()
7071
}
7172

7273
base, right := parent.String(), commit.ID.String()

services/repository/files/temp_repo.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ func (t *TemporaryUploadRepository) Clone(branch string) error {
7777
}
7878

7979
// Init the repository
80-
func (t *TemporaryUploadRepository) Init(objectFormat git.ObjectFormat) error {
81-
if err := git.InitRepository(t.ctx, t.basePath, false, objectFormat); err != nil {
80+
func (t *TemporaryUploadRepository) Init(objectFormatName string) error {
81+
if err := git.InitRepository(t.ctx, t.basePath, false, objectFormatName); err != nil {
8282
return err
8383
}
8484
gitRepo, err := git.OpenRepository(t.ctx, t.basePath)

services/repository/files/update.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,7 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
155155
if !git.IsErrBranchNotExist(err) || !repo.IsEmpty {
156156
return nil, err
157157
}
158-
objectFormat, _ := gitRepo.GetObjectFormat()
159-
if err := t.Init(objectFormat); err != nil {
158+
if err := t.Init(repo.ObjectFormatName); err != nil {
160159
return nil, err
161160
}
162161
hasOldBranch = false

services/repository/files/upload.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func UploadRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
9191
if !git.IsErrBranchNotExist(err) || !repo.IsEmpty {
9292
return err
9393
}
94-
if err = t.Init(repo.ObjectFormat); err != nil {
94+
if err = t.Init(repo.ObjectFormatName); err != nil {
9595
return err
9696
}
9797
hasOldBranch = false

services/wiki/wiki.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func InitWiki(ctx context.Context, repo *repo_model.Repository) error {
3636
return nil
3737
}
3838

39-
if err := git.InitRepository(ctx, repo.WikiPath(), true, git.Sha1ObjectFormat{}); err != nil {
39+
if err := git.InitRepository(ctx, repo.WikiPath(), true, repo.ObjectFormatName); err != nil {
4040
return fmt.Errorf("InitRepository: %w", err)
4141
} else if err = repo_module.CreateDelegateHooks(repo.WikiPath()); err != nil {
4242
return fmt.Errorf("createDelegateHooks: %w", err)

services/wiki/wiki_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ func TestPrepareWikiFileName_FirstPage(t *testing.T) {
302302
// Now create a temporaryDirectory
303303
tmpDir := t.TempDir()
304304

305-
err := git.InitRepository(git.DefaultContext, tmpDir, true, git.Sha1ObjectFormat{})
305+
err := git.InitRepository(git.DefaultContext, tmpDir, true, git.Sha1ObjectFormatName)
306306
assert.NoError(t, err)
307307

308308
gitRepo, err := git.OpenRepository(git.DefaultContext, tmpDir)

tests/integration/git_helper_for_declarative_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func doGitCloneFail(u *url.URL) func(*testing.T) {
120120
func doGitInitTestRepository(dstPath string) func(*testing.T) {
121121
return func(t *testing.T) {
122122
// Init repository in dstPath
123-
assert.NoError(t, git.InitRepository(git.DefaultContext, dstPath, false, git.Sha1ObjectFormat{}))
123+
assert.NoError(t, git.InitRepository(git.DefaultContext, dstPath, false, git.Sha1ObjectFormatName))
124124
// forcibly set default branch to master
125125
_, _, err := git.NewCommand(git.DefaultContext, "symbolic-ref", "HEAD", git.BranchPrefix+"master").RunStdString(&git.RunOpts{Dir: dstPath})
126126
assert.NoError(t, err)

0 commit comments

Comments
 (0)