diff --git a/models/fixtures/branch.yml b/models/fixtures/branch.yml index 17b1869ab6364..5cdb51202ce5f 100644 --- a/models/fixtures/branch.yml +++ b/models/fixtures/branch.yml @@ -93,3 +93,75 @@ is_deleted: false deleted_by_id: 0 deleted_unix: 0 + +- + id: 16 + repo_id: 1 + name: 'DefaultBranch' + commit_id: '90c1019714259b24fb81711d4416ac0f18667dfa' + commit_message: 'add license' + commit_time: 1709259547 + pusher_id: 1 + is_deleted: false + deleted_by_id: 0 + deleted_unix: 0 + +- + id: 17 + repo_id: 1 + name: 'develop' + commit_id: '65f1bf27bc3bf70f64657658635e66094edbcb4d' + commit_message: 'Initial commit' + commit_time: 1489927679 + pusher_id: 1 + is_deleted: false + deleted_by_id: 0 + deleted_unix: 0 + +- + id: 18 + repo_id: 1 + name: 'feature/1' + commit_id: '65f1bf27bc3bf70f64657658635e66094edbcb4d' + commit_message: 'Initial commit' + commit_time: 1489927679 + pusher_id: 1 + is_deleted: false + deleted_by_id: 0 + deleted_unix: 0 + +- + id: 19 + repo_id: 1 + name: 'home-md-img-check' + commit_id: '78fb907e3a3309eae4fe8fef030874cebbf1cd5e' + commit_message: 'add test fake img' + commit_time: 1677760557 + pusher_id: 1 + is_deleted: false + deleted_by_id: 0 + deleted_unix: 0 + +- + id: 20 + repo_id: 1 + name: 'pr-to-update' + commit_id: '62fb502a7172d4453f0322a2cc85bddffa57f07a' + commit_message: 'add WoW File' + commit_time: 1579200695 + pusher_id: 1 + is_deleted: false + deleted_by_id: 0 + deleted_unix: 0 + +- + id: 21 + repo_id: 1 + name: 'sub-home-md-img-check' + commit_id: '4649299398e4d39a5c09eb4f534df6f1e1eb87cc' + commit_message: 'Test how READMEs render images when found in a subfolder' + commit_time: 1678403550 + pusher_id: 1 + is_deleted: false + deleted_by_id: 0 + deleted_unix: 0 diff --git a/models/git/branch.go b/models/git/branch.go index e683ce47e657e..47ebc2b7a31c7 100644 --- a/models/git/branch.go +++ b/models/git/branch.go @@ -170,6 +170,20 @@ func GetBranch(ctx context.Context, repoID int64, branchName string) (*Branch, e return &branch, nil } +func GetNonDeletedBranch(ctx context.Context, repoID int64, branchName string) (*Branch, error) { + b, err := GetBranch(ctx, repoID, branchName) + if err != nil { + return nil, err + } + if b.IsDeleted { + return nil, ErrBranchNotExist{ + RepoID: repoID, + BranchName: branchName, + } + } + return b, nil +} + func GetBranches(ctx context.Context, repoID int64, branchNames []string, includeDeleted bool) ([]*Branch, error) { branches := make([]*Branch, 0, len(branchNames)) diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index 67dfda39a8138..e36d7d69af92b 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -58,9 +58,9 @@ func GetBranch(ctx *context.APIContext) { branchName := ctx.PathParam("*") - branch, err := ctx.Repo.GitRepo.GetBranch(branchName) + branch, err := git_model.GetNonDeletedBranch(ctx, ctx.Repo.Repository.ID, branchName) if err != nil { - if git.IsErrBranchNotExist(err) { + if git_model.IsErrBranchNotExist(err) { ctx.NotFound(err) } else { ctx.Error(http.StatusInternalServerError, "GetBranch", err) @@ -68,7 +68,7 @@ func GetBranch(ctx *context.APIContext) { return } - c, err := branch.GetCommit() + c, err := ctx.Repo.GitRepo.GetCommit(branch.CommitID) if err != nil { ctx.Error(http.StatusInternalServerError, "GetCommit", err) return @@ -152,7 +152,7 @@ func DeleteBranch(ctx *context.APIContext) { if err := repo_service.DeleteBranch(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, branchName); err != nil { switch { - case git.IsErrBranchNotExist(err): + case git_model.IsErrBranchNotExist(err): ctx.NotFound(err) case errors.Is(err, repo_service.ErrBranchIsDefault): ctx.Error(http.StatusForbidden, "DefaultBranch", fmt.Errorf("can not delete default branch")) @@ -259,13 +259,17 @@ func CreateBranch(ctx *context.APIContext) { return } - branch, err := ctx.Repo.GitRepo.GetBranch(opt.BranchName) + branch, err := git_model.GetNonDeletedBranch(ctx, ctx.Repo.Repository.ID, opt.BranchName) if err != nil { - ctx.Error(http.StatusInternalServerError, "GetBranch", err) + if git_model.IsErrBranchNotExist(err) { + ctx.NotFound(err) + } else { + ctx.Error(http.StatusInternalServerError, "GetBranch", err) + } return } - commit, err := branch.GetCommit() + commit, err := ctx.Repo.GitRepo.GetCommit(branch.CommitID) if err != nil { ctx.Error(http.StatusInternalServerError, "GetCommit", err) return diff --git a/routers/api/v1/repo/file.go b/routers/api/v1/repo/file.go index 6591b9a752faa..b4749168bb303 100644 --- a/routers/api/v1/repo/file.go +++ b/routers/api/v1/repo/file.go @@ -886,7 +886,7 @@ func DeleteFile(ctx *context.APIContext) { } if filesResponse, err := files_service.ChangeRepoFiles(ctx, ctx.Repo.Repository, ctx.Doer, opts); err != nil { - if git.IsErrBranchNotExist(err) || files_service.IsErrRepoFileDoesNotExist(err) || git.IsErrNotExist(err) { + if git_model.IsErrBranchNotExist(err) || files_service.IsErrRepoFileDoesNotExist(err) || git.IsErrNotExist(err) { ctx.Error(http.StatusNotFound, "DeleteFile", err) return } else if git_model.IsErrBranchAlreadyExists(err) || diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index d0c3459b6343b..f0788f75b7d5c 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -1049,7 +1049,7 @@ func MergePullRequest(ctx *context.APIContext) { } if err := repo_service.DeleteBranch(ctx, ctx.Doer, pr.HeadRepo, headRepo, pr.HeadBranch); err != nil { switch { - case git.IsErrBranchNotExist(err): + case git_model.IsErrBranchNotExist(err): ctx.NotFound(err) case errors.Is(err, repo_service.ErrBranchIsDefault): ctx.Error(http.StatusForbidden, "DefaultBranch", fmt.Errorf("can not delete default branch")) diff --git a/routers/web/repo/branch.go b/routers/web/repo/branch.go index 72fd958e28b35..db1e6d41077f8 100644 --- a/routers/web/repo/branch.go +++ b/routers/web/repo/branch.go @@ -99,7 +99,7 @@ func DeleteBranchPost(ctx *context.Context) { if err := repo_service.DeleteBranch(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, branchName); err != nil { switch { - case git.IsErrBranchNotExist(err): + case git_model.IsErrBranchNotExist(err): log.Debug("DeleteBranch: Can't delete non existing branch '%s'", branchName) ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName)) case errors.Is(err, repo_service.ErrBranchIsDefault): diff --git a/routers/web/repo/editor.go b/routers/web/repo/editor.go index 5fbdeee27e3a5..eeea0269c0e57 100644 --- a/routers/web/repo/editor.go +++ b/routers/web/repo/editor.go @@ -324,7 +324,7 @@ func editFilePost(ctx *context.Context, form forms.EditRepoFileForm, isNewFile b } else if files_service.IsErrRepoFileAlreadyExists(err) { ctx.Data["Err_TreePath"] = true ctx.RenderWithErr(ctx.Tr("repo.editor.file_already_exists", form.TreePath), tplEditFile, &form) - } else if git.IsErrBranchNotExist(err) { + } else if git_model.IsErrBranchNotExist(err) { // For when a user adds/updates a file to a branch that no longer exists if branchErr, ok := err.(git.ErrBranchNotExist); ok { ctx.RenderWithErr(ctx.Tr("repo.editor.branch_does_not_exist", branchErr.Name), tplEditFile, &form) @@ -526,7 +526,7 @@ func DeleteFilePost(ctx *context.Context) { } else { ctx.ServerError("DeleteRepoFile", err) } - } else if git.IsErrBranchNotExist(err) { + } else if git_model.IsErrBranchNotExist(err) { // For when a user deletes a file to a branch that no longer exists if branchErr, ok := err.(git.ErrBranchNotExist); ok { ctx.RenderWithErr(ctx.Tr("repo.editor.branch_does_not_exist", branchErr.Name), tplDeleteFile, &form) @@ -654,7 +654,7 @@ func UploadFilePost(ctx *context.Context) { } if oldBranchName != branchName { - if _, err := ctx.Repo.GitRepo.GetBranch(branchName); err == nil { + if _, err := git_model.GetNonDeletedBranch(ctx, ctx.Repo.Repository.ID, branchName); err == nil { ctx.Data["Err_NewBranchName"] = true ctx.RenderWithErr(ctx.Tr("repo.editor.branch_already_exists", branchName), tplUploadFile, &form) return @@ -852,8 +852,8 @@ func GetUniquePatchBranchName(ctx *context.Context) string { prefix := ctx.Doer.LowerName + "-patch-" for i := 1; i <= 1000; i++ { branchName := fmt.Sprintf("%s%d", prefix, i) - if _, err := ctx.Repo.GitRepo.GetBranch(branchName); err != nil { - if git.IsErrBranchNotExist(err) { + if _, err := git_model.GetNonDeletedBranch(ctx, ctx.Repo.Repository.ID, branchName); err != nil { + if git_model.IsErrBranchNotExist(err) { return branchName } log.Error("GetUniquePatchBranchName: %v", err) diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index 9f3d1c1b7c032..4f42075b38137 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -1511,7 +1511,7 @@ func deleteBranch(ctx *context.Context, pr *issues_model.PullRequest, gitRepo *g if err := repo_service.DeleteBranch(ctx, ctx.Doer, pr.HeadRepo, gitRepo, pr.HeadBranch); err != nil { switch { - case git.IsErrBranchNotExist(err): + case git_model.IsErrBranchNotExist(err): ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName)) case errors.Is(err, repo_service.ErrBranchIsDefault): ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName)) diff --git a/services/convert/pull.go b/services/convert/pull.go index a1ab7eeb8eca6..bcd80f7292e6b 100644 --- a/services/convert/pull.go +++ b/services/convert/pull.go @@ -25,8 +25,6 @@ import ( // Optional - Merger func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *user_model.User) *api.PullRequest { var ( - baseBranch *git.Branch - headBranch *git.Branch baseCommit *git.Commit err error ) @@ -143,14 +141,14 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u } defer gitRepo.Close() - baseBranch, err = gitRepo.GetBranch(pr.BaseBranch) - if err != nil && !git.IsErrBranchNotExist(err) { + baseBranch, err := git_model.GetNonDeletedBranch(ctx, pr.BaseRepoID, pr.BaseBranch) + if err != nil && !git_model.IsErrBranchNotExist(err) { log.Error("GetBranch[%s]: %v", pr.BaseBranch, err) return nil } if err == nil { - baseCommit, err = baseBranch.GetCommit() + baseCommit, err = gitRepo.GetCommit(baseBranch.CommitID) if err != nil && !git.IsErrNotExist(err) { log.Error("GetCommit[%s]: %v", baseBranch.Name, err) return nil @@ -196,8 +194,8 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u } defer headGitRepo.Close() - headBranch, err = headGitRepo.GetBranch(pr.HeadBranch) - if err != nil && !git.IsErrBranchNotExist(err) { + headBranch, err := git_model.GetNonDeletedBranch(ctx, pr.HeadRepoID, pr.HeadBranch) + if err != nil && !git_model.IsErrBranchNotExist(err) { log.Error("GetBranch[%s]: %v", pr.HeadBranch, err) return nil } @@ -208,7 +206,7 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u endCommitID string ) - if git.IsErrBranchNotExist(err) { + if git_model.IsErrBranchNotExist(err) { headCommitID, err := headGitRepo.GetRefCommitID(apiPullRequest.Head.Ref) if err != nil && !git.IsErrNotExist(err) { log.Error("GetCommit[%s]: %v", pr.HeadBranch, err) @@ -219,7 +217,7 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u endCommitID = headCommitID } } else { - commit, err := headBranch.GetCommit() + commit, err := headGitRepo.GetCommit(headBranch.CommitID) if err != nil && !git.IsErrNotExist(err) { log.Error("GetCommit[%s]: %v", headBranch.Name, err) return nil diff --git a/services/repository/branch.go b/services/repository/branch.go index fc476298ca5c4..0b806629d9e1b 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -38,7 +38,7 @@ import ( // CreateNewBranch creates a new repository branch func CreateNewBranch(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, gitRepo *git.Repository, oldBranchName, branchName string) (err error) { - branch, err := git_model.GetBranch(ctx, repo.ID, oldBranchName) + branch, err := git_model.GetNonDeletedBranch(ctx, repo.ID, oldBranchName) if err != nil { return err } diff --git a/services/repository/files/patch.go b/services/repository/files/patch.go index 38c17b4073d13..32f07b80cdc19 100644 --- a/services/repository/files/patch.go +++ b/services/repository/files/patch.go @@ -12,7 +12,6 @@ import ( repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" - "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" @@ -62,27 +61,21 @@ func (opts *ApplyDiffPatchOptions) Validate(ctx context.Context, repo *repo_mode opts.NewBranch = opts.OldBranch } - gitRepo, closer, err := gitrepo.RepositoryFromContextOrOpen(ctx, repo) - if err != nil { - return err - } - defer closer.Close() - // oldBranch must exist for this operation - if _, err := gitRepo.GetBranch(opts.OldBranch); err != nil { + if _, err := git_model.GetNonDeletedBranch(ctx, repo.ID, opts.OldBranch); err != nil { return err } // A NewBranch can be specified for the patch to be applied to. // Check to make sure the branch does not already exist, otherwise we can't proceed. // If we aren't branching to a new branch, make sure user can commit to the given branch if opts.NewBranch != opts.OldBranch { - existingBranch, err := gitRepo.GetBranch(opts.NewBranch) + existingBranch, err := git_model.GetNonDeletedBranch(ctx, repo.ID, opts.NewBranch) if existingBranch != nil { return git_model.ErrBranchAlreadyExists{ BranchName: opts.NewBranch, } } - if err != nil && !git.IsErrBranchNotExist(err) { + if err != nil && !git_model.IsErrBranchNotExist(err) { return err } } else { diff --git a/services/repository/files/update.go b/services/repository/files/update.go index a2763105b0c66..735839bb4b5b9 100644 --- a/services/repository/files/update.go +++ b/services/repository/files/update.go @@ -107,7 +107,7 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use defer closer.Close() // oldBranch must exist for this operation - if _, err := gitRepo.GetBranch(opts.OldBranch); err != nil && !repo.IsEmpty { + if _, err := git_model.GetNonDeletedBranch(ctx, repo.ID, opts.OldBranch); err != nil && !repo.IsEmpty { return nil, err } @@ -145,13 +145,13 @@ func ChangeRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use // Check to make sure the branch does not already exist, otherwise we can't proceed. // If we aren't branching to a new branch, make sure user can commit to the given branch if opts.NewBranch != opts.OldBranch { - existingBranch, err := gitRepo.GetBranch(opts.NewBranch) + existingBranch, err := git_model.GetNonDeletedBranch(ctx, repo.ID, opts.NewBranch) if existingBranch != nil { return nil, git_model.ErrBranchAlreadyExists{ BranchName: opts.NewBranch, } } - if err != nil && !git.IsErrBranchNotExist(err) { + if err != nil && !git_model.IsErrBranchNotExist(err) { return nil, err } } else if err := VerifyBranchProtection(ctx, repo, doer, opts.OldBranch, treePaths); err != nil {