diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2023-12-17 19:56:08 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-17 11:56:08 +0000 |
commit | 408a4842240e7dd906e682196bd4254d6c76fcb9 (patch) | |
tree | ec3962d5769a5891a24b4d79f725673fc1e46e35 /routers | |
parent | 7fb6b5147038649bfaa26147a6173cf99b322a30 (diff) | |
download | gitea-408a4842240e7dd906e682196bd4254d6c76fcb9.tar.gz gitea-408a4842240e7dd906e682196bd4254d6c76fcb9.zip |
Adjust object format interface (#28469)
- Remove `ObjectFormatID`
- Remove function `ObjectFormatFromID`.
- Use `Sha1ObjectFormat` directly but not a pointer because it's an
empty struct.
- Store `ObjectFormatName` in `repository` struct
Diffstat (limited to 'routers')
-rw-r--r-- | routers/api/v1/repo/repo.go | 24 | ||||
-rw-r--r-- | routers/api/v1/utils/git.go | 2 | ||||
-rw-r--r-- | routers/private/hook_pre_receive.go | 6 | ||||
-rw-r--r-- | routers/private/hook_verification.go | 2 | ||||
-rw-r--r-- | routers/private/hook_verification_test.go | 4 | ||||
-rw-r--r-- | routers/web/repo/branch.go | 2 | ||||
-rw-r--r-- | routers/web/repo/compare.go | 2 | ||||
-rw-r--r-- | routers/web/repo/githttp.go | 2 | ||||
-rw-r--r-- | routers/web/repo/repo.go | 24 | ||||
-rw-r--r-- | routers/web/repo/setting/webhook.go | 2 |
10 files changed, 35 insertions, 35 deletions
diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index 1767a7fa67..6eb2cc4227 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -242,18 +242,18 @@ func CreateUserRepo(ctx *context.APIContext, owner *user_model.User, opt api.Cre } repo, err := repo_service.CreateRepository(ctx, ctx.Doer, owner, repo_service.CreateRepoOptions{ - Name: opt.Name, - Description: opt.Description, - IssueLabels: opt.IssueLabels, - Gitignores: opt.Gitignores, - License: opt.License, - Readme: opt.Readme, - IsPrivate: opt.Private, - AutoInit: opt.AutoInit, - DefaultBranch: opt.DefaultBranch, - TrustModel: repo_model.ToTrustModel(opt.TrustModel), - IsTemplate: opt.Template, - ObjectFormat: git.ObjectFormatFromID(git.Sha1), + Name: opt.Name, + Description: opt.Description, + IssueLabels: opt.IssueLabels, + Gitignores: opt.Gitignores, + License: opt.License, + Readme: opt.Readme, + IsPrivate: opt.Private, + AutoInit: opt.AutoInit, + DefaultBranch: opt.DefaultBranch, + TrustModel: repo_model.ToTrustModel(opt.TrustModel), + IsTemplate: opt.Template, + ObjectFormatName: git.Sha1ObjectFormat.Name(), }) if err != nil { if repo_model.IsErrRepoAlreadyExist(err) { diff --git a/routers/api/v1/utils/git.go b/routers/api/v1/utils/git.go index eb82c50544..dfb1a130c3 100644 --- a/routers/api/v1/utils/git.go +++ b/routers/api/v1/utils/git.go @@ -81,7 +81,7 @@ func ConvertToObjectID(ctx gocontext.Context, repo *context.Repository, commitID gitRepo, closer, err := git.RepositoryFromContextOrOpen(ctx, repo.Repository.RepoPath()) if err != nil { - return objectFormat.Empty(), fmt.Errorf("RepositoryFromContextOrOpen: %w", err) + return objectFormat.EmptyObjectID(), fmt.Errorf("RepositoryFromContextOrOpen: %w", err) } defer closer.Close() diff --git a/routers/private/hook_pre_receive.go b/routers/private/hook_pre_receive.go index 8811809710..90d8287f06 100644 --- a/routers/private/hook_pre_receive.go +++ b/routers/private/hook_pre_receive.go @@ -147,7 +147,7 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID string, r gitRepo := ctx.Repo.GitRepo objectFormat, _ := gitRepo.GetObjectFormat() - if branchName == repo.DefaultBranch && newCommitID == objectFormat.Empty().String() { + if branchName == repo.DefaultBranch && newCommitID == objectFormat.EmptyObjectID().String() { log.Warn("Forbidden: Branch: %s is the default branch in %-v and cannot be deleted", branchName, repo) ctx.JSON(http.StatusForbidden, private.Response{ UserMsg: fmt.Sprintf("branch %s is the default branch and cannot be deleted", branchName), @@ -175,7 +175,7 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID string, r // First of all we need to enforce absolutely: // // 1. Detect and prevent deletion of the branch - if newCommitID == objectFormat.Empty().String() { + if newCommitID == objectFormat.EmptyObjectID().String() { log.Warn("Forbidden: Branch: %s in %-v is protected from deletion", branchName, repo) ctx.JSON(http.StatusForbidden, private.Response{ UserMsg: fmt.Sprintf("branch %s is protected from deletion", branchName), @@ -184,7 +184,7 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID string, r } // 2. Disallow force pushes to protected branches - if oldCommitID != objectFormat.Empty().String() { + if oldCommitID != objectFormat.EmptyObjectID().String() { output, _, err := git.NewCommand(ctx, "rev-list", "--max-count=1").AddDynamicArguments(oldCommitID, "^"+newCommitID).RunStdString(&git.RunOpts{Dir: repo.RepoPath(), Env: ctx.env}) if err != nil { log.Error("Unable to detect force push between: %s and %s in %-v Error: %v", oldCommitID, newCommitID, repo, err) diff --git a/routers/private/hook_verification.go b/routers/private/hook_verification.go index 6725205cc6..8b2d0dd848 100644 --- a/routers/private/hook_verification.go +++ b/routers/private/hook_verification.go @@ -30,7 +30,7 @@ func verifyCommits(oldCommitID, newCommitID string, repo *git.Repository, env [] var command *git.Command objectFormat, _ := repo.GetObjectFormat() - if oldCommitID == objectFormat.Empty().String() { + if oldCommitID == objectFormat.EmptyObjectID().String() { // When creating a new branch, the oldCommitID is empty, by using "newCommitID --not --all": // List commits that are reachable by following the newCommitID, exclude "all" existing heads/tags commits // So, it only lists the new commits received, doesn't list the commits already present in the receiving repository diff --git a/routers/private/hook_verification_test.go b/routers/private/hook_verification_test.go index 7263ebc423..04445b8eaf 100644 --- a/routers/private/hook_verification_test.go +++ b/routers/private/hook_verification_test.go @@ -30,9 +30,9 @@ func TestVerifyCommits(t *testing.T) { verified bool }{ {"72920278f2f999e3005801e5d5b8ab8139d3641c", "d766f2917716d45be24bfa968b8409544941be32", true}, - {objectFormat.Empty().String(), "93eac826f6188f34646cea81bf426aa5ba7d3bfe", true}, // New branch with verified commit + {objectFormat.EmptyObjectID().String(), "93eac826f6188f34646cea81bf426aa5ba7d3bfe", true}, // New branch with verified commit {"9779d17a04f1e2640583d35703c62460b2d86e0a", "72920278f2f999e3005801e5d5b8ab8139d3641c", false}, - {objectFormat.Empty().String(), "9ce3f779ae33f31fce17fac3c512047b75d7498b", false}, // New branch with unverified commit + {objectFormat.EmptyObjectID().String(), "9ce3f779ae33f31fce17fac3c512047b75d7498b", false}, // New branch with unverified commit } for _, tc := range testCases { diff --git a/routers/web/repo/branch.go b/routers/web/repo/branch.go index b6de5bf800..c543160f42 100644 --- a/routers/web/repo/branch.go +++ b/routers/web/repo/branch.go @@ -158,7 +158,7 @@ func RestoreBranchPost(ctx *context.Context) { if err := repo_service.PushUpdate( &repo_module.PushUpdateOptions{ RefFullName: git.RefNameFromBranch(deletedBranch.Name), - OldCommitID: objectFormat.Empty().String(), + OldCommitID: objectFormat.EmptyObjectID().String(), NewCommitID: deletedBranch.CommitID, PusherID: ctx.Doer.ID, PusherName: ctx.Doer.Name, diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 5b1638d3f5..f3b95b68fe 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -317,7 +317,7 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo { ci.BaseBranch = baseCommit.ID.String() ctx.Data["BaseBranch"] = ci.BaseBranch baseIsCommit = true - } else if ci.BaseBranch == objectFormat.Empty().String() { + } else if ci.BaseBranch == objectFormat.EmptyObjectID().String() { if isSameRepo { ctx.Redirect(ctx.Repo.RepoLink + "/compare/" + util.PathEscapeSegments(ci.HeadBranch)) } else { diff --git a/routers/web/repo/githttp.go b/routers/web/repo/githttp.go index dd47bd79d9..6d3dd5a3fe 100644 --- a/routers/web/repo/githttp.go +++ b/routers/web/repo/githttp.go @@ -329,7 +329,7 @@ func dummyInfoRefs(ctx *context.Context) { } }() - if err := git.InitRepository(ctx, tmpDir, true, git.ObjectFormatFromID(git.Sha1)); err != nil { + if err := git.InitRepository(ctx, tmpDir, true, git.Sha1ObjectFormat.Name()); err != nil { log.Error("Failed to init bare repo for git-receive-pack cache: %v", err) return } diff --git a/routers/web/repo/repo.go b/routers/web/repo/repo.go index 7a2976f8dc..b16e283836 100644 --- a/routers/web/repo/repo.go +++ b/routers/web/repo/repo.go @@ -278,18 +278,18 @@ func CreatePost(ctx *context.Context) { } } else { repo, err = repo_service.CreateRepository(ctx, ctx.Doer, ctxUser, repo_service.CreateRepoOptions{ - Name: form.RepoName, - Description: form.Description, - Gitignores: form.Gitignores, - IssueLabels: form.IssueLabels, - License: form.License, - Readme: form.Readme, - IsPrivate: form.Private || setting.Repository.ForcePrivate, - DefaultBranch: form.DefaultBranch, - AutoInit: form.AutoInit, - IsTemplate: form.Template, - TrustModel: repo_model.ToTrustModel(form.TrustModel), - ObjectFormat: form.ObjectFormat, + Name: form.RepoName, + Description: form.Description, + Gitignores: form.Gitignores, + IssueLabels: form.IssueLabels, + License: form.License, + Readme: form.Readme, + IsPrivate: form.Private || setting.Repository.ForcePrivate, + DefaultBranch: form.DefaultBranch, + AutoInit: form.AutoInit, + IsTemplate: form.Template, + TrustModel: repo_model.ToTrustModel(form.TrustModel), + ObjectFormatName: form.ObjectFormatName, }) if err == nil { log.Trace("Repository created [%d]: %s/%s", repo.ID, ctxUser.Name, repo.Name) diff --git a/routers/web/repo/setting/webhook.go b/routers/web/repo/setting/webhook.go index 8c232a4cb8..ab3c70006f 100644 --- a/routers/web/repo/setting/webhook.go +++ b/routers/web/repo/setting/webhook.go @@ -662,7 +662,7 @@ func TestWebhook(ctx *context.Context) { return } commit = &git.Commit{ - ID: objectFormat.NewEmptyID(), + ID: objectFormat.EmptyObjectID(), Author: ghost.NewGitSig(), Committer: ghost.NewGitSig(), CommitMessage: "This is a fake commit", |