Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0eff23d

Browse files
a1012112796zeripath
andauthoredMar 23, 2022
Fix compare link in active feeds for new branch (#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 <[email protected]> Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: Andrew Thornton <[email protected]>
1 parent d8f5784 commit 0eff23d

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
@@ -222,7 +222,34 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error {
222222
if len(commits.Commits) > setting.UI.FeedMaxCommitNum {
223223
commits.Commits = commits.Commits[:setting.UI.FeedMaxCommitNum]
224224
}
225-
commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID)
225+
226+
oldCommitID := opts.OldCommitID
227+
if oldCommitID == git.EmptySHA && len(commits.Commits) > 0 {
228+
oldCommit, err := gitRepo.GetCommit(commits.Commits[len(commits.Commits)-1].Sha1)
229+
if err != nil && !git.IsErrNotExist(err) {
230+
log.Error("unable to GetCommit %s from %-v: %v", oldCommitID, repo, err)
231+
}
232+
if oldCommit != nil {
233+
for i := 0; i < oldCommit.ParentCount(); i++ {
234+
commitID, _ := oldCommit.ParentID(i)
235+
if !commitID.IsZero() {
236+
oldCommitID = commitID.String()
237+
break
238+
}
239+
}
240+
}
241+
}
242+
243+
if oldCommitID == git.EmptySHA && repo.DefaultBranch != branch {
244+
oldCommitID = repo.DefaultBranch
245+
}
246+
247+
if oldCommitID != git.EmptySHA {
248+
commits.CompareURL = repo.ComposeCompareURL(oldCommitID, opts.NewCommitID)
249+
} else {
250+
commits.CompareURL = ""
251+
}
252+
226253
notification.NotifyPushCommits(pusher, repo, opts, commits)
227254

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

0 commit comments

Comments
 (0)
Please sign in to comment.