read-cache: introduce chmod_index_entry

As there are chmod options for both add and update-index, introduce a new chmod_index_entry function to do the work. Use it in update-index, while it will be used in add in the next patch. Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Gummerer committed Sep 14, 2016 at 22:07 UTC d9d7096662122f6b82ad6e4c08397b75906da78d
3 files changed +33 -14
builtin/update-index.c
+2 -14
@@ -423,26 +423,14 @@ static void chmod_path(char flip, const char *path)
423 {
424 int pos;
425 struct cache_entry *ce;
426 - unsigned int mode;
426
427 pos = cache_name_pos(path, strlen(path));
428 if (pos < 0)
429 goto fail;
430 ce = active_cache[pos];
432 - mode = ce->ce_mode;
433 - if (!S_ISREG(mode))
434 - goto fail;
435 - switch (flip) {
436 - case '+':
437 - ce->ce_mode |= 0111; break;
438 - case '-':
439 - ce->ce_mode &= ~0111; break;
440 - default:
431 + if (chmod_cache_entry(ce, flip) < 0)
432 goto fail;
442 - }
443 - cache_tree_invalidate_path(&the_index, path);
444 - ce->ce_flags |= CE_UPDATE_IN_BASE;
445 - active_cache_changed |= CE_ENTRY_CHANGED;
433 +
434 report("chmod %cx '%s'", flip, path);
435 return;
436 fail:
cache.h
+2
@@ -369,6 +369,7 @@ extern void free_name_hash(struct index_state *istate);
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), 0)
371 #define add_file_to_cache(path, flags) add_file_to_index(&the_index, (path), (flags), 0)
372 +#define chmod_cache_entry(ce, flip) chmod_index_entry(&the_index, (ce), (flip))
373 #define refresh_cache(flags) refresh_index(&the_index, (flags), NULL, NULL, NULL)
374 #define ce_match_stat(ce, st, options) ie_match_stat(&the_index, (ce), (st), (options))
375 #define ce_modified(ce, st, options) ie_modified(&the_index, (ce), (st), (options))
@@ -584,6 +585,7 @@ extern int remove_file_from_index(struct index_state *, const char *path);
585 extern int add_to_index(struct index_state *, const char *path, struct stat *, int flags, int force_mode);
586 extern int add_file_to_index(struct index_state *, const char *path, int flags, int force_mode);
587 extern struct cache_entry *make_cache_entry(unsigned int mode, const unsigned char *sha1, const char *path, int stage, unsigned int refresh_options);
588 +extern int chmod_index_entry(struct index_state *, struct cache_entry *ce, char flip);
589 extern int ce_same_name(const struct cache_entry *a, const struct cache_entry *b);
590 extern void set_object_name_for_intent_to_add_entry(struct cache_entry *ce);
591 extern int index_name_is_other(const struct index_state *, const char *, int);
read-cache.c
+29
@@ -759,6 +759,35 @@ struct cache_entry *make_cache_entry(unsigned int mode,
759 return ret;
760 }
761
762 +/*
763 + * Chmod an index entry with either +x or -x.
764 + *
765 + * Returns -1 if the chmod for the particular cache entry failed (if it's
766 + * not a regular file), -2 if an invalid flip argument is passed in, 0
767 + * otherwise.
768 + */
769 +int chmod_index_entry(struct index_state *istate, struct cache_entry *ce,
770 + char flip)
771 +{
772 + if (!S_ISREG(ce->ce_mode))
773 + return -1;
774 + switch (flip) {
775 + case '+':
776 + ce->ce_mode |= 0111;
777 + break;
778 + case '-':
779 + ce->ce_mode &= ~0111;
780 + break;
781 + default:
782 + return -2;
783 + }
784 + cache_tree_invalidate_path(istate, ce->name);
785 + ce->ce_flags |= CE_UPDATE_IN_BASE;
786 + istate->cache_changed |= CE_ENTRY_CHANGED;
787 +
788 + return 0;
789 +}
790 +
791 int ce_same_name(const struct cache_entry *a, const struct cache_entry *b)
792 {
793 int len = ce_namelen(a);