0
0
mirror of https://github.com/go-gitea/gitea.git synced 2026-01-02 13:46:07 +01:00

Some improvements

This commit is contained in:
Lunny Xiao 2025-11-28 11:28:38 -08:00
parent c8f5aa920c
commit f424bab47a
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
2 changed files with 6 additions and 38 deletions

View File

@ -436,15 +436,11 @@ func UpdateBranch(ctx *context.APIContext) {
return
}
if ctx.Repo.GitRepo == nil {
ctx.APIErrorInternal(nil)
return
}
if err := repo_service.UpdateBranch(ctx, repo, ctx.Doer, branchName, opt.NewCommitID, opt.OldCommitID, opt.Force); err != nil {
// permission check has been done in api.go
if err := repo_service.UpdateBranch(ctx, repo, ctx.Repo.GitRepo, ctx.Doer, branchName, opt.NewCommitID, opt.OldCommitID, opt.Force); err != nil {
switch {
case git_model.IsErrBranchNotExist(err):
ctx.APIError(http.StatusNotFound, "Branch doesn't exist.")
case git.IsErrNotExist(err):
ctx.APIError(http.StatusUnprocessableEntity, err)
case repo_service.IsErrBranchCommitDoesNotMatch(err):
ctx.APIError(http.StatusConflict, err)
case git.IsErrPushOutOfDate(err):
@ -452,10 +448,6 @@ func UpdateBranch(ctx *context.APIContext) {
case git.IsErrPushRejected(err):
rej := err.(*git.ErrPushRejected)
ctx.APIError(http.StatusForbidden, rej.Message)
case repo_model.IsErrUserDoesNotHaveAccessToRepo(err):
ctx.APIError(http.StatusForbidden, err)
case git.IsErrNotExist(err):
ctx.APIError(http.StatusUnprocessableEntity, err)
default:
ctx.APIErrorInternal(err)
}

View File

@ -484,34 +484,10 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, doer *user_m
return "", nil
}
// UpdateBranch moves a branch reference to the provided commit.
func UpdateBranch(ctx context.Context, repo *repo_model.Repository, doer *user_model.User, branchName, newCommitID, expectedOldCommitID string, force bool) error {
if err := repo.MustNotBeArchived(); err != nil {
return err
}
perm, err := access_model.GetUserRepoPermission(ctx, repo, doer)
if err != nil {
return err
}
if !perm.CanWrite(unit.TypeCode) {
return repo_model.ErrUserDoesNotHaveAccessToRepo{
UserID: doer.ID,
RepoName: repo.LowerName,
}
}
gitRepo, err := gitrepo.OpenRepository(ctx, repo)
if err != nil {
return fmt.Errorf("OpenRepository: %w", err)
}
defer gitRepo.Close()
// UpdateBranch moves a branch reference to the provided commit. permission check should be done before calling this function.
func UpdateBranch(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, doer *user_model.User, branchName, newCommitID, expectedOldCommitID string, force bool) error {
branchCommit, err := gitRepo.GetBranchCommit(branchName)
if err != nil {
if git.IsErrNotExist(err) {
return git_model.ErrBranchNotExist{RepoID: repo.ID, BranchName: branchName}
}
return err
}
currentCommitID := branchCommit.ID.String()