mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-23 03:24:01 +09:00
Backport #39887 by @silverwind Code comments next to a diff hunk could show one line too high in the conversation view vs. in the code view that showed them on the correct line. - Treat the hunk end in `CutDiffAroundLine` as exclusive, so the line after a hunk falls back to the unchanged-line context - Don't count `\ No newline at end of file` markers as lines when cutting the hunk - Share the unchanged-line fallback with migrated review comments Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
@@ -1620,6 +1620,22 @@ func CommentAsDiff(ctx context.Context, c *issues_model.Comment) (*Diff, error)
|
||||
return diff, nil
|
||||
}
|
||||
|
||||
// GetCodeCommentPatch returns the code snippet stored with a code comment
|
||||
func GetCodeCommentPatch(ctx context.Context, gitRepo *git.Repository, mergeBase, headCommitID, treePath string, line int64) (string, error) {
|
||||
patch, err := git.GetFileDiffCutAroundLine(ctx, gitRepo, mergeBase, headCommitID, treePath,
|
||||
int64((&issues_model.Comment{Line: line}).UnsignedLine()), line < 0, setting.UI.CodeCommentLines,
|
||||
)
|
||||
contextCommitID := util.Iif(line < 0, mergeBase, headCommitID)
|
||||
if err != nil || patch != "" || contextCommitID == "" {
|
||||
return patch, err
|
||||
}
|
||||
patch, err = GeneratePatchForUnchangedLine(ctx, gitRepo, contextCommitID, treePath, line, setting.UI.CodeCommentLines)
|
||||
if err != nil {
|
||||
log.Debug("Unable to generate patch for unchanged line (file=%s, line=%d, commit=%s): %v", treePath, line, contextCommitID, err)
|
||||
}
|
||||
return patch, nil
|
||||
}
|
||||
|
||||
// GeneratePatchForUnchangedLine creates a patch showing code context for an unchanged line
|
||||
func GeneratePatchForUnchangedLine(ctx context.Context, gitRepo *git.Repository, commitID, treePath string, line int64, contextLines int) (string, error) {
|
||||
commit, err := gitRepo.GetCommit(ctx, commitID)
|
||||
|
||||
Reference in New Issue
Block a user