]> git.ipfire.org Git - thirdparty/git.git/commitdiff
add --chmod: don't update index when --dry-run is used
authorMatheus Tavares <matheus.bernardino@usp.br>
Tue, 23 Feb 2021 01:10:33 +0000 (22:10 -0300)
committerJunio C Hamano <gitster@pobox.com>
Wed, 24 Feb 2021 20:14:51 +0000 (12:14 -0800)
`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>
builtin/add.c
t/t3700-add.sh

index a825887c503dd38450a5dfc5ca750b31e3db00f3..1e33ab81f2e80d8d66b404e5bc53334de4ca7311 100644 (file)
@@ -38,17 +38,23 @@ struct update_callback_data {
        int add_errors;
 };
 
-static void chmod_pathspec(struct pathspec *pathspec, char flip)
+static void chmod_pathspec(struct pathspec *pathspec, char flip, int show_only)
 {
        int i;
 
        for (i = 0; i < active_nr; i++) {
                struct cache_entry *ce = active_cache[i];
+               int err;
 
                if (pathspec && !ce_path_match(&the_index, ce, pathspec, NULL))
                        continue;
 
-               if (chmod_cache_entry(ce, flip) < 0)
+               if (!show_only)
+                       err = chmod_cache_entry(ce, flip);
+               else
+                       err = S_ISREG(ce->ce_mode) ? 0 : -1;
+
+               if (err < 0)
                        fprintf(stderr, "cannot chmod %cx '%s'\n", flip, ce->name);
        }
 }
@@ -609,7 +615,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
                exit_status |= add_files(&dir, flags);
 
        if (chmod_arg && pathspec.nr)
-               chmod_pathspec(&pathspec, chmod_arg[0]);
+               chmod_pathspec(&pathspec, chmod_arg[0], show_only);
        unplug_bulk_checkin();
 
 finish:
index b7d4ba608cbc96998fdac714ebb53a0f53c46f9e..fc81f2ef005877d5004b4176c198700d95e3dc33 100755 (executable)
@@ -386,6 +386,26 @@ test_expect_success POSIXPERM 'git add --chmod=[+-]x does not change the working
        ! test -x foo4
 '
 
+test_expect_success 'git add --chmod honors --dry-run' '
+       git reset --hard &&
+       echo foo >foo4 &&
+       git add foo4 &&
+       git add --chmod=+x --dry-run foo4 &&
+       test_mode_in_index 100644 foo4
+'
+
+test_expect_success 'git add --chmod --dry-run reports error for non regular files' '
+       git reset --hard &&
+       test_ln_s_add foo foo4 &&
+       git add --chmod=+x --dry-run foo4 2>stderr &&
+       grep "cannot chmod +x .foo4." stderr
+'
+
+test_expect_success 'git add --chmod --dry-run reports error for unmatched pathspec' '
+       test_must_fail git add --chmod=+x --dry-run nonexistent 2>stderr &&
+       test_i18ngrep "pathspec .nonexistent. did not match any files" stderr
+'
+
 test_expect_success 'no file status change if no pathspec is given' '
        >foo5 &&
        >foo6 &&