Skip to content

Commit c937d70

Browse files
matheustavaresgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Matheus Tavares <[email protected]> Reviewed-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 966e671 commit c937d70

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

builtin/add.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,23 @@ struct update_callback_data {
3838
int add_errors;
3939
};
4040

41-
static void chmod_pathspec(struct pathspec *pathspec, char flip)
41+
static void chmod_pathspec(struct pathspec *pathspec, char flip, int show_only)
4242
{
4343
int i;
4444

4545
for (i = 0; i < active_nr; i++) {
4646
struct cache_entry *ce = active_cache[i];
47+
int err;
4748

4849
if (pathspec && !ce_path_match(&the_index, ce, pathspec, NULL))
4950
continue;
5051

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)
5258
fprintf(stderr, "cannot chmod %cx '%s'\n", flip, ce->name);
5359
}
5460
}
@@ -609,7 +615,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
609615
exit_status |= add_files(&dir, flags);
610616

611617
if (chmod_arg && pathspec.nr)
612-
chmod_pathspec(&pathspec, chmod_arg[0]);
618+
chmod_pathspec(&pathspec, chmod_arg[0], show_only);
613619
unplug_bulk_checkin();
614620

615621
finish:

t/t3700-add.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,26 @@ test_expect_success POSIXPERM 'git add --chmod=[+-]x does not change the working
386386
! test -x foo4
387387
'
388388

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+
389409
test_expect_success 'no file status change if no pathspec is given' '
390410
>foo5 &&
391411
>foo6 &&

0 commit comments

Comments
 (0)