add: add --chmod=+x / --chmod=-x options

The executable bit will not be detected (and therefore will not be set) for paths in a repository with `core.filemode` set to false, though the users may still wish to add files as executable for compatibility with other users who _do_ have `core.filemode` functionality. For example, Windows users adding shell scripts may wish to add them as executable for compatibility with users on non-Windows. Although this can be done with a plumbing command (`git update-index --add --chmod=+x foo`), teaching the `git-add` command allows users to set a file executable with a command that they're already familiar with. Signed-off-by: Edward Thomson <ethomson@edwardthomson.com> Helped-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Edward Thomson committed May 31, 2016 at 17:08 UTC 4e55ed32db81d06a4f618e2cc0f9da0e223ae304
6 files changed +68 -20
builtin/add.c
+24 -9
@@ -26,7 +26,7 @@ static int patch_interactive, add_interactive, edit_interactive;
26 static int take_worktree_changes;
27
28 struct update_callback_data {
29 - int flags;
29 + int flags, force_mode;
30 int add_errors;
31 };
32
@@ -65,7 +65,8 @@ static void update_callback(struct diff_queue_struct *q,
65 die(_("unexpected diff status %c"), p->status);
66 case DIFF_STATUS_MODIFIED:
67 case DIFF_STATUS_TYPE_CHANGED:
68 - if (add_file_to_index(&the_index, path, data->flags)) {
68 + if (add_file_to_index(&the_index, path,
69 + data->flags, data->force_mode)) {
70 if (!(data->flags & ADD_CACHE_IGNORE_ERRORS))
71 die(_("updating files failed"));
72 data->add_errors++;
@@ -83,14 +84,15 @@ static void update_callback(struct diff_queue_struct *q,
84 }
85 }
86
86 -int add_files_to_cache(const char *prefix,
87 - const struct pathspec *pathspec, int flags)
87 +int add_files_to_cache(const char *prefix, const struct pathspec *pathspec,
88 + int flags, int force_mode)
89 {
90 struct update_callback_data data;
91 struct rev_info rev;
92
93 memset(&data, 0, sizeof(data));
94 data.flags = flags;
95 + data.force_mode = force_mode;
96
97 init_revisions(&rev, prefix);
98 setup_revisions(0, NULL, &rev, NULL);
@@ -238,6 +240,8 @@ static int ignore_add_errors, intent_to_add, ignore_missing;
240 static int addremove = ADDREMOVE_DEFAULT;
241 static int addremove_explicit = -1; /* unspecified */
242
243 +static char *chmod_arg;
244 +
245 static int ignore_removal_cb(const struct option *opt, const char *arg, int unset)
246 {
247 /* if we are told to ignore, we are not adding removals */
@@ -263,6 +267,7 @@ static struct option builtin_add_options[] = {
267 OPT_BOOL( 0 , "refresh", &refresh_only, N_("don't add, only refresh the index")),
268 OPT_BOOL( 0 , "ignore-errors", &ignore_add_errors, N_("just skip files which cannot be added because of errors")),
269 OPT_BOOL( 0 , "ignore-missing", &ignore_missing, N_("check if - even missing - files are ignored in dry run")),
270 + OPT_STRING( 0 , "chmod", &chmod_arg, N_("(+/-)x"), N_("override the executable bit of the listed files")),
271 OPT_END(),
272 };
273
@@ -276,7 +281,7 @@ static int add_config(const char *var, const char *value, void *cb)
281 return git_default_config(var, value, cb);
282 }
283
279 -static int add_files(struct dir_struct *dir, int flags)
284 +static int add_files(struct dir_struct *dir, int flags, int force_mode)
285 {
286 int i, exit_status = 0;
287
@@ -289,7 +294,8 @@ static int add_files(struct dir_struct *dir, int flags)
294 }
295
296 for (i = 0; i < dir->nr; i++)
292 - if (add_file_to_cache(dir->entries[i]->name, flags)) {
297 + if (add_file_to_index(&the_index, dir->entries[i]->name,
298 + flags, force_mode)) {
299 if (!ignore_add_errors)
300 die(_("adding files failed"));
301 exit_status = 1;
@@ -302,7 +308,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
308 int exit_status = 0;
309 struct pathspec pathspec;
310 struct dir_struct dir;
305 - int flags;
311 + int flags, force_mode;
312 int add_new_files;
313 int require_pathspec;
314 char *seen = NULL;
@@ -336,6 +342,15 @@ int cmd_add(int argc, const char **argv, const char *prefix)
342 if (!show_only && ignore_missing)
343 die(_("Option --ignore-missing can only be used together with --dry-run"));
344
345 + if (!chmod_arg)
346 + force_mode = 0;
347 + else if (!strcmp(chmod_arg, "-x"))
348 + force_mode = 0666;
349 + else if (!strcmp(chmod_arg, "+x"))
350 + force_mode = 0777;
351 + else
352 + die(_("--chmod param '%s' must be either -x or +x"), chmod_arg);
353 +
354 add_new_files = !take_worktree_changes && !refresh_only;
355 require_pathspec = !(take_worktree_changes || (0 < addremove_explicit));
356
@@ -426,10 +441,10 @@ int cmd_add(int argc, const char **argv, const char *prefix)
441
442 plug_bulk_checkin();
443
429 - exit_status |= add_files_to_cache(prefix, &pathspec, flags);
444 + exit_status |= add_files_to_cache(prefix, &pathspec, flags, force_mode);
445
446 if (add_new_files)
432 - exit_status |= add_files(&dir, flags);
447 + exit_status |= add_files(&dir, flags, force_mode);
448
449 unplug_bulk_checkin();
450
builtin/checkout.c
+1 -1
@@ -548,7 +548,7 @@ static int merge_working_tree(const struct checkout_opts *opts,
548 * entries in the index.
549 */
550
551 - add_files_to_cache(NULL, NULL, 0);
551 + add_files_to_cache(NULL, NULL, 0, 0);
552 /*
553 * NEEDSWORK: carrying over local changes
554 * when branches have different end-of-line
builtin/commit.c
+1 -1
@@ -386,7 +386,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
386 */
387 if (all || (also && pathspec.nr)) {
388 hold_locked_index(&index_lock, 1);
389 - add_files_to_cache(also ? prefix : NULL, &pathspec, 0);
389 + add_files_to_cache(also ? prefix : NULL, &pathspec, 0, 0);
390 refresh_cache_or_die(refresh_flags);
391 update_main_cache_tree(WRITE_TREE_SILENT);
392 if (write_locked_index(&the_index, &index_lock, CLOSE_LOCK))
cache.h
+5 -5
@@ -367,8 +367,8 @@ extern void free_name_hash(struct index_state *istate);
367 #define rename_cache_entry_at(pos, new_name) rename_index_entry_at(&the_index, (pos), (new_name))
368 #define remove_cache_entry_at(pos) remove_index_entry_at(&the_index, (pos))
369 #define remove_file_from_cache(path) remove_file_from_index(&the_index, (path))
370 -#define add_to_cache(path, st, flags) add_to_index(&the_index, (path), (st), (flags))
371 -#define add_file_to_cache(path, flags) add_file_to_index(&the_index, (path), (flags))
370 +#define add_to_cache(path, st, flags) add_to_index(&the_index, (path), (st), (flags), 0)
371 +#define add_file_to_cache(path, flags) add_file_to_index(&the_index, (path), (flags), 0)
372 #define refresh_cache(flags) refresh_index(&the_index, (flags), NULL, NULL, NULL)
373 #define ce_match_stat(ce, st, options) ie_match_stat(&the_index, (ce), (st), (options))
374 #define ce_modified(ce, st, options) ie_modified(&the_index, (ce), (st), (options))
@@ -581,8 +581,8 @@ extern int remove_file_from_index(struct index_state *, const char *path);
581 #define ADD_CACHE_IGNORE_ERRORS 4
582 #define ADD_CACHE_IGNORE_REMOVAL 8
583 #define ADD_CACHE_INTENT 16
584 -extern int add_to_index(struct index_state *, const char *path, struct stat *, int flags);
585 -extern int add_file_to_index(struct index_state *, const char *path, int flags);
584 +extern int add_to_index(struct index_state *, const char *path, struct stat *, int flags, int force_mode);
585 +extern int add_file_to_index(struct index_state *, const char *path, int flags, int force_mode);
586 extern struct cache_entry *make_cache_entry(unsigned int mode, const unsigned char *sha1, const char *path, int stage, unsigned int refresh_options);
587 extern int ce_same_name(const struct cache_entry *a, const struct cache_entry *b);
588 extern void set_object_name_for_intent_to_add_entry(struct cache_entry *ce);
@@ -1772,7 +1772,7 @@ void packet_trace_identity(const char *prog);
1772 * return 0 if success, 1 - if addition of a file failed and
1773 * ADD_FILES_IGNORE_ERRORS was specified in flags
1774 */
1775 -int add_files_to_cache(const char *prefix, const struct pathspec *pathspec, int flags);
1775 +int add_files_to_cache(const char *prefix, const struct pathspec *pathspec, int flags, int force_mode);
1776
1777 /* diff.c */
1778 extern int diff_auto_refresh_index;
read-cache.c
+7 -4
@@ -630,7 +630,7 @@ void set_object_name_for_intent_to_add_entry(struct cache_entry *ce)
630 hashcpy(ce->sha1, sha1);
631 }
632
633 -int add_to_index(struct index_state *istate, const char *path, struct stat *st, int flags)
633 +int add_to_index(struct index_state *istate, const char *path, struct stat *st, int flags, int force_mode)
634 {
635 int size, namelen, was_same;
636 mode_t st_mode = st->st_mode;
@@ -659,7 +659,9 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
659 else
660 ce->ce_flags |= CE_INTENT_TO_ADD;
661
662 - if (trust_executable_bit && has_symlinks)
662 + if (S_ISREG(st_mode) && force_mode)
663 + ce->ce_mode = create_ce_mode(force_mode);
664 + else if (trust_executable_bit && has_symlinks)
665 ce->ce_mode = create_ce_mode(st_mode);
666 else {
667 /* If there is an existing entry, pick the mode bits and type
@@ -720,12 +722,13 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
722 return 0;
723 }
724
723 -int add_file_to_index(struct index_state *istate, const char *path, int flags)
725 +int add_file_to_index(struct index_state *istate, const char *path,
726 + int flags, int force_mode)
727 {
728 struct stat st;
729 if (lstat(path, &st))
730 die_errno("unable to stat '%s'", path);
728 - return add_to_index(istate, path, &st, flags);
731 + return add_to_index(istate, path, &st, flags, force_mode);
732 }
733
734 struct cache_entry *make_cache_entry(unsigned int mode,
t/t3700-add.sh
+30
@@ -332,4 +332,34 @@ test_expect_success 'git add --dry-run --ignore-missing of non-existing file out
332 test_i18ncmp expect.err actual.err
333 '
334
335 +test_expect_success 'git add --chmod=+x stages a non-executable file with +x' '
336 + echo foo >foo1 &&
337 + git add --chmod=+x foo1 &&
338 + case "$(git ls-files --stage foo1)" in
339 + 100755" "*foo1) echo pass;;
340 + *) echo fail; git ls-files --stage foo1; (exit 1);;
341 + esac
342 +'
343 +
344 +test_expect_success 'git add --chmod=-x stages an executable file with -x' '
345 + echo foo >xfoo1 &&
346 + chmod 755 xfoo1 &&
347 + git add --chmod=-x xfoo1 &&
348 + case "$(git ls-files --stage xfoo1)" in
349 + 100644" "*xfoo1) echo pass;;
350 + *) echo fail; git ls-files --stage xfoo1; (exit 1);;
351 + esac
352 +'
353 +
354 +test_expect_success POSIXPERM,SYMLINKS 'git add --chmod=+x with symlinks' '
355 + git config core.filemode 1 &&
356 + git config core.symlinks 1 &&
357 + echo foo >foo2 &&
358 + git add --chmod=+x foo2 &&
359 + case "$(git ls-files --stage foo2)" in
360 + 100755" "*foo2) echo pass;;
361 + *) echo fail; git ls-files --stage foo2; (exit 1);;
362 + esac
363 +'
364 +
365 test_done