diff options
author | a1012112796 <1012112796@qq.com> | 2021-07-28 17:42:56 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-07-28 17:42:56 +0800 |
commit | 370516883717de0e6e2087c12d368eb1465ee3b0 (patch) | |
tree | 1dd7b0946ac2bbeb3b9f13518cf2df2f4ebca348 /services/pull | |
parent | 5b2e2d29ca50ba4c11a44d4f1de18ffcf215ba1b (diff) | |
download | gitea-370516883717de0e6e2087c12d368eb1465ee3b0.tar.gz gitea-370516883717de0e6e2087c12d368eb1465ee3b0.zip |
Add agit flow support in gitea (#14295)
* feature: add agit flow support
ref: https://git-repo.info/en/2020/03/agit-flow-and-git-repo/
example:
```Bash
git checkout -b test
echo "test" >> README.md
git commit -m "test"
git push origin HEAD:refs/for/master -o topic=test
```
Signed-off-by: a1012112796 <1012112796@qq.com>
* fix lint
* simplify code add fix some nits
* update merge help message
* Apply suggestions from code review. Thanks @jiangxin
* add forced-update message
* fix lint
* splite writePktLine
* add refs/for/<target-branch>/<topic-branch> support also
* Add test code add fix api
* fix lint
* fix test
* skip test if git version < 2.29
* try test with git 2.30.1
* fix permission check bug
* fix some nit
* logic implify and test code update
* fix bug
* apply suggestions from code review
* prepare for merge
Signed-off-by: Andrew Thornton <art27@cantab.net>
* fix permission check bug
- test code update
- apply suggestions from code review @zeripath
Signed-off-by: a1012112796 <1012112796@qq.com>
* fix bug when target branch isn't exist
* prevent some special push and fix some nits
* fix lint
* try splite
* Apply suggestions from code review
- fix permission check
- handle user rename
* fix version negotiation
* remane
* fix template
* handle empty repo
* ui: fix branch link under the title
* fix nits
Co-authored-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: 6543 <6543@obermui.de>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'services/pull')
-rw-r--r-- | services/pull/commit_status.go | 14 | ||||
-rw-r--r-- | services/pull/pull.go | 62 | ||||
-rw-r--r-- | services/pull/temp_repo.go | 12 | ||||
-rw-r--r-- | services/pull/update.go | 9 |
4 files changed, 84 insertions, 13 deletions
diff --git a/services/pull/commit_status.go b/services/pull/commit_status.go index b8fb109440..c5c930ee0d 100644 --- a/services/pull/commit_status.go +++ b/services/pull/commit_status.go @@ -108,13 +108,21 @@ func GetPullRequestCommitStatusState(pr *models.PullRequest) (structs.CommitStat } defer headGitRepo.Close() - if !headGitRepo.IsBranchExist(pr.HeadBranch) { + if pr.Flow == models.PullRequestFlowGithub && !headGitRepo.IsBranchExist(pr.HeadBranch) { + return "", errors.New("Head branch does not exist, can not merge") + } + if pr.Flow == models.PullRequestFlowAGit && !git.IsReferenceExist(headGitRepo.Path, pr.GetGitRefName()) { return "", errors.New("Head branch does not exist, can not merge") } - sha, err := headGitRepo.GetBranchCommitID(pr.HeadBranch) + var sha string + if pr.Flow == models.PullRequestFlowGithub { + sha, err = headGitRepo.GetBranchCommitID(pr.HeadBranch) + } else { + sha, err = headGitRepo.GetRefCommitID(pr.GetGitRefName()) + } if err != nil { - return "", errors.Wrap(err, "GetBranchCommitID") + return "", err } if err := pr.LoadBaseRepo(); err != nil { diff --git a/services/pull/pull.go b/services/pull/pull.go index 6c108c224f..b33f641130 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -49,7 +49,12 @@ func NewPullRequest(repo *models.Repository, pull *models.Issue, labelIDs []int6 pr.Issue = pull pull.PullRequest = pr - if err := PushToBaseRepo(pr); err != nil { + if pr.Flow == models.PullRequestFlowGithub { + err = PushToBaseRepo(pr) + } else { + err = UpdateRef(pr) + } + if err != nil { return err } @@ -145,7 +150,7 @@ func ChangeTargetBranch(pr *models.PullRequest, doer *models.User, targetBranch } // Check if pull request for the new target branch already exists - existingPr, err := models.GetUnmergedPullRequest(pr.HeadRepoID, pr.BaseRepoID, pr.HeadBranch, targetBranch) + existingPr, err := models.GetUnmergedPullRequest(pr.HeadRepoID, pr.BaseRepoID, pr.HeadBranch, targetBranch, models.PullRequestFlowGithub) if existingPr != nil { return models.ErrPullRequestAlreadyExists{ ID: existingPr.ID, @@ -281,8 +286,12 @@ func AddTestPullRequestTask(doer *models.User, repoID int64, branch string, isSy for _, pr := range prs { log.Trace("Updating PR[%d]: composing new test task", pr.ID) - if err := PushToBaseRepo(pr); err != nil { - log.Error("PushToBaseRepo: %v", err) + if pr.Flow == models.PullRequestFlowGithub { + if err := PushToBaseRepo(pr); err != nil { + log.Error("PushToBaseRepo: %v", err) + continue + } + } else { continue } @@ -451,6 +460,22 @@ func pushToBaseRepoHelper(pr *models.PullRequest, prefixHeadBranch string) (err return nil } +// UpdateRef update refs/pull/id/head directly for agit flow pull request +func UpdateRef(pr *models.PullRequest) (err error) { + log.Trace("UpdateRef[%d]: upgate pull request ref in base repo '%s'", pr.ID, pr.GetGitRefName()) + if err := pr.LoadBaseRepo(); err != nil { + log.Error("Unable to load base repository for PR[%d] Error: %v", pr.ID, err) + return err + } + + _, err = git.NewCommand("update-ref", pr.GetGitRefName(), pr.HeadCommitID).RunInDir(pr.BaseRepo.RepoPath()) + if err != nil { + log.Error("Unable to update ref in base repository for PR[%d] Error: %v", pr.ID, err) + } + + return err +} + type errlist []error func (errs errlist) Error() string { @@ -562,7 +587,17 @@ func GetSquashMergeCommitMessages(pr *models.PullRequest) string { } defer gitRepo.Close() - headCommit, err := gitRepo.GetBranchCommit(pr.HeadBranch) + var headCommit *git.Commit + if pr.Flow == models.PullRequestFlowGithub { + headCommit, err = gitRepo.GetBranchCommit(pr.HeadBranch) + } else { + pr.HeadCommitID, err = gitRepo.GetRefCommitID(pr.GetGitRefName()) + if err != nil { + log.Error("Unable to get head commit: %s Error: %v", pr.GetGitRefName(), err) + return "" + } + headCommit, err = gitRepo.GetCommit(pr.HeadCommitID) + } if err != nil { log.Error("Unable to get head commit: %s Error: %v", pr.HeadBranch, err) return "" @@ -781,9 +816,20 @@ func IsHeadEqualWithBranch(pr *models.PullRequest, branchName string) (bool, err } defer headGitRepo.Close() - headCommit, err := headGitRepo.GetBranchCommit(pr.HeadBranch) - if err != nil { - return false, err + var headCommit *git.Commit + if pr.Flow == models.PullRequestFlowGithub { + headCommit, err = headGitRepo.GetBranchCommit(pr.HeadBranch) + if err != nil { + return false, err + } + } else { + pr.HeadCommitID, err = baseGitRepo.GetRefCommitID(pr.GetGitRefName()) + if err != nil { + return false, err + } + if headCommit, err = baseGitRepo.GetCommit(pr.HeadCommitID); err != nil { + return false, err + } } return baseCommit.HasPreviousCommit(headCommit.ID) } diff --git a/services/pull/temp_repo.go b/services/pull/temp_repo.go index 19b488790a..54d09c8158 100644 --- a/services/pull/temp_repo.go +++ b/services/pull/temp_repo.go @@ -140,7 +140,15 @@ func createTemporaryRepo(pr *models.PullRequest) (string, error) { trackingBranch := "tracking" // Fetch head branch - if err := git.NewCommand("fetch", "--no-tags", remoteRepoName, git.BranchPrefix+pr.HeadBranch+":"+trackingBranch).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil { + var headBranch string + if pr.Flow == models.PullRequestFlowGithub { + headBranch = git.BranchPrefix + pr.HeadBranch + } else if len(pr.HeadCommitID) == 40 { // for not created pull request + headBranch = pr.HeadCommitID + } else { + headBranch = pr.GetGitRefName() + } + if err := git.NewCommand("fetch", "--no-tags", remoteRepoName, headBranch+":"+trackingBranch).RunInDirPipeline(tmpBasePath, &outbuf, &errbuf); err != nil { if err := models.RemoveTemporaryPath(tmpBasePath); err != nil { log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err) } @@ -150,7 +158,7 @@ func createTemporaryRepo(pr *models.PullRequest) (string, error) { } } log.Error("Unable to fetch head_repo head branch [%s:%s -> tracking in %s]: %v:\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, tmpBasePath, err, outbuf.String(), errbuf.String()) - return "", fmt.Errorf("Unable to fetch head_repo head branch [%s:%s -> tracking in tmpBasePath]: %v\n%s\n%s", pr.HeadRepo.FullName(), pr.HeadBranch, err, outbuf.String(), errbuf.String()) + return "", fmt.Errorf("Unable to fetch head_repo head branch [%s:%s -> tracking in tmpBasePath]: %v\n%s\n%s", pr.HeadRepo.FullName(), headBranch, err, outbuf.String(), errbuf.String()) } outbuf.Reset() errbuf.Reset() diff --git a/services/pull/update.go b/services/pull/update.go index f35e47cbf8..c2c13845e3 100644 --- a/services/pull/update.go +++ b/services/pull/update.go @@ -22,6 +22,11 @@ func Update(pull *models.PullRequest, doer *models.User, message string) error { BaseBranch: pull.HeadBranch, } + if pull.Flow == models.PullRequestFlowAGit { + // TODO: Not support update agit flow pull request's head branch + return fmt.Errorf("Not support update agit flow pull request's head branch") + } + if err := pr.LoadHeadRepo(); err != nil { log.Error("LoadHeadRepo: %v", err) return fmt.Errorf("LoadHeadRepo: %v", err) @@ -48,6 +53,10 @@ func Update(pull *models.PullRequest, doer *models.User, message string) error { // IsUserAllowedToUpdate check if user is allowed to update PR with given permissions and branch protections func IsUserAllowedToUpdate(pull *models.PullRequest, user *models.User) (bool, error) { + if pull.Flow == models.PullRequestFlowAGit { + return false, nil + } + if user == nil { return false, nil } |