Git 2.16
[alt-git.git] / t / t4065-diff-anchored.sh
blobb3f510f040ec3b955ff53c3de81ffc5a60ad3862
1 #!/bin/sh
3 test_description='anchored diff algorithm'
5 . ./test-lib.sh
7 test_expect_success '--anchored' '
8 printf "a\nb\nc\n" >pre &&
9 printf "c\na\nb\n" >post &&
11 # normally, c is moved to produce the smallest diff
12 test_expect_code 1 git diff --no-index pre post >diff &&
13 grep "^+c" diff &&
15 # with anchor, a is moved
16 test_expect_code 1 git diff --no-index --anchored=c pre post >diff &&
17 grep "^+a" diff
20 test_expect_success '--anchored multiple' '
21 printf "a\nb\nc\nd\ne\nf\n" >pre &&
22 printf "c\na\nb\nf\nd\ne\n" >post &&
24 # with 1 anchor, c is not moved, but f is moved
25 test_expect_code 1 git diff --no-index --anchored=c pre post >diff &&
26 grep "^+a" diff && # a is moved instead of c
27 grep "^+f" diff &&
29 # with 2 anchors, c and f are not moved
30 test_expect_code 1 git diff --no-index --anchored=c --anchored=f pre post >diff &&
31 grep "^+a" diff &&
32 grep "^+d" diff # d is moved instead of f
35 test_expect_success '--anchored with nonexistent line has no effect' '
36 printf "a\nb\nc\n" >pre &&
37 printf "c\na\nb\n" >post &&
39 test_expect_code 1 git diff --no-index --anchored=x pre post >diff &&
40 grep "^+c" diff
43 test_expect_success '--anchored with non-unique line has no effect' '
44 printf "a\nb\nc\nd\ne\nc\n" >pre &&
45 printf "c\na\nb\nc\nd\ne\n" >post &&
47 test_expect_code 1 git diff --no-index --anchored=c pre post >diff &&
48 grep "^+c" diff
51 test_expect_success 'diff still produced with impossible multiple --anchored' '
52 printf "a\nb\nc\n" >pre &&
53 printf "c\na\nb\n" >post &&
55 test_expect_code 1 git diff --no-index --anchored=a --anchored=c pre post >diff &&
56 mv post expected_post &&
58 # Ensure that the diff is correct by applying it and then
59 # comparing the result with the original
60 git apply diff &&
61 diff expected_post post
64 test_expect_success 'later algorithm arguments override earlier ones' '
65 printf "a\nb\nc\n" >pre &&
66 printf "c\na\nb\n" >post &&
68 test_expect_code 1 git diff --no-index --patience --anchored=c pre post >diff &&
69 grep "^+a" diff &&
71 test_expect_code 1 git diff --no-index --anchored=c --patience pre post >diff &&
72 grep "^+c" diff &&
74 test_expect_code 1 git diff --no-index --histogram --anchored=c pre post >diff &&
75 grep "^+a" diff &&
77 test_expect_code 1 git diff --no-index --anchored=c --histogram pre post >diff &&
78 grep "^+c" diff
81 test_expect_success '--anchored works with other commands like "git show"' '
82 printf "a\nb\nc\n" >file &&
83 git add file &&
84 git commit -m foo &&
85 printf "c\na\nb\n" >file &&
86 git add file &&
87 git commit -m foo &&
89 # with anchor, a is moved
90 git show --patience --anchored=c >diff &&
91 grep "^+a" diff
94 test_done