mirror of
https://github.com/gogs/gogs.git
synced 2025-05-03 14:12:10 +00:00
* WIP * Finish `internal/db/git_diff.go` * FInish internal/db/mirror.go * Finish internal/db/pull.go * Finish internal/db/release.go * Finish internal/db/repo.go * Finish internal/db/repo_branch.go * Finish internal/db/repo_editor.go * Finish internal/db/update.go * Save my work * Add license header * Compile! * Merge master * Finish internal/cmd/hook.go * Finish internal/conf/static.go * Finish internal/context/repo.go * Finish internal/db/action.go * Finish internal/db/git_diff.go * Fix submodule URL inferring * Finish internal/db/mirror.go * Updat to beta.4 * css: update fonts * Finish internal/db/pull.go * Finish internal/db/release.go * Finish internal/db/repo_branch.go * Finish internal/db/wiki.go * gitutil: enhance infer submodule UR * Finish internal/route/api/v1/repo/commits.go * mirror: only collect branch commits after sync * mirror: fix tag support * Finish internal/db/repo.go * Finish internal/db/repo_editor.go * Finish internal/db/update.go * Finish internal/gitutil/pull_request.go * Make it compile * Finish internal/route/repo/setting.go * Finish internal/route/repo/branch.go * Finish internal/route/api/v1/repo/file.go * Finish internal/route/repo/download.go * Finish internal/route/repo/editor.go * Use helper * Finish internal/route/repo/issue.go * Finish internal/route/repo/pull.go * Finish internal/route/repo/release.go * Finish internal/route/repo/repo.go * Finish internal/route/repo/wiki.go * Finish internal/route/repo/commit.go * Finish internal/route/repo/view.go * Finish internal/gitutil/tag.go * go.sum
70 lines
1.9 KiB
Go
70 lines
1.9 KiB
Go
// Copyright 2020 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 gitutil
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/gogs/git-module"
|
|
"github.com/pkg/errors"
|
|
log "unknwon.dev/clog/v2"
|
|
)
|
|
|
|
// PullRequestMeta contains metadata for a pull request.
|
|
type PullRequestMeta struct {
|
|
// The merge base of the pull request.
|
|
MergeBase string
|
|
// The commits that are requested to be merged.
|
|
Commits []*git.Commit
|
|
// The number of files changed.
|
|
NumFiles int
|
|
}
|
|
|
|
func (moduler) PullRequestMeta(headPath, basePath, headBranch, baseBranch string) (*PullRequestMeta, error) {
|
|
tmpRemoteBranch := baseBranch
|
|
|
|
// We need to create a temporary remote when the pull request is sent from a forked repository.
|
|
if headPath != basePath {
|
|
tmpRemote := strconv.FormatInt(time.Now().UnixNano(), 10)
|
|
err := Module.RepoAddRemote(headPath, tmpRemote, basePath, git.AddRemoteOptions{Fetch: true})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("add remote: %v", err)
|
|
}
|
|
defer func() {
|
|
err := Module.RepoRemoveRemote(headPath, tmpRemote)
|
|
if err != nil {
|
|
log.Error("Failed to remove remote %q [path: %s]: %v", tmpRemote, headPath, err)
|
|
return
|
|
}
|
|
}()
|
|
|
|
tmpRemoteBranch = "remotes/" + tmpRemote + "/" + baseBranch
|
|
}
|
|
|
|
mergeBase, err := Module.RepoMergeBase(headPath, tmpRemoteBranch, headBranch)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "get merge base")
|
|
}
|
|
|
|
commits, err := Module.RepoLog(headPath, mergeBase+"..."+headBranch)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "get commits")
|
|
}
|
|
|
|
// Count number of changed files
|
|
names, err := Module.RepoDiffNameOnly(headPath, tmpRemoteBranch, headBranch, git.DiffNameOnlyOptions{NeedsMergeBase: true})
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "get changed files")
|
|
}
|
|
|
|
return &PullRequestMeta{
|
|
MergeBase: mergeBase,
|
|
Commits: commits,
|
|
NumFiles: len(names),
|
|
}, nil
|
|
}
|