summaryrefslogtreecommitdiffstats
path: root/modules/git
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2021-09-27 13:19:34 +0100
committerGitHub <noreply@github.com>2021-09-27 13:19:34 +0100
commit920608e592549fa18991758eb2aa1761cfb8c6ba (patch)
tree2cd5fd512782719d5936f724e680fdbc7a4fb45e /modules/git
parent123f0aea00687d98893036e252efa7ff672b3ad5 (diff)
downloadgitea-920608e592549fa18991758eb2aa1761cfb8c6ba.tar.gz
gitea-920608e592549fa18991758eb2aa1761cfb8c6ba.zip
Support direct comparison (git diff a..b) as well merge comparison (a...b) (#16635)
This PR changes the compare page to make the "..." in the between branches a clickable link. This changes the comparison type from "..." to "..". Similarly it makes the initial compare icon clickable to switch the head and base branches. Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'modules/git')
-rw-r--r--modules/git/repo_compare.go22
1 files changed, 17 insertions, 5 deletions
diff --git a/modules/git/repo_compare.go b/modules/git/repo_compare.go
index 5d1208aab1..866b3d9133 100644
--- a/modules/git/repo_compare.go
+++ b/modules/git/repo_compare.go
@@ -46,7 +46,7 @@ func (repo *Repository) GetMergeBase(tmpRemote string, base, head string) (strin
}
// GetCompareInfo generates and returns compare information between base and head branches of repositories.
-func (repo *Repository) GetCompareInfo(basePath, baseBranch, headBranch string) (_ *CompareInfo, err error) {
+func (repo *Repository) GetCompareInfo(basePath, baseBranch, headBranch string, directComparison bool) (_ *CompareInfo, err error) {
var (
remoteBranch string
tmpRemote string
@@ -79,8 +79,15 @@ func (repo *Repository) GetCompareInfo(basePath, baseBranch, headBranch string)
if err != nil {
compareInfo.BaseCommitID = remoteBranch
}
+ separator := "..."
+ baseCommitID := compareInfo.MergeBase
+ if directComparison {
+ separator = ".."
+ baseCommitID = compareInfo.BaseCommitID
+ }
+
// We have a common base - therefore we know that ... should work
- logs, err := NewCommand("log", compareInfo.MergeBase+"..."+headBranch, prettyLogFormat).RunInDirBytes(repo.Path)
+ logs, err := NewCommand("log", baseCommitID+separator+headBranch, prettyLogFormat).RunInDirBytes(repo.Path)
if err != nil {
return nil, err
}
@@ -100,7 +107,7 @@ func (repo *Repository) GetCompareInfo(basePath, baseBranch, headBranch string)
// Count number of changed files.
// This probably should be removed as we need to use shortstat elsewhere
// Now there is git diff --shortstat but this appears to be slower than simply iterating with --nameonly
- compareInfo.NumFiles, err = repo.GetDiffNumChangedFiles(remoteBranch, headBranch)
+ compareInfo.NumFiles, err = repo.GetDiffNumChangedFiles(remoteBranch, headBranch, directComparison)
if err != nil {
return nil, err
}
@@ -120,12 +127,17 @@ func (l *lineCountWriter) Write(p []byte) (n int, err error) {
// GetDiffNumChangedFiles counts the number of changed files
// This is substantially quicker than shortstat but...
-func (repo *Repository) GetDiffNumChangedFiles(base, head string) (int, error) {
+func (repo *Repository) GetDiffNumChangedFiles(base, head string, directComparison bool) (int, error) {
// Now there is git diff --shortstat but this appears to be slower than simply iterating with --nameonly
w := &lineCountWriter{}
stderr := new(bytes.Buffer)
- if err := NewCommand("diff", "-z", "--name-only", base+"..."+head).
+ separator := "..."
+ if directComparison {
+ separator = ".."
+ }
+
+ if err := NewCommand("diff", "-z", "--name-only", base+separator+head).
RunInDirPipeline(repo.Path, w, stderr); err != nil {
if strings.Contains(stderr.String(), "no merge base") {
// git >= 2.28 now returns an error if base and head have become unrelated.