summaryrefslogtreecommitdiff
path: root/t/t4072-diff-max-depth.sh
blob: 0fbf1321f760489687b64010b6ceb7f315fd54dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/bin/sh

test_description='check that diff --max-depth will limit recursion'
. ./test-lib.sh

make_dir() {
	mkdir -p "$1" &&
	echo "$2" >"$1/file"
}

make_files() {
	echo "$1" >file &&
	make_dir one "$1" &&
	make_dir one/two "$1" &&
	make_dir one/two/three "$1"
}

test_expect_success 'setup' '
	git commit --allow-empty -m empty &&
	git tag empty &&
	make_files added &&
	git add . &&
	git commit -m added &&
	make_files modified &&
	git add . &&
	git commit -m modified &&
	make_files index &&
	git add . &&
	make_files worktree
'

test_expect_success '--max-depth is disallowed with wildcard pathspecs' '
	test_must_fail git diff-tree --max-depth=0 HEAD^ HEAD -- "f*"
'

check_one() {
	type=$1; shift
	args=$1; shift
	path=$1; shift
	depth=$1; shift
	test_expect_${expect:-success} "diff-$type $args, path=$path, depth=$depth" "
		for i in $*; do echo \$i; done >expect &&
		git diff-$type --max-depth=$depth --name-only $args -- $path >actual &&
		test_cmp expect actual
	"
}

# For tree comparisons, we expect to see subtrees at the boundary
# get their own entry.
check_trees() {
	check_one tree "$*" '' 0 file one
	check_one tree "$*" '' 1 file one/file one/two
	check_one tree "$*" '' 2 file one/file one/two/file one/two/three
	check_one tree "$*" '' 3 file one/file one/two/file one/two/three/file
	check_one tree "$*" '' -1 file one/file one/two/file one/two/three/file
	check_one tree "$*" one 0 one
	check_one tree "$*" one 1 one/file one/two
	check_one tree "$*" one 2 one/file one/two/file one/two/three
	check_one tree "$*" one 3 one/file one/two/file one/two/three/file
	check_one tree "$*" one/two 0 one/two
	check_one tree "$*" one/two 1 one/two/file one/two/three
	check_one tree "$*" one/two 2 one/two/file one/two/three/file
	check_one tree "$*" one/two 2 one/two/file one/two/three/file
	check_one tree "$*" one/two/three 0 one/two/three
	check_one tree "$*" one/two/three 1 one/two/three/file
}

# But for index comparisons, we do not store subtrees at all, so we do not
# expect them.
check_index() {
	check_one "$@" '' 0 file
	check_one "$@" '' 1 file one/file
	check_one "$@" '' 2 file one/file one/two/file
	check_one "$@" '' 3 file one/file one/two/file one/two/three/file
	check_one "$@" one 0
	check_one "$@" one 1 one/file
	check_one "$@" one 2 one/file one/two/file
	check_one "$@" one 3 one/file one/two/file one/two/three/file
	check_one "$@" one/two 0
	check_one "$@" one/two 1 one/two/file
	check_one "$@" one/two 2 one/two/file one/two/three/file
	check_one "$@" one/two/three 0
	check_one "$@" one/two/three 1 one/two/three/file

	# Value '-1' for '--max-depth is the same as recursion without limit,
	# and thus should always succeed.
	local expect=
	check_one "$@" '' -1 file one/file one/two/file one/two/three/file
}

# Check as a modification...
check_trees HEAD^ HEAD
# ...and as an addition...
check_trees empty HEAD
# ...and as a deletion.
check_trees HEAD empty

# We currently only implement max-depth for trees.
expect=failure
# Check index against a tree
check_index index "--cached HEAD"
# and index against the worktree
check_index files ""
expect=

test_expect_success 'find shortest path within embedded pathspecs' '
	cat >expect <<-\EOF &&
	one/file
	one/two/file
	one/two/three/file
	EOF
	git diff-tree --max-depth=2 --name-only HEAD^ HEAD -- one one/two >actual &&
	test_cmp expect actual
'

test_done