From b21b63c61ac1038a1597acee45084896f5e39a3a Mon Sep 17 00:00:00 2001 From: Laurent Cahour Date: Thu, 24 Aug 2023 12:03:49 +0200 Subject: [PATCH] Add merge files files to GetCommitFileStatus (#20515) Hi, We'd like to add merge files files to GetCommitFileStatus fucntions so API returns the list of all the files associated to a merged pull request commit, like GitHub API does. The list of affectedFiles for an API commit is fetched from toCommit() function in routers/api/v1/repo/commits.go, and API was returning no file in case of a pull request with no conflict, or just files associated to the confict resolution, but NOT the full list of merged files. This would lead to situations where a CI polling a repo for changes could miss some file changes due to API returning an empty / partial list in case of such merged pull requests. (Hope this makes sense :) ) NOTE: I'd like to add a unittest in integrations/api_repo_git_commits_test.go but failed to understand how to add my own test bare repo so I can make a test on a merged pull request commit to check for affectedFiles. Is there a merged pull request in there that I could use maybe? Could someone please direct me to the relevant ressources with informations on how to do that please? Thanks for your time, Laurent. --------- Co-authored-by: Thomas Desveaux --- modules/git/commit.go | 2 +- modules/git/commit_test.go | 23 ++++++++++++++++++ modules/git/tests/repos/repo6_merge/HEAD | 1 + .../02/2f4ce6214973e018f02bf363bf8a2e3691f699 | Bin 0 -> 280 bytes .../05/45879290cc368a8becebc4aa34002c52d5fecc | Bin 0 -> 152 bytes .../1e/5d0a65fe099ef12d24b28f783896e4b8172576 | Bin 0 -> 88 bytes .../37/d35c7ed39e4e16d0b579a5b995b7e30b0e9411 | 2 ++ .../38/ec3e0cdc88bde01014bda4a5dd9fc835f41439 | 2 ++ .../66/7e0fbc6bc02c2285d17f542e89b23c0fa5482b | Bin 0 -> 122 bytes .../9f/d90b1d524c0fea776ed5e6476da02ea1740597 | Bin 0 -> 26 bytes .../ae/4b035e7c4afbc000576cee3f713ea0c2f1e1e2 | 5 ++++ .../ba/2906d0666cf726c7eaadd2cd3db615dedfdf3a | Bin 0 -> 20 bytes .../c1/a95c2eff8151c6d1437a0d5d3322a73ff38fb8 | Bin 0 -> 120 bytes .../cc/d1d4d594029e68c388ecef5aa3063fa1055831 | Bin 0 -> 26 bytes .../cd/fc1aaf7a149151cb7bff639fafe05668d4bbd2 | Bin 0 -> 120 bytes .../d1/792641396ff7630d35fbb0b74b86b0c71bca77 | Bin 0 -> 145 bytes .../ec/d11d8da0f25eaa99f64a37a82da98685f381e2 | Bin 0 -> 33 bytes .../fa/49b077972391ad58037050f2a75f74e3671e92 | Bin 0 -> 24 bytes .../tests/repos/repo6_merge/refs/heads/main | 1 + .../repo6_merge/refs/heads/merge/add_file | 1 + .../repo6_merge/refs/heads/merge/modify_file | 1 + .../repo6_merge/refs/heads/merge/remove_file | 1 + 22 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 modules/git/tests/repos/repo6_merge/HEAD create mode 100644 modules/git/tests/repos/repo6_merge/objects/02/2f4ce6214973e018f02bf363bf8a2e3691f699 create mode 100644 modules/git/tests/repos/repo6_merge/objects/05/45879290cc368a8becebc4aa34002c52d5fecc create mode 100644 modules/git/tests/repos/repo6_merge/objects/1e/5d0a65fe099ef12d24b28f783896e4b8172576 create mode 100644 modules/git/tests/repos/repo6_merge/objects/37/d35c7ed39e4e16d0b579a5b995b7e30b0e9411 create mode 100644 modules/git/tests/repos/repo6_merge/objects/38/ec3e0cdc88bde01014bda4a5dd9fc835f41439 create mode 100644 modules/git/tests/repos/repo6_merge/objects/66/7e0fbc6bc02c2285d17f542e89b23c0fa5482b create mode 100644 modules/git/tests/repos/repo6_merge/objects/9f/d90b1d524c0fea776ed5e6476da02ea1740597 create mode 100644 modules/git/tests/repos/repo6_merge/objects/ae/4b035e7c4afbc000576cee3f713ea0c2f1e1e2 create mode 100644 modules/git/tests/repos/repo6_merge/objects/ba/2906d0666cf726c7eaadd2cd3db615dedfdf3a create mode 100644 modules/git/tests/repos/repo6_merge/objects/c1/a95c2eff8151c6d1437a0d5d3322a73ff38fb8 create mode 100644 modules/git/tests/repos/repo6_merge/objects/cc/d1d4d594029e68c388ecef5aa3063fa1055831 create mode 100644 modules/git/tests/repos/repo6_merge/objects/cd/fc1aaf7a149151cb7bff639fafe05668d4bbd2 create mode 100644 modules/git/tests/repos/repo6_merge/objects/d1/792641396ff7630d35fbb0b74b86b0c71bca77 create mode 100644 modules/git/tests/repos/repo6_merge/objects/ec/d11d8da0f25eaa99f64a37a82da98685f381e2 create mode 100644 modules/git/tests/repos/repo6_merge/objects/fa/49b077972391ad58037050f2a75f74e3671e92 create mode 100644 modules/git/tests/repos/repo6_merge/refs/heads/main create mode 100644 modules/git/tests/repos/repo6_merge/refs/heads/merge/add_file create mode 100644 modules/git/tests/repos/repo6_merge/refs/heads/merge/modify_file create mode 100644 modules/git/tests/repos/repo6_merge/refs/heads/merge/remove_file diff --git a/modules/git/commit.go b/modules/git/commit.go index c44882d88617..b09be25ba099 100644 --- a/modules/git/commit.go +++ b/modules/git/commit.go @@ -495,7 +495,7 @@ func GetCommitFileStatus(ctx context.Context, repoPath, commitID string) (*Commi }() stderr := new(bytes.Buffer) - err := NewCommand(ctx, "log", "--name-status", "-c", "--pretty=format:", "--parents", "--no-renames", "-z", "-1").AddDynamicArguments(commitID).Run(&RunOpts{ + err := NewCommand(ctx, "log", "--name-status", "-m", "--pretty=format:", "--first-parent", "--no-renames", "-z", "-1").AddDynamicArguments(commitID).Run(&RunOpts{ Dir: repoPath, Stdout: w, Stderr: stderr, diff --git a/modules/git/commit_test.go b/modules/git/commit_test.go index acf4beb029fc..ac586fdf09d3 100644 --- a/modules/git/commit_test.go +++ b/modules/git/commit_test.go @@ -255,3 +255,26 @@ func TestParseCommitFileStatus(t *testing.T) { assert.Equal(t, kase.modified, fileStatus.Modified) } } + +func TestGetCommitFileStatusMerges(t *testing.T) { + bareRepo1Path := filepath.Join(testReposDir, "repo6_merge") + + commitFileStatus, err := GetCommitFileStatus(DefaultContext, bareRepo1Path, "022f4ce6214973e018f02bf363bf8a2e3691f699") + assert.NoError(t, err) + + expected := CommitFileStatus{ + []string{ + "add_file.txt", + }, + []string{ + "to_remove.txt", + }, + []string{ + "to_modify.txt", + }, + } + + assert.Equal(t, commitFileStatus.Added, expected.Added) + assert.Equal(t, commitFileStatus.Removed, expected.Removed) + assert.Equal(t, commitFileStatus.Modified, expected.Modified) +} diff --git a/modules/git/tests/repos/repo6_merge/HEAD b/modules/git/tests/repos/repo6_merge/HEAD new file mode 100644 index 000000000000..b870d82622c1 --- /dev/null +++ b/modules/git/tests/repos/repo6_merge/HEAD @@ -0,0 +1 @@ +ref: refs/heads/main diff --git a/modules/git/tests/repos/repo6_merge/objects/02/2f4ce6214973e018f02bf363bf8a2e3691f699 b/modules/git/tests/repos/repo6_merge/objects/02/2f4ce6214973e018f02bf363bf8a2e3691f699 new file mode 100644 index 0000000000000000000000000000000000000000..0778a1c88c74c272f3e597e6a2f5fabe07c52d43 GIT binary patch literal 280 zcmV+z0q6dB0gaGNP6IIvg;~!jvg?AMI*Ah}5<*;nGoaXU+7ZkQng%3pPlu`-R&3tp z`=0z{S*B_11~J|ARn^2u3XLVQfD`9ZsFqqlrzx}Z0<8p0j)#x3>fDW|>VuG0pB2@{ z3LGexz&Qsd^}wJRt-bn1k--Ng!E!zW0D+>Y`bO4k0ddW$Rd@A$Qt=8>B_*y37A$ft zSVFD2N%EngMW3(!d2MzPt#` e8{c(z_XlkKFTF5R=K2RuOC8(y1rI-BV~#m*q=*mz literal 0 HcmV?d00001 diff --git a/modules/git/tests/repos/repo6_merge/objects/05/45879290cc368a8becebc4aa34002c52d5fecc b/modules/git/tests/repos/repo6_merge/objects/05/45879290cc368a8becebc4aa34002c52d5fecc new file mode 100644 index 0000000000000000000000000000000000000000..c71794f3291ea726569ff279e1668d3ca58c694f GIT binary patch literal 152 zcmV;J0B8Sr0V^p=O;s>7wO}wbFfcPQQAkWliBHSSN!2T*HCtMa_7&)jnt=q)T^oels% GV?#3{$4L(W literal 0 HcmV?d00001 diff --git a/modules/git/tests/repos/repo6_merge/objects/1e/5d0a65fe099ef12d24b28f783896e4b8172576 b/modules/git/tests/repos/repo6_merge/objects/1e/5d0a65fe099ef12d24b28f783896e4b8172576 new file mode 100644 index 0000000000000000000000000000000000000000..365f368d35bc627422f9d91945304ddeaf5336ec GIT binary patch literal 88 zcmV-e0H^AXD~D{Ff%bx$W6@5(<`YcVc4a~b|Ee2yV~(rYcHL(-6neP u{(UQ`nv(qZ-29Zxv`VnTGZ(L1ox(IP<8a5D_fd=4>=&{|7y>!#1;p]xuyIwN4 \ No newline at end of file diff --git a/modules/git/tests/repos/repo6_merge/objects/38/ec3e0cdc88bde01014bda4a5dd9fc835f41439 b/modules/git/tests/repos/repo6_merge/objects/38/ec3e0cdc88bde01014bda4a5dd9fc835f41439 new file mode 100644 index 000000000000..582d98ce306f --- /dev/null +++ b/modules/git/tests/repos/repo6_merge/objects/38/ec3e0cdc88bde01014bda4a5dd9fc835f41439 @@ -0,0 +1,2 @@ +xM +0a9M2 U =yv/ 0:@$UѬ.[> lIsx 8'TRĤS,$x. ֚=n[נus!+9BGvfm ur> \ No newline at end of file diff --git a/modules/git/tests/repos/repo6_merge/objects/66/7e0fbc6bc02c2285d17f542e89b23c0fa5482b b/modules/git/tests/repos/repo6_merge/objects/66/7e0fbc6bc02c2285d17f542e89b23c0fa5482b new file mode 100644 index 0000000000000000000000000000000000000000..d7faff6bf2494bda69c5357c728dc7fa54e786e4 GIT binary patch literal 122 zcmV-=0EPc}0V^p=O;s>7G-oh0FfcPQQAkWliBHSSN!2T& \ No newline at end of file diff --git a/modules/git/tests/repos/repo6_merge/objects/ba/2906d0666cf726c7eaadd2cd3db615dedfdf3a b/modules/git/tests/repos/repo6_merge/objects/ba/2906d0666cf726c7eaadd2cd3db615dedfdf3a new file mode 100644 index 0000000000000000000000000000000000000000..edef2a7f0cb64247cd41bab23f6df7e63d599083 GIT binary patch literal 20 ccmb7v|unaFfcPQQOHfq%+o8WC}G&8$#x+v=eyeRS8FewwcRFq z@BV!&sG5@e_}u)I%(P0d!Z#OXdl!6)TQ&2Wm-!0am2It`8y_JQ7NzFqm!*Og&cDem a8|1_Psyy%NGxyvDdJ9Wfrvm__RW!bw20MlT literal 0 HcmV?d00001 diff --git a/modules/git/tests/repos/repo6_merge/objects/cc/d1d4d594029e68c388ecef5aa3063fa1055831 b/modules/git/tests/repos/repo6_merge/objects/cc/d1d4d594029e68c388ecef5aa3063fa1055831 new file mode 100644 index 0000000000000000000000000000000000000000..5449d726cd885825f9bde49b951215c8800bffd5 GIT binary patch literal 26 icmb7v|unaFfcPQQOHfq%+o8WC}G&8$#x+v=eyeRS8FewwcRFq z@BV!&sG5@e_}u)I%(P0d!ZR1IT%E!+FXM2>oA*(R+3XjxMi?R#7NzFqm!*Og&cDem a8|1_Psyy%NGxyvDdJ9Wfrvm_M1T#m|qdTYo literal 0 HcmV?d00001 diff --git a/modules/git/tests/repos/repo6_merge/objects/d1/792641396ff7630d35fbb0b74b86b0c71bca77 b/modules/git/tests/repos/repo6_merge/objects/d1/792641396ff7630d35fbb0b74b86b0c71bca77 new file mode 100644 index 0000000000000000000000000000000000000000..867e4c0b866fd796704426d6a221b2d68959b3ba GIT binary patch literal 145 zcmV;C0B-+y0gcW}3IZ_}wRp zijp0XDQ~@PJE*)A&eQAUG`RqU&y7SSnguJX{0|=G&M&X literal 0 HcmV?d00001 diff --git a/modules/git/tests/repos/repo6_merge/objects/ec/d11d8da0f25eaa99f64a37a82da98685f381e2 b/modules/git/tests/repos/repo6_merge/objects/ec/d11d8da0f25eaa99f64a37a82da98685f381e2 new file mode 100644 index 0000000000000000000000000000000000000000..52d300cdf184df35f4e5437d784a790ca37c2013 GIT binary patch literal 33 pcmb