]> source.dussan.org Git - gitea.git/commitdiff
Fix compare link in active feeds for new branch (#19149) (#19185)
authorzeripath <art27@cantab.net>
Wed, 23 Mar 2022 19:04:50 +0000 (19:04 +0000)
committerGitHub <noreply@github.com>
Wed, 23 Mar 2022 19:04:50 +0000 (19:04 +0000)
Backport #19149

When a new branch is pushed the old SHA is always listed as the empty sha and thus the compare link that is created does not work correctly.

Therefore when creating the compare link for new branches:

1. Attempt to get the parent of the first commit and use that as the basis
for the compare link.
2. If this is not possible make a comparison to the default branch
3. Finally if that is not possible simply do not show a compare link.

However, there are multiple broken compare links remaining therefore, in order for these to not break we will simply make the compare link redirect to the default branch.

Fix #19144

Signed-off-by: a1012112796 <1012112796@qq.com>
Signed-off-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: a1012112796 <1012112796@qq.com>
routers/web/repo/compare.go
services/repository/push.go

index e0855b1834843d6de8d9d59427424fc27d8dee31..f1b89a7be0d792bae8199a7e6b38b3ef0edb8a65 100644 (file)
@@ -298,6 +298,13 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo {
                        ci.BaseBranch = baseCommit.ID.String()
                        ctx.Data["BaseBranch"] = ci.BaseBranch
                        baseIsCommit = true
+               } else if ci.BaseBranch == git.EmptySHA {
+                       if isSameRepo {
+                               ctx.Redirect(ctx.Repo.RepoLink + "/compare/" + util.PathEscapeSegments(ci.HeadBranch))
+                       } else {
+                               ctx.Redirect(ctx.Repo.RepoLink + "/compare/" + util.PathEscapeSegments(ci.HeadRepo.FullName()) + ":" + util.PathEscapeSegments(ci.HeadBranch))
+                       }
+                       return nil
                } else {
                        ctx.NotFound("IsRefExist", nil)
                        return nil
index 11854ccb3978c78cb206499aae9ed8f0bd4efa31..b1dbe7d7f0e994d5ebbcd874ce6ea19df7cddfae 100644 (file)
@@ -216,7 +216,34 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
                                if len(commits.Commits) > setting.UI.FeedMaxCommitNum {
                                        commits.Commits = commits.Commits[:setting.UI.FeedMaxCommitNum]
                                }
-                               commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID)
+
+                               oldCommitID := opts.OldCommitID
+                               if oldCommitID == git.EmptySHA && len(commits.Commits) > 0 {
+                                       oldCommit, err := gitRepo.GetCommit(commits.Commits[len(commits.Commits)-1].Sha1)
+                                       if err != nil && !git.IsErrNotExist(err) {
+                                               log.Error("unable to GetCommit %s from %-v: %v", oldCommitID, repo, err)
+                                       }
+                                       if oldCommit != nil {
+                                               for i := 0; i < oldCommit.ParentCount(); i++ {
+                                                       commitID, _ := oldCommit.ParentID(i)
+                                                       if !commitID.IsZero() {
+                                                               oldCommitID = commitID.String()
+                                                               break
+                                                       }
+                                               }
+                                       }
+                               }
+
+                               if oldCommitID == git.EmptySHA && repo.DefaultBranch != branch {
+                                       oldCommitID = repo.DefaultBranch
+                               }
+
+                               if oldCommitID != git.EmptySHA {
+                                       commits.CompareURL = repo.ComposeCompareURL(oldCommitID, opts.NewCommitID)
+                               } else {
+                                       commits.CompareURL = ""
+                               }
+
                                notification.NotifyPushCommits(pusher, repo, opts, commits)
 
                                if err = models.RemoveDeletedBranchByName(repo.ID, branch); err != nil {