Skip to content

Commit 6fc73a8

Browse files
Fix compare link in active feeds for new branch (#19149) (#19185)
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>
1 parent b1a0a78 commit 6fc73a8

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

routers/web/repo/compare.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,13 @@ func ParseCompareInfo(ctx *context.Context) *CompareInfo {
298298
ci.BaseBranch = baseCommit.ID.String()
299299
ctx.Data["BaseBranch"] = ci.BaseBranch
300300
baseIsCommit = true
301+
} else if ci.BaseBranch == git.EmptySHA {
302+
if isSameRepo {
303+
ctx.Redirect(ctx.Repo.RepoLink + "/compare/" + util.PathEscapeSegments(ci.HeadBranch))
304+
} else {
305+
ctx.Redirect(ctx.Repo.RepoLink + "/compare/" + util.PathEscapeSegments(ci.HeadRepo.FullName()) + ":" + util.PathEscapeSegments(ci.HeadBranch))
306+
}
307+
return nil
301308
} else {
302309
ctx.NotFound("IsRefExist", nil)
303310
return nil

services/repository/push.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,34 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
216216
if len(commits.Commits) > setting.UI.FeedMaxCommitNum {
217217
commits.Commits = commits.Commits[:setting.UI.FeedMaxCommitNum]
218218
}
219-
commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID)
219+
220+
oldCommitID := opts.OldCommitID
221+
if oldCommitID == git.EmptySHA && len(commits.Commits) > 0 {
222+
oldCommit, err := gitRepo.GetCommit(commits.Commits[len(commits.Commits)-1].Sha1)
223+
if err != nil && !git.IsErrNotExist(err) {
224+
log.Error("unable to GetCommit %s from %-v: %v", oldCommitID, repo, err)
225+
}
226+
if oldCommit != nil {
227+
for i := 0; i < oldCommit.ParentCount(); i++ {
228+
commitID, _ := oldCommit.ParentID(i)
229+
if !commitID.IsZero() {
230+
oldCommitID = commitID.String()
231+
break
232+
}
233+
}
234+
}
235+
}
236+
237+
if oldCommitID == git.EmptySHA && repo.DefaultBranch != branch {
238+
oldCommitID = repo.DefaultBranch
239+
}
240+
241+
if oldCommitID != git.EmptySHA {
242+
commits.CompareURL = repo.ComposeCompareURL(oldCommitID, opts.NewCommitID)
243+
} else {
244+
commits.CompareURL = ""
245+
}
246+
220247
notification.NotifyPushCommits(pusher, repo, opts, commits)
221248

222249
if err = models.RemoveDeletedBranchByName(repo.ID, branch); err != nil {

0 commit comments

Comments
 (0)