From 622a59427e123f993be5128874c8d140de97953e Mon Sep 17 00:00:00 2001 From: a1012112796 <1012112796@qq.com> Date: Sun, 20 Mar 2022 10:24:56 +0800 Subject: [PATCH 1/4] fix compare link in active feeds for new branch fix #19144 Signed-off-by: a1012112796 <1012112796@qq.com> --- services/repository/push.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/services/repository/push.go b/services/repository/push.go index fafe4736ab4e5..9a73b447b9eb4 100644 --- a/services/repository/push.go +++ b/services/repository/push.go @@ -222,7 +222,25 @@ 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 { + oldCommitID = commits.Commits[len(commits.Commits)-1].Sha1 + oldCommit, err := gitRepo.GetCommit(oldCommitID) + if err != nil { + log.Error("gitRepo.GetCommit %s/%s failed: %v", repo.ID, oldCommitID, err) + } + + for i := 0; i < oldCommit.ParentCount(); i++ { + commitID, _ := oldCommit.ParentID(i) + if !commitID.IsZero() { + oldCommitID = commitID.String() + break + } + } + } + + commits.CompareURL = repo.ComposeCompareURL(oldCommitID, opts.NewCommitID) notification.NotifyPushCommits(pusher, repo, opts, commits) if err = models.RemoveDeletedBranchByName(repo.ID, branch); err != nil { From a7a22ea86ca26f118b85abf1097a339e419aaf61 Mon Sep 17 00:00:00 2001 From: a1012112796 <1012112796@qq.com> Date: Mon, 21 Mar 2022 20:38:42 +0800 Subject: [PATCH 2/4] use default branch for absolutely new branch --- services/repository/push.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/services/repository/push.go b/services/repository/push.go index 9a73b447b9eb4..9975f24b03d78 100644 --- a/services/repository/push.go +++ b/services/repository/push.go @@ -225,8 +225,7 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { oldCommitID := opts.OldCommitID if oldCommitID == git.EmptySHA && len(commits.Commits) > 0 { - oldCommitID = commits.Commits[len(commits.Commits)-1].Sha1 - oldCommit, err := gitRepo.GetCommit(oldCommitID) + oldCommit, err := gitRepo.GetCommit(commits.Commits[len(commits.Commits)-1].Sha1) if err != nil { log.Error("gitRepo.GetCommit %s/%s failed: %v", repo.ID, oldCommitID, err) } @@ -240,7 +239,16 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { } } - commits.CompareURL = repo.ComposeCompareURL(oldCommitID, opts.NewCommitID) + 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 { From 8eaf8233e2c07f703a3896504381961080e279c8 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Wed, 23 Mar 2022 09:57:47 +0000 Subject: [PATCH 3/4] ... also nicely handle the broken compare links ... Signed-off-by: Andrew Thornton --- routers/web/repo/compare.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 23ec662cfae5b..902da0c3fe3dc 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -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 From ac2dd38becc01079e23171396bae24a6caf98153 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Wed, 23 Mar 2022 12:26:07 +0000 Subject: [PATCH 4/4] Prevent NPE if unable to get first commit in a chain of pushes Signed-off-by: Andrew Thornton --- services/repository/push.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/services/repository/push.go b/services/repository/push.go index b89a412a8a4ee..8fbd5f5186635 100644 --- a/services/repository/push.go +++ b/services/repository/push.go @@ -226,15 +226,16 @@ func pushUpdates(optsList []*repo_module.PushUpdateOptions) error { 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 { - log.Error("gitRepo.GetCommit %s/%s failed: %v", repo.ID, oldCommitID, err) + if err != nil && !git.IsErrNotExist(err) { + log.Error("unable to GetCommit %s from %-v: %v", oldCommitID, repo, err) } - - for i := 0; i < oldCommit.ParentCount(); i++ { - commitID, _ := oldCommit.ParentID(i) - if !commitID.IsZero() { - oldCommitID = commitID.String() - break + if oldCommit != nil { + for i := 0; i < oldCommit.ParentCount(); i++ { + commitID, _ := oldCommit.ParentID(i) + if !commitID.IsZero() { + oldCommitID = commitID.String() + break + } } } }