From 57ffed1a67e7f9b6753e5af60797c4595ae9a2d4 Mon Sep 17 00:00:00 2001 From: bytedream Date: Tue, 3 Mar 2026 18:54:37 +0100 Subject: [PATCH 01/11] Fix incorrect viewed files counter if reverted change was viewed --- services/gitdiff/gitdiff.go | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index b23e5b1b1ccc8..8d9907ca4b155 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -14,6 +14,7 @@ import ( "io" "net/url" "path" + "slices" "sort" "strings" "time" @@ -1479,7 +1480,6 @@ func SyncUserSpecificDiff(ctx context.Context, userID int64, pull *issues_model. } filesChangedSinceLastDiff := make(map[string]pull_model.ViewedState) -outer: for _, diffFile := range diff.Files { fileViewedState := review.UpdatedFiles[diffFile.GetDiffFileName()] @@ -1490,21 +1490,25 @@ outer: } filename := diffFile.GetDiffFileName() + if changedFileIdx := slices.Index(changedFiles, filename); changedFileIdx != -1 { // Check whether the file has changed since the last review + diffFile.HasChangedSinceLastReview = true + filesChangedSinceLastDiff[filename] = pull_model.HasChanged - // Check explicitly whether the file has changed since the last review - for _, changedFile := range changedFiles { - diffFile.HasChangedSinceLastReview = filename == changedFile - if diffFile.HasChangedSinceLastReview { - filesChangedSinceLastDiff[filename] = pull_model.HasChanged - continue outer // We don't want to check if the file is viewed here as that would fold the file, which is in this case unwanted - } - } - // Check whether the file has already been viewed - if fileViewedState == pull_model.Viewed { + changedFiles = slices.Delete(changedFiles, changedFileIdx, changedFileIdx+1) + } else if fileViewedState == pull_model.Viewed { // Check whether the file has already been viewed diffFile.IsViewed = true } } + // All changed files still present at this point aren't part of the diff anymore, this occurs + // when a file was modified in a previous commit of the diff and the modification got reverted afterwards. + // Marking the files as unviewed to prevent errors where a non-existing file has a view state + for _, changedFile := range changedFiles { + if _, ok := review.UpdatedFiles[changedFile]; ok { + filesChangedSinceLastDiff[changedFile] = pull_model.Unviewed + } + } + if len(filesChangedSinceLastDiff) > 0 { // Explicitly store files that have changed in the database, if any is present at all. // This has the benefit that the "Has Changed" attribute will be present as long as the user does not explicitly mark this file as viewed, so it will even survive a page reload after marking another file as viewed. From f48d1dc2cc5ed4444a584aef674a03a0303bfdea Mon Sep 17 00:00:00 2001 From: bytedream Date: Thu, 5 Mar 2026 18:42:31 +0100 Subject: [PATCH 02/11] Use more performant method of removing changed files --- services/gitdiff/gitdiff.go | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index 8d9907ca4b155..65c623a5aaa83 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -14,7 +14,6 @@ import ( "io" "net/url" "path" - "slices" "sort" "strings" "time" @@ -1480,8 +1479,10 @@ func SyncUserSpecificDiff(ctx context.Context, userID int64, pull *issues_model. } filesChangedSinceLastDiff := make(map[string]pull_model.ViewedState) +outer: for _, diffFile := range diff.Files { - fileViewedState := review.UpdatedFiles[diffFile.GetDiffFileName()] + filename := diffFile.GetDiffFileName() + fileViewedState := review.UpdatedFiles[filename] // Check whether it was previously detected that the file has changed since the last review if fileViewedState == pull_model.HasChanged { @@ -1489,13 +1490,21 @@ func SyncUserSpecificDiff(ctx context.Context, userID int64, pull *issues_model. continue } - filename := diffFile.GetDiffFileName() - if changedFileIdx := slices.Index(changedFiles, filename); changedFileIdx != -1 { // Check whether the file has changed since the last review - diffFile.HasChangedSinceLastReview = true - filesChangedSinceLastDiff[filename] = pull_model.HasChanged + // Check explicitly whether the file has changed since the last review + for i, changedFile := range changedFiles { + diffFile.HasChangedSinceLastReview = filename == changedFile + if diffFile.HasChangedSinceLastReview { + filesChangedSinceLastDiff[filename] = pull_model.HasChanged - changedFiles = slices.Delete(changedFiles, changedFileIdx, changedFileIdx+1) - } else if fileViewedState == pull_model.Viewed { // Check whether the file has already been viewed + // File changes are processed, no need to check them again + changedFiles[i] = changedFiles[len(changedFiles)-1] + changedFiles = changedFiles[:len(changedFiles)-1] + + continue outer // We don't want to check if the file is viewed here as that would fold the file, which is in this case unwanted + } + } + // Check whether the file has already been viewed + if fileViewedState == pull_model.Viewed { diffFile.IsViewed = true } } From 774dc50c958ca04ffabd099f6ce7a5bac95cb9be Mon Sep 17 00:00:00 2001 From: bytedream Date: Thu, 5 Mar 2026 19:39:56 +0100 Subject: [PATCH 03/11] Add test --- models/fixtures/issue.yml | 16 ++++++ models/fixtures/pull_request.yml | 13 +++++ services/gitdiff/gitdiff_test.go | 48 ++++++++++++++++++ .../logs/refs/heads/reverted-commit | 5 ++ .../45/bb15aa8b2f555b88cddc23086c18dcad776774 | Bin 0 -> 205 bytes .../51/25901fb2a9baf20f75c58f3e41547057eea699 | Bin 0 -> 86 bytes .../51/b94473765408cb71db3f32da268df60422d3fd | Bin 0 -> 36 bytes .../76/fa0ad17c213245ecf0bb0ce46bdd9a4d27646b | Bin 0 -> 201 bytes .../9d/aeafb9864cf43055ae93beb0afd6c7d144bfa4 | Bin 0 -> 20 bytes .../b8/de5391c6329e56f93ad3b8a8297f4f456a38d1 | Bin 0 -> 54 bytes .../e3/03f5ef01a33709911c6b8b3d1cbcba64a57777 | Bin 0 -> 227 bytes .../refs/heads/reverted-commit | 1 + 12 files changed, 83 insertions(+) create mode 100644 tests/gitea-repositories-meta/user2/commitsonpr.git/logs/refs/heads/reverted-commit create mode 100644 tests/gitea-repositories-meta/user2/commitsonpr.git/objects/45/bb15aa8b2f555b88cddc23086c18dcad776774 create mode 100644 tests/gitea-repositories-meta/user2/commitsonpr.git/objects/51/25901fb2a9baf20f75c58f3e41547057eea699 create mode 100644 tests/gitea-repositories-meta/user2/commitsonpr.git/objects/51/b94473765408cb71db3f32da268df60422d3fd create mode 100644 tests/gitea-repositories-meta/user2/commitsonpr.git/objects/76/fa0ad17c213245ecf0bb0ce46bdd9a4d27646b create mode 100644 tests/gitea-repositories-meta/user2/commitsonpr.git/objects/9d/aeafb9864cf43055ae93beb0afd6c7d144bfa4 create mode 100644 tests/gitea-repositories-meta/user2/commitsonpr.git/objects/b8/de5391c6329e56f93ad3b8a8297f4f456a38d1 create mode 100644 tests/gitea-repositories-meta/user2/commitsonpr.git/objects/e3/03f5ef01a33709911c6b8b3d1cbcba64a57777 create mode 100644 tests/gitea-repositories-meta/user2/commitsonpr.git/refs/heads/reverted-commit diff --git a/models/fixtures/issue.yml b/models/fixtures/issue.yml index ca5b1c6cd1df5..1ca3f687dc06b 100644 --- a/models/fixtures/issue.yml +++ b/models/fixtures/issue.yml @@ -372,3 +372,19 @@ created_unix: 1707270422 updated_unix: 1707270422 is_locked: false + +- + id: 23 + repo_id: 58 + index: 2 + poster_id: 2 + original_author_id: 0 + name: pull with reverted file + content: content + milestone_id: 0 + priority: 0 + is_closed: false + is_pull: true + created_unix: 946684830 + updated_unix: 978307200 + is_locked: false diff --git a/models/fixtures/pull_request.yml b/models/fixtures/pull_request.yml index 9a16316e5a2b4..d4eb3781f2fba 100644 --- a/models/fixtures/pull_request.yml +++ b/models/fixtures/pull_request.yml @@ -117,3 +117,16 @@ index: 1 head_repo_id: 61 base_repo_id: 61 + +- + id: 11 + type: 0 # gitea pull request + status: 2 # mergeable + issue_id: 23 + index: 2 + head_repo_id: 58 + base_repo_id: 58 + head_branch: reverted-commit + base_branch: main + merge_base: cbff181af4c9c7fee3cf6c106699e07d9a3f54e6 + has_merged: false diff --git a/services/gitdiff/gitdiff_test.go b/services/gitdiff/gitdiff_test.go index 62b17c223c558..a96d1ebf98fb8 100644 --- a/services/gitdiff/gitdiff_test.go +++ b/services/gitdiff/gitdiff_test.go @@ -11,6 +11,7 @@ import ( "testing" issues_model "code.gitea.io/gitea/models/issues" + pull_model "code.gitea.io/gitea/models/pull" "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" @@ -1143,3 +1144,50 @@ func TestHighlightCodeLines(t *testing.T) { }, ret) }) } + +func TestSyncUserSpecificDiff_UpdatedFiles(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) + pull := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 11}) + assert.NoError(t, pull.LoadBaseRepo(t.Context())) + + gitRepo, err := git.OpenRepository(t.Context(), pull.BaseRepo.RepoPath()) + assert.NoError(t, err) + defer gitRepo.Close() + + firstReviewCommit := "76fa0ad17c213245ecf0bb0ce46bdd9a4d27646b" + firstReviewUpdatedFiles := map[string]pull_model.ViewedState{ + "README.md": pull_model.Viewed, + "test.txt": pull_model.Viewed, + } + + _, err = pull_model.UpdateReviewState(t.Context(), user.ID, pull.ID, firstReviewCommit, firstReviewUpdatedFiles) + assert.NoError(t, err) + firstReview, err := pull_model.GetNewestReviewState(t.Context(), user.ID, pull.ID) + assert.NoError(t, err) + assert.NotNil(t, firstReview) + assert.Equal(t, firstReviewUpdatedFiles, firstReview.UpdatedFiles) + assert.Equal(t, 2, firstReview.GetViewedFileCount()) + + secondReviewCommit := "e303f5ef01a33709911c6b8b3d1cbcba64a57777" + secondReviewUpdatedFiles := map[string]pull_model.ViewedState{ + "README.md": pull_model.Viewed, + "test.txt": pull_model.Unviewed, + } + + opts := &DiffOptions{ + AfterCommitID: secondReviewCommit, + BeforeCommitID: pull.MergeBase, + } + diff := &Diff{ + Files: []*DiffFile{ + {Name: "README.md"}, + }, + } + secondReview, err := SyncUserSpecificDiff(t.Context(), user.ID, pull, gitRepo, diff, opts) + assert.NoError(t, err) + assert.NotNil(t, secondReview) + assert.Equal(t, secondReviewUpdatedFiles, secondReview.UpdatedFiles) + assert.Equal(t, 1, secondReview.GetViewedFileCount()) +} diff --git a/tests/gitea-repositories-meta/user2/commitsonpr.git/logs/refs/heads/reverted-commit b/tests/gitea-repositories-meta/user2/commitsonpr.git/logs/refs/heads/reverted-commit new file mode 100644 index 0000000000000..4342991d15763 --- /dev/null +++ b/tests/gitea-repositories-meta/user2/commitsonpr.git/logs/refs/heads/reverted-commit @@ -0,0 +1,5 @@ +0000000000000000000000000000000000000000 cbff181af4c9c7fee3cf6c106699e07d9a3f54e6 root 1688672317 +0200 push +cbff181af4c9c7fee3cf6c106699e07d9a3f54e6 cbff181af4c9c7fee3cf6c106699e07d9a3f54e6 bytedream 1772735796 +0100 Branch: renamed refs/heads/main to refs/heads/reverted-commit +cbff181af4c9c7fee3cf6c106699e07d9a3f54e6 45bb15aa8b2f555b88cddc23086c18dcad776774 bytedream 1772735845 +0100 commit: edit README.md +45bb15aa8b2f555b88cddc23086c18dcad776774 76fa0ad17c213245ecf0bb0ce46bdd9a4d27646b bytedream 1772735860 +0100 commit: add test.txt +76fa0ad17c213245ecf0bb0ce46bdd9a4d27646b e303f5ef01a33709911c6b8b3d1cbcba64a57777 bytedream 1772735864 +0100 revert: Revert "add test.txt" diff --git a/tests/gitea-repositories-meta/user2/commitsonpr.git/objects/45/bb15aa8b2f555b88cddc23086c18dcad776774 b/tests/gitea-repositories-meta/user2/commitsonpr.git/objects/45/bb15aa8b2f555b88cddc23086c18dcad776774 new file mode 100644 index 0000000000000000000000000000000000000000..dc2a34a375e8cd957db325106e442f774dc90e96 GIT binary patch literal 205 zcmV;;05bo00ZY!$&CM)PFfue}C@D%!RYv`jTH zPq9oiPBS%0HRDPwEy>6)QblO H-6}+h6(Ugl literal 0 HcmV?d00001 diff --git a/tests/gitea-repositories-meta/user2/commitsonpr.git/objects/51/25901fb2a9baf20f75c58f3e41547057eea699 b/tests/gitea-repositories-meta/user2/commitsonpr.git/objects/51/25901fb2a9baf20f75c58f3e41547057eea699 new file mode 100644 index 0000000000000000000000000000000000000000..4ab362a50dc54e1adb1cda0bc871d5170f291b73 GIT binary patch literal 86 zcmV-c0IC0Y0V^p=O;s>AW-v4`Ff%bx2y%6F@paY9O<@S!=~7%4!g0Frw!P6Uwcc+m sN|*ma6_unGm*|yLlrYR)w|-}v&liKxb(8mPSby#KMVI|c0A@xY*)@D9U;qFB literal 0 HcmV?d00001 diff --git a/tests/gitea-repositories-meta/user2/commitsonpr.git/objects/51/b94473765408cb71db3f32da268df60422d3fd b/tests/gitea-repositories-meta/user2/commitsonpr.git/objects/51/b94473765408cb71db3f32da268df60422d3fd new file mode 100644 index 0000000000000000000000000000000000000000..c0e53b3a1e4f7d431d52a7af4b61b56594b99670 GIT binary patch literal 36 scmb6)Qb9XD~D{Ff%bx2y%6F@paY9O<@S!=~7%4!g0Frw!P6Uwcc+m MN|*lv04`S#%OsW-WdHyG literal 0 HcmV?d00001 diff --git a/tests/gitea-repositories-meta/user2/commitsonpr.git/objects/e3/03f5ef01a33709911c6b8b3d1cbcba64a57777 b/tests/gitea-repositories-meta/user2/commitsonpr.git/objects/e3/03f5ef01a33709911c6b8b3d1cbcba64a57777 new file mode 100644 index 0000000000000000000000000000000000000000..d6d322215de26468c58818cda2bb25fe3ee12e85 GIT binary patch literal 227 zcmV<90382#0ZY!$&CM)PFtRXbC@D%!RY6)Qb Date: Thu, 5 Mar 2026 21:12:27 +0100 Subject: [PATCH 04/11] Get test diff from repo --- services/gitdiff/gitdiff_test.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/services/gitdiff/gitdiff_test.go b/services/gitdiff/gitdiff_test.go index a96d1ebf98fb8..f528e1964e460 100644 --- a/services/gitdiff/gitdiff_test.go +++ b/services/gitdiff/gitdiff_test.go @@ -1177,14 +1177,13 @@ func TestSyncUserSpecificDiff_UpdatedFiles(t *testing.T) { } opts := &DiffOptions{ - AfterCommitID: secondReviewCommit, - BeforeCommitID: pull.MergeBase, - } - diff := &Diff{ - Files: []*DiffFile{ - {Name: "README.md"}, - }, + AfterCommitID: secondReviewCommit, + BeforeCommitID: pull.MergeBase, + MaxLines: setting.Git.MaxGitDiffLines, + MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters, } + diff, err := GetDiffForAPI(t.Context(), gitRepo, opts) + assert.NoError(t, err) secondReview, err := SyncUserSpecificDiff(t.Context(), user.ID, pull, gitRepo, diff, opts) assert.NoError(t, err) assert.NotNil(t, secondReview) From 4208bb69346ba919b90292615359585e46f332d5 Mon Sep 17 00:00:00 2001 From: bytedream Date: Thu, 5 Mar 2026 21:16:30 +0100 Subject: [PATCH 05/11] Update test commit user --- .../user2/commitsonpr.git/logs/refs/heads/reverted-commit | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/gitea-repositories-meta/user2/commitsonpr.git/logs/refs/heads/reverted-commit b/tests/gitea-repositories-meta/user2/commitsonpr.git/logs/refs/heads/reverted-commit index 4342991d15763..ff88da40743f4 100644 --- a/tests/gitea-repositories-meta/user2/commitsonpr.git/logs/refs/heads/reverted-commit +++ b/tests/gitea-repositories-meta/user2/commitsonpr.git/logs/refs/heads/reverted-commit @@ -1,5 +1,5 @@ 0000000000000000000000000000000000000000 cbff181af4c9c7fee3cf6c106699e07d9a3f54e6 root 1688672317 +0200 push -cbff181af4c9c7fee3cf6c106699e07d9a3f54e6 cbff181af4c9c7fee3cf6c106699e07d9a3f54e6 bytedream 1772735796 +0100 Branch: renamed refs/heads/main to refs/heads/reverted-commit -cbff181af4c9c7fee3cf6c106699e07d9a3f54e6 45bb15aa8b2f555b88cddc23086c18dcad776774 bytedream 1772735845 +0100 commit: edit README.md -45bb15aa8b2f555b88cddc23086c18dcad776774 76fa0ad17c213245ecf0bb0ce46bdd9a4d27646b bytedream 1772735860 +0100 commit: add test.txt -76fa0ad17c213245ecf0bb0ce46bdd9a4d27646b e303f5ef01a33709911c6b8b3d1cbcba64a57777 bytedream 1772735864 +0100 revert: Revert "add test.txt" +cbff181af4c9c7fee3cf6c106699e07d9a3f54e6 cbff181af4c9c7fee3cf6c106699e07d9a3f54e6 admin 1772735796 +0100 Branch: renamed refs/heads/main to refs/heads/reverted-commit +cbff181af4c9c7fee3cf6c106699e07d9a3f54e6 45bb15aa8b2f555b88cddc23086c18dcad776774 admin 1772735845 +0100 commit: edit README.md +45bb15aa8b2f555b88cddc23086c18dcad776774 76fa0ad17c213245ecf0bb0ce46bdd9a4d27646b admin 1772735860 +0100 commit: add test.txt +76fa0ad17c213245ecf0bb0ce46bdd9a4d27646b e303f5ef01a33709911c6b8b3d1cbcba64a57777 admin 1772735864 +0100 revert: Revert "add test.txt" From ebc08f0ae1e4ef8bd80f56ae702f4b4abc8c0186 Mon Sep 17 00:00:00 2001 From: bytedream Date: Thu, 5 Mar 2026 21:21:47 +0100 Subject: [PATCH 06/11] Update test branch log --- .../user2/commitsonpr.git/logs/refs/heads/reverted-commit | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/gitea-repositories-meta/user2/commitsonpr.git/logs/refs/heads/reverted-commit b/tests/gitea-repositories-meta/user2/commitsonpr.git/logs/refs/heads/reverted-commit index ff88da40743f4..900a4d44b84af 100644 --- a/tests/gitea-repositories-meta/user2/commitsonpr.git/logs/refs/heads/reverted-commit +++ b/tests/gitea-repositories-meta/user2/commitsonpr.git/logs/refs/heads/reverted-commit @@ -1,5 +1 @@ -0000000000000000000000000000000000000000 cbff181af4c9c7fee3cf6c106699e07d9a3f54e6 root 1688672317 +0200 push -cbff181af4c9c7fee3cf6c106699e07d9a3f54e6 cbff181af4c9c7fee3cf6c106699e07d9a3f54e6 admin 1772735796 +0100 Branch: renamed refs/heads/main to refs/heads/reverted-commit -cbff181af4c9c7fee3cf6c106699e07d9a3f54e6 45bb15aa8b2f555b88cddc23086c18dcad776774 admin 1772735845 +0100 commit: edit README.md -45bb15aa8b2f555b88cddc23086c18dcad776774 76fa0ad17c213245ecf0bb0ce46bdd9a4d27646b admin 1772735860 +0100 commit: add test.txt -76fa0ad17c213245ecf0bb0ce46bdd9a4d27646b e303f5ef01a33709911c6b8b3d1cbcba64a57777 admin 1772735864 +0100 revert: Revert "add test.txt" +0000000000000000000000000000000000000000 e303f5ef01a33709911c6b8b3d1cbcba64a57777 Gitea 1772735864 +0100 push From 2687503d33886ffe478433f56d74f957145ce61f Mon Sep 17 00:00:00 2001 From: bytedream Date: Thu, 5 Mar 2026 22:22:45 +0100 Subject: [PATCH 07/11] Use existing pr for test --- models/fixtures/issue.yml | 16 ---------------- models/fixtures/pull_request.yml | 13 ------------- services/gitdiff/gitdiff_test.go | 14 +++++++------- .../commitsonpr.git/logs/refs/heads/branch1 | 2 +- .../logs/refs/heads/reverted-commit | 1 - .../32/0690f9a6e71a97013d2331eb21b279252ef2a4 | Bin 0 -> 196 bytes .../45/bb15aa8b2f555b88cddc23086c18dcad776774 | Bin 205 -> 0 bytes .../51/25901fb2a9baf20f75c58f3e41547057eea699 | Bin 86 -> 0 bytes .../51/b94473765408cb71db3f32da268df60422d3fd | Bin 36 -> 0 bytes .../5b/0e7161b29ba37428da947be383e85a89b6ce32 | Bin 0 -> 222 bytes .../76/fa0ad17c213245ecf0bb0ce46bdd9a4d27646b | Bin 201 -> 0 bytes .../9d/aeafb9864cf43055ae93beb0afd6c7d144bfa4 | Bin 20 -> 0 bytes .../b4/a5b8335ec2bcec29fae36d58653a853dc75346 | Bin 0 -> 349 bytes .../b8/de5391c6329e56f93ad3b8a8297f4f456a38d1 | Bin 54 -> 0 bytes .../c5/0b3b292b4b1ae93cc73e80cb7ea897bf349d45 | Bin 0 -> 33 bytes .../e3/03f5ef01a33709911c6b8b3d1cbcba64a57777 | Bin 227 -> 0 bytes .../user2/commitsonpr.git/refs/heads/branch1 | 2 +- .../commitsonpr.git/refs/heads/reverted-commit | 1 - 18 files changed, 9 insertions(+), 40 deletions(-) delete mode 100644 tests/gitea-repositories-meta/user2/commitsonpr.git/logs/refs/heads/reverted-commit create mode 100644 tests/gitea-repositories-meta/user2/commitsonpr.git/objects/32/0690f9a6e71a97013d2331eb21b279252ef2a4 delete mode 100644 tests/gitea-repositories-meta/user2/commitsonpr.git/objects/45/bb15aa8b2f555b88cddc23086c18dcad776774 delete mode 100644 tests/gitea-repositories-meta/user2/commitsonpr.git/objects/51/25901fb2a9baf20f75c58f3e41547057eea699 delete mode 100644 tests/gitea-repositories-meta/user2/commitsonpr.git/objects/51/b94473765408cb71db3f32da268df60422d3fd create mode 100644 tests/gitea-repositories-meta/user2/commitsonpr.git/objects/5b/0e7161b29ba37428da947be383e85a89b6ce32 delete mode 100644 tests/gitea-repositories-meta/user2/commitsonpr.git/objects/76/fa0ad17c213245ecf0bb0ce46bdd9a4d27646b delete mode 100644 tests/gitea-repositories-meta/user2/commitsonpr.git/objects/9d/aeafb9864cf43055ae93beb0afd6c7d144bfa4 create mode 100644 tests/gitea-repositories-meta/user2/commitsonpr.git/objects/b4/a5b8335ec2bcec29fae36d58653a853dc75346 delete mode 100644 tests/gitea-repositories-meta/user2/commitsonpr.git/objects/b8/de5391c6329e56f93ad3b8a8297f4f456a38d1 create mode 100644 tests/gitea-repositories-meta/user2/commitsonpr.git/objects/c5/0b3b292b4b1ae93cc73e80cb7ea897bf349d45 delete mode 100644 tests/gitea-repositories-meta/user2/commitsonpr.git/objects/e3/03f5ef01a33709911c6b8b3d1cbcba64a57777 delete mode 100644 tests/gitea-repositories-meta/user2/commitsonpr.git/refs/heads/reverted-commit diff --git a/models/fixtures/issue.yml b/models/fixtures/issue.yml index 1ca3f687dc06b..ca5b1c6cd1df5 100644 --- a/models/fixtures/issue.yml +++ b/models/fixtures/issue.yml @@ -372,19 +372,3 @@ created_unix: 1707270422 updated_unix: 1707270422 is_locked: false - -- - id: 23 - repo_id: 58 - index: 2 - poster_id: 2 - original_author_id: 0 - name: pull with reverted file - content: content - milestone_id: 0 - priority: 0 - is_closed: false - is_pull: true - created_unix: 946684830 - updated_unix: 978307200 - is_locked: false diff --git a/models/fixtures/pull_request.yml b/models/fixtures/pull_request.yml index d4eb3781f2fba..9a16316e5a2b4 100644 --- a/models/fixtures/pull_request.yml +++ b/models/fixtures/pull_request.yml @@ -117,16 +117,3 @@ index: 1 head_repo_id: 61 base_repo_id: 61 - -- - id: 11 - type: 0 # gitea pull request - status: 2 # mergeable - issue_id: 23 - index: 2 - head_repo_id: 58 - base_repo_id: 58 - head_branch: reverted-commit - base_branch: main - merge_base: cbff181af4c9c7fee3cf6c106699e07d9a3f54e6 - has_merged: false diff --git a/services/gitdiff/gitdiff_test.go b/services/gitdiff/gitdiff_test.go index f528e1964e460..4b34bd90afa37 100644 --- a/services/gitdiff/gitdiff_test.go +++ b/services/gitdiff/gitdiff_test.go @@ -1149,17 +1149,17 @@ func TestSyncUserSpecificDiff_UpdatedFiles(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) - pull := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 11}) + pull := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 7}) assert.NoError(t, pull.LoadBaseRepo(t.Context())) gitRepo, err := git.OpenRepository(t.Context(), pull.BaseRepo.RepoPath()) assert.NoError(t, err) defer gitRepo.Close() - firstReviewCommit := "76fa0ad17c213245ecf0bb0ce46bdd9a4d27646b" + firstReviewCommit := "320690f9a6e71a97013d2331eb21b279252ef2a4" firstReviewUpdatedFiles := map[string]pull_model.ViewedState{ - "README.md": pull_model.Viewed, - "test.txt": pull_model.Viewed, + "test1.txt": pull_model.Viewed, + "test11.txt": pull_model.Viewed, } _, err = pull_model.UpdateReviewState(t.Context(), user.ID, pull.ID, firstReviewCommit, firstReviewUpdatedFiles) @@ -1170,10 +1170,10 @@ func TestSyncUserSpecificDiff_UpdatedFiles(t *testing.T) { assert.Equal(t, firstReviewUpdatedFiles, firstReview.UpdatedFiles) assert.Equal(t, 2, firstReview.GetViewedFileCount()) - secondReviewCommit := "e303f5ef01a33709911c6b8b3d1cbcba64a57777" + secondReviewCommit := "5b0e7161b29ba37428da947be383e85a89b6ce32" secondReviewUpdatedFiles := map[string]pull_model.ViewedState{ - "README.md": pull_model.Viewed, - "test.txt": pull_model.Unviewed, + "test1.txt": pull_model.Viewed, + "test11.txt": pull_model.Unviewed, } opts := &DiffOptions{ diff --git a/tests/gitea-repositories-meta/user2/commitsonpr.git/logs/refs/heads/branch1 b/tests/gitea-repositories-meta/user2/commitsonpr.git/logs/refs/heads/branch1 index cf96195665d1c..9f19363816b1c 100644 --- a/tests/gitea-repositories-meta/user2/commitsonpr.git/logs/refs/heads/branch1 +++ b/tests/gitea-repositories-meta/user2/commitsonpr.git/logs/refs/heads/branch1 @@ -1 +1 @@ -0000000000000000000000000000000000000000 1978192d98bb1b65e11c2cf37da854fbf94bffd6 Gitea 1688672383 +0200 push +0000000000000000000000000000000000000000 5b0e7161b29ba37428da947be383e85a89b6ce32 Gitea 1772745057 +0100 push diff --git a/tests/gitea-repositories-meta/user2/commitsonpr.git/logs/refs/heads/reverted-commit b/tests/gitea-repositories-meta/user2/commitsonpr.git/logs/refs/heads/reverted-commit deleted file mode 100644 index 900a4d44b84af..0000000000000 --- a/tests/gitea-repositories-meta/user2/commitsonpr.git/logs/refs/heads/reverted-commit +++ /dev/null @@ -1 +0,0 @@ -0000000000000000000000000000000000000000 e303f5ef01a33709911c6b8b3d1cbcba64a57777 Gitea 1772735864 +0100 push diff --git a/tests/gitea-repositories-meta/user2/commitsonpr.git/objects/32/0690f9a6e71a97013d2331eb21b279252ef2a4 b/tests/gitea-repositories-meta/user2/commitsonpr.git/objects/32/0690f9a6e71a97013d2331eb21b279252ef2a4 new file mode 100644 index 0000000000000000000000000000000000000000..dce61aece99575d7315151d6cb3834ee5085d67d GIT binary patch literal 196 zcmV;#06YJ90ZY!$&CM)PFfue?C@D%!RY)>PG)=NFHa1O7HcCoPO*XPjOH4I3OEI-D zGc``MFf~p|Ha9gkG2<#oEK1EQQ82VLw=lFcO0l#^N-|6`Gfg!#Og2hRGd532v@kVE zOG>jeNlHsgG2==sEy>6)Qb4_2Bszo y+6IOO23!!gm82FaBvqEArWBv`jTH zPq9oiPBS%0HRDPwEy>6)QblO H-6}+h6(Ugl diff --git a/tests/gitea-repositories-meta/user2/commitsonpr.git/objects/51/25901fb2a9baf20f75c58f3e41547057eea699 b/tests/gitea-repositories-meta/user2/commitsonpr.git/objects/51/25901fb2a9baf20f75c58f3e41547057eea699 deleted file mode 100644 index 4ab362a50dc54e1adb1cda0bc871d5170f291b73..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 86 zcmV-c0IC0Y0V^p=O;s>AW-v4`Ff%bx2y%6F@paY9O<@S!=~7%4!g0Frw!P6Uwcc+m sN|*ma6_unGm*|yLlrYR)w|-}v&liKxb(8mPSby#KMVI|c0A@xY*)@D9U;qFB diff --git a/tests/gitea-repositories-meta/user2/commitsonpr.git/objects/51/b94473765408cb71db3f32da268df60422d3fd b/tests/gitea-repositories-meta/user2/commitsonpr.git/objects/51/b94473765408cb71db3f32da268df60422d3fd deleted file mode 100644 index c0e53b3a1e4f7d431d52a7af4b61b56594b99670..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 36 scmb6)Qb4_2Bzi; z+6IOO23!!gm82FaBvqEArWB6)Qb4F=H?^FfcPQQ3!H%bn$i7%S~apmhYO+A-uFjeh>50k4K7G z?xcrJg(@mZEiN(CE2$`9cysgbt(ZoK(07Sbg&!X7*d@JZQva(x}e*y`&|qFYEg4mNFkn<`YPYEWnDma&I}y6fIk_ v>Ol02$CA^vq(UFaAStp0D-xKs@xTs0j^4UEA0*7DN^F^&_^A~DtTwPSNCBwI literal 0 HcmV?d00001 diff --git a/tests/gitea-repositories-meta/user2/commitsonpr.git/objects/b8/de5391c6329e56f93ad3b8a8297f4f456a38d1 b/tests/gitea-repositories-meta/user2/commitsonpr.git/objects/b8/de5391c6329e56f93ad3b8a8297f4f456a38d1 deleted file mode 100644 index 4aabf56172c070ab6d3e83c07e7a95b53019af67..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 54 zcmV-60LlM&0V^p=O;s>9XD~D{Ff%bx2y%6F@paY9O<@S!=~7%4!g0Frw!P6Uwcc+m MN|*lv04`S#%OsW-WdHyG diff --git a/tests/gitea-repositories-meta/user2/commitsonpr.git/objects/c5/0b3b292b4b1ae93cc73e80cb7ea897bf349d45 b/tests/gitea-repositories-meta/user2/commitsonpr.git/objects/c5/0b3b292b4b1ae93cc73e80cb7ea897bf349d45 new file mode 100644 index 0000000000000000000000000000000000000000..3903d7700c3a9e80d2f3cf59cc35748b4f29b759 GIT binary patch literal 33 pcmbA>ONy)NS(`m2msxN4H5tV literal 0 HcmV?d00001 diff --git a/tests/gitea-repositories-meta/user2/commitsonpr.git/objects/e3/03f5ef01a33709911c6b8b3d1cbcba64a57777 b/tests/gitea-repositories-meta/user2/commitsonpr.git/objects/e3/03f5ef01a33709911c6b8b3d1cbcba64a57777 deleted file mode 100644 index d6d322215de26468c58818cda2bb25fe3ee12e85..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 227 zcmV<90382#0ZY!$&CM)PFtRXbC@D%!RY6)Qb Date: Fri, 6 Mar 2026 13:20:01 +0100 Subject: [PATCH 08/11] Use fast-import for test repo changes --- services/gitdiff/gitdiff_test.go | 17 +++++++++++++---- .../commitsonpr.git/logs/refs/heads/branch1 | 2 +- .../32/0690f9a6e71a97013d2331eb21b279252ef2a4 | Bin 196 -> 0 bytes .../5b/0e7161b29ba37428da947be383e85a89b6ce32 | Bin 222 -> 0 bytes .../b4/a5b8335ec2bcec29fae36d58653a853dc75346 | Bin 349 -> 0 bytes .../c5/0b3b292b4b1ae93cc73e80cb7ea897bf349d45 | Bin 33 -> 0 bytes .../user2/commitsonpr.git/refs/heads/branch1 | 2 +- 7 files changed, 15 insertions(+), 6 deletions(-) delete mode 100644 tests/gitea-repositories-meta/user2/commitsonpr.git/objects/32/0690f9a6e71a97013d2331eb21b279252ef2a4 delete mode 100644 tests/gitea-repositories-meta/user2/commitsonpr.git/objects/5b/0e7161b29ba37428da947be383e85a89b6ce32 delete mode 100644 tests/gitea-repositories-meta/user2/commitsonpr.git/objects/b4/a5b8335ec2bcec29fae36d58653a853dc75346 delete mode 100644 tests/gitea-repositories-meta/user2/commitsonpr.git/objects/c5/0b3b292b4b1ae93cc73e80cb7ea897bf349d45 diff --git a/services/gitdiff/gitdiff_test.go b/services/gitdiff/gitdiff_test.go index 4b34bd90afa37..8c3b0a76f09bd 100644 --- a/services/gitdiff/gitdiff_test.go +++ b/services/gitdiff/gitdiff_test.go @@ -1152,14 +1152,23 @@ func TestSyncUserSpecificDiff_UpdatedFiles(t *testing.T) { pull := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 7}) assert.NoError(t, pull.LoadBaseRepo(t.Context())) + stdin := `commit refs/heads/branch1 +author test 1772749114 +0000 +committer test 1772749114 +0000 +data 7 +revert +from 1978192d98bb1b65e11c2cf37da854fbf94bffd6 +D test10.txt` + require.NoError(t, gitcmd.NewCommand("fast-import").WithDir(pull.BaseRepo.RepoPath()).WithStdinBytes([]byte(stdin)).Run(t.Context())) + gitRepo, err := git.OpenRepository(t.Context(), pull.BaseRepo.RepoPath()) assert.NoError(t, err) defer gitRepo.Close() - firstReviewCommit := "320690f9a6e71a97013d2331eb21b279252ef2a4" + firstReviewCommit := "1978192d98bb1b65e11c2cf37da854fbf94bffd6" firstReviewUpdatedFiles := map[string]pull_model.ViewedState{ "test1.txt": pull_model.Viewed, - "test11.txt": pull_model.Viewed, + "test10.txt": pull_model.Viewed, } _, err = pull_model.UpdateReviewState(t.Context(), user.ID, pull.ID, firstReviewCommit, firstReviewUpdatedFiles) @@ -1170,10 +1179,10 @@ func TestSyncUserSpecificDiff_UpdatedFiles(t *testing.T) { assert.Equal(t, firstReviewUpdatedFiles, firstReview.UpdatedFiles) assert.Equal(t, 2, firstReview.GetViewedFileCount()) - secondReviewCommit := "5b0e7161b29ba37428da947be383e85a89b6ce32" + secondReviewCommit := "ec334573ae49726c78c6ff793138fd5334f31695" secondReviewUpdatedFiles := map[string]pull_model.ViewedState{ "test1.txt": pull_model.Viewed, - "test11.txt": pull_model.Unviewed, + "test10.txt": pull_model.Unviewed, } opts := &DiffOptions{ diff --git a/tests/gitea-repositories-meta/user2/commitsonpr.git/logs/refs/heads/branch1 b/tests/gitea-repositories-meta/user2/commitsonpr.git/logs/refs/heads/branch1 index 9f19363816b1c..cf96195665d1c 100644 --- a/tests/gitea-repositories-meta/user2/commitsonpr.git/logs/refs/heads/branch1 +++ b/tests/gitea-repositories-meta/user2/commitsonpr.git/logs/refs/heads/branch1 @@ -1 +1 @@ -0000000000000000000000000000000000000000 5b0e7161b29ba37428da947be383e85a89b6ce32 Gitea 1772745057 +0100 push +0000000000000000000000000000000000000000 1978192d98bb1b65e11c2cf37da854fbf94bffd6 Gitea 1688672383 +0200 push diff --git a/tests/gitea-repositories-meta/user2/commitsonpr.git/objects/32/0690f9a6e71a97013d2331eb21b279252ef2a4 b/tests/gitea-repositories-meta/user2/commitsonpr.git/objects/32/0690f9a6e71a97013d2331eb21b279252ef2a4 deleted file mode 100644 index dce61aece99575d7315151d6cb3834ee5085d67d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 196 zcmV;#06YJ90ZY!$&CM)PFfue?C@D%!RY)>PG)=NFHa1O7HcCoPO*XPjOH4I3OEI-D zGc``MFf~p|Ha9gkG2<#oEK1EQQ82VLw=lFcO0l#^N-|6`Gfg!#Og2hRGd532v@kVE zOG>jeNlHsgG2==sEy>6)Qb4_2Bszo y+6IOO23!!gm82FaBvqEArWB6)Qb4_2Bzi; z+6IOO23!!gm82FaBvqEArWB4F=H?^FfcPQQ3!H%bn$i7%S~apmhYO+A-uFjeh>50k4K7G z?xcrJg(@mZEiN(CE2$`9cysgbt(ZoK(07Sbg&!X7*d@JZQva(x}e*y`&|qFYEg4mNFkn<`YPYEWnDma&I}y6fIk_ v>Ol02$CA^vq(UFaAStp0D-xKs@xTs0j^4UEA0*7DN^F^&_^A~DtTwPSNCBwI diff --git a/tests/gitea-repositories-meta/user2/commitsonpr.git/objects/c5/0b3b292b4b1ae93cc73e80cb7ea897bf349d45 b/tests/gitea-repositories-meta/user2/commitsonpr.git/objects/c5/0b3b292b4b1ae93cc73e80cb7ea897bf349d45 deleted file mode 100644 index 3903d7700c3a9e80d2f3cf59cc35748b4f29b759..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 33 pcmbA>ONy)NS(`m2msxN4H5tV diff --git a/tests/gitea-repositories-meta/user2/commitsonpr.git/refs/heads/branch1 b/tests/gitea-repositories-meta/user2/commitsonpr.git/refs/heads/branch1 index e7c0854d917bc..357fc9d6edf07 100644 --- a/tests/gitea-repositories-meta/user2/commitsonpr.git/refs/heads/branch1 +++ b/tests/gitea-repositories-meta/user2/commitsonpr.git/refs/heads/branch1 @@ -1 +1 @@ -5b0e7161b29ba37428da947be383e85a89b6ce32 +1978192d98bb1b65e11c2cf37da854fbf94bffd6 From 8d807675c7329ea5ab3eba2dcd426f18c953735b Mon Sep 17 00:00:00 2001 From: bytedream Date: Fri, 6 Mar 2026 13:23:14 +0100 Subject: [PATCH 09/11] Remove unnecessary commit author --- services/gitdiff/gitdiff_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/services/gitdiff/gitdiff_test.go b/services/gitdiff/gitdiff_test.go index 8c3b0a76f09bd..5d4ffbb5d48a4 100644 --- a/services/gitdiff/gitdiff_test.go +++ b/services/gitdiff/gitdiff_test.go @@ -1153,7 +1153,6 @@ func TestSyncUserSpecificDiff_UpdatedFiles(t *testing.T) { assert.NoError(t, pull.LoadBaseRepo(t.Context())) stdin := `commit refs/heads/branch1 -author test 1772749114 +0000 committer test 1772749114 +0000 data 7 revert From 36375fc35497acb1b98c2871fe5447237b09306e Mon Sep 17 00:00:00 2001 From: bytedream Date: Sun, 8 Mar 2026 21:08:12 +0100 Subject: [PATCH 10/11] Use map/set to store changed files --- services/gitdiff/gitdiff.go | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index 65c623a5aaa83..cae33a4f5c61f 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -1477,9 +1477,12 @@ func SyncUserSpecificDiff(ctx context.Context, userID int64, pull *issues_model. if errIgnored != nil { log.Error("Could not get changed files between %s and %s for pull request %d in repo with path %s. Assuming no changes. Error: %w", review.CommitSHA, latestCommit, pull.Index, gitRepo.Path, err) } + changedFilesSet := make(map[string]struct{}, len(changedFiles)) + for _, changedFile := range changedFiles { + changedFilesSet[changedFile] = struct{}{} + } filesChangedSinceLastDiff := make(map[string]pull_model.ViewedState) -outer: for _, diffFile := range diff.Files { filename := diffFile.GetDiffFileName() fileViewedState := review.UpdatedFiles[filename] @@ -1490,21 +1493,12 @@ outer: continue } - // Check explicitly whether the file has changed since the last review - for i, changedFile := range changedFiles { - diffFile.HasChangedSinceLastReview = filename == changedFile - if diffFile.HasChangedSinceLastReview { - filesChangedSinceLastDiff[filename] = pull_model.HasChanged - - // File changes are processed, no need to check them again - changedFiles[i] = changedFiles[len(changedFiles)-1] - changedFiles = changedFiles[:len(changedFiles)-1] + if _, ok := changedFilesSet[filename]; ok { // Check explicitly whether the file has changed since the last review + diffFile.HasChangedSinceLastReview = true + filesChangedSinceLastDiff[filename] = pull_model.HasChanged - continue outer // We don't want to check if the file is viewed here as that would fold the file, which is in this case unwanted - } - } - // Check whether the file has already been viewed - if fileViewedState == pull_model.Viewed { + delete(changedFilesSet, filename) + } else if fileViewedState == pull_model.Viewed { // Check whether the file has already been viewed diffFile.IsViewed = true } } @@ -1512,7 +1506,7 @@ outer: // All changed files still present at this point aren't part of the diff anymore, this occurs // when a file was modified in a previous commit of the diff and the modification got reverted afterwards. // Marking the files as unviewed to prevent errors where a non-existing file has a view state - for _, changedFile := range changedFiles { + for changedFile := range changedFilesSet { if _, ok := review.UpdatedFiles[changedFile]; ok { filesChangedSinceLastDiff[changedFile] = pull_model.Unviewed } @@ -1521,7 +1515,6 @@ outer: if len(filesChangedSinceLastDiff) > 0 { // Explicitly store files that have changed in the database, if any is present at all. // This has the benefit that the "Has Changed" attribute will be present as long as the user does not explicitly mark this file as viewed, so it will even survive a page reload after marking another file as viewed. - // 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. updatedReview, err := pull_model.UpdateReviewState(ctx, review.UserID, review.PullID, review.CommitSHA, filesChangedSinceLastDiff) if err != nil { log.Warn("Could not update review for user %d, pull %d, commit %s and the changed files %v: %v", review.UserID, review.PullID, review.CommitSHA, filesChangedSinceLastDiff, err) From 13581752110ede33b6278a56439508adfb32abbc Mon Sep 17 00:00:00 2001 From: bytedream Date: Sun, 8 Mar 2026 22:37:17 +0100 Subject: [PATCH 11/11] Fix bug --- services/gitdiff/gitdiff.go | 10 ++---- services/gitdiff/gitdiff_test.go | 61 ++++++++++++++++++++++++++------ 2 files changed, 53 insertions(+), 18 deletions(-) diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index cae33a4f5c61f..6bc339bc61e90 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -1487,16 +1487,12 @@ func SyncUserSpecificDiff(ctx context.Context, userID int64, pull *issues_model. filename := diffFile.GetDiffFileName() fileViewedState := review.UpdatedFiles[filename] - // Check whether it was previously detected that the file has changed since the last review - if fileViewedState == pull_model.HasChanged { + if fileViewedState == pull_model.HasChanged { // Check whether it was previously detected that the file has changed since the last review diffFile.HasChangedSinceLastReview = true - continue - } - - if _, ok := changedFilesSet[filename]; ok { // Check explicitly whether the file has changed since the last review + delete(changedFilesSet, filename) + } else if _, ok := changedFilesSet[filename]; ok { // Check explicitly whether the file has changed since the last review diffFile.HasChangedSinceLastReview = true filesChangedSinceLastDiff[filename] = pull_model.HasChanged - delete(changedFilesSet, filename) } else if fileViewedState == pull_model.Viewed { // Check whether the file has already been viewed diffFile.IsViewed = true diff --git a/services/gitdiff/gitdiff_test.go b/services/gitdiff/gitdiff_test.go index 5d4ffbb5d48a4..cfd99544ccc56 100644 --- a/services/gitdiff/gitdiff_test.go +++ b/services/gitdiff/gitdiff_test.go @@ -1152,11 +1152,26 @@ func TestSyncUserSpecificDiff_UpdatedFiles(t *testing.T) { pull := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 7}) assert.NoError(t, pull.LoadBaseRepo(t.Context())) - stdin := `commit refs/heads/branch1 + stdin := `blob +mark :1 +data 7 +change + +commit refs/heads/branch1 +mark :2 committer test 1772749114 +0000 data 7 -revert +change from 1978192d98bb1b65e11c2cf37da854fbf94bffd6 +M 100644 :1 test2.txt +M 100644 :1 test3.txt + +commit refs/heads/branch1 +committer test 1772749114 +0000 +data 7 +revert +from :2 +D test2.txt D test10.txt` require.NoError(t, gitcmd.NewCommand("fast-import").WithDir(pull.BaseRepo.RepoPath()).WithStdinBytes([]byte(stdin)).Run(t.Context())) @@ -1167,34 +1182,58 @@ D test10.txt` firstReviewCommit := "1978192d98bb1b65e11c2cf37da854fbf94bffd6" firstReviewUpdatedFiles := map[string]pull_model.ViewedState{ "test1.txt": pull_model.Viewed, + "test2.txt": pull_model.Viewed, "test10.txt": pull_model.Viewed, } - _, err = pull_model.UpdateReviewState(t.Context(), user.ID, pull.ID, firstReviewCommit, firstReviewUpdatedFiles) assert.NoError(t, err) firstReview, err := pull_model.GetNewestReviewState(t.Context(), user.ID, pull.ID) assert.NoError(t, err) assert.NotNil(t, firstReview) assert.Equal(t, firstReviewUpdatedFiles, firstReview.UpdatedFiles) - assert.Equal(t, 2, firstReview.GetViewedFileCount()) + assert.Equal(t, 3, firstReview.GetViewedFileCount()) - secondReviewCommit := "ec334573ae49726c78c6ff793138fd5334f31695" + secondReviewCommit := "f80737c7dc9de0a9c1e051e83cb6897f950c6bb8" secondReviewUpdatedFiles := map[string]pull_model.ViewedState{ "test1.txt": pull_model.Viewed, - "test10.txt": pull_model.Unviewed, + "test2.txt": pull_model.HasChanged, + "test3.txt": pull_model.HasChanged, + "test10.txt": pull_model.Viewed, } - - opts := &DiffOptions{ + secondReviewDiffOpts := &DiffOptions{ AfterCommitID: secondReviewCommit, BeforeCommitID: pull.MergeBase, MaxLines: setting.Git.MaxGitDiffLines, MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters, + MaxFiles: setting.Git.MaxGitDiffFiles, } - diff, err := GetDiffForAPI(t.Context(), gitRepo, opts) + secondReviewDiff, err := GetDiffForAPI(t.Context(), gitRepo, secondReviewDiffOpts) assert.NoError(t, err) - secondReview, err := SyncUserSpecificDiff(t.Context(), user.ID, pull, gitRepo, diff, opts) + secondReview, err := SyncUserSpecificDiff(t.Context(), user.ID, pull, gitRepo, secondReviewDiff, secondReviewDiffOpts) assert.NoError(t, err) assert.NotNil(t, secondReview) assert.Equal(t, secondReviewUpdatedFiles, secondReview.UpdatedFiles) - assert.Equal(t, 1, secondReview.GetViewedFileCount()) + assert.Equal(t, 2, secondReview.GetViewedFileCount()) + + thirdReviewCommit := "73424f3a99e140f6399c73a1712654e122d2a74b" + thirdReviewUpdatedFiles := map[string]pull_model.ViewedState{ + "test1.txt": pull_model.Viewed, + "test2.txt": pull_model.Unviewed, + "test3.txt": pull_model.HasChanged, + "test10.txt": pull_model.Unviewed, + } + thirdReviewDiffOpts := &DiffOptions{ + AfterCommitID: thirdReviewCommit, + BeforeCommitID: pull.MergeBase, + MaxLines: setting.Git.MaxGitDiffLines, + MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters, + MaxFiles: setting.Git.MaxGitDiffFiles, + } + thirdReviewDiff, err := GetDiffForAPI(t.Context(), gitRepo, thirdReviewDiffOpts) + assert.NoError(t, err) + thirdReview, err := SyncUserSpecificDiff(t.Context(), user.ID, pull, gitRepo, thirdReviewDiff, thirdReviewDiffOpts) + assert.NoError(t, err) + assert.NotNil(t, thirdReview) + assert.Equal(t, thirdReviewUpdatedFiles, thirdReview.UpdatedFiles) + assert.Equal(t, 1, thirdReview.GetViewedFileCount()) }