Skip to content

Commit b79c304

Browse files
authored
Use the database object format name but not read from git repoisitory everytime and fix possible migration wrong objectformat when migrating a sha256 repository (#29294)
Now we can get object format name from git command line or from the database repository table. Assume the column is right, we don't need to read from git command line every time. This also fixed a possible bug that the object format is wrong when migrating a sha256 repository from external. <img width="658" alt="image" src="https://github.com/go-gitea/gitea/assets/81045/6e9a9dcf-13bf-4267-928b-6bf2c2560423">
1 parent 4ba642d commit b79c304

File tree

17 files changed

+41
-56
lines changed

17 files changed

+41
-56
lines changed

modules/context/api.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,6 @@ func RepoRefForAPI(next http.Handler) http.Handler {
307307
return
308308
}
309309

310-
objectFormat, err := ctx.Repo.GitRepo.GetObjectFormat()
311-
if err != nil {
312-
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
313-
return
314-
}
315-
316310
if ref := ctx.FormTrim("ref"); len(ref) > 0 {
317311
commit, err := ctx.Repo.GitRepo.GetCommit(ref)
318312
if err != nil {
@@ -331,6 +325,7 @@ func RepoRefForAPI(next http.Handler) http.Handler {
331325
}
332326

333327
refName := getRefName(ctx.Base, ctx.Repo, RepoRefAny)
328+
var err error
334329

335330
if ctx.Repo.GitRepo.IsBranchExist(refName) {
336331
ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetBranchCommit(refName)
@@ -346,7 +341,7 @@ func RepoRefForAPI(next http.Handler) http.Handler {
346341
return
347342
}
348343
ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
349-
} else if len(refName) == objectFormat.FullLength() {
344+
} else if len(refName) == ctx.Repo.GetObjectFormat().FullLength() {
350345
ctx.Repo.CommitID = refName
351346
ctx.Repo.Commit, err = ctx.Repo.GitRepo.GetCommit(refName)
352347
if err != nil {

modules/context/repo.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ func (r *Repository) CanCreateBranch() bool {
8383
return r.Permission.CanWrite(unit_model.TypeCode) && r.Repository.CanCreateBranch()
8484
}
8585

86+
func (r *Repository) GetObjectFormat() git.ObjectFormat {
87+
return git.ObjectFormatFromName(r.Repository.ObjectFormatName)
88+
}
89+
8690
// RepoMustNotBeArchived checks if a repo is archived
8791
func RepoMustNotBeArchived() func(ctx *Context) {
8892
return func(ctx *Context) {
@@ -830,9 +834,8 @@ func getRefName(ctx *Base, repo *Repository, pathType RepoRefType) string {
830834
}
831835
// For legacy and API support only full commit sha
832836
parts := strings.Split(path, "/")
833-
objectFormat, _ := repo.GitRepo.GetObjectFormat()
834837

835-
if len(parts) > 0 && len(parts[0]) == objectFormat.FullLength() {
838+
if len(parts) > 0 && len(parts[0]) == git.ObjectFormatFromName(repo.Repository.ObjectFormatName).FullLength() {
836839
repo.TreePath = strings.Join(parts[1:], "/")
837840
return parts[0]
838841
}
@@ -876,9 +879,8 @@ func getRefName(ctx *Base, repo *Repository, pathType RepoRefType) string {
876879
return getRefNameFromPath(ctx, repo, path, repo.GitRepo.IsTagExist)
877880
case RepoRefCommit:
878881
parts := strings.Split(path, "/")
879-
objectFormat, _ := repo.GitRepo.GetObjectFormat()
880882

881-
if len(parts) > 0 && len(parts[0]) >= 7 && len(parts[0]) <= objectFormat.FullLength() {
883+
if len(parts) > 0 && len(parts[0]) >= 7 && len(parts[0]) <= repo.GetObjectFormat().FullLength() {
882884
repo.TreePath = strings.Join(parts[1:], "/")
883885
return parts[0]
884886
}
@@ -937,12 +939,6 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context
937939
}
938940
}
939941

940-
objectFormat, err := ctx.Repo.GitRepo.GetObjectFormat()
941-
if err != nil {
942-
log.Error("Cannot determine objectFormat for repository: %w", err)
943-
ctx.Repo.Repository.MarkAsBrokenEmpty()
944-
}
945-
946942
// Get default branch.
947943
if len(ctx.Params("*")) == 0 {
948944
refName = ctx.Repo.Repository.DefaultBranch
@@ -1009,7 +1005,7 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context
10091005
return cancel
10101006
}
10111007
ctx.Repo.CommitID = ctx.Repo.Commit.ID.String()
1012-
} else if len(refName) >= 7 && len(refName) <= objectFormat.FullLength() {
1008+
} else if len(refName) >= 7 && len(refName) <= ctx.Repo.GetObjectFormat().FullLength() {
10131009
ctx.Repo.IsViewCommit = true
10141010
ctx.Repo.CommitID = refName
10151011

@@ -1019,7 +1015,7 @@ func RepoRefByType(refType RepoRefType, ignoreNotExistErr ...bool) func(*Context
10191015
return cancel
10201016
}
10211017
// If short commit ID add canonical link header
1022-
if len(refName) < objectFormat.FullLength() {
1018+
if len(refName) < ctx.Repo.GetObjectFormat().FullLength() {
10231019
ctx.RespHeader().Set("Link", fmt.Sprintf("<%s>; rel=\"canonical\"",
10241020
util.URLJoin(setting.AppURL, strings.Replace(ctx.Req.URL.RequestURI(), util.PathEscapeSegments(refName), url.PathEscape(ctx.Repo.Commit.ID.String()), 1))))
10251021
}

routers/api/v1/utils/git.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func searchRefCommitByType(ctx *context.APIContext, refType, filter string) (str
7272

7373
// ConvertToObjectID returns a full-length SHA1 from a potential ID string
7474
func ConvertToObjectID(ctx gocontext.Context, repo *context.Repository, commitID string) (git.ObjectID, error) {
75-
objectFormat, _ := repo.GitRepo.GetObjectFormat()
75+
objectFormat := repo.GetObjectFormat()
7676
if len(commitID) == objectFormat.FullLength() && objectFormat.IsValid(commitID) {
7777
sha, err := git.NewIDFromString(commitID)
7878
if err == nil {

routers/private/hook_pre_receive.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID string, r
145145

146146
repo := ctx.Repo.Repository
147147
gitRepo := ctx.Repo.GitRepo
148-
objectFormat, _ := gitRepo.GetObjectFormat()
148+
objectFormat := ctx.Repo.GetObjectFormat()
149149

150150
if branchName == repo.DefaultBranch && newCommitID == objectFormat.EmptyObjectID().String() {
151151
log.Warn("Forbidden: Branch: %s is the default branch in %-v and cannot be deleted", branchName, repo)

routers/web/repo/blame.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,8 @@ type blameResult struct {
132132
}
133133

134134
func performBlame(ctx *context.Context, repoPath string, commit *git.Commit, file string, bypassBlameIgnore bool) (*blameResult, error) {
135-
objectFormat, err := ctx.Repo.GitRepo.GetObjectFormat()
136-
if err != nil {
137-
ctx.NotFound("CreateBlameReader", err)
138-
return nil, err
139-
}
135+
objectFormat := ctx.Repo.GetObjectFormat()
136+
140137
blameReader, err := git.CreateBlameReader(ctx, objectFormat, repoPath, commit, file, bypassBlameIgnore)
141138
if err != nil {
142139
return nil, err

routers/web/repo/compare.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,14 +312,14 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo {
312312
baseIsCommit := ctx.Repo.GitRepo.IsCommitExist(ci.BaseBranch)
313313
baseIsBranch := ctx.Repo.GitRepo.IsBranchExist(ci.BaseBranch)
314314
baseIsTag := ctx.Repo.GitRepo.IsTagExist(ci.BaseBranch)
315-
objectFormat, _ := ctx.Repo.GitRepo.GetObjectFormat()
315+
316316
if !baseIsCommit && !baseIsBranch && !baseIsTag {
317317
// Check if baseBranch is short sha commit hash
318318
if baseCommit, _ := ctx.Repo.GitRepo.GetCommit(ci.BaseBranch); baseCommit != nil {
319319
ci.BaseBranch = baseCommit.ID.String()
320320
ctx.Data["BaseBranch"] = ci.BaseBranch
321321
baseIsCommit = true
322-
} else if ci.BaseBranch == objectFormat.EmptyObjectID().String() {
322+
} else if ci.BaseBranch == ctx.Repo.GetObjectFormat().EmptyObjectID().String() {
323323
if isSameRepo {
324324
ctx.Redirect(ctx.Repo.RepoLink + "/compare/" + util.PathEscapeSegments(ci.HeadBranch))
325325
} else {

routers/web/repo/setting/lfs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ func LFSFileFind(ctx *context.Context) {
388388
sha := ctx.FormString("sha")
389389
ctx.Data["Title"] = oid
390390
ctx.Data["PageIsSettingsLFS"] = true
391-
objectFormat, _ := ctx.Repo.GitRepo.GetObjectFormat()
391+
objectFormat := ctx.Repo.GetObjectFormat()
392392
var objectID git.ObjectID
393393
if len(sha) == 0 {
394394
pointer := lfs.Pointer{Oid: oid, Size: size}

services/agit/agit.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
3636

3737
topicBranch = opts.GitPushOptions["topic"]
3838
_, forcePush = opts.GitPushOptions["force-push"]
39-
objectFormat, _ := gitRepo.GetObjectFormat()
39+
objectFormat := git.ObjectFormatFromName(repo.ObjectFormatName)
4040

4141
pusher, err := user_model.GetUserByID(ctx, opts.UserID)
4242
if err != nil {
@@ -149,7 +149,6 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
149149

150150
log.Trace("Pull request created: %d/%d", repo.ID, prIssue.ID)
151151

152-
objectFormat, _ := gitRepo.GetObjectFormat()
153152
results = append(results, private.HookProcReceiveRefResult{
154153
Ref: pr.GetGitRefName(),
155154
OriginalRef: opts.RefFullNames[i],

services/migrations/gitea_uploader.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,18 @@ func (g *GiteaLocalUploader) CreateRepo(repo *base.Repository, opts base.Migrate
140140
if err != nil {
141141
return err
142142
}
143-
g.gitRepo, err = gitrepo.OpenRepository(g.ctx, r)
144-
return err
143+
g.gitRepo, err = gitrepo.OpenRepository(g.ctx, g.repo)
144+
if err != nil {
145+
return err
146+
}
147+
148+
// detect object format from git repository and update to database
149+
objectFormat, err := g.gitRepo.GetObjectFormat()
150+
if err != nil {
151+
return err
152+
}
153+
g.repo.ObjectFormatName = objectFormat.Name()
154+
return repo_model.UpdateRepositoryCols(g.ctx, g.repo, "object_format_name")
145155
}
146156

147157
// Close closes this uploader
@@ -896,7 +906,7 @@ func (g *GiteaLocalUploader) CreateReviews(reviews ...*base.Review) error {
896906
comment.UpdatedAt = comment.CreatedAt
897907
}
898908

899-
objectFormat, _ := g.gitRepo.GetObjectFormat()
909+
objectFormat := git.ObjectFormatFromName(g.repo.ObjectFormatName)
900910
if !objectFormat.IsValid(comment.CommitID) {
901911
log.Warn("Invalid comment CommitID[%s] on comment[%d] in PR #%d of %s/%s replaced with %s", comment.CommitID, pr.Index, g.repoOwner, g.repoName, headCommitID)
902912
comment.CommitID = headCommitID

services/pull/check.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,7 @@ func getMergeCommit(ctx context.Context, pr *issues_model.PullRequest) (*git.Com
222222
}
223223
defer gitRepo.Close()
224224

225-
objectFormat, err := gitRepo.GetObjectFormat()
226-
if err != nil {
227-
return nil, fmt.Errorf("%-v GetObjectFormat: %w", pr.BaseRepo, err)
228-
}
225+
objectFormat := git.ObjectFormatFromName(pr.BaseRepo.ObjectFormatName)
229226

230227
// Get the commit from BaseBranch where the pull request got merged
231228
mergeCommit, _, err := git.NewCommand(ctx, "rev-list", "--ancestry-path", "--merges", "--reverse").

services/pull/merge.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ func MergedManually(ctx context.Context, pr *issues_model.PullRequest, doer *use
497497
return models.ErrInvalidMergeStyle{ID: pr.BaseRepo.ID, Style: repo_model.MergeStyleManuallyMerged}
498498
}
499499

500-
objectFormat, _ := baseGitRepo.GetObjectFormat()
500+
objectFormat := git.ObjectFormatFromName(pr.BaseRepo.ObjectFormatName)
501501
if len(commitID) != objectFormat.FullLength() {
502502
return fmt.Errorf("Wrong commit ID")
503503
}

services/release/release.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func createTag(ctx context.Context, gitRepo *git.Repository, rel *repo_model.Rel
8888
created = true
8989
rel.LowerTagName = strings.ToLower(rel.TagName)
9090

91-
objectFormat, _ := gitRepo.GetObjectFormat()
91+
objectFormat := git.ObjectFormatFromName(rel.Repo.ObjectFormatName)
9292
commits := repository.NewPushCommits()
9393
commits.HeadCommit = repository.CommitToPushCommit(commit)
9494
commits.CompareURL = rel.Repo.ComposeCompareURL(objectFormat.EmptyObjectID().String(), commit.ID.String())

services/repository/branch.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -380,11 +380,6 @@ func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.R
380380
return fmt.Errorf("GetBranch: %vc", err)
381381
}
382382

383-
objectFormat, err := gitRepo.GetObjectFormat()
384-
if err != nil {
385-
return err
386-
}
387-
388383
if rawBranch.IsDeleted {
389384
return nil
390385
}
@@ -406,6 +401,8 @@ func DeleteBranch(ctx context.Context, doer *user_model.User, repo *repo_model.R
406401
return err
407402
}
408403

404+
objectFormat := git.ObjectFormatFromName(repo.ObjectFormatName)
405+
409406
// Don't return error below this
410407
if err := PushUpdate(
411408
&repo_module.PushUpdateOptions{

services/repository/files/commit.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ func CreateCommitStatus(ctx context.Context, repo *repo_model.Repository, creato
3030
}
3131
defer closer.Close()
3232

33-
objectFormat, err := gitRepo.GetObjectFormat()
34-
if err != nil {
35-
return fmt.Errorf("GetObjectFormat[%s]: %w", repoPath, err)
36-
}
33+
objectFormat := git.ObjectFormatFromName(repo.ObjectFormatName)
34+
3735
commit, err := gitRepo.GetCommit(sha)
3836
if err != nil {
3937
gitRepo.Close()

services/repository/files/tree.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func GetTreeBySHA(ctx context.Context, repo *repo_model.Repository, gitRepo *git
3737
}
3838
apiURL := repo.APIURL()
3939
apiURLLen := len(apiURL)
40-
objectFormat, _ := gitRepo.GetObjectFormat()
40+
objectFormat := git.ObjectFormatFromName(repo.ObjectFormatName)
4141
hashLen := objectFormat.FullLength()
4242

4343
const gitBlobsPath = "/git/blobs/"

services/repository/lfs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func GarbageCollectLFSMetaObjectsForRepo(ctx context.Context, repo *repo_model.R
7979

8080
store := lfs.NewContentStore()
8181
errStop := errors.New("STOPERR")
82-
objectFormat, _ := gitRepo.GetObjectFormat()
82+
objectFormat := git.ObjectFormatFromName(repo.ObjectFormatName)
8383

8484
err = git_model.IterateLFSMetaObjectsForRepo(ctx, repo.ID, func(ctx context.Context, metaObject *git_model.LFSMetaObject, count int64) error {
8585
if opts.NumberToCheckPerRepo > 0 && total > opts.NumberToCheckPerRepo {

services/repository/push.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,14 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
9393
}
9494
defer gitRepo.Close()
9595

96-
objectFormat, err := gitRepo.GetObjectFormat()
97-
if err != nil {
98-
return fmt.Errorf("unknown repository ObjectFormat [%s]: %w", repo.FullName(), err)
99-
}
100-
10196
if err = repo_module.UpdateRepoSize(ctx, repo); err != nil {
10297
return fmt.Errorf("Failed to update size for repository: %v", err)
10398
}
10499

105100
addTags := make([]string, 0, len(optsList))
106101
delTags := make([]string, 0, len(optsList))
107102
var pusher *user_model.User
103+
objectFormat := git.ObjectFormatFromName(repo.ObjectFormatName)
108104

109105
for _, opts := range optsList {
110106
log.Trace("pushUpdates: %-v %s %s %s", repo, opts.OldCommitID, opts.NewCommitID, opts.RefFullName)

0 commit comments

Comments
 (0)