Sync with Git 2.46.1
[git/gitster.git] / t / t4065-diff-anchored.sh
blob647537c12ea99db2594b1e29aa2c68027055d611
1 #!/bin/sh
3 test_description='anchored diff algorithm'
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
8 test_expect_success '--anchored' '
9 printf "a\nb\nc\n" >pre &&
10 printf "c\na\nb\n" >post &&
12 # normally, c is moved to produce the smallest diff
13 test_expect_code 1 git diff --no-index pre post >diff &&
14 grep "^+c" diff &&
16 # with anchor, a is moved
17 test_expect_code 1 git diff --no-index --anchored=c pre post >diff &&
18 grep "^+a" diff
21 test_expect_success '--anchored multiple' '
22 printf "a\nb\nc\nd\ne\nf\n" >pre &&
23 printf "c\na\nb\nf\nd\ne\n" >post &&
25 # with 1 anchor, c is not moved, but f is moved
26 test_expect_code 1 git diff --no-index --anchored=c pre post >diff &&
27 grep "^+a" diff && # a is moved instead of c
28 grep "^+f" diff &&
30 # with 2 anchors, c and f are not moved
31 test_expect_code 1 git diff --no-index --anchored=c --anchored=f pre post >diff &&
32 grep "^+a" diff &&
33 grep "^+d" diff # d is moved instead of f
36 test_expect_success '--anchored with nonexistent line has no effect' '
37 printf "a\nb\nc\n" >pre &&
38 printf "c\na\nb\n" >post &&
40 test_expect_code 1 git diff --no-index --anchored=x pre post >diff &&
41 grep "^+c" diff
44 test_expect_success '--anchored with non-unique line has no effect' '
45 printf "a\nb\nc\nd\ne\nc\n" >pre &&
46 printf "c\na\nb\nc\nd\ne\n" >post &&
48 test_expect_code 1 git diff --no-index --anchored=c pre post >diff &&
49 grep "^+c" diff
52 test_expect_success 'diff still produced with impossible multiple --anchored' '
53 printf "a\nb\nc\n" >pre &&
54 printf "c\na\nb\n" >post &&
56 test_expect_code 1 git diff --no-index --anchored=a --anchored=c pre post >diff &&
57 mv post expected_post &&
59 # Ensure that the diff is correct by applying it and then
60 # comparing the result with the original
61 git apply diff &&
62 diff expected_post post
65 test_expect_success 'later algorithm arguments override earlier ones' '
66 printf "a\nb\nc\n" >pre &&
67 printf "c\na\nb\n" >post &&
69 test_expect_code 1 git diff --no-index --patience --anchored=c pre post >diff &&
70 grep "^+a" diff &&
72 test_expect_code 1 git diff --no-index --anchored=c --patience pre post >diff &&
73 grep "^+c" diff &&
75 test_expect_code 1 git diff --no-index --histogram --anchored=c pre post >diff &&
76 grep "^+a" diff &&
78 test_expect_code 1 git diff --no-index --anchored=c --histogram pre post >diff &&
79 grep "^+c" diff
82 test_expect_success '--anchored works with other commands like "git show"' '
83 printf "a\nb\nc\n" >file &&
84 git add file &&
85 git commit -m foo &&
86 printf "c\na\nb\n" >file &&
87 git add file &&
88 git commit -m foo &&
90 # with anchor, a is moved
91 git show --patience --anchored=c >diff &&
92 grep "^+a" diff
95 test_done