forgejo/modules/git
Mathieu Fenniak a797a71dea fix: display code comments on removed lines-of-code to correct locations in PR view (#12092)
With the completion of #12015, when a comment is left on a changed line in a pull request, we track the comment against the line of code with `git blame` and then identify where it currently is in any diff with `git blame --reverse`.  However, this strategy only works for the *modified* lines of code -- eg. the `+...` in diffs, and not the `-...` in diffs.  The reason is that `git blame --reverse` can't track a line of code's location past the commit that it was removed in.

To permit comments that are left on lines of code that are removed to appear correctly in the UI, a separate approach is required for those comments.  This PR performs two major changes, which have been complex to figure out, but are reasonably easy to understand:

- When a comment is placed on a removed line in a PR, perform a `git blame --reverse` from the PR's base to the currently viewed commit, and use this information to record in the comment:
    - the **last commit that the line of code existed in** (stored in the `commit_sha` field)
    - the **line of code as of that commit** (stored in the `line` field, negative, to indicate that the comment is on a removal).
    - the **patch** where the comment was placed (stored in the field `patch`); existing functionality unchanged in this PR
- When viewing any diff in the PR, for each comment on a removal, perform a diff from the `commit_sha` (last commit that the line of code existed in) to the current commit being viewed, and verify that within that diff the left-hand-side line removal still exists at the same line of code in the diff, by comparing the current diff with the stored patch.
    - If present, place the commit in the UI at the line number.
    - If the line of code no longer exists in the diff at that point (for example, it was removed, commented upon, and then re-added in a later commit), then the comment is considered outdated and isn't displayed.

The algorithm used for marking a comment as "outdated" is also updated to use this approach.

## 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...
  - [x] `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.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/12092
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-13 18:26:53 +02:00
..
foreachref chore: add modernizer linter (#11936) 2026-04-02 03:29:37 +02:00
internal Refactor git command package to improve security and maintainability (#22678) 2023-02-04 10:30:43 +08:00
pipeline feat: bump the minimum required Git version from 2.0.0 to 2.34.1 (#8328) 2025-06-29 00:44:18 +02:00
pushoptions chore: add modernizer linter (#11936) 2026-04-02 03:29:37 +02:00
tests/repos fix: detect renames when using diff-tree (#11038) 2026-01-25 15:51:18 +01:00
url Update dependency go to v1.25 (forgejo) (#8908) 2025-10-08 15:57:37 +02:00
batch.go [PORT] Refactor the usage of batch catfile (gitea#31754) 2024-08-26 03:48:51 +02:00
batch_reader.go chore: handle error types consistently (#9873) 2026-03-06 00:48:06 +01:00
blame.go feat: bump the minimum required Git version from 2.0.0 to 2.34.1 (#8328) 2025-06-29 00:44:18 +02:00
blame_sha256_test.go Update module github.com/golangci/golangci-lint/cmd/golangci-lint to v1.64.6 (forgejo) (#7118) 2025-03-04 21:38:35 +00:00
blame_test.go Update module github.com/golangci/golangci-lint/cmd/golangci-lint to v1.64.6 (forgejo) (#7118) 2025-03-04 21:38:35 +00:00
blob.go fix: internal server error on a large .gitmodules (#10744) 2026-01-10 10:44:59 +01:00
blob_test.go feat(ui): add switch between formats when previewing CITATION.{cff,bib} files (#9103) 2025-11-14 14:39:20 +01:00
command.go fix: store pull mirror creds encrypted with keying (#11909) 2026-04-04 13:53:22 +02:00
command_race_test.go Implement FSFE REUSE for golang files (#21840) 2022-11-27 18:20:29 +00:00
command_test.go Update module github.com/golangci/golangci-lint/cmd/golangci-lint to v2 (forgejo) (#7367) 2025-03-28 22:22:21 +00:00
commit.go chore: add modernizer linter (#11936) 2026-04-02 03:29:37 +02:00
commit_info.go chore: add modernizer linter (#11936) 2026-04-02 03:29:37 +02:00
commit_info_test.go Update module github.com/golangci/golangci-lint/cmd/golangci-lint to v1.64.6 (forgejo) (#7118) 2025-03-04 21:38:35 +00:00
commit_reader.go feat: include non-conventional headers in payload for git signatures (#9558) 2025-10-14 02:18:01 +02:00
commit_sha256_test.go Update module github.com/golangci/golangci-lint/cmd/golangci-lint to v2 (forgejo) (#7367) 2025-03-28 22:22:21 +00:00
commit_test.go chore: two small refactors in git module (#10109) 2025-11-15 13:24:00 +01:00
diff.go fix: display code comments on removed lines-of-code to correct locations in PR view (#12092) 2026-04-13 18:26:53 +02:00
diff_compare.go feat: improve checking if diffs differ (#8451) 2025-07-16 18:19:27 +02:00
diff_compare_test.go chore: fix typos throughout the codebase (#10753) 2026-01-26 22:57:33 +01:00
diff_test.go fix: display code comments on removed lines-of-code to correct locations in PR view (#12092) 2026-04-13 18:26:53 +02:00
error.go Update module github.com/golangci/golangci-lint/cmd/golangci-lint to v2 (forgejo) (#7367) 2025-03-28 22:22:21 +00:00
fetch.go chore: unify the usage of CryptoRandomString (#10110) 2025-11-15 13:24:53 +01:00
fetch_test.go feat: Use receive.hideRefs (#10015) 2025-11-10 14:36:15 +01:00
git.go fix: make Fetch work with git < 2.41 (#10095) 2025-11-13 13:22:09 +01:00
git_test.go feat: bump the minimum required Git version from 2.0.0 to 2.34.1 (#8328) 2025-06-29 00:44:18 +02:00
grep.go feat: add support to opt-in for fuzzy search (#10378) 2025-12-17 13:51:48 +01:00
grep_test.go fix: make test suite run on older git version (#8188) 2025-06-14 19:50:58 +02:00
hook.go chore: add modernizer linter (#11936) 2026-04-02 03:29:37 +02:00
last_commit_cache.go chore: add modernizer linter (#11936) 2026-04-02 03:29:37 +02:00
log_name_status.go chore: add modernizer linter (#11936) 2026-04-02 03:29:37 +02:00
notes.go chore: add modernizer linter (#11936) 2026-04-02 03:29:37 +02:00
notes_test.go chore: simplify GetNote (#9985) 2025-11-06 13:23:35 +01:00
object_format.go Enable unparam linter (#31277) 2024-06-16 13:42:58 +02:00
object_id.go chore: fix typos identified by mispell 2024-05-25 09:43:50 +02:00
object_id_test.go fix: hook post-receive for sha256 repos 2024-05-15 16:43:16 +02:00
object_signature.go Rename CommitGPGSignature to ObjectSignature 2024-03-26 08:09:36 +00:00
parse.go chore: add modernizer linter (#11936) 2026-04-02 03:29:37 +02:00
parse_test.go fix: do better parsing of file modes (#9161) 2025-09-04 21:58:12 +02:00
README.md move code.gitea.io/git to code.gitea.io/gitea/modules/git (#6364) 2019-03-27 17:33:00 +08:00
ref.go chore: add modernizer linter (#11936) 2026-04-02 03:29:37 +02:00
ref_test.go Fix mirror bug (#33224) 2025-01-19 09:42:57 +01:00
remote.go feat: bump the minimum required Git version from 2.0.0 to 2.34.1 (#8328) 2025-06-29 00:44:18 +02:00
repo.go fix: store pull mirror creds encrypted with keying (#11909) 2026-04-04 13:53:22 +02:00
repo_archive.go Fix archive creating LFS hooks and breaking pull requests (#28848) 2024-01-19 05:49:18 +00:00
repo_attribute.go chore: add modernizer linter (#11936) 2026-04-02 03:29:37 +02:00
repo_attribute_test.go chore: use standard library function (#8334) 2025-06-29 08:06:38 +02:00
repo_base.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
repo_base_test.go Update module github.com/golangci/golangci-lint/cmd/golangci-lint to v2 (forgejo) (#7367) 2025-03-28 22:22:21 +00:00
repo_blame.go fix: relocate PR review comments using git blame --reverse, improving comment placement (#12015) 2026-04-11 21:45:39 +02:00
repo_blame_test.go fix: relocate PR review comments using git blame --reverse, improving comment placement (#12015) 2026-04-11 21:45:39 +02:00
repo_blob_test.go Add testifylint to lint checks (#4535) 2024-07-30 19:41:10 +00:00
repo_branch.go chore: Refactor Is{Reference,Branch}Exist (#8425) 2025-07-06 07:16:43 +02:00
repo_branch_test.go chore: Refactor Is{Reference,Branch}Exist (#8425) 2025-07-06 07:16:43 +02:00
repo_commit.go chore: fix typos throughout the codebase (#10753) 2026-01-26 22:57:33 +01:00
repo_commit_test.go chore: two small refactors in git module (#10109) 2025-11-15 13:24:00 +01:00
repo_commitgraph.go feat: bump the minimum required Git version from 2.0.0 to 2.34.1 (#8328) 2025-06-29 00:44:18 +02:00
repo_compare.go fix: detect renames when using diff-tree (#11038) 2026-01-25 15:51:18 +01:00
repo_compare_test.go fix: detect renames when using diff-tree (#11038) 2026-01-25 15:51:18 +01:00
repo_gpg.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
repo_hook.go Implement FSFE REUSE for golang files (#21840) 2022-11-27 18:20:29 +00:00
repo_index.go chore: add modernizer linter (#11936) 2026-04-02 03:29:37 +02:00
repo_language_stats.go fix: use linguist-generated for language stats (#7685) 2025-04-27 23:29:21 +00:00
repo_language_stats_test.go fix: use linguist-generated for language stats (#7685) 2025-04-27 23:29:21 +00:00
repo_object.go Improve ObjectFormat interface (#28496) 2023-12-19 07:20:47 +00:00
repo_ref.go Update module github.com/golangci/golangci-lint/cmd/golangci-lint to v2 (forgejo) (#7367) 2025-03-28 22:22:21 +00:00
repo_ref_test.go Add testifylint to lint checks (#4535) 2024-07-30 19:41:10 +00:00
repo_stats.go ci: detect and prevent empty case statements in Go code (#11593) 2026-03-10 02:50:28 +01:00
repo_stats_test.go Update module github.com/golangci/golangci-lint/cmd/golangci-lint to v2 (forgejo) (#7367) 2025-03-28 22:22:21 +00:00
repo_tag.go chore: add modernizer linter (#11936) 2026-04-02 03:29:37 +02:00
repo_tag_test.go chore: require.ErrorContains() is intended, but require.Errorf() is used (#11035) 2026-01-25 19:48:15 +01:00
repo_test.go fix: support git clone when /tmp has noexec (#10146) 2025-11-19 16:36:34 +01:00
repo_tree.go Update module github.com/golangci/golangci-lint/cmd/golangci-lint to v2 (forgejo) (#7367) 2025-03-28 22:22:21 +00:00
signature.go chore: branding import path (#7337) 2025-03-27 19:40:14 +00:00
signature_test.go Update module github.com/golangci/golangci-lint/cmd/golangci-lint to v2 (forgejo) (#7367) 2025-03-28 22:22:21 +00:00
submodule.go fix: internal server error on a large .gitmodules (#10744) 2026-01-10 10:44:59 +01:00
submodule_test.go git/commit: re-implement submodules file reader (#8438) 2025-07-15 00:20:00 +02:00
tag.go chore: add modernizer linter (#11936) 2026-04-02 03:29:37 +02:00
tag_test.go Update module github.com/golangci/golangci-lint/cmd/golangci-lint to v2 (forgejo) (#7367) 2025-03-28 22:22:21 +00:00
tree.go chore: add modernizer linter (#11936) 2026-04-02 03:29:37 +02:00
tree_blob.go git/commit: re-implement submodules file reader (#8438) 2025-07-15 00:20:00 +02:00
tree_entry.go chore: add modernizer linter (#11936) 2026-04-02 03:29:37 +02:00
tree_entry_mode.go Implement FSFE REUSE for golang files (#21840) 2022-11-27 18:20:29 +00:00
tree_test.go chore: add modernizer linter (#11936) 2026-04-02 03:29:37 +02:00
utils.go chore: Remove LimitedReaderCloser 2025-01-31 10:43:29 +01:00
utils_test.go [CHORE] Drop go-git support 2024-08-12 19:11:09 +02:00

Git Module

This module is merged from https://github.com/go-gitea/git which is a Go module to access Git through shell commands. Now it's a part of gitea's main repository for easier pull request.