Skip to content

Commit 311ce2d

Browse files
saithotechknowlogick
authored andcommitted
Compare branches, commits and tags with each other (#6991)
* Supports tags when comparing commits or branches Signed-off-by: Mario Lubenka <[email protected]> * Hide headline when only comparing and don't load unused data Signed-off-by: Mario Lubenka <[email protected]> * Merges compare logics to allow comparing branches, commits and tags with eachother Signed-off-by: Mario Lubenka <[email protected]> * Display branch or tag instead of commit when used for comparing Signed-off-by: Mario Lubenka <[email protected]> * Show pull request form after click on button Signed-off-by: Mario Lubenka <[email protected]> * Transfers relevant pull.go changes from master to compare.go Signed-off-by: Mario Lubenka <[email protected]> * Fixes error when comparing forks against a commit or tag Signed-off-by: Mario Lubenka <[email protected]> * Removes console.log from JavaScript file Signed-off-by: Mario Lubenka <[email protected]> * Show icon next to commit reference when comparing branch or tag Signed-off-by: Mario Lubenka <[email protected]> * Updates css file Signed-off-by: Mario Lubenka <[email protected]> * Fixes import order * Renames template variable * Update routers/repo/compare.go Co-Authored-By: zeripath <[email protected]> * Update from master Signed-off-by: Mario Lubenka <[email protected]> * Allow short-shas in compare * Renames prInfo to compareInfo Signed-off-by: Mario Lubenka <[email protected]> * Check PR permissions only if compare is pull request Signed-off-by: Mario Lubenka <[email protected]> * Adjusts comment Signed-off-by: Mario Lubenka <[email protected]> * Use compareInfo instead of prInfo
1 parent bd55f6f commit 311ce2d

File tree

17 files changed

+584
-527
lines changed

17 files changed

+584
-527
lines changed

models/pull.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,8 +1144,7 @@ func (pr *PullRequest) UpdatePatch() (err error) {
11441144
defer func() {
11451145
headGitRepo.RemoveRemote(tmpRemote)
11461146
}()
1147-
remoteBranch := "remotes/" + tmpRemote + "/" + pr.BaseBranch
1148-
pr.MergeBase, err = headGitRepo.GetMergeBase(remoteBranch, pr.HeadBranch)
1147+
pr.MergeBase, err = headGitRepo.GetMergeBase(tmpRemote, pr.BaseBranch, pr.HeadBranch)
11491148
if err != nil {
11501149
return fmt.Errorf("GetMergeBase: %v", err)
11511150
} else if err = pr.Update(); err != nil {

modules/git/repo_commit.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ func (repo *Repository) GetRefCommitID(name string) (string, error) {
2727
return ref.Hash().String(), nil
2828
}
2929

30+
// IsCommitExist returns true if given commit exists in current repository.
31+
func (repo *Repository) IsCommitExist(name string) bool {
32+
hash := plumbing.NewHash(name)
33+
_, err := repo.gogitRepo.CommitObject(hash)
34+
if err != nil {
35+
return false
36+
}
37+
return true
38+
}
39+
3040
// GetBranchCommitID returns last commit ID string of given branch.
3141
func (repo *Repository) GetBranchCommitID(name string) (string, error) {
3242
return repo.GetRefCommitID(BranchPrefix + name)

modules/git/repo_pull.go renamed to modules/git/repo_compare.go

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2015 The Gogs Authors. All rights reserved.
2+
// Copyright 2019 The Gitea Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
45

@@ -14,55 +15,66 @@ import (
1415
"time"
1516
)
1617

17-
// PullRequestInfo represents needed information for a pull request.
18-
type PullRequestInfo struct {
18+
// CompareInfo represents needed information for comparing references.
19+
type CompareInfo struct {
1920
MergeBase string
2021
Commits *list.List
2122
NumFiles int
2223
}
2324

2425
// GetMergeBase checks and returns merge base of two branches.
25-
func (repo *Repository) GetMergeBase(base, head string) (string, error) {
26+
func (repo *Repository) GetMergeBase(tmpRemote string, base, head string) (string, error) {
27+
if tmpRemote == "" {
28+
tmpRemote = "origin"
29+
}
30+
31+
if tmpRemote != "origin" {
32+
tmpBaseName := "refs/remotes/" + tmpRemote + "/tmp_" + base
33+
// Fetch commit into a temporary branch in order to be able to handle commits and tags
34+
_, err := NewCommand("fetch", tmpRemote, base+":"+tmpBaseName).RunInDir(repo.Path)
35+
if err == nil {
36+
base = tmpBaseName
37+
}
38+
}
39+
2640
stdout, err := NewCommand("merge-base", base, head).RunInDir(repo.Path)
2741
return strings.TrimSpace(stdout), err
2842
}
2943

30-
// GetPullRequestInfo generates and returns pull request information
31-
// between base and head branches of repositories.
32-
func (repo *Repository) GetPullRequestInfo(basePath, baseBranch, headBranch string) (_ *PullRequestInfo, err error) {
33-
var remoteBranch string
44+
// GetCompareInfo generates and returns compare information between base and head branches of repositories.
45+
func (repo *Repository) GetCompareInfo(basePath, baseBranch, headBranch string) (_ *CompareInfo, err error) {
46+
var (
47+
remoteBranch string
48+
tmpRemote string
49+
)
3450

3551
// We don't need a temporary remote for same repository.
3652
if repo.Path != basePath {
3753
// Add a temporary remote
38-
tmpRemote := strconv.FormatInt(time.Now().UnixNano(), 10)
54+
tmpRemote = strconv.FormatInt(time.Now().UnixNano(), 10)
3955
if err = repo.AddRemote(tmpRemote, basePath, true); err != nil {
4056
return nil, fmt.Errorf("AddRemote: %v", err)
4157
}
4258
defer repo.RemoveRemote(tmpRemote)
43-
44-
remoteBranch = "remotes/" + tmpRemote + "/" + baseBranch
45-
} else {
46-
remoteBranch = baseBranch
4759
}
4860

49-
prInfo := new(PullRequestInfo)
50-
prInfo.MergeBase, err = repo.GetMergeBase(remoteBranch, headBranch)
61+
compareInfo := new(CompareInfo)
62+
compareInfo.MergeBase, err = repo.GetMergeBase(tmpRemote, baseBranch, headBranch)
5163
if err == nil {
5264
// We have a common base
53-
logs, err := NewCommand("log", prInfo.MergeBase+"..."+headBranch, prettyLogFormat).RunInDirBytes(repo.Path)
65+
logs, err := NewCommand("log", compareInfo.MergeBase+"..."+headBranch, prettyLogFormat).RunInDirBytes(repo.Path)
5466
if err != nil {
5567
return nil, err
5668
}
57-
prInfo.Commits, err = repo.parsePrettyFormatLogToList(logs)
69+
compareInfo.Commits, err = repo.parsePrettyFormatLogToList(logs)
5870
if err != nil {
5971
return nil, fmt.Errorf("parsePrettyFormatLogToList: %v", err)
6072
}
6173
} else {
62-
prInfo.Commits = list.New()
63-
prInfo.MergeBase, err = GetFullCommitID(repo.Path, remoteBranch)
74+
compareInfo.Commits = list.New()
75+
compareInfo.MergeBase, err = GetFullCommitID(repo.Path, remoteBranch)
6476
if err != nil {
65-
prInfo.MergeBase = remoteBranch
77+
compareInfo.MergeBase = remoteBranch
6678
}
6779
}
6880

@@ -71,9 +83,9 @@ func (repo *Repository) GetPullRequestInfo(basePath, baseBranch, headBranch stri
7183
if err != nil {
7284
return nil, err
7385
}
74-
prInfo.NumFiles = len(strings.Split(stdout, "\n")) - 1
86+
compareInfo.NumFiles = len(strings.Split(stdout, "\n")) - 1
7587

76-
return prInfo, nil
88+
return compareInfo, nil
7789
}
7890

7991
// GetPatch generates and returns patch data between given revisions.
File renamed without changes.

public/css/index.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,7 @@ footer .ui.left,footer .ui.right{line-height:40px}
591591
.repository .milestone.list>.item .content{padding-top:10px}
592592
.repository.new.milestone textarea{height:200px}
593593
.repository.new.milestone #deadline{width:150px}
594+
.repository.compare.pull .show-form-container{text-align:left}
594595
.repository.compare.pull .choose.branch .octicon{padding-right:10px}
595596
.repository.compare.pull .comment.form .content:after,.repository.compare.pull .comment.form .content:before{right:100%;top:20px;border:solid transparent;content:" ";height:0;width:0;position:absolute;pointer-events:none}
596597
.repository.compare.pull .comment.form .content:before{border-right-color:#d3d3d4;border-width:9px;margin-top:-9px}

public/js/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -959,8 +959,15 @@ function initRepository() {
959959
});
960960

961961
// Pull request
962-
if ($('.repository.compare.pull').length > 0) {
962+
var $repoComparePull = $('.repository.compare.pull');
963+
if ($repoComparePull.length > 0) {
963964
initFilterSearchDropdown('.choose.branch .dropdown');
965+
// show pull request form
966+
$repoComparePull.find('button.show-form').on('click', function(e) {
967+
e.preventDefault();
968+
$repoComparePull.find('.pullrequest-form').show();
969+
$(this).parent().hide();
970+
});
964971
}
965972

966973
// Branches

public/less/_repository.less

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,9 @@
11091109
}
11101110

11111111
&.compare.pull {
1112+
.show-form-container {
1113+
text-align: left;
1114+
}
11121115
.choose.branch {
11131116
.octicon {
11141117
padding-right: 10px;

routers/api/v1/repo/pull.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func CreatePullRequest(ctx *context.APIContext, form api.CreatePullRequestOption
188188
)
189189

190190
// Get repo/branch information
191-
headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch := parseCompareInfo(ctx, form)
191+
headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch := parseCompareInfo(ctx, form)
192192
if ctx.Written() {
193193
return
194194
}
@@ -240,7 +240,7 @@ func CreatePullRequest(ctx *context.APIContext, form api.CreatePullRequestOption
240240
milestoneID = milestone.ID
241241
}
242242

243-
patch, err := headGitRepo.GetPatch(prInfo.MergeBase, headBranch)
243+
patch, err := headGitRepo.GetPatch(compareInfo.MergeBase, headBranch)
244244
if err != nil {
245245
ctx.Error(500, "GetPatch", err)
246246
return
@@ -277,7 +277,7 @@ func CreatePullRequest(ctx *context.APIContext, form api.CreatePullRequestOption
277277
BaseBranch: baseBranch,
278278
HeadRepo: headRepo,
279279
BaseRepo: repo,
280-
MergeBase: prInfo.MergeBase,
280+
MergeBase: compareInfo.MergeBase,
281281
Type: models.PullRequestGitea,
282282
}
283283

@@ -600,7 +600,7 @@ func MergePullRequest(ctx *context.APIContext, form auth.MergePullRequestForm) {
600600
ctx.Status(200)
601601
}
602602

603-
func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption) (*models.User, *models.Repository, *git.Repository, *git.PullRequestInfo, string, string) {
603+
func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption) (*models.User, *models.Repository, *git.Repository, *git.CompareInfo, string, string) {
604604
baseRepo := ctx.Repo.Repository
605605

606606
// Get compared branches information
@@ -712,11 +712,11 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption)
712712
return nil, nil, nil, nil, "", ""
713713
}
714714

715-
prInfo, err := headGitRepo.GetPullRequestInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch)
715+
compareInfo, err := headGitRepo.GetCompareInfo(models.RepoPath(baseRepo.Owner.Name, baseRepo.Name), baseBranch, headBranch)
716716
if err != nil {
717-
ctx.Error(500, "GetPullRequestInfo", err)
717+
ctx.Error(500, "GetCompareInfo", err)
718718
return nil, nil, nil, nil, "", ""
719719
}
720720

721-
return headUser, headRepo, headGitRepo, prInfo, baseBranch, headBranch
721+
return headUser, headRepo, headGitRepo, compareInfo, baseBranch, headBranch
722722
}

routers/repo/commit.go

Lines changed: 4 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import (
1919
)
2020

2121
const (
22-
tplCommits base.TplName = "repo/commits"
23-
tplGraph base.TplName = "repo/graph"
24-
tplDiff base.TplName = "repo/diff/page"
22+
tplCommits base.TplName = "repo/commits"
23+
tplGraph base.TplName = "repo/graph"
24+
tplCommitPage base.TplName = "repo/commit_page"
2525
)
2626

2727
// RefCommits render commits page
@@ -261,7 +261,7 @@ func Diff(ctx *context.Context) {
261261
}
262262
ctx.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "raw", "commit", commitID)
263263
ctx.Data["BranchName"], err = commit.GetBranchName()
264-
ctx.HTML(200, tplDiff)
264+
ctx.HTML(200, tplCommitPage)
265265
}
266266

267267
// RawDiff dumps diff results of repository in given commit ID to io.Writer
@@ -276,54 +276,3 @@ func RawDiff(ctx *context.Context) {
276276
return
277277
}
278278
}
279-
280-
// CompareDiff show different from one commit to another commit
281-
func CompareDiff(ctx *context.Context) {
282-
ctx.Data["IsRepoToolbarCommits"] = true
283-
ctx.Data["IsDiffCompare"] = true
284-
userName := ctx.Repo.Owner.Name
285-
repoName := ctx.Repo.Repository.Name
286-
beforeCommitID := ctx.Params(":before")
287-
afterCommitID := ctx.Params(":after")
288-
289-
commit, err := ctx.Repo.GitRepo.GetCommit(afterCommitID)
290-
if err != nil {
291-
ctx.NotFound("GetCommit", err)
292-
return
293-
}
294-
295-
diff, err := models.GetDiffRange(models.RepoPath(userName, repoName), beforeCommitID,
296-
afterCommitID, setting.Git.MaxGitDiffLines,
297-
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
298-
if err != nil {
299-
ctx.NotFound("GetDiffRange", err)
300-
return
301-
}
302-
303-
commits, err := commit.CommitsBeforeUntil(beforeCommitID)
304-
if err != nil {
305-
ctx.ServerError("CommitsBeforeUntil", err)
306-
return
307-
}
308-
commits = models.ValidateCommitsWithEmails(commits)
309-
commits = models.ParseCommitsWithSignature(commits)
310-
commits = models.ParseCommitsWithStatus(commits, ctx.Repo.Repository)
311-
312-
ctx.Data["CommitRepoLink"] = ctx.Repo.RepoLink
313-
ctx.Data["Commits"] = commits
314-
ctx.Data["CommitCount"] = commits.Len()
315-
ctx.Data["BeforeCommitID"] = beforeCommitID
316-
ctx.Data["AfterCommitID"] = afterCommitID
317-
ctx.Data["Username"] = userName
318-
ctx.Data["Reponame"] = repoName
319-
ctx.Data["IsImageFile"] = commit.IsImageFile
320-
ctx.Data["Title"] = "Comparing " + base.ShortSha(beforeCommitID) + "..." + base.ShortSha(afterCommitID) + " · " + userName + "/" + repoName
321-
ctx.Data["Commit"] = commit
322-
ctx.Data["Diff"] = diff
323-
ctx.Data["DiffNotAvailable"] = diff.NumFiles() == 0
324-
ctx.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", "commit", afterCommitID)
325-
ctx.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", "commit", beforeCommitID)
326-
ctx.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "raw", "commit", afterCommitID)
327-
ctx.Data["RequireHighlightJS"] = true
328-
ctx.HTML(200, tplDiff)
329-
}

0 commit comments

Comments
 (0)