Skip to content

Commit 8d2846a

Browse files
authored
Merge branch 'main' into fix-editor-js
2 parents d38f18f + e45ffc5 commit 8d2846a

File tree

10 files changed

+162
-250
lines changed

10 files changed

+162
-250
lines changed

contrib/fixtures/fixture_generation.go

Lines changed: 0 additions & 80 deletions
This file was deleted.

models/fixture_generation.go

Lines changed: 0 additions & 50 deletions
This file was deleted.

models/fixture_test.go

Lines changed: 0 additions & 37 deletions
This file was deleted.

modules/git/repo_commit_gogit.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,16 @@ import (
1414
"github.com/go-git/go-git/v5/plumbing/object"
1515
)
1616

17-
// GetRefCommitID returns the last commit ID string of given reference (branch or tag).
17+
// GetRefCommitID returns the last commit ID string of given reference.
1818
func (repo *Repository) GetRefCommitID(name string) (string, error) {
19-
ref, err := repo.gogitRepo.Reference(plumbing.ReferenceName(name), true)
19+
if plumbing.IsHash(name) {
20+
return name, nil
21+
}
22+
refName := plumbing.ReferenceName(name)
23+
if err := refName.Validate(); err != nil {
24+
return "", err
25+
}
26+
ref, err := repo.gogitRepo.Reference(refName, true)
2027
if err != nil {
2128
if err == plumbing.ErrReferenceNotFound {
2229
return "", ErrNotExist{

modules/git/repo_commit_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,28 @@ func TestRepository_CommitsBetweenIDs(t *testing.T) {
101101
assert.Len(t, commits, c.ExpectedCommits, "case %d", i)
102102
}
103103
}
104+
105+
func TestGetRefCommitID(t *testing.T) {
106+
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
107+
bareRepo1, err := openRepositoryWithDefaultContext(bareRepo1Path)
108+
assert.NoError(t, err)
109+
defer bareRepo1.Close()
110+
111+
// these test case are specific to the repo1_bare test repo
112+
testCases := []struct {
113+
Ref string
114+
ExpectedCommitID string
115+
}{
116+
{RefNameFromBranch("master").String(), "ce064814f4a0d337b333e646ece456cd39fab612"},
117+
{RefNameFromBranch("branch1").String(), "2839944139e0de9737a044f78b0e4b40d989a9e3"},
118+
{RefNameFromTag("test").String(), "3ad28a9149a2864384548f3d17ed7f38014c9e8a"},
119+
{"ce064814f4a0d337b333e646ece456cd39fab612", "ce064814f4a0d337b333e646ece456cd39fab612"},
120+
}
121+
122+
for _, testCase := range testCases {
123+
commitID, err := bareRepo1.GetRefCommitID(testCase.Ref)
124+
if assert.NoError(t, err) {
125+
assert.Equal(t, testCase.ExpectedCommitID, commitID)
126+
}
127+
}
128+
}

routers/api/v1/repo/branch.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,6 @@ func DeleteBranch(ctx *context.APIContext) {
150150
}
151151
}
152152

153-
if ctx.Repo.Repository.IsMirror {
154-
ctx.Error(http.StatusForbidden, "IsMirrored", fmt.Errorf("can not delete branch of an mirror repository"))
155-
return
156-
}
157-
158153
if err := repo_service.DeleteBranch(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, branchName); err != nil {
159154
switch {
160155
case git.IsErrBranchNotExist(err):

routers/api/v1/repo/pull.go

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,49 +1057,54 @@ func MergePullRequest(ctx *context.APIContext) {
10571057
}
10581058
log.Trace("Pull request merged: %d", pr.ID)
10591059

1060-
if form.DeleteBranchAfterMerge {
1061-
// Don't cleanup when there are other PR's that use this branch as head branch.
1062-
exist, err := issues_model.HasUnmergedPullRequestsByHeadInfo(ctx, pr.HeadRepoID, pr.HeadBranch)
1063-
if err != nil {
1064-
ctx.ServerError("HasUnmergedPullRequestsByHeadInfo", err)
1065-
return
1066-
}
1067-
if exist {
1068-
ctx.Status(http.StatusOK)
1069-
return
1070-
}
1071-
1072-
var headRepo *git.Repository
1073-
if ctx.Repo != nil && ctx.Repo.Repository != nil && ctx.Repo.Repository.ID == pr.HeadRepoID && ctx.Repo.GitRepo != nil {
1074-
headRepo = ctx.Repo.GitRepo
1075-
} else {
1076-
headRepo, err = gitrepo.OpenRepository(ctx, pr.HeadRepo)
1060+
// for agit flow, we should not delete the agit reference after merge
1061+
if form.DeleteBranchAfterMerge && pr.Flow == issues_model.PullRequestFlowGithub {
1062+
// check permission even it has been checked in repo_service.DeleteBranch so that we don't need to
1063+
// do RetargetChildrenOnMerge
1064+
if err := repo_service.CanDeleteBranch(ctx, pr.HeadRepo, pr.HeadBranch, ctx.Doer); err == nil {
1065+
// Don't cleanup when there are other PR's that use this branch as head branch.
1066+
exist, err := issues_model.HasUnmergedPullRequestsByHeadInfo(ctx, pr.HeadRepoID, pr.HeadBranch)
10771067
if err != nil {
1078-
ctx.ServerError(fmt.Sprintf("OpenRepository[%s]", pr.HeadRepo.FullName()), err)
1068+
ctx.ServerError("HasUnmergedPullRequestsByHeadInfo", err)
10791069
return
10801070
}
1081-
defer headRepo.Close()
1082-
}
1083-
if err := pull_service.RetargetChildrenOnMerge(ctx, ctx.Doer, pr); err != nil {
1084-
ctx.Error(http.StatusInternalServerError, "RetargetChildrenOnMerge", err)
1085-
return
1086-
}
1087-
if err := repo_service.DeleteBranch(ctx, ctx.Doer, pr.HeadRepo, headRepo, pr.HeadBranch); err != nil {
1088-
switch {
1089-
case git.IsErrBranchNotExist(err):
1090-
ctx.NotFound(err)
1091-
case errors.Is(err, repo_service.ErrBranchIsDefault):
1092-
ctx.Error(http.StatusForbidden, "DefaultBranch", fmt.Errorf("can not delete default branch"))
1093-
case errors.Is(err, git_model.ErrBranchIsProtected):
1094-
ctx.Error(http.StatusForbidden, "IsProtectedBranch", fmt.Errorf("branch protected"))
1095-
default:
1096-
ctx.Error(http.StatusInternalServerError, "DeleteBranch", err)
1071+
if exist {
1072+
ctx.Status(http.StatusOK)
1073+
return
1074+
}
1075+
1076+
var headRepo *git.Repository
1077+
if ctx.Repo != nil && ctx.Repo.Repository != nil && ctx.Repo.Repository.ID == pr.HeadRepoID && ctx.Repo.GitRepo != nil {
1078+
headRepo = ctx.Repo.GitRepo
1079+
} else {
1080+
headRepo, err = gitrepo.OpenRepository(ctx, pr.HeadRepo)
1081+
if err != nil {
1082+
ctx.ServerError(fmt.Sprintf("OpenRepository[%s]", pr.HeadRepo.FullName()), err)
1083+
return
1084+
}
1085+
defer headRepo.Close()
1086+
}
1087+
if err := pull_service.RetargetChildrenOnMerge(ctx, ctx.Doer, pr); err != nil {
1088+
ctx.Error(http.StatusInternalServerError, "RetargetChildrenOnMerge", err)
1089+
return
1090+
}
1091+
if err := repo_service.DeleteBranch(ctx, ctx.Doer, pr.HeadRepo, headRepo, pr.HeadBranch); err != nil {
1092+
switch {
1093+
case git.IsErrBranchNotExist(err):
1094+
ctx.NotFound(err)
1095+
case errors.Is(err, repo_service.ErrBranchIsDefault):
1096+
ctx.Error(http.StatusForbidden, "DefaultBranch", fmt.Errorf("can not delete default branch"))
1097+
case errors.Is(err, git_model.ErrBranchIsProtected):
1098+
ctx.Error(http.StatusForbidden, "IsProtectedBranch", fmt.Errorf("branch protected"))
1099+
default:
1100+
ctx.Error(http.StatusInternalServerError, "DeleteBranch", err)
1101+
}
1102+
return
1103+
}
1104+
if err := issues_model.AddDeletePRBranchComment(ctx, ctx.Doer, pr.BaseRepo, pr.Issue.ID, pr.HeadBranch); err != nil {
1105+
// Do not fail here as branch has already been deleted
1106+
log.Error("DeleteBranch: %v", err)
10971107
}
1098-
return
1099-
}
1100-
if err := issues_model.AddDeletePRBranchComment(ctx, ctx.Doer, pr.BaseRepo, pr.Issue.ID, pr.HeadBranch); err != nil {
1101-
// Do not fail here as branch has already been deleted
1102-
log.Error("DeleteBranch: %v", err)
11031108
}
11041109
}
11051110

0 commit comments

Comments
 (0)