Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions models/fixtures/branch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 14 additions & 0 deletions models/git/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
18 changes: 11 additions & 7 deletions routers/api/v1/repo/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,17 @@ 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)
}
return
}

c, err := branch.GetCommit()
c, err := ctx.Repo.GitRepo.GetCommit(branch.CommitID)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
return
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/repo/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) ||
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
10 changes: 5 additions & 5 deletions routers/web/repo/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
16 changes: 7 additions & 9 deletions services/convert/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion services/repository/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
13 changes: 3 additions & 10 deletions services/repository/files/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions services/repository/files/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down
Loading