1#!/bin/sh
   2test_description='anchored diff algorithm'
   4. ./test-lib.sh
   6test_expect_success '--anchored' '
   8        printf "a\nb\nc\n" >pre &&
   9        printf "c\na\nb\n" >post &&
  10        # 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 &&
  14        # with anchor, a is moved
  16        test_expect_code 1 git diff --no-index --anchored=c pre post >diff &&
  17        grep "^+a" diff
  18'
  19test_expect_success '--anchored multiple' '
  21        printf "a\nb\nc\nd\ne\nf\n" >pre &&
  22        printf "c\na\nb\nf\nd\ne\n" >post &&
  23        # 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 &&
  28        # 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
  33'
  34test_expect_success '--anchored with nonexistent line has no effect' '
  36        printf "a\nb\nc\n" >pre &&
  37        printf "c\na\nb\n" >post &&
  38        test_expect_code 1 git diff --no-index --anchored=x pre post >diff &&
  40        grep "^+c" diff
  41'
  42test_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 &&
  46        test_expect_code 1 git diff --no-index --anchored=c pre post >diff &&
  48        grep "^+c" diff
  49'
  50test_expect_success 'diff still produced with impossible multiple --anchored' '
  52        printf "a\nb\nc\n" >pre &&
  53        printf "c\na\nb\n" >post &&
  54        test_expect_code 1 git diff --no-index --anchored=a --anchored=c pre post >diff &&
  56        mv post expected_post &&
  57        # 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
  62'
  63test_expect_success 'later algorithm arguments override earlier ones' '
  65        printf "a\nb\nc\n" >pre &&
  66        printf "c\na\nb\n" >post &&
  67        test_expect_code 1 git diff --no-index --patience --anchored=c pre post >diff &&
  69        grep "^+a" diff &&
  70        test_expect_code 1 git diff --no-index --anchored=c --patience pre post >diff &&
  72        grep "^+c" diff &&
  73        test_expect_code 1 git diff --no-index --histogram --anchored=c pre post >diff &&
  75        grep "^+a" diff &&
  76        test_expect_code 1 git diff --no-index --anchored=c --histogram pre post >diff &&
  78        grep "^+c" diff
  79'
  80test_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 &&
  88        # with anchor, a is moved
  90        git show --patience --anchored=c >diff &&
  91        grep "^+a" diff
  92'
  93test_done