mirror of https://github.com/go-gitea/gitea.git
Use modules/git for git commands (#6775)
This commit is contained in:
parent
5ffc9656c6
commit
0b785481d7
124
models/pull.go
124
models/pull.go
|
@ -363,16 +363,13 @@ func (pr *PullRequest) CheckUserAllowedToMerge(doer *User) (err error) {
|
||||||
|
|
||||||
func getDiffTree(repoPath, baseBranch, headBranch string) (string, error) {
|
func getDiffTree(repoPath, baseBranch, headBranch string) (string, error) {
|
||||||
getDiffTreeFromBranch := func(repoPath, baseBranch, headBranch string) (string, error) {
|
getDiffTreeFromBranch := func(repoPath, baseBranch, headBranch string) (string, error) {
|
||||||
var stdout, stderr string
|
var outbuf, errbuf strings.Builder
|
||||||
// Compute the diff-tree for sparse-checkout
|
// Compute the diff-tree for sparse-checkout
|
||||||
// The branch argument must be enclosed with double-quotes ("") in case it contains slashes (e.g "feature/test")
|
// The branch argument must be enclosed with double-quotes ("") in case it contains slashes (e.g "feature/test")
|
||||||
stdout, stderr, err := process.GetManager().ExecDir(-1, repoPath,
|
if err := git.NewCommand("diff-tree", "--no-commit-id", "--name-only", "-r", "--root", baseBranch, headBranch).RunInDirPipeline(repoPath, &outbuf, &errbuf); err != nil {
|
||||||
fmt.Sprintf("PullRequest.Merge (git diff-tree): %s", repoPath),
|
return "", fmt.Errorf("git diff-tree [%s base:%s head:%s]: %s", repoPath, baseBranch, headBranch, errbuf.String())
|
||||||
"git", "diff-tree", "--no-commit-id", "--name-only", "-r", "--root", baseBranch, headBranch)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("git diff-tree [%s base:%s head:%s]: %s", repoPath, baseBranch, headBranch, stderr)
|
|
||||||
}
|
}
|
||||||
return stdout, nil
|
return outbuf.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
list, err := getDiffTreeFromBranch(repoPath, baseBranch, headBranch)
|
list, err := getDiffTreeFromBranch(repoPath, baseBranch, headBranch)
|
||||||
|
@ -455,17 +452,15 @@ func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository, mergeStyle
|
||||||
if err := addCacheRepo(tmpBasePath, headRepoPath); err != nil {
|
if err := addCacheRepo(tmpBasePath, headRepoPath); err != nil {
|
||||||
return fmt.Errorf("addCacheRepo [%s -> %s]: %v", headRepoPath, tmpBasePath, err)
|
return fmt.Errorf("addCacheRepo [%s -> %s]: %v", headRepoPath, tmpBasePath, err)
|
||||||
}
|
}
|
||||||
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
|
|
||||||
fmt.Sprintf("PullRequest.Merge (git remote add): %s", tmpBasePath),
|
var errbuf strings.Builder
|
||||||
"git", "remote", "add", remoteRepoName, headRepoPath); err != nil {
|
if err := git.NewCommand("remote", "add", remoteRepoName, headRepoPath).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
||||||
return fmt.Errorf("git remote add [%s -> %s]: %s", headRepoPath, tmpBasePath, stderr)
|
return fmt.Errorf("git remote add [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch head branch
|
// Fetch head branch
|
||||||
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
|
if err := git.NewCommand("fetch", remoteRepoName).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
||||||
fmt.Sprintf("PullRequest.Merge (git fetch): %s", tmpBasePath),
|
return fmt.Errorf("git fetch [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
|
||||||
"git", "fetch", remoteRepoName); err != nil {
|
|
||||||
return fmt.Errorf("git fetch [%s -> %s]: %s", headRepoPath, tmpBasePath, stderr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
trackingBranch := path.Join(remoteRepoName, pr.HeadBranch)
|
trackingBranch := path.Join(remoteRepoName, pr.HeadBranch)
|
||||||
|
@ -486,108 +481,75 @@ func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository, mergeStyle
|
||||||
return fmt.Errorf("Writing sparse-checkout file to %s: %v", sparseCheckoutListPath, err)
|
return fmt.Errorf("Writing sparse-checkout file to %s: %v", sparseCheckoutListPath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
|
if err := git.NewCommand("config", "--local", "core.sparseCheckout", "true").RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
||||||
fmt.Sprintf("PullRequest.Merge (git config): %s", tmpBasePath),
|
return fmt.Errorf("git config [core.sparsecheckout -> true]: %v", errbuf.String())
|
||||||
"git", "config", "--local", "core.sparseCheckout", "true"); err != nil {
|
|
||||||
return fmt.Errorf("git config [core.sparsecheckout -> true]: %v", stderr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read base branch index
|
// Read base branch index
|
||||||
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
|
if err := git.NewCommand("read-tree", "HEAD").RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
||||||
fmt.Sprintf("PullRequest.Merge (git read-tree): %s", tmpBasePath),
|
return fmt.Errorf("git read-tree HEAD: %s", errbuf.String())
|
||||||
"git", "read-tree", "HEAD"); err != nil {
|
|
||||||
return fmt.Errorf("git read-tree HEAD: %s", stderr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Merge commits.
|
// Merge commits.
|
||||||
switch mergeStyle {
|
switch mergeStyle {
|
||||||
case MergeStyleMerge:
|
case MergeStyleMerge:
|
||||||
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
|
if err := git.NewCommand("merge", "--no-ff", "--no-commit", trackingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
||||||
fmt.Sprintf("PullRequest.Merge (git merge --no-ff --no-commit): %s", tmpBasePath),
|
return fmt.Errorf("git merge --no-ff --no-commit [%s]: %v - %s", tmpBasePath, err, errbuf.String())
|
||||||
"git", "merge", "--no-ff", "--no-commit", trackingBranch); err != nil {
|
|
||||||
return fmt.Errorf("git merge --no-ff --no-commit [%s]: %v - %s", tmpBasePath, err, stderr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sig := doer.NewGitSig()
|
sig := doer.NewGitSig()
|
||||||
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
|
if err := git.NewCommand("commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), "-m", message).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
||||||
fmt.Sprintf("PullRequest.Merge (git merge): %s", tmpBasePath),
|
return fmt.Errorf("git commit [%s]: %v - %s", tmpBasePath, err, errbuf.String())
|
||||||
"git", "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email),
|
|
||||||
"-m", message); err != nil {
|
|
||||||
return fmt.Errorf("git commit [%s]: %v - %s", tmpBasePath, err, stderr)
|
|
||||||
}
|
}
|
||||||
case MergeStyleRebase:
|
case MergeStyleRebase:
|
||||||
// Checkout head branch
|
// Checkout head branch
|
||||||
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
|
if err := git.NewCommand("checkout", "-b", stagingBranch, trackingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
||||||
fmt.Sprintf("PullRequest.Merge (git checkout): %s", tmpBasePath),
|
return fmt.Errorf("git checkout: %s", errbuf.String())
|
||||||
"git", "checkout", "-b", stagingBranch, trackingBranch); err != nil {
|
|
||||||
return fmt.Errorf("git checkout: %s", stderr)
|
|
||||||
}
|
}
|
||||||
// Rebase before merging
|
// Rebase before merging
|
||||||
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
|
if err := git.NewCommand("rebase", "-q", pr.BaseBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
||||||
fmt.Sprintf("PullRequest.Merge (git rebase): %s", tmpBasePath),
|
return fmt.Errorf("git rebase [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
|
||||||
"git", "rebase", "-q", pr.BaseBranch); err != nil {
|
|
||||||
return fmt.Errorf("git rebase [%s -> %s]: %s", headRepoPath, tmpBasePath, stderr)
|
|
||||||
}
|
}
|
||||||
// Checkout base branch again
|
// Checkout base branch again
|
||||||
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
|
if err := git.NewCommand("checkout", pr.BaseBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
||||||
fmt.Sprintf("PullRequest.Merge (git checkout): %s", tmpBasePath),
|
return fmt.Errorf("git checkout: %s", errbuf.String())
|
||||||
"git", "checkout", pr.BaseBranch); err != nil {
|
|
||||||
return fmt.Errorf("git checkout: %s", stderr)
|
|
||||||
}
|
}
|
||||||
// Merge fast forward
|
// Merge fast forward
|
||||||
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
|
if err := git.NewCommand("merge", "--ff-only", "-q", stagingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
||||||
fmt.Sprintf("PullRequest.Merge (git rebase): %s", tmpBasePath),
|
return fmt.Errorf("git merge --ff-only [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
|
||||||
"git", "merge", "--ff-only", "-q", stagingBranch); err != nil {
|
|
||||||
return fmt.Errorf("git merge --ff-only [%s -> %s]: %s", headRepoPath, tmpBasePath, stderr)
|
|
||||||
}
|
}
|
||||||
case MergeStyleRebaseMerge:
|
case MergeStyleRebaseMerge:
|
||||||
// Checkout head branch
|
// Checkout head branch
|
||||||
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
|
if err := git.NewCommand("checkout", "-b", stagingBranch, trackingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
||||||
fmt.Sprintf("PullRequest.Merge (git checkout): %s", tmpBasePath),
|
return fmt.Errorf("git checkout: %s", errbuf.String())
|
||||||
"git", "checkout", "-b", stagingBranch, trackingBranch); err != nil {
|
|
||||||
return fmt.Errorf("git checkout: %s", stderr)
|
|
||||||
}
|
}
|
||||||
// Rebase before merging
|
// Rebase before merging
|
||||||
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
|
if err := git.NewCommand("rebase", "-q", pr.BaseBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
||||||
fmt.Sprintf("PullRequest.Merge (git rebase): %s", tmpBasePath),
|
return fmt.Errorf("git rebase [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
|
||||||
"git", "rebase", "-q", pr.BaseBranch); err != nil {
|
|
||||||
return fmt.Errorf("git rebase [%s -> %s]: %s", headRepoPath, tmpBasePath, stderr)
|
|
||||||
}
|
}
|
||||||
// Checkout base branch again
|
// Checkout base branch again
|
||||||
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
|
if err := git.NewCommand("checkout", pr.BaseBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
||||||
fmt.Sprintf("PullRequest.Merge (git checkout): %s", tmpBasePath),
|
return fmt.Errorf("git checkout: %s", errbuf.String())
|
||||||
"git", "checkout", pr.BaseBranch); err != nil {
|
|
||||||
return fmt.Errorf("git checkout: %s", stderr)
|
|
||||||
}
|
}
|
||||||
// Prepare merge with commit
|
// Prepare merge with commit
|
||||||
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
|
if err := git.NewCommand("merge", "--no-ff", "--no-commit", "-q", stagingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
||||||
fmt.Sprintf("PullRequest.Merge (git merge): %s", tmpBasePath),
|
return fmt.Errorf("git merge --no-ff [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
|
||||||
"git", "merge", "--no-ff", "--no-commit", "-q", stagingBranch); err != nil {
|
|
||||||
return fmt.Errorf("git merge --no-ff [%s -> %s]: %s", headRepoPath, tmpBasePath, stderr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set custom message and author and create merge commit
|
// Set custom message and author and create merge commit
|
||||||
sig := doer.NewGitSig()
|
sig := doer.NewGitSig()
|
||||||
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
|
if err := git.NewCommand("commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), "-m", message).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
||||||
fmt.Sprintf("PullRequest.Merge (git commit): %s", tmpBasePath),
|
return fmt.Errorf("git commit [%s]: %v - %s", tmpBasePath, err, errbuf.String())
|
||||||
"git", "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email),
|
|
||||||
"-m", message); err != nil {
|
|
||||||
return fmt.Errorf("git commit [%s]: %v - %s", tmpBasePath, err, stderr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case MergeStyleSquash:
|
case MergeStyleSquash:
|
||||||
// Merge with squash
|
// Merge with squash
|
||||||
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
|
if err := git.NewCommand("merge", "-q", "--squash", trackingBranch).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
||||||
fmt.Sprintf("PullRequest.Merge (git squash): %s", tmpBasePath),
|
return fmt.Errorf("git merge --squash [%s -> %s]: %s", headRepoPath, tmpBasePath, errbuf.String())
|
||||||
"git", "merge", "-q", "--squash", trackingBranch); err != nil {
|
|
||||||
return fmt.Errorf("git merge --squash [%s -> %s]: %s", headRepoPath, tmpBasePath, stderr)
|
|
||||||
}
|
}
|
||||||
sig := pr.Issue.Poster.NewGitSig()
|
sig := pr.Issue.Poster.NewGitSig()
|
||||||
if _, stderr, err := process.GetManager().ExecDir(-1, tmpBasePath,
|
if err := git.NewCommand("commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email), "-m", message).RunInDirPipeline(tmpBasePath, nil, &errbuf); err != nil {
|
||||||
fmt.Sprintf("PullRequest.Merge (git squash): %s", tmpBasePath),
|
return fmt.Errorf("git commit [%s]: %v - %s", tmpBasePath, err, errbuf.String())
|
||||||
"git", "commit", fmt.Sprintf("--author='%s <%s>'", sig.Name, sig.Email),
|
|
||||||
"-m", message); err != nil {
|
|
||||||
return fmt.Errorf("git commit [%s]: %v - %s", tmpBasePath, err, stderr)
|
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return ErrInvalidMergeStyle{pr.BaseRepo.ID, mergeStyle}
|
return ErrInvalidMergeStyle{pr.BaseRepo.ID, mergeStyle}
|
||||||
|
@ -596,10 +558,8 @@ func (pr *PullRequest) Merge(doer *User, baseGitRepo *git.Repository, mergeStyle
|
||||||
env := PushingEnvironment(doer, pr.BaseRepo)
|
env := PushingEnvironment(doer, pr.BaseRepo)
|
||||||
|
|
||||||
// Push back to upstream.
|
// Push back to upstream.
|
||||||
if _, stderr, err := process.GetManager().ExecDirEnv(-1, tmpBasePath,
|
if err := git.NewCommand("push", baseGitRepo.Path, pr.BaseBranch).RunInDirTimeoutEnvPipeline(env, -1, tmpBasePath, nil, &errbuf); err != nil {
|
||||||
fmt.Sprintf("PullRequest.Merge (git push): %s", tmpBasePath),
|
return fmt.Errorf("git push: %s", errbuf.String())
|
||||||
env, "git", "push", baseGitRepo.Path, pr.BaseBranch); err != nil {
|
|
||||||
return fmt.Errorf("git push: %s", stderr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pr.MergedCommitID, err = baseGitRepo.GetBranchCommitID(pr.BaseBranch)
|
pr.MergedCommitID, err = baseGitRepo.GetBranchCommitID(pr.BaseBranch)
|
||||||
|
|
Loading…
Reference in New Issue