Skip to content

Fix incorrect viewed files counter if reverted change was viewed#36819

Merged
silverwind merged 12 commits intogo-gitea:mainfrom
bytedream:fix-reverted-file-view-state
Mar 9, 2026
Merged

Fix incorrect viewed files counter if reverted change was viewed#36819
silverwind merged 12 commits intogo-gitea:mainfrom
bytedream:fix-reverted-file-view-state

Conversation

@bytedream
Copy link
Contributor

@bytedream bytedream commented Mar 3, 2026

If a file is marked as viewed in a PR and all changes to those file are reverted afterwards, the file is still stored as viewed in the db, which causes an incorrect viewed files counter


image

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Mar 3, 2026
@github-actions github-actions bot added the modifies/go Pull requests that update Go code label Mar 3, 2026
@lunny
Copy link
Member

lunny commented Mar 3, 2026

Could we have a test for that?

}
// Check whether the file has already been viewed
if fileViewedState == pull_model.Viewed {
changedFiles = slices.Delete(changedFiles, changedFileIdx, changedFileIdx+1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changedFiles needs to be a map now

Copy link
Contributor Author

@bytedream bytedream Mar 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've reverted to keep the old logic - I haven't thought about the performance bottleneck here. Done

@bytedream
Copy link
Contributor Author

Could we have a test for that?

Test added

@lunny
Copy link
Member

lunny commented Mar 5, 2026

Could we have a test for that?

Test added

It's better to use fast-import to import the fixture git repository.

@bytedream
Copy link
Contributor Author

bytedream commented Mar 6, 2026

I've added the test repo changes as fast-import, but I'm still using a test repo as base, as the used pull request fixture depends on it

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Mar 6, 2026
@silverwind
Copy link
Member

Comment authored by Claude on behalf of @silverwind

Review of #36819 — Fix incorrect viewed files counter if reverted change was viewed

The fix is correct in concept — leftover changedFiles entries after the main loop represent files that changed between the review commit and HEAD but are no longer in the diff (i.e. reverted). Marking these as Unviewed properly fixes the counter.

Two points:

  1. Outdated comment should be updated. Line 1511 currently says:

    On the other hand, this means that even if a commit reverting an unseen change is committed, the file will still be seen as changed.

    This PR addresses exactly that case, so the comment is now misleading and should be removed or reworded.

  2. Consider a map for changedFiles instead of slice swap-and-shrink. wxiaoguang suggested this earlier. Converting changedFiles to a map[string]struct{} upfront would:

    • Reduce the inner loop from O(n*m) to O(n+m)
    • Eliminate the swap-and-shrink pattern which, while correct here (the continue outer saves it), is tricky to reason about inside a range loop
    • Make the "leftover files" logic at the end simpler — just iterate the map

    Something like:

    changedFileSet := make(map[string]struct{}, len(changedFiles))
    for _, f := range changedFiles {
        changedFileSet[f] = struct{}{}
    }
    // ... in the loop: _, changed := changedFileSet[filename]; delete(changedFileSet, filename)
    // ... after the loop: iterate changedFileSet for leftovers

The test is well-structured and validates the scenario. Overall looks good pending the above.

@bytedream
Copy link
Contributor Author

Addressed the ai suggestions

Copy link
Member

@silverwind silverwind left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This review was written by Claude.

@GiteaBot GiteaBot added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels Mar 8, 2026
@silverwind
Copy link
Member

Not sure about backport, likely won't merge cleanly and it's better to focus on the 1.26 release.

@silverwind silverwind enabled auto-merge (squash) March 9, 2026 07:53
@silverwind silverwind merged commit 47085f3 into go-gitea:main Mar 9, 2026
26 checks passed
@GiteaBot GiteaBot added this to the 1.26.0 milestone Mar 9, 2026
GiteaBot pushed a commit to GiteaBot/gitea that referenced this pull request Mar 9, 2026
…gitea#36819)

If a file is marked as viewed in a PR and all changes to those file are
reverted afterwards, the file is still stored as viewed in the db, which
causes an incorrect viewed files counter

---

<img width="468" height="139" alt="image"
src="https://github.com/user-attachments/assets/f13bf161-142d-49a9-8425-3884ee7abb84"
/>
@GiteaBot GiteaBot added the backport/done All backports for this PR have been created label Mar 9, 2026
@silverwind silverwind removed backport/done All backports for this PR have been created backport/v1.25 labels Mar 9, 2026
@silverwind
Copy link
Member

Cancelled the backport PR.

zjjhot added a commit to zjjhot/gitea that referenced this pull request Mar 10, 2026
* giteaofficial/main:
  Update minimum go version to 1.26.1, golangci-lint to 2.11.2, fix test style (go-gitea#36876)
  Add render cache for SVG icons (go-gitea#36863)
  Fix incorrect viewed files counter if reverted change was viewed (go-gitea#36819)
  [skip ci] Updated translations via Crowdin
  Clean up `refreshViewedFilesSummary` (go-gitea#36868)
  Remove `util.URLJoin` and replace all callers with direct path concatenation (go-gitea#36867)
  Optimize Docker build with dependency layer caching (go-gitea#36864)
  Fix URLJoin, markup render link reoslving, sign-in/up/linkaccount page common data (go-gitea#36861)
  Fix CodeQL code scanning alerts (go-gitea#36858)
  Refactor auth middleware (go-gitea#36848)
  Update Nix flake (go-gitea#36857)
  Update JS deps (go-gitea#36850)
  Load `mentionValues` asynchronously (go-gitea#36739)
  [skip ci] Updated translations via Crowdin
  Fix dbfs error handling (go-gitea#36844)
  Fix OAuth2 authorization code expiry and reuse handling (go-gitea#36797)
  Fix org permission API visibility checks for hidden members and private orgs (go-gitea#36798)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. modifies/go Pull requests that update Go code type/bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants