Skip to content

Commit d9d7096

Browse files
tgummerergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 22433ce commit d9d7096

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

builtin/update-index.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -423,26 +423,14 @@ static void chmod_path(char flip, const char *path)
423423
{
424424
int pos;
425425
struct cache_entry *ce;
426-
unsigned int mode;
427426

428427
pos = cache_name_pos(path, strlen(path));
429428
if (pos < 0)
430429
goto fail;
431430
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)
441432
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+
446434
report("chmod %cx '%s'", flip, path);
447435
return;
448436
fail:

cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ extern void free_name_hash(struct index_state *istate);
369369
#define remove_file_from_cache(path) remove_file_from_index(&the_index, (path))
370370
#define add_to_cache(path, st, flags) add_to_index(&the_index, (path), (st), (flags), 0)
371371
#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))
372373
#define refresh_cache(flags) refresh_index(&the_index, (flags), NULL, NULL, NULL)
373374
#define ce_match_stat(ce, st, options) ie_match_stat(&the_index, (ce), (st), (options))
374375
#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);
584585
extern int add_to_index(struct index_state *, const char *path, struct stat *, int flags, int force_mode);
585586
extern int add_file_to_index(struct index_state *, const char *path, int flags, int force_mode);
586587
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);
587589
extern int ce_same_name(const struct cache_entry *a, const struct cache_entry *b);
588590
extern void set_object_name_for_intent_to_add_entry(struct cache_entry *ce);
589591
extern int index_name_is_other(const struct index_state *, const char *, int);

read-cache.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,35 @@ struct cache_entry *make_cache_entry(unsigned int mode,
759759
return ret;
760760
}
761761

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+
762791
int ce_same_name(const struct cache_entry *a, const struct cache_entry *b)
763792
{
764793
int len = ce_namelen(a);

0 commit comments

Comments
 (0)