From 5838cc817257dbc1676ea478e1b727571b5c09c0 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 21 Oct 2026 01:46:56 +0800 Subject: [PATCH] fix(pulls): tolerate invalid stored review commit_id (#39849) (#39857) Simple backport for #39849 to fix #39848 --- modules/git/gitcmd/error.go | 1 + services/pull/review.go | 11 ++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/modules/git/gitcmd/error.go b/modules/git/gitcmd/error.go index 5219b7c1012..85371071095 100644 --- a/modules/git/gitcmd/error.go +++ b/modules/git/gitcmd/error.go @@ -93,6 +93,7 @@ const ( StderrPathSpec StderrPrefix = "fatal: pathspec" StderrBadRevision StderrPrefix = "fatal: bad revision" StderrNoSuchPath StderrPrefix = "fatal: no such path" + StderrBadObject StderrPrefix = "fatal: bad object " StderrNoSuchRemote1 StderrPrefix = "fatal: no such remote" // git < 2.30, exit status 128 StderrNoSuchRemote2 StderrPrefix = "error: no such remote" // git >= 2.30. exit status 2 diff --git a/services/pull/review.go b/services/pull/review.go index 3d423306375..08c7e08514e 100644 --- a/services/pull/review.go +++ b/services/pull/review.go @@ -24,7 +24,7 @@ import ( ) func isErrBlameNotFoundOrNotEnoughLines(err error) bool { - return gitcmd.IsStderr(err, gitcmd.StderrNoSuchPath, gitcmd.StderrFileNoEnoughLines) + return gitcmd.IsStderr(err, gitcmd.StderrNoSuchPath, gitcmd.StderrFileNoEnoughLines, gitcmd.StderrBadObject) } // ErrDismissRequestOnClosedPR represents an error when a user tries to dismiss a review associated to a closed or merged PR. @@ -248,10 +248,15 @@ func createCodeComment(ctx context.Context, doer *user_model.User, repo *repo_mo // Get latest commit referencing the commented line // No need for get commit for base branch changes commit, err := lineBlame(ctx, pr.BaseRepo, gitRepo, head, treePath, uint(line)) + // A force push may have removed the review's stored commit. + if err != nil && head != pr.GetGitHeadRefName() { + head = pr.GetGitHeadRefName() + commit, err = lineBlame(ctx, pr.BaseRepo, gitRepo, head, treePath, uint(line)) + } if err == nil { commitID = commit.ID.String() - } else if !isErrBlameNotFoundOrNotEnoughLines(err) { - return nil, fmt.Errorf("LineBlame[%s, %s, %s, %d]: %w", pr.GetGitHeadRefName(), gitRepo.LogString(), treePath, line, err) + } else { + log.Debug("LineBlame[%s, %s, %s, %d]: %v", head, gitRepo.LogString(), treePath, line, err) } } }