add --chmod: don't update index when --dry-run is used
`git add --chmod` applies the mode changes even when `--dry-run` is used. Fix that and add some tests for this option combination. Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Matheus Tavares <matheus.bernardino@usp.br> Reviewed-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Matheus Tavares committed
Feb 22, 2021 at 22:10 UTC
c937d70bfb7661d5122e08758ae0531073a719ed
2 files changed
+29
-3
builtin/add.c
+9
-3
@@ -38,17 +38,23 @@ struct update_callback_data {
38
int add_errors;
39
};
40
41
-static void chmod_pathspec(struct pathspec *pathspec, char flip)
41
+static void chmod_pathspec(struct pathspec *pathspec, char flip, int show_only)
42
{
43
int i;
44
45
for (i = 0; i < active_nr; i++) {
46
struct cache_entry *ce = active_cache[i];
47
+ int err;
48
49
if (pathspec && !ce_path_match(&the_index, ce, pathspec, NULL))
50
continue;
51
51
- if (chmod_cache_entry(ce, flip) < 0)
52
+ if (!show_only)
53
+ err = chmod_cache_entry(ce, flip);
54
+ else
55
+ err = S_ISREG(ce->ce_mode) ? 0 : -1;
56
+
57
+ if (err < 0)
58
fprintf(stderr, "cannot chmod %cx '%s'\n", flip, ce->name);
59
}
60
}
@@ -609,7 +615,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
615
exit_status |= add_files(&dir, flags);
616
617
if (chmod_arg && pathspec.nr)
612
- chmod_pathspec(&pathspec, chmod_arg[0]);
618
+ chmod_pathspec(&pathspec, chmod_arg[0], show_only);
619
unplug_bulk_checkin();
620
621
finish:
t/t3700-add.sh
+20
@@ -386,6 +386,26 @@ test_expect_success POSIXPERM 'git add --chmod=[+-]x does not change the working
386
! test -x foo4
387
'
388
389
+test_expect_success 'git add --chmod honors --dry-run' '
390
+ git reset --hard &&
391
+ echo foo >foo4 &&
392
+ git add foo4 &&
393
+ git add --chmod=+x --dry-run foo4 &&
394
+ test_mode_in_index 100644 foo4
395
+'
396
+
397
+test_expect_success 'git add --chmod --dry-run reports error for non regular files' '
398
+ git reset --hard &&
399
+ test_ln_s_add foo foo4 &&
400
+ git add --chmod=+x --dry-run foo4 2>stderr &&
401
+ grep "cannot chmod +x .foo4." stderr
402
+'
403
+
404
+test_expect_success 'git add --chmod --dry-run reports error for unmatched pathspec' '
405
+ test_must_fail git add --chmod=+x --dry-run nonexistent 2>stderr &&
406
+ test_i18ngrep "pathspec .nonexistent. did not match any files" stderr
407
+'
408
+
409
test_expect_success 'no file status change if no pathspec is given' '
410
>foo5 &&
411
>foo6 &&