mirror of https://github.com/gogits/gogs.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
764 lines
19 KiB
764 lines
19 KiB
11 years ago
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
|
package repo
|
||
|
|
||
|
import (
|
||
9 years ago
|
"container/list"
|
||
9 years ago
|
"path"
|
||
9 years ago
|
"strings"
|
||
|
|
||
9 years ago
|
"github.com/Unknwon/com"
|
||
8 years ago
|
log "gopkg.in/clog.v1"
|
||
9 years ago
|
|
||
9 years ago
|
"github.com/gogits/git-module"
|
||
9 years ago
|
|
||
9 years ago
|
"github.com/gogits/gogs/models"
|
||
8 years ago
|
"github.com/gogits/gogs/models/errors"
|
||
8 years ago
|
"github.com/gogits/gogs/pkg/context"
|
||
|
"github.com/gogits/gogs/pkg/form"
|
||
|
"github.com/gogits/gogs/pkg/setting"
|
||
8 years ago
|
"github.com/gogits/gogs/pkg/tool"
|
||
11 years ago
|
)
|
||
|
|
||
11 years ago
|
const (
|
||
8 years ago
|
FORK = "repo/pulls/fork"
|
||
|
COMPARE_PULL = "repo/pulls/compare"
|
||
|
PULL_COMMITS = "repo/pulls/commits"
|
||
|
PULL_FILES = "repo/pulls/files"
|
||
9 years ago
|
|
||
|
PULL_REQUEST_TEMPLATE_KEY = "PullRequestTemplate"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
PullRequestTemplateCandidates = []string{
|
||
|
"PULL_REQUEST.md",
|
||
|
".gogs/PULL_REQUEST.md",
|
||
|
".github/PULL_REQUEST.md",
|
||
|
}
|
||
11 years ago
|
)
|
||
|
|
||
8 years ago
|
func parseBaseRepository(c *context.Context) *models.Repository {
|
||
|
baseRepo, err := models.GetRepositoryByID(c.ParamsInt64(":repoid"))
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.NotFoundOrServerError("GetRepositoryByID", errors.IsRepoNotExist, err)
|
||
9 years ago
|
return nil
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
if !baseRepo.CanBeForked() || !baseRepo.HasAccess(c.User.ID) {
|
||
|
c.NotFound()
|
||
9 years ago
|
return nil
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Data["repo_name"] = baseRepo.Name
|
||
|
c.Data["description"] = baseRepo.Description
|
||
|
c.Data["IsPrivate"] = baseRepo.IsPrivate
|
||
9 years ago
|
|
||
8 years ago
|
if err = baseRepo.GetOwner(); err != nil {
|
||
8 years ago
|
c.ServerError("GetOwner", err)
|
||
9 years ago
|
return nil
|
||
|
}
|
||
8 years ago
|
c.Data["ForkFrom"] = baseRepo.Owner.Name + "/" + baseRepo.Name
|
||
9 years ago
|
|
||
8 years ago
|
if err := c.User.GetOrganizations(true); err != nil {
|
||
8 years ago
|
c.ServerError("GetOrganizations", err)
|
||
9 years ago
|
return nil
|
||
|
}
|
||
8 years ago
|
c.Data["Orgs"] = c.User.Orgs
|
||
9 years ago
|
|
||
8 years ago
|
return baseRepo
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func Fork(c *context.Context) {
|
||
|
c.Data["Title"] = c.Tr("new_fork")
|
||
9 years ago
|
|
||
8 years ago
|
parseBaseRepository(c)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Data["ContextUser"] = c.User
|
||
8 years ago
|
c.Success(FORK)
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func ForkPost(c *context.Context, f form.CreateRepo) {
|
||
|
c.Data["Title"] = c.Tr("new_fork")
|
||
9 years ago
|
|
||
8 years ago
|
baseRepo := parseBaseRepository(c)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
ctxUser := checkContextUser(c, f.UserID)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["ContextUser"] = ctxUser
|
||
9 years ago
|
|
||
8 years ago
|
if c.HasError() {
|
||
8 years ago
|
c.Success(FORK)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
repo, has := models.HasForkedRepo(ctxUser.ID, baseRepo.ID)
|
||
9 years ago
|
if has {
|
||
8 years ago
|
c.Redirect(repo.Link())
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
9 years ago
|
// Check ownership of organization.
|
||
8 years ago
|
if ctxUser.IsOrganization() && !ctxUser.IsOwnedBy(c.User.ID) {
|
||
|
c.Error(403)
|
||
8 years ago
|
return
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
// Cannot fork to same owner
|
||
8 years ago
|
if ctxUser.ID == baseRepo.OwnerID {
|
||
8 years ago
|
c.RenderWithErr(c.Tr("repo.settings.cannot_fork_to_same_owner"), FORK, &f)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
repo, err := models.ForkRepository(c.User, ctxUser, baseRepo, f.RepoName, f.Description)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.Data["Err_RepoName"] = true
|
||
9 years ago
|
switch {
|
||
|
case models.IsErrRepoAlreadyExist(err):
|
||
8 years ago
|
c.RenderWithErr(c.Tr("repo.settings.new_owner_has_same_repo"), FORK, &f)
|
||
9 years ago
|
case models.IsErrNameReserved(err):
|
||
8 years ago
|
c.RenderWithErr(c.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), FORK, &f)
|
||
9 years ago
|
case models.IsErrNamePatternNotAllowed(err):
|
||
8 years ago
|
c.RenderWithErr(c.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), FORK, &f)
|
||
9 years ago
|
default:
|
||
8 years ago
|
c.ServerError("ForkPost", err)
|
||
9 years ago
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
log.Trace("Repository forked from '%s' -> '%s'", baseRepo.FullName(), repo.FullName())
|
||
8 years ago
|
c.Redirect(repo.Link())
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func checkPullInfo(c *context.Context) *models.Issue {
|
||
|
issue, err := models.GetIssueByIndex(c.Repo.Repository.ID, c.ParamsInt64(":index"))
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.NotFoundOrServerError("GetIssueByIndex", errors.IsIssueNotExist, err)
|
||
9 years ago
|
return nil
|
||
|
}
|
||
8 years ago
|
c.Data["Title"] = issue.Title
|
||
|
c.Data["Issue"] = issue
|
||
9 years ago
|
|
||
9 years ago
|
if !issue.IsPull {
|
||
8 years ago
|
c.Handle(404, "ViewPullCommits", nil)
|
||
9 years ago
|
return nil
|
||
|
}
|
||
|
|
||
8 years ago
|
if c.IsLogged {
|
||
9 years ago
|
// Update issue-user.
|
||
8 years ago
|
if err = issue.ReadBy(c.User.ID); err != nil {
|
||
8 years ago
|
c.ServerError("ReadBy", err)
|
||
9 years ago
|
return nil
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
return issue
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func PrepareMergedViewPullInfo(c *context.Context, issue *models.Issue) {
|
||
8 years ago
|
pull := issue.PullRequest
|
||
8 years ago
|
c.Data["HasMerged"] = true
|
||
|
c.Data["HeadTarget"] = issue.PullRequest.HeadUserName + "/" + pull.HeadBranch
|
||
|
c.Data["BaseTarget"] = c.Repo.Owner.Name + "/" + pull.BaseBranch
|
||
9 years ago
|
|
||
8 years ago
|
var err error
|
||
8 years ago
|
c.Data["NumCommits"], err = c.Repo.GitRepo.CommitsCountBetween(pull.MergeBase, pull.MergedCommitID)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.ServerError("Repo.GitRepo.CommitsCountBetween", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["NumFiles"], err = c.Repo.GitRepo.FilesCountBetween(pull.MergeBase, pull.MergedCommitID)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.ServerError("Repo.GitRepo.FilesCountBetween", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
func PrepareViewPullInfo(c *context.Context, issue *models.Issue) *git.PullRequestInfo {
|
||
|
repo := c.Repo.Repository
|
||
8 years ago
|
pull := issue.PullRequest
|
||
9 years ago
|
|
||
8 years ago
|
c.Data["HeadTarget"] = pull.HeadUserName + "/" + pull.HeadBranch
|
||
|
c.Data["BaseTarget"] = c.Repo.Owner.Name + "/" + pull.BaseBranch
|
||
9 years ago
|
|
||
9 years ago
|
var (
|
||
|
headGitRepo *git.Repository
|
||
|
err error
|
||
|
)
|
||
9 years ago
|
|
||
9 years ago
|
if pull.HeadRepo != nil {
|
||
9 years ago
|
headGitRepo, err = git.OpenRepository(pull.HeadRepo.RepoPath())
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.ServerError("OpenRepository", err)
|
||
9 years ago
|
return nil
|
||
|
}
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
if pull.HeadRepo == nil || !headGitRepo.IsBranchExist(pull.HeadBranch) {
|
||
8 years ago
|
c.Data["IsPullReuqestBroken"] = true
|
||
|
c.Data["HeadTarget"] = "deleted"
|
||
|
c.Data["NumCommits"] = 0
|
||
|
c.Data["NumFiles"] = 0
|
||
9 years ago
|
return nil
|
||
|
}
|
||
|
|
||
9 years ago
|
prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(repo.Owner.Name, repo.Name),
|
||
9 years ago
|
pull.BaseBranch, pull.HeadBranch)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
if strings.Contains(err.Error(), "fatal: Not a valid object name") {
|
||
8 years ago
|
c.Data["IsPullReuqestBroken"] = true
|
||
|
c.Data["BaseTarget"] = "deleted"
|
||
|
c.Data["NumCommits"] = 0
|
||
|
c.Data["NumFiles"] = 0
|
||
8 years ago
|
return nil
|
||
|
}
|
||
|
|
||
8 years ago
|
c.ServerError("GetPullRequestInfo", err)
|
||
9 years ago
|
return nil
|
||
|
}
|
||
8 years ago
|
c.Data["NumCommits"] = prInfo.Commits.Len()
|
||
|
c.Data["NumFiles"] = prInfo.NumFiles
|
||
9 years ago
|
return prInfo
|
||
|
}
|
||
|
|
||
8 years ago
|
func ViewPullCommits(c *context.Context) {
|
||
|
c.Data["PageIsPullList"] = true
|
||
|
c.Data["PageIsPullCommits"] = true
|
||
9 years ago
|
|
||
8 years ago
|
issue := checkPullInfo(c)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
pull := issue.PullRequest
|
||
8 years ago
|
|
||
|
if pull.HeadRepo != nil {
|
||
8 years ago
|
c.Data["Username"] = pull.HeadUserName
|
||
|
c.Data["Reponame"] = pull.HeadRepo.Name
|
||
8 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
var commits *list.List
|
||
|
if pull.HasMerged {
|
||
8 years ago
|
PrepareMergedViewPullInfo(c, issue)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
startCommit, err := c.Repo.GitRepo.GetCommit(pull.MergeBase)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.ServerError("Repo.GitRepo.GetCommit", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
endCommit, err := c.Repo.GitRepo.GetCommit(pull.MergedCommitID)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.ServerError("Repo.GitRepo.GetCommit", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
commits, err = c.Repo.GitRepo.CommitsBetween(endCommit, startCommit)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.ServerError("Repo.GitRepo.CommitsBetween", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
|
} else {
|
||
8 years ago
|
prInfo := PrepareViewPullInfo(c, issue)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
} else if prInfo == nil {
|
||
8 years ago
|
c.Handle(404, "ViewPullCommits", nil)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
commits = prInfo.Commits
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
commits = models.ValidateCommitsWithEmails(commits)
|
||
8 years ago
|
c.Data["Commits"] = commits
|
||
|
c.Data["CommitsCount"] = commits.Len()
|
||
9 years ago
|
|
||
8 years ago
|
c.Success(PULL_COMMITS)
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func ViewPullFiles(c *context.Context) {
|
||
|
c.Data["PageIsPullList"] = true
|
||
|
c.Data["PageIsPullFiles"] = true
|
||
9 years ago
|
|
||
8 years ago
|
issue := checkPullInfo(c)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
pull := issue.PullRequest
|
||
9 years ago
|
|
||
9 years ago
|
var (
|
||
|
diffRepoPath string
|
||
|
startCommitID string
|
||
|
endCommitID string
|
||
|
gitRepo *git.Repository
|
||
|
)
|
||
|
|
||
|
if pull.HasMerged {
|
||
8 years ago
|
PrepareMergedViewPullInfo(c, issue)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
diffRepoPath = c.Repo.GitRepo.Path
|
||
9 years ago
|
startCommitID = pull.MergeBase
|
||
|
endCommitID = pull.MergedCommitID
|
||
8 years ago
|
gitRepo = c.Repo.GitRepo
|
||
9 years ago
|
} else {
|
||
8 years ago
|
prInfo := PrepareViewPullInfo(c, issue)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
} else if prInfo == nil {
|
||
8 years ago
|
c.Handle(404, "ViewPullFiles", nil)
|
||
9 years ago
|
return
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
headRepoPath := models.RepoPath(pull.HeadUserName, pull.HeadRepo.Name)
|
||
9 years ago
|
|
||
9 years ago
|
headGitRepo, err := git.OpenRepository(headRepoPath)
|
||
|
if err != nil {
|
||
8 years ago
|
c.ServerError("OpenRepository", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
9 years ago
|
headCommitID, err := headGitRepo.GetBranchCommitID(pull.HeadBranch)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.ServerError("GetBranchCommitID", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
|
diffRepoPath = headRepoPath
|
||
|
startCommitID = prInfo.MergeBase
|
||
|
endCommitID = headCommitID
|
||
|
gitRepo = headGitRepo
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
diff, err := models.GetDiffRange(diffRepoPath,
|
||
8 years ago
|
startCommitID, endCommitID, setting.Git.MaxGitDiffLines,
|
||
|
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.ServerError("GetDiffRange", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["Diff"] = diff
|
||
|
c.Data["DiffNotAvailable"] = diff.NumFiles() == 0
|
||
9 years ago
|
|
||
9 years ago
|
commit, err := gitRepo.GetCommit(endCommitID)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.ServerError("GetCommit", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
setEditorconfigIfExists(c)
|
||
|
if c.Written() {
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Data["IsSplitStyle"] = c.Query("style") == "split"
|
||
|
c.Data["IsImageFile"] = commit.IsImageFile
|
||
9 years ago
|
|
||
8 years ago
|
// It is possible head repo has been deleted for merged pull requests
|
||
|
if pull.HeadRepo != nil {
|
||
8 years ago
|
c.Data["Username"] = pull.HeadUserName
|
||
|
c.Data["Reponame"] = pull.HeadRepo.Name
|
||
8 years ago
|
|
||
|
headTarget := path.Join(pull.HeadUserName, pull.HeadRepo.Name)
|
||
8 years ago
|
c.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", endCommitID)
|
||
|
c.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", startCommitID)
|
||
|
c.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(headTarget, "raw", endCommitID)
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
c.Data["RequireHighlightJS"] = true
|
||
8 years ago
|
c.Success(PULL_FILES)
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
func MergePullRequest(c *context.Context) {
|
||
|
issue := checkPullInfo(c)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
9 years ago
|
if issue.IsClosed {
|
||
8 years ago
|
c.Handle(404, "MergePullRequest", nil)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
9 years ago
|
pr, err := models.GetPullRequestByIssueID(issue.ID)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.NotFoundOrServerError("GetPullRequestByIssueID", models.IsErrPullRequestNotExist, err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
9 years ago
|
if !pr.CanAutoMerge() || pr.HasMerged {
|
||
8 years ago
|
c.Handle(404, "MergePullRequest", nil)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
9 years ago
|
pr.Issue = issue
|
||
8 years ago
|
pr.Issue.Repo = c.Repo.Repository
|
||
|
if err = pr.Merge(c.User, c.Repo.GitRepo); err != nil {
|
||
8 years ago
|
c.ServerError("Merge", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
|
log.Trace("Pull request merged: %d", pr.ID)
|
||
8 years ago
|
c.Redirect(c.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func ParseCompareInfo(c *context.Context) (*models.User, *models.Repository, *git.Repository, *git.PullRequestInfo, string, string) {
|
||
|
baseRepo := c.Repo.Repository
|
||
9 years ago
|
|
||
9 years ago
|
// Get compared branches information
|
||
|
// format: <base branch>...[<head repo>:]<head branch>
|
||
|
// base<-head: master...head:feature
|
||
|
// same repo: master...feature
|
||
8 years ago
|
infos := strings.Split(c.Params("*"), "...")
|
||
9 years ago
|
if len(infos) != 2 {
|
||
9 years ago
|
log.Trace("ParseCompareInfo[%d]: not enough compared branches information %s", baseRepo.ID, infos)
|
||
8 years ago
|
c.NotFound()
|
||
9 years ago
|
return nil, nil, nil, nil, "", ""
|
||
9 years ago
|
}
|
||
|
|
||
|
baseBranch := infos[0]
|
||
8 years ago
|
c.Data["BaseBranch"] = baseBranch
|
||
9 years ago
|
|
||
9 years ago
|
var (
|
||
|
headUser *models.User
|
||
|
headBranch string
|
||
|
isSameRepo bool
|
||
|
err error
|
||
|
)
|
||
|
|
||
|
// If there is no head repository, it means pull request between same repository.
|
||
9 years ago
|
headInfos := strings.Split(infos[1], ":")
|
||
9 years ago
|
if len(headInfos) == 1 {
|
||
|
isSameRepo = true
|
||
8 years ago
|
headUser = c.Repo.Owner
|
||
9 years ago
|
headBranch = headInfos[0]
|
||
9 years ago
|
|
||
9 years ago
|
} else if len(headInfos) == 2 {
|
||
|
headUser, err = models.GetUserByName(headInfos[0])
|
||
|
if err != nil {
|
||
8 years ago
|
c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
|
||
9 years ago
|
return nil, nil, nil, nil, "", ""
|
||
9 years ago
|
}
|
||
9 years ago
|
headBranch = headInfos[1]
|
||
8 years ago
|
isSameRepo = headUser.ID == baseRepo.OwnerID
|
||
9 years ago
|
|
||
|
} else {
|
||
8 years ago
|
c.NotFound()
|
||
9 years ago
|
return nil, nil, nil, nil, "", ""
|
||
|
}
|
||
8 years ago
|
c.Data["HeadUser"] = headUser
|
||
|
c.Data["HeadBranch"] = headBranch
|
||
|
c.Repo.PullRequest.SameRepo = isSameRepo
|
||
9 years ago
|
|
||
|
// Check if base branch is valid.
|
||
8 years ago
|
if !c.Repo.GitRepo.IsBranchExist(baseBranch) {
|
||
8 years ago
|
c.NotFound()
|
||
9 years ago
|
return nil, nil, nil, nil, "", ""
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
var (
|
||
|
headRepo *models.Repository
|
||
|
headGitRepo *git.Repository
|
||
|
)
|
||
|
|
||
|
// In case user included redundant head user name for comparison in same repository,
|
||
|
// no need to check the fork relation.
|
||
|
if !isSameRepo {
|
||
|
var has bool
|
||
|
headRepo, has = models.HasForkedRepo(headUser.ID, baseRepo.ID)
|
||
|
if !has {
|
||
8 years ago
|
log.Trace("ParseCompareInfo [base_repo_id: %d]: does not have fork or in same repository", baseRepo.ID)
|
||
|
c.NotFound()
|
||
8 years ago
|
return nil, nil, nil, nil, "", ""
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
headGitRepo, err = git.OpenRepository(models.RepoPath(headUser.Name, headRepo.Name))
|
||
|
if err != nil {
|
||
8 years ago
|
c.ServerError("OpenRepository", err)
|
||
9 years ago
|
return nil, nil, nil, nil, "", ""
|
||
|
}
|
||
8 years ago
|
} else {
|
||
8 years ago
|
headRepo = c.Repo.Repository
|
||
|
headGitRepo = c.Repo.GitRepo
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
if !c.User.IsWriterOfRepo(headRepo) && !c.User.IsAdmin {
|
||
8 years ago
|
log.Trace("ParseCompareInfo [base_repo_id: %d]: does not have write access or site admin", baseRepo.ID)
|
||
|
c.NotFound()
|
||
9 years ago
|
return nil, nil, nil, nil, "", ""
|
||
|
}
|
||
|
|
||
9 years ago
|
// Check if head branch is valid.
|
||
|
if !headGitRepo.IsBranchExist(headBranch) {
|
||
8 years ago
|
c.NotFound()
|
||
9 years ago
|
return nil, nil, nil, nil, "", ""
|
||
9 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
headBranches, err := headGitRepo.GetBranches()
|
||
|
if err != nil {
|
||
8 years ago
|
c.ServerError("GetBranches", err)
|
||
9 years ago
|
return nil, nil, nil, nil, "", ""
|
||
9 years ago
|
}
|
||
8 years ago
|
c.Data["HeadBranches"] = headBranches
|
||
9 years ago
|
|
||
9 years ago
|
prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
if git.IsErrNoMergeBase(err) {
|
||
|
c.Data["IsNoMergeBase"] = true
|
||
|
c.Success(COMPARE_PULL)
|
||
|
} else {
|
||
|
c.ServerError("GetPullRequestInfo", err)
|
||
|
}
|
||
9 years ago
|
return nil, nil, nil, nil, "", ""
|
||
|
}
|
||
8 years ago
|
c.Data["BeforeCommitID"] = prInfo.MergeBase
|
||
9 years ago
|
|
||
|
return headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch
|
||
|
}
|
||
|
|
||
|
func PrepareCompareDiff(
|
||
8 years ago
|
c *context.Context,
|
||
9 years ago
|
headUser *models.User,
|
||
|
headRepo *models.Repository,
|
||
|
headGitRepo *git.Repository,
|
||
|
prInfo *git.PullRequestInfo,
|
||
9 years ago
|
baseBranch, headBranch string) bool {
|
||
9 years ago
|
|
||
|
var (
|
||
8 years ago
|
repo = c.Repo.Repository
|
||
9 years ago
|
err error
|
||
|
)
|
||
|
|
||
|
// Get diff information.
|
||
8 years ago
|
c.Data["CommitRepoLink"] = headRepo.Link()
|
||
9 years ago
|
|
||
9 years ago
|
headCommitID, err := headGitRepo.GetBranchCommitID(headBranch)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.ServerError("GetBranchCommitID", err)
|
||
9 years ago
|
return false
|
||
9 years ago
|
}
|
||
8 years ago
|
c.Data["AfterCommitID"] = headCommitID
|
||
9 years ago
|
|
||
9 years ago
|
if headCommitID == prInfo.MergeBase {
|
||
8 years ago
|
c.Data["IsNothingToCompare"] = true
|
||
9 years ago
|
return true
|
||
|
}
|
||
|
|
||
9 years ago
|
diff, err := models.GetDiffRange(models.RepoPath(headUser.Name, headRepo.Name),
|
||
8 years ago
|
prInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines,
|
||
|
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.ServerError("GetDiffRange", err)
|
||
9 years ago
|
return false
|
||
9 years ago
|
}
|
||
8 years ago
|
c.Data["Diff"] = diff
|
||
|
c.Data["DiffNotAvailable"] = diff.NumFiles() == 0
|
||
9 years ago
|
|
||
|
headCommit, err := headGitRepo.GetCommit(headCommitID)
|
||
|
if err != nil {
|
||
8 years ago
|
c.ServerError("GetCommit", err)
|
||
9 years ago
|
return false
|
||
9 years ago
|
}
|
||
|
|
||
|
prInfo.Commits = models.ValidateCommitsWithEmails(prInfo.Commits)
|
||
8 years ago
|
c.Data["Commits"] = prInfo.Commits
|
||
|
c.Data["CommitCount"] = prInfo.Commits.Len()
|
||
|
c.Data["Username"] = headUser.Name
|
||
|
c.Data["Reponame"] = headRepo.Name
|
||
|
c.Data["IsImageFile"] = headCommit.IsImageFile
|
||
9 years ago
|
|
||
|
headTarget := path.Join(headUser.Name, repo.Name)
|
||
8 years ago
|
c.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", headCommitID)
|
||
|
c.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(headTarget, "src", prInfo.MergeBase)
|
||
|
c.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(headTarget, "raw", headCommitID)
|
||
9 years ago
|
return false
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func CompareAndPullRequest(c *context.Context) {
|
||
|
c.Data["Title"] = c.Tr("repo.pulls.compare_changes")
|
||
|
c.Data["PageIsComparePull"] = true
|
||
|
c.Data["IsDiffCompare"] = true
|
||
|
c.Data["RequireHighlightJS"] = true
|
||
|
setTemplateIfExists(c, PULL_REQUEST_TEMPLATE_KEY, PullRequestTemplateCandidates)
|
||
|
renderAttachmentSettings(c)
|
||
9 years ago
|
|
||
8 years ago
|
headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(c)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
pr, err := models.GetUnmergedPullRequest(headRepo.ID, c.Repo.Repository.ID, headBranch, baseBranch)
|
||
9 years ago
|
if err != nil {
|
||
|
if !models.IsErrPullRequestNotExist(err) {
|
||
8 years ago
|
c.ServerError("GetUnmergedPullRequest", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
} else {
|
||
8 years ago
|
c.Data["HasPullRequest"] = true
|
||
|
c.Data["PullRequest"] = pr
|
||
8 years ago
|
c.Success(COMPARE_PULL)
|
||
9 years ago
|
return
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
nothingToCompare := PrepareCompareDiff(c, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
9 years ago
|
if !nothingToCompare {
|
||
|
// Setup information for new form.
|
||
8 years ago
|
RetrieveRepoMetas(c, c.Repo.Repository)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
setEditorconfigIfExists(c)
|
||
|
if c.Written() {
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Data["IsSplitStyle"] = c.Query("style") == "split"
|
||
8 years ago
|
c.Success(COMPARE_PULL)
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func CompareAndPullRequestPost(c *context.Context, f form.NewIssue) {
|
||
|
c.Data["Title"] = c.Tr("repo.pulls.compare_changes")
|
||
|
c.Data["PageIsComparePull"] = true
|
||
|
c.Data["IsDiffCompare"] = true
|
||
|
c.Data["RequireHighlightJS"] = true
|
||
|
renderAttachmentSettings(c)
|
||
9 years ago
|
|
||
|
var (
|
||
8 years ago
|
repo = c.Repo.Repository
|
||
9 years ago
|
attachments []string
|
||
|
)
|
||
|
|
||
8 years ago
|
headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := ParseCompareInfo(c)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
labelIDs, milestoneID, assigneeID := ValidateRepoMetas(c, f)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
|
if setting.AttachmentEnabled {
|
||
8 years ago
|
attachments = f.Files
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
if c.HasError() {
|
||
|
form.Assign(f, c.Data)
|
||
8 years ago
|
|
||
|
// This stage is already stop creating new pull request, so it does not matter if it has
|
||
|
// something to compare or not.
|
||
8 years ago
|
PrepareCompareDiff(c, headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch)
|
||
|
if c.Written() {
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Success(COMPARE_PULL)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
patch, err := headGitRepo.GetPatch(prInfo.MergeBase, headBranch)
|
||
|
if err != nil {
|
||
8 years ago
|
c.ServerError("GetPatch", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
9 years ago
|
pullIssue := &models.Issue{
|
||
9 years ago
|
RepoID: repo.ID,
|
||
9 years ago
|
Index: repo.NextIssueIndex(),
|
||
8 years ago
|
Title: f.Title,
|
||
8 years ago
|
PosterID: c.User.ID,
|
||
|
Poster: c.User,
|
||
9 years ago
|
MilestoneID: milestoneID,
|
||
|
AssigneeID: assigneeID,
|
||
|
IsPull: true,
|
||
8 years ago
|
Content: f.Content,
|
||
9 years ago
|
}
|
||
9 years ago
|
pullRequest := &models.PullRequest{
|
||
9 years ago
|
HeadRepoID: headRepo.ID,
|
||
|
BaseRepoID: repo.ID,
|
||
|
HeadUserName: headUser.Name,
|
||
9 years ago
|
HeadBranch: headBranch,
|
||
9 years ago
|
BaseBranch: baseBranch,
|
||
9 years ago
|
HeadRepo: headRepo,
|
||
|
BaseRepo: repo,
|
||
9 years ago
|
MergeBase: prInfo.MergeBase,
|
||
|
Type: models.PULL_REQUEST_GOGS,
|
||
9 years ago
|
}
|
||
8 years ago
|
// FIXME: check error in the case two people send pull request at almost same time, give nice error prompt
|
||
|
// instead of 500.
|
||
9 years ago
|
if err := models.NewPullRequest(repo, pullIssue, labelIDs, attachments, pullRequest, patch); err != nil {
|
||
8 years ago
|
c.ServerError("NewPullRequest", err)
|
||
9 years ago
|
return
|
||
9 years ago
|
} else if err := pullRequest.PushToBaseRepo(); err != nil {
|
||
8 years ago
|
c.ServerError("PushToBaseRepo", err)
|
||
9 years ago
|
return
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
log.Trace("Pull request created: %d/%d", repo.ID, pullIssue.ID)
|
||
8 years ago
|
c.Redirect(c.Repo.RepoLink + "/pulls/" + com.ToStr(pullIssue.Index))
|
||
11 years ago
|
}
|
||
9 years ago
|
|
||
8 years ago
|
func parseOwnerAndRepo(c *context.Context) (*models.User, *models.Repository) {
|
||
|
owner, err := models.GetUserByName(c.Params(":username"))
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
c.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err)
|
||
8 years ago
|
return nil, nil
|
||
|
}
|
||
|
|
||
8 years ago
|
repo, err := models.GetRepositoryByName(owner.ID, c.Params(":reponame"))
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
c.NotFoundOrServerError("GetRepositoryByName", errors.IsRepoNotExist, err)
|
||
8 years ago
|
return nil, nil
|
||
|
}
|
||
|
|
||
|
return owner, repo
|
||
|
}
|
||
|
|
||
8 years ago
|
func TriggerTask(c *context.Context) {
|
||
|
pusherID := c.QueryInt64("pusher")
|
||
|
branch := c.Query("branch")
|
||
|
secret := c.Query("secret")
|
||
8 years ago
|
if len(branch) == 0 || len(secret) == 0 || pusherID <= 0 {
|
||
8 years ago
|
c.Error(404)
|
||
8 years ago
|
log.Trace("TriggerTask: branch or secret is empty, or pusher ID is not valid")
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
owner, repo := parseOwnerAndRepo(c)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
if secret != tool.MD5(owner.Salt) {
|
||
8 years ago
|
c.Error(404)
|
||
9 years ago
|
log.Trace("TriggerTask [%s/%s]: invalid secret", owner.Name, repo.Name)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
pusher, err := models.GetUserByID(pusherID)
|
||
|
if err != nil {
|
||
8 years ago
|
c.NotFoundOrServerError("GetUserByID", errors.IsUserNotExist, err)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
log.Trace("TriggerTask '%s/%s' by '%s'", repo.Name, branch, pusher.Name)
|
||
9 years ago
|
|
||
|
go models.HookQueue.Add(repo.ID)
|
||
8 years ago
|
go models.AddTestPullRequestTask(pusher, repo.ID, branch, true)
|
||
8 years ago
|
c.Status(202)
|
||
9 years ago
|
}
|