Skip to content

Commit f277234

Browse files
committed
Merge branch 'mt/add-chmod-fixes'
Various fixes on "git add --chmod". * mt/add-chmod-fixes: add: propagate --chmod errors to exit status add: mark --chmod error string for translation add --chmod: don't update index when --dry-run is used
2 parents 48923e8 + 9ebd7fe commit f277234

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

builtin/add.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,27 @@ struct update_callback_data {
3838
int add_errors;
3939
};
4040

41-
static void chmod_pathspec(struct pathspec *pathspec, char flip)
41+
static int chmod_pathspec(struct pathspec *pathspec, char flip, int show_only)
4242
{
43-
int i;
43+
int i, ret = 0;
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-
fprintf(stderr, "cannot chmod %cx '%s'\n", flip, ce->name);
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+
ret = error(_("cannot chmod %cx '%s'"), flip, ce->name);
5359
}
60+
61+
return ret;
5462
}
5563

5664
static int fix_unmerged_status(struct diff_filepair *p,
@@ -609,7 +617,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
609617
exit_status |= add_files(&dir, flags);
610618

611619
if (chmod_arg && pathspec.nr)
612-
chmod_pathspec(&pathspec, chmod_arg[0]);
620+
exit_status |= chmod_pathspec(&pathspec, chmod_arg[0], show_only);
613621
unplug_bulk_checkin();
614622

615623
finish:

t/t3700-add.sh

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,36 @@ 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 fails with non regular files (but updates the other paths)' '
390+
git reset --hard &&
391+
test_ln_s_add foo foo3 &&
392+
touch foo4 &&
393+
test_must_fail git add --chmod=+x foo3 foo4 2>stderr &&
394+
test_i18ngrep "cannot chmod +x .foo3." stderr &&
395+
test_mode_in_index 120000 foo3 &&
396+
test_mode_in_index 100755 foo4
397+
'
398+
399+
test_expect_success 'git add --chmod honors --dry-run' '
400+
git reset --hard &&
401+
echo foo >foo4 &&
402+
git add foo4 &&
403+
git add --chmod=+x --dry-run foo4 &&
404+
test_mode_in_index 100644 foo4
405+
'
406+
407+
test_expect_success 'git add --chmod --dry-run reports error for non regular files' '
408+
git reset --hard &&
409+
test_ln_s_add foo foo4 &&
410+
test_must_fail git add --chmod=+x --dry-run foo4 2>stderr &&
411+
test_i18ngrep "cannot chmod +x .foo4." stderr
412+
'
413+
414+
test_expect_success 'git add --chmod --dry-run reports error for unmatched pathspec' '
415+
test_must_fail git add --chmod=+x --dry-run nonexistent 2>stderr &&
416+
test_i18ngrep "pathspec .nonexistent. did not match any files" stderr
417+
'
418+
389419
test_expect_success 'no file status change if no pathspec is given' '
390420
>foo5 &&
391421
>foo6 &&
@@ -409,11 +439,17 @@ test_expect_success 'no file status change if no pathspec is given in subdir' '
409439
'
410440

411441
test_expect_success 'all statuses changed in folder if . is given' '
412-
rm -fr empty &&
413-
git add --chmod=+x . &&
414-
test $(git ls-files --stage | grep ^100644 | wc -l) -eq 0 &&
415-
git add --chmod=-x . &&
416-
test $(git ls-files --stage | grep ^100755 | wc -l) -eq 0
442+
git init repo &&
443+
(
444+
cd repo &&
445+
mkdir -p sub/dir &&
446+
touch x y z sub/a sub/dir/b &&
447+
git add -A &&
448+
git add --chmod=+x . &&
449+
test $(git ls-files --stage | grep ^100644 | wc -l) -eq 0 &&
450+
git add --chmod=-x . &&
451+
test $(git ls-files --stage | grep ^100755 | wc -l) -eq 0
452+
)
417453
'
418454

419455
test_expect_success CASE_INSENSITIVE_FS 'path is case-insensitive' '

0 commit comments

Comments
 (0)