Skip to content

Commit ea5070c

Browse files
jlehmanngitster
authored andcommitted
Teach read-tree the -n|--dry-run option
The option can be used to check if read-tree with the same set of other options like "-m" and "-u" would succeed without actually changing either the index or the working tree. The relevant tests in the t10?? range were extended to do a read-tree -n before the real read-tree to make sure neither the index nor any local files were changed with -n and the same exit code as without -n is returned. The helper functions added for that purpose reside in the new t/lib-read-tree.sh file. The only exception is #13 in t1004 ("unlinking an un-unlink-able symlink"). As this is an issue of wrong directory permissions it is not detected with -n. Signed-off-by: Jens Lehmann <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2c9078d commit ea5070c

12 files changed

+198
-140
lines changed

Documentation/git-read-tree.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ OPTIONS
5353
trees that are not directly related to the current
5454
working tree status into a temporary index file.
5555

56+
-n::
57+
--dry-run::
58+
Check if the command would error out, without updating the index
59+
nor the files in the working tree for real.
60+
5661
-v::
5762
Show the progress of checking files out.
5863

builtin/read-tree.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
130130
PARSE_OPT_NONEG, exclude_per_directory_cb },
131131
OPT_SET_INT('i', NULL, &opts.index_only,
132132
"don't check the working tree after merging", 1),
133+
OPT__DRY_RUN(&opts.dry_run, "don't update the index or the work tree"),
133134
OPT_SET_INT(0, "no-sparse-checkout", &opts.skip_sparse_checkout,
134135
"skip applying sparse checkout filter", 1),
135136
OPT_SET_INT(0, "debug-unpack", &opts.debug_unpack,
@@ -219,7 +220,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
219220
if (unpack_trees(nr_trees, t, &opts))
220221
return 128;
221222

222-
if (opts.debug_unpack)
223+
if (opts.debug_unpack || opts.dry_run)
223224
return 0; /* do not write the index out */
224225

225226
/*

t/lib-read-tree.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/sh
2+
#
3+
# Helper functions to check if read-tree would succeed/fail as expected with
4+
# and without the dry-run option. They also test that the dry-run does not
5+
# write the index and that together with -u it doesn't touch the work tree.
6+
#
7+
read_tree_must_succeed () {
8+
git ls-files -s >pre-dry-run &&
9+
git read-tree -n "$@" &&
10+
git ls-files -s >post-dry-run &&
11+
test_cmp pre-dry-run post-dry-run &&
12+
git read-tree "$@"
13+
}
14+
15+
read_tree_must_fail () {
16+
git ls-files -s >pre-dry-run &&
17+
test_must_fail git read-tree -n "$@" &&
18+
git ls-files -s >post-dry-run &&
19+
test_cmp pre-dry-run post-dry-run &&
20+
test_must_fail git read-tree "$@"
21+
}
22+
23+
read_tree_u_must_succeed () {
24+
git ls-files -s >pre-dry-run &&
25+
git diff-files -p >pre-dry-run-wt &&
26+
git read-tree -n "$@" &&
27+
git ls-files -s >post-dry-run &&
28+
git diff-files -p >post-dry-run-wt &&
29+
test_cmp pre-dry-run post-dry-run &&
30+
test_cmp pre-dry-run-wt post-dry-run-wt &&
31+
git read-tree "$@"
32+
}
33+
34+
read_tree_u_must_fail () {
35+
git ls-files -s >pre-dry-run &&
36+
git diff-files -p >pre-dry-run-wt &&
37+
test_must_fail git read-tree -n "$@" &&
38+
git ls-files -s >post-dry-run &&
39+
git diff-files -p >post-dry-run-wt &&
40+
test_cmp pre-dry-run post-dry-run &&
41+
test_cmp pre-dry-run-wt post-dry-run-wt &&
42+
test_must_fail git read-tree "$@"
43+
}

0 commit comments

Comments
 (0)