mirror of
https://github.com/go-gitea/gitea.git
synced 2026-05-13 08:55:40 +02:00
Some improvements
This commit is contained in:
parent
c8f5aa920c
commit
f424bab47a
@ -436,15 +436,11 @@ func UpdateBranch(ctx *context.APIContext) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if ctx.Repo.GitRepo == nil {
|
// permission check has been done in api.go
|
||||||
ctx.APIErrorInternal(nil)
|
if err := repo_service.UpdateBranch(ctx, repo, ctx.Repo.GitRepo, ctx.Doer, branchName, opt.NewCommitID, opt.OldCommitID, opt.Force); err != nil {
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := repo_service.UpdateBranch(ctx, repo, ctx.Doer, branchName, opt.NewCommitID, opt.OldCommitID, opt.Force); err != nil {
|
|
||||||
switch {
|
switch {
|
||||||
case git_model.IsErrBranchNotExist(err):
|
case git.IsErrNotExist(err):
|
||||||
ctx.APIError(http.StatusNotFound, "Branch doesn't exist.")
|
ctx.APIError(http.StatusUnprocessableEntity, err)
|
||||||
case repo_service.IsErrBranchCommitDoesNotMatch(err):
|
case repo_service.IsErrBranchCommitDoesNotMatch(err):
|
||||||
ctx.APIError(http.StatusConflict, err)
|
ctx.APIError(http.StatusConflict, err)
|
||||||
case git.IsErrPushOutOfDate(err):
|
case git.IsErrPushOutOfDate(err):
|
||||||
@ -452,10 +448,6 @@ func UpdateBranch(ctx *context.APIContext) {
|
|||||||
case git.IsErrPushRejected(err):
|
case git.IsErrPushRejected(err):
|
||||||
rej := err.(*git.ErrPushRejected)
|
rej := err.(*git.ErrPushRejected)
|
||||||
ctx.APIError(http.StatusForbidden, rej.Message)
|
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:
|
default:
|
||||||
ctx.APIErrorInternal(err)
|
ctx.APIErrorInternal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -484,34 +484,10 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, doer *user_m
|
|||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateBranch moves a branch reference to the provided commit.
|
// 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, doer *user_model.User, branchName, newCommitID, expectedOldCommitID string, force bool) error {
|
func UpdateBranch(ctx context.Context, repo *repo_model.Repository, gitRepo *git.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()
|
|
||||||
|
|
||||||
branchCommit, err := gitRepo.GetBranchCommit(branchName)
|
branchCommit, err := gitRepo.GetBranchCommit(branchName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if git.IsErrNotExist(err) {
|
|
||||||
return git_model.ErrBranchNotExist{RepoID: repo.ID, BranchName: branchName}
|
|
||||||
}
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
currentCommitID := branchCommit.ID.String()
|
currentCommitID := branchCommit.ID.String()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user