git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 14/33] directory rename detection: tests for handling overwriting dirty files

From
Elijah Newren <newren@gmail.com>
Date
Nov 21, 2017, 08:00 UTC
Message-ID
<20171121080059.32304-15-newren@gmail.com>
In-Reply-To
<20171121080059.32304-1-newren@gmail.com>
Signed-off-by: Elijah Newren <newren@gmail.com>
---
 t/t6043-merge-rename-directories.sh | 436 ++++++++++++++++++++++++++++++++++++
 1 file changed, 436 insertions(+)
diff --git a/t/t6043-merge-rename-directories.sh b/t/t6043-merge-rename-directories.sh
index 7408c788fc..88243651f7 100755
--- a/t/t6043-merge-rename-directories.sh
+++ b/t/t6043-merge-rename-directories.sh
@@ -3103,4 +3103,440 @@ test_expect_failure '10e-check: Does git complain about untracked file that is n
 	)
 '
 
+###########################################################################
+# SECTION 11: Handling dirty (not up-to-date) files
+#
+# unpack_trees(), upon which the recursive merge algorithm is based, aborts
+# the operation if untracked or dirty files would be deleted or overwritten
+# by the merge.  Unfortunately, unpack_trees() does not understand renames,
+# and if it doesn't abort, then it muddies up the working directory before
+# we even get to the point of detecting renames, so we need some special
+# handling.  This was true even of normal renames, but there are additional
+# codepaths that need special handling with directory renames.  Add
+# testcases for both renamed-by-directory-rename-detection and standard
+# rename cases.
+###########################################################################
+
+# Testcase 11a, Avoid losing dirty contents with simple rename
+#   Commit O: z/{a,b_v1},
+#   Commit A: z/{a,c_v1}, and z/c_v1 has uncommitted mods
+#   Commit B: z/{a,b_v2}
+#   Expected: ERROR_MSG(Refusing to lose dirty file at z/c) +
+#             z/a, staged version of z/c has sha1sum matching B:z/b_v2,
+#             z/c~HEAD with contents of B:z/b_v2,
+#             z/c with uncommitted mods on top of A:z/c_v1
+
+test_expect_success '11a-setup: Avoid losing dirty contents with simple rename' '
+	test_create_repo 11a &&
+	(
+		cd 11a &&
+
+		mkdir z &&
+		echo a >z/a &&
+		test_seq 1 10 >z/b &&
+		git add z &&
+		test_tick &&
+		git commit -m "O" &&
+
+		git branch O &&
+		git branch A &&
+		git branch B &&
+
+		git checkout A &&
+		git mv z/b z/c &&
+		test_tick &&
+		git commit -m "A" &&
+
+		git checkout B &&
+		echo 11 >>z/b &&
+		git add z/b &&
+		test_tick &&
+		git commit -m "B"
+	)
+'
+
+test_expect_failure '11a-check: Avoid losing dirty contents with simple rename' '
+	(
+		cd 11a &&
+
+		git checkout A^0 &&
+		echo stuff >>z/c &&
+
+		test_must_fail git merge -s recursive B^0 >out 2>err &&
+		test_i18ngrep "Refusing to lose dirty file at z/c" out &&
+
+		test_seq 1 10 >expected &&
+		echo stuff >>expected &&
+
+		test 2 -eq $(git ls-files -s | wc -l) &&
+		test 1 -eq $(git ls-files -u | wc -l) &&
+		test 4 -eq $(git ls-files -o | wc -l) &&
+
+		git rev-parse >actual \
+			:0:z/a :2:z/c &&
+		git rev-parse >expect \
+			O:z/a B:z/b &&
+		test_cmp expect actual &&
+
+		test "$(git hash-object z/c~HEAD)" = $(git rev-parse B:z/b) &&
+		test_cmp expected z/c
+	)
+'
+
+# Testcase 11b, Avoid losing dirty file involved in directory rename
+#   Commit O: z/a,         x/{b,c_v1}
+#   Commit A: z/{a,c_v1},  x/b,       and z/c_v1 has uncommitted mods
+#   Commit B: y/a,         x/{b,c_v2}
+#   Expected: y/{a,c_v2}, x/b, z/c_v1 with uncommitted mods untracked,
+#             ERROR_MSG(Refusing to lose dirty file at z/c)
+
+
+test_expect_success '11b-setup: Avoid losing dirty file involved in directory rename' '
+	test_create_repo 11b &&
+	(
+		cd 11b &&
+
+		mkdir z x &&
+		echo a >z/a &&
+		echo b >x/b &&
+		test_seq 1 10 >x/c &&
+		git add z x &&
+		test_tick &&
+		git commit -m "O" &&
+
+		git branch O &&
+		git branch A &&
+		git branch B &&
+
+		git checkout A &&
+		git mv x/c z/c &&
+		test_tick &&
+		git commit -m "A" &&
+
+		git checkout B &&
+		git mv z y &&
+		echo 11 >>x/c &&
+		git add x/c &&
+		test_tick &&
+		git commit -m "B"
+	)
+'
+
+test_expect_failure '11b-check: Avoid losing dirty file involved in directory rename' '
+	(
+		cd 11b &&
+
+		git checkout A^0 &&
+		echo stuff >>z/c &&
+
+		git merge -s recursive B^0 >out 2>err &&
+		test_i18ngrep "Refusing to lose dirty file at z/c" out &&
+
+		grep -q stuff */* &&
+		test_seq 1 10 >expected &&
+		echo stuff >>expected &&
+
+		test 3 -eq $(git ls-files -s | wc -l) &&
+		test 0 -eq $(git ls-files -u | wc -l) &&
+		test 0 -eq $(git ls-files -m | wc -l) &&
+		test 4 -eq $(git ls-files -o | wc -l) &&
+
+		git rev-parse >actual \
+			:0:x/b :0:y/a :0:y/c &&
+		git rev-parse >expect \
+			O:x/b O:z/a B:x/c &&
+		test_cmp expect actual &&
+
+		test "$(git hash-object y/c)" = $(git rev-parse B:x/c) &&
+		test_cmp expected z/c
+	)
+'
+
+# Testcase 11c, Avoid losing not-up-to-date with rename + D/F conflict
+#   Commit O: y/a,         x/{b,c_v1}
+#   Commit A: y/{a,c_v1},  x/b,       and y/c_v1 has uncommitted mods
+#   Commit B: y/{a,c/d},   x/{b,c_v2}
+#   Expected: Abort_msg("following files would be overwritten by merge") +
+#             y/c left untouched (still has uncommitted mods)
+
+test_expect_success '11c-setup: Avoid losing not-uptodate with rename + D/F conflict' '
+	test_create_repo 11c &&
+	(
+		cd 11c &&
+
+		mkdir y x &&
+		echo a >y/a &&
+		echo b >x/b &&
+		test_seq 1 10 >x/c &&
+		git add y x &&
+		test_tick &&
+		git commit -m "O" &&
+
+		git branch O &&
+		git branch A &&
+		git branch B &&
+
+		git checkout A &&
+		git mv x/c y/c &&
+		test_tick &&
+		git commit -m "A" &&
+
+		git checkout B &&
+		mkdir y/c &&
+		echo d >y/c/d &&
+		echo 11 >>x/c &&
+		git add x/c y/c/d &&
+		test_tick &&
+		git commit -m "B"
+	)
+'
+
+test_expect_success '11c-check: Avoid losing not-uptodate with rename + D/F conflict' '
+	(
+		cd 11c &&
+
+		git checkout A^0 &&
+		echo stuff >>y/c &&
+
+		test_must_fail git merge -s recursive B^0 >out 2>err &&
+		test_i18ngrep "following files would be overwritten by merge" err &&
+
+		grep -q stuff */* &&
+		test_seq 1 10 >expected &&
+		echo stuff >>expected &&
+
+		test 3 -eq $(git ls-files -s | wc -l) &&
+		test 0 -eq $(git ls-files -u | wc -l) &&
+		test 1 -eq $(git ls-files -m | wc -l) &&
+		test 3 -eq $(git ls-files -o | wc -l) &&
+
+		test $(git rev-parse HEAD) = $(git rev-parse A) &&
+		test_cmp expected y/c
+	)
+'
+
+# Testcase 11d, Avoid losing not-up-to-date with rename + D/F conflict
+#   Commit O: z/a,         x/{b,c_v1}
+#   Commit A: z/{a,c_v1},  x/b,       and z/c_v1 has uncommitted mods
+#   Commit B: y/{a,c/d},   x/{b,c_v2}
+#   Expected: D/F: y/c_v2 vs y/c/d) +
+#             Warning_Msg("Refusing to lose dirty file at z/c) +
+#             y/{a,c~HEAD,c/d}, x/b, now-untracked z/c_v1 with uncommitted mods
+
+test_expect_success '11d-setup: Avoid losing not-uptodate with rename + D/F conflict' '
+	test_create_repo 11d &&
+	(
+		cd 11d &&
+
+		mkdir z x &&
+		echo a >z/a &&
+		echo b >x/b &&
+		test_seq 1 10 >x/c &&
+		git add z x &&
+		test_tick &&
+		git commit -m "O" &&
+
+		git branch O &&
+		git branch A &&
+		git branch B &&
+
+		git checkout A &&
+		git mv x/c z/c &&
+		test_tick &&
+		git commit -m "A" &&
+
+		git checkout B &&
+		git mv z y &&
+		mkdir y/c &&
+		echo d >y/c/d &&
+		echo 11 >>x/c &&
+		git add x/c y/c/d &&
+		test_tick &&
+		git commit -m "B"
+	)
+'
+
+test_expect_failure '11d-check: Avoid losing not-uptodate with rename + D/F conflict' '
+	(
+		cd 11d &&
+
+		git checkout A^0 &&
+		echo stuff >>z/c &&
+
+		test_must_fail git merge -s recursive B^0 >out 2>err &&
+		test_i18ngrep "Refusing to lose dirty file at z/c" out &&
+
+		grep -q stuff */* &&
+		test_seq 1 10 >expected &&
+		echo stuff >>expected &&
+
+		test 4 -eq $(git ls-files -s | wc -l) &&
+		test 1 -eq $(git ls-files -u | wc -l) &&
+		test 5 -eq $(git ls-files -o | wc -l) &&
+
+		git rev-parse >actual \
+			:0:x/b :0:y/a :0:y/c/d :3:y/c &&
+		git rev-parse >expect \
+			O:x/b O:z/a B:y/c/d B:x/c &&
+		test_cmp expect actual &&
+
+		test "$(git hash-object y/c~HEAD)" = $(git rev-parse B:x/c) &&
+		test_cmp expected z/c
+	)
+'
+
+# Testcase 11e, Avoid deleting not-up-to-date with dir rename/rename(1to2)/add
+#   Commit O: z/{a,b},      x/{c_1,d}
+#   Commit A: y/{a,b,c_2},  x/d, w/c_1, and y/c_2 has uncommitted mods
+#   Commit B: z/{a,b,c_1},  x/d
+#   Expected: Failed Merge; y/{a,b} + x/d +
+#             CONFLICT(rename/rename) x/c_1 -> w/c_1 vs y/c_1 +
+#             ERROR_MSG(Refusing to lose dirty file at y/c)
+#             y/c~B^0 has O:x/c_1 contents
+#             y/c~HEAD has A:y/c_2 contents
+#             y/c has dirty file from before merge
+
+test_expect_success '11e-setup: Avoid deleting not-uptodate with dir rename/rename(1to2)/add' '
+	test_create_repo 11e &&
+	(
+		cd 11e &&
+
+		mkdir z x &&
+		echo a >z/a &&
+		echo b >z/b &&
+		echo c >x/c &&
+		echo d >x/d &&
+		git add z x &&
+		test_tick &&
+		git commit -m "O" &&
+
+		git branch O &&
+		git branch A &&
+		git branch B &&
+
+		git checkout A &&
+		git mv z/ y/ &&
+		echo different >y/c &&
+		mkdir w &&
+		git mv x/c w/ &&
+		git add y/c &&
+		test_tick &&
+		git commit -m "A" &&
+
+		git checkout B &&
+		git mv x/c z/ &&
+		test_tick &&
+		git commit -m "B"
+	)
+'
+
+test_expect_failure '11e-check: Avoid deleting not-uptodate with dir rename/rename(1to2)/add' '
+	(
+		cd 11e &&
+
+		git checkout A^0 &&
+		echo mods >>y/c &&
+
+		test_must_fail git merge -s recursive B^0 >out 2>err &&
+		test_i18ngrep "CONFLICT (rename/rename)" out &&
+		test_i18ngrep "Refusing to lose dirty file at y/c" out &&
+
+		test 7 -eq $(git ls-files -s | wc -l) &&
+		test 4 -eq $(git ls-files -u | wc -l) &&
+		test 4 -eq $(git ls-files -o | wc -l) &&
+
+		echo different >expected &&
+		echo mods >>expected &&
+
+		git rev-parse >actual \
+			:0:y/a :0:y/b :0:x/d :1:x/c :2:w/c :2:y/c :3:y/c &&
+		git rev-parse >expect \
+			O:z/a O:z/b O:x/d O:x/c O:x/c A:y/c O:x/c &&
+		test_cmp expect actual &&
+
+		git hash-object >actual \
+			y/c~B^0 y/c~HEAD &&
+		git rev-parse >expect \
+			O:x/c A:y/c &&
+		test_cmp expect actual &&
+
+		test_cmp expected y/c
+	)
+'
+
+# Testcase 11f, Avoid deleting not-up-to-date w/ dir rename/rename(2to1)
+#   Commit O: z/{a,b},        x/{c_1,d_2}
+#   Commit A: y/{a,b,wham_1}, x/d_2, except y/wham has uncommitted mods
+#   Commit B: z/{a,b,wham_2}, x/c_1
+#   Expected: Failed Merge; y/{a,b} + untracked y/{wham~B^0,wham~B^HEAD} +
+#             y/wham with dirty changes from before merge +
+#             CONFLICT(rename/rename) x/c vs x/d -> y/wham
+#             ERROR_MSG(Refusing to lose dirty file at y/wham)
+
+test_expect_success '11f-setup: Avoid deleting not-uptodate with dir rename/rename(2to1)' '
+	test_create_repo 11f &&
+	(
+		cd 11f &&
+
+		mkdir z x &&
+		echo a >z/a &&
+		echo b >z/b &&
+		test_seq 1 10 >x/c &&
+		echo d >x/d &&
+		git add z x &&
+		test_tick &&
+		git commit -m "O" &&
+
+		git branch O &&
+		git branch A &&
+		git branch B &&
+
+		git checkout A &&
+		git mv z/ y/ &&
+		git mv x/c y/wham &&
+		test_tick &&
+		git commit -m "A" &&
+
+		git checkout B &&
+		git mv x/d z/wham &&
+		test_tick &&
+		git commit -m "B"
+	)
+'
+
+test_expect_failure '11f-check: Avoid deleting not-uptodate with dir rename/rename(2to1)' '
+	(
+		cd 11f &&
+
+		git checkout A^0 &&
+		echo important >>y/wham &&
+
+		test_must_fail git merge -s recursive B^0 >out 2>err &&
+		test_i18ngrep "CONFLICT (rename/rename)" out &&
+		test_i18ngrep "Refusing to lose dirty file at y/wham" out &&
+
+		test 4 -eq $(git ls-files -s | wc -l) &&
+		test 2 -eq $(git ls-files -u | wc -l) &&
+		test 4 -eq $(git ls-files -o | wc -l) &&
+
+		test_seq 1 10 >expected &&
+		echo important >>expected &&
+
+		git rev-parse >actual \
+			:0:y/a :0:y/b :2:y/wham :3:y/wham &&
+		git rev-parse >expect \
+			O:z/a O:z/b O:x/c O:x/d &&
+		test_cmp expect actual &&
+
+		test_must_fail git rev-parse :1:y/wham &&
+
+		test_cmp expected y/wham &&
+		git hash-object >actual \
+			y/wham~B^0 y/wham~HEAD &&
+		git rev-parse >expect \
+			O:x/d O:x/c &&
+		test_cmp expect actual
+	)
+'
+
 test_done
-- 
2.15.0.309.g62ce55426d
Previous: Elijah NewrenNext: Elijah Newren
Message 33 of 48 in “Add directory rename detection to git”
  1. 00/33 Add directory rename detection to gitElijah Newren, Nov 21, 2017
  2. 10/33 directory rename detection: more involved edge/corner testcasesElijah Newren, Nov 21, 2017
  3. 21/33 merge-recursive: add get_directory_renames()Elijah Newren, Nov 21, 2017
  4. Johannes SchindelinNov 26, 2017
  5. Elijah NewrenNov 26, 2017
  6. 06/33 directory rename detection: testcases to avoid taking detection too farElijah Newren, Nov 21, 2017
  7. 15/33 merge-recursive: move the get_renames() functionElijah Newren, Nov 21, 2017
  8. 29/33 merge-recursive: fix overwriting dirty files involved in renamesElijah Newren, Nov 21, 2017
  9. 03/33 merge-recursive: add explanation for src_entry and dst_entryElijah Newren, Nov 21, 2017
  10. 22/33 merge-recursive: check for directory level conflictsElijah Newren, Nov 21, 2017
  11. Stefan BellerNov 22, 2017
  12. 25/33 merge-recursive: check for file level conflicts then get new nameElijah Newren, Nov 21, 2017
  13. Stefan BellerNov 22, 2017
  14. 24/33 merge-recursive: add computation of collisions due to dir rename & mergingElijah Newren, Nov 21, 2017
  15. 04/33 directory rename detection: basic testcasesElijah Newren, Nov 21, 2017
  16. 05/33 directory rename detection: directory splitting testcasesElijah Newren, Nov 21, 2017
  17. 20/33 merge-recursive: add a new hashmap for storing directory renamesElijah Newren, Nov 21, 2017
  18. 18/33 merge-recursive: make !o->detect_rename codepath more obviousElijah Newren, Nov 21, 2017
  19. 02/33 merge-recursive: fix logic ordering issueElijah Newren, Nov 21, 2017
  20. 19/33 merge-recursive: split out code for determining diff_filepairsElijah Newren, Nov 21, 2017
  21. 31/33 directory rename detection: new testcases showcasing a pair of bugsElijah Newren, Nov 21, 2017
  22. 11/33 directory rename detection: testcases exploring possibly suboptimal mergesElijah Newren, Nov 21, 2017
  23. Stefan BellerNov 22, 2017
  24. 23/33 merge-recursive: add a new hashmap for storing file collisionsElijah Newren, Nov 21, 2017
  25. 09/33 directory rename detection: testcases checking which side did the renameElijah Newren, Nov 21, 2017
  26. 16/33 merge-recursive: introduce new functions to handle rename logicElijah Newren, Nov 21, 2017
  27. 13/33 directory rename detection: tests for handling overwriting untracked filesElijah Newren, Nov 21, 2017
  28. 26/33 merge-recursive: when comparing files, don't include treesElijah Newren, Nov 21, 2017
  29. 28/33 merge-recursive: avoid clobbering untracked files with directory renamesElijah Newren, Nov 21, 2017
  30. 27/33 merge-recursive: apply necessary modifications for directory renamesElijah Newren, Nov 21, 2017
  31. 33/33 merge-recursive: ensure we write updates for directory-renamed fileElijah Newren, Nov 21, 2017
  32. 32/33 merge-recursive: avoid spurious rename/rename conflict from dir renamesElijah Newren, Nov 21, 2017
  33. 14/33 directory rename detection: tests for handling overwriting dirty filesElijah Newren, Nov 21, 2017
  34. 12/33 directory rename detection: miscellaneous testcases to complete coverageElijah Newren, Nov 21, 2017
  35. 17/33 merge-recursive: fix leaks of allocated renames and diff_filepairsElijah Newren, Nov 21, 2017
  36. 08/33 directory rename detection: files/directories in the way of some renamesElijah Newren, Nov 21, 2017
  37. 07/33 directory rename detection: partially renamed directory testcase/discussionElijah Newren, Nov 21, 2017
  38. 01/33 Tighten and correct a few testcases for merging and cherry-pickingElijah Newren, Nov 21, 2017
  39. 30/33 merge-recursive: fix remaining directory rename + dirty overwrite casesElijah Newren, Nov 21, 2017
  40. Stefan BellerNov 22, 2017
  41. Elijah NewrenNov 22, 2017
  42. Junio C HamanoNov 22, 2017
  43. Stefan BellerNov 22, 2017
  44. Elijah NewrenNov 23, 2017
  45. Adam DinwoodieNov 23, 2017
  46. Elijah NewrenNov 23, 2017
  47. Elijah NewrenNov 24, 2017
  48. Elijah NewrenNov 24, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.