jojo/modules/git/repo_blame.go
Mathieu Fenniak 9fe0cbee02 fix: relocate PR review comments using git blame --reverse, improving comment placement (#12015)
When a review comment is placed on a PR in Forgejo, Forgejo performs a `git blame` to identify which commit originated the line, and records that commit and line number in the comment's database record.  Later when the review is viewed, Forgejo currently makes no effort to place that comment in the correct *current* location, which may vary -- for example, if a PR had two commits and the comment was made on a line in the first commit, but the second commit changes line numbers in that file, the comment will appear in the incorrect location.

This PR adds the usage of `git blame --reverse` to calculate the correct location to display the comment in the current view (whether reviewing the PR commit-by-commit, or "Files changed").  It certainly does not fix all problems with comment placement (see comments).

Another major addition in this PR is a test harness for making relatively complex PRs and reviewing the diffs on the per-commit view and PR-diff views.

## Checklist

The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. All work and communication must conform to Forgejo's [AI Agreement](https://codeberg.org/forgejo/governance/src/branch/main/AIAgreement.md). There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org).

### Tests for Go changes

- I added test coverage for Go changes...
  - [x] in their respective `*_test.go` for unit tests.
  - [x] in the `tests/integration` directory if it involves interactions with a live Forgejo server.
- I ran...
  - [ ] `make pr-go` before pushing

### Documentation

- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [x] I did not document these changes and I do not expect someone else to do it.

### Release notes

- [x] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change.
- [ ] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change.

<!--start release-notes-assistant-->

## Release notes
<!--URL:https://codeberg.org/forgejo/forgejo-->
- Bug fixes
  - [PR](https://codeberg.org/forgejo/forgejo/pulls/12015): <!--number 12015 --><!--line 0 --><!--description cmVsb2NhdGUgUFIgcmV2aWV3IGNvbW1lbnRzIHVzaW5nIGBnaXQgYmxhbWUgLS1yZXZlcnNlYCwgaW1wcm92aW5nIGNvbW1lbnQgcGxhY2VtZW50-->relocate PR review comments using `git blame --reverse`, improving comment placement<!--description-->
<!--end release-notes-assistant-->

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/12015
Reviewed-by: Andreas Ahlenstorf <aahlenst@noreply.codeberg.org>
Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net>
Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
2026-04-11 21:45:39 +02:00

161 lines
5 KiB
Go

// Copyright 2017 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package git
import (
"errors"
"fmt"
"regexp"
"strconv"
"strings"
)
var (
ErrBlameFileDoesNotExist = errors.New("the blamed file does not exist")
ErrBlameFileNotEnoughLines = errors.New("the blamed file has not enough lines")
notEnoughLinesRe = regexp.MustCompile(`^fatal: file .+ has only \d+ lines?\n$`)
)
// LineBlame returns the latest commit at the given line
func (repo *Repository) LineBlame(revision, file string, line uint64) (*Commit, uint64, error) {
res, _, gitErr := NewCommand(repo.Ctx, "blame").
AddOptionFormat("-L %d,%d", line, line).
AddOptionValues("-p", revision).
AddDashesAndList(file).RunStdString(&RunOpts{Dir: repo.Path})
if gitErr != nil {
stdErr := gitErr.Stderr()
if stdErr == fmt.Sprintf("fatal: no such path %s in %s\n", file, revision) {
return nil, 0, ErrBlameFileDoesNotExist
}
if notEnoughLinesRe.MatchString(stdErr) {
return nil, 0, ErrBlameFileNotEnoughLines
}
return nil, 0, gitErr
}
objectFormat, err := repo.GetObjectFormat()
if err != nil {
return nil, 0, err
}
objectIDLen := objectFormat.FullLength()
if len(res) < objectIDLen+1 {
return nil, 0, fmt.Errorf("output of blame is invalid, cannot contain commit ID: %s", res)
}
commit, err := repo.GetCommit(res[:objectIDLen])
if err != nil {
return nil, 0, fmt.Errorf("GetCommit: %w", err)
}
endIdxOriginalLineNo := strings.IndexRune(res[objectIDLen+1:], ' ')
if endIdxOriginalLineNo == -1 {
return nil, 0, fmt.Errorf("output of blame is invalid, cannot contain original line number: %s", res)
}
originalLineNo, err := strconv.ParseUint(res[objectIDLen+1:objectIDLen+1+endIdxOriginalLineNo], 10, 64)
if err != nil {
return nil, 0, fmt.Errorf("strconv.ParseUint: %w", err)
}
return commit, originalLineNo, nil
}
type ReverseLineBlame struct {
CommitID string
LineNumber uint64
FilePath string
}
// Reverses the effect of [LineBlame]. If a file was modified at originalLine number in originalRevision,
// ReverseLineBlame will identify the last commit up-to-and-including currentRevision where that line exists, including
// its new path and line number. If the returned commit is not the same as currentRevision, then it indicates that
// content can no longer be located in currentRevision, and the returned commit is the last commit that had it.
func (repo *Repository) ReverseLineBlame(originalRevision, file string, originalLine uint64, currentRevision string) (*ReverseLineBlame, error) {
if originalRevision == currentRevision {
// Would cause an error to run the reverse, "fatal: More than one commit to dig up from, (N) and (N)"
return &ReverseLineBlame{
CommitID: originalRevision,
LineNumber: originalLine,
FilePath: file,
}, nil
}
res, _, gitErr := NewCommand(repo.Ctx, "blame").
AddOptionValues("--reverse").
AddDynamicArguments(fmt.Sprintf("%s..%s", originalRevision, currentRevision)).
AddOptionFormat("-L %d,%d", originalLine, originalLine).
AddOptionValues("-p").
AddDashesAndList(file).RunStdString(&RunOpts{Dir: repo.Path})
if gitErr != nil {
return nil, gitErr
}
// Example output:
//
// 74be0e8aa338d1374ab7ca0a25a4f594955a69c2 16 9 1
// author FirstName LastName
// author-mail <author@example.org>
// author-time 1775492007
// author-tz -0600
// committer FirstName LastName
// committer-mail <author@example.org>
// committer-time 1775492007
// committer-tz -0600
// summary restore file-in-base to orig, now not present in diff
// filename README.md
//
// Header (https://git-scm.com/docs/git-blame#_the_porcelain_format):
// - 40-byte SHA-1 of the commit the line is attributed to;
// - the line number of the line in the original file; [note: opposite in reverse]
// - the line number of the line in the final file; [note: opposite in reverse]
// - on a line that starts a group of lines from a different commit than the previous one, the number of lines in
// this group. On subsequent lines this field is absent.
lines := strings.Split(res, "\n")
header := lines[0]
headerValues := strings.Split(header, " ")
if len(headerValues) < 2 {
return nil, fmt.Errorf("failed to parse blame --reverse header: %q", header)
}
objectFormat, err := repo.GetObjectFormat()
if err != nil {
return nil, err
}
objectIDLen := objectFormat.FullLength()
objectID := headerValues[0]
if len(objectID) != objectIDLen {
return nil, fmt.Errorf("output of blame is invalid, cannot contain commit ID: %s", objectID)
}
commit, err := repo.GetCommit(objectID)
if err != nil {
return nil, fmt.Errorf("GetCommit: %w", err)
}
currentLineStr := headerValues[1]
currentLineNo, err := strconv.ParseUint(currentLineStr, 10, 64)
if err != nil {
return nil, fmt.Errorf("strconv.ParseUint: %w", err)
}
var filename string
for _, otherLine := range lines {
if strings.HasPrefix(otherLine, "filename ") {
filename = otherLine[len("filename "):]
break
}
}
return &ReverseLineBlame{
CommitID: commit.ID.String(),
LineNumber: currentLineNo,
FilePath: filename,
}, nil
}