Skip to content
/ git Public
forked from git/git

Commit

Permalink
ce_match_stat, run_diff_files: use symbolic constants for readability
Browse files Browse the repository at this point in the history
ce_match_stat() can be told:

 (1) to ignore CE_VALID bit (used under "assume unchanged" mode)
     and perform the stat comparison anyway;

 (2) not to perform the contents comparison for racily clean
     entries and report mismatch of cached stat information;

using its "option" parameter.  Give them symbolic constants.

Similarly, run_diff_files() can be told not to report anything
on removed paths.  Also give it a symbolic constant for that.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
gitster committed Nov 10, 2007
1 parent d048a96 commit 4bd5b7d
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 34 deletions.
2 changes: 1 addition & 1 deletion builtin-apply.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2099,7 +2099,7 @@ static int verify_index_match(struct cache_entry *ce, struct stat *st)
return -1; return -1;
return 0; return 0;
} }
return ce_match_stat(ce, st, 1); return ce_match_stat(ce, st, CE_MATCH_IGNORE_VALID);
} }


static int check_patch(struct patch *patch, struct patch *prev_patch) static int check_patch(struct patch *patch, struct patch *prev_patch)
Expand Down
14 changes: 10 additions & 4 deletions cache.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ extern struct index_state the_index;
#define remove_file_from_cache(path) remove_file_from_index(&the_index, (path)) #define remove_file_from_cache(path) remove_file_from_index(&the_index, (path))
#define add_file_to_cache(path, verbose) add_file_to_index(&the_index, (path), (verbose)) #define add_file_to_cache(path, verbose) add_file_to_index(&the_index, (path), (verbose))
#define refresh_cache(flags) refresh_index(&the_index, (flags), NULL, NULL) #define refresh_cache(flags) refresh_index(&the_index, (flags), NULL, NULL)
#define ce_match_stat(ce, st, really) ie_match_stat(&the_index, (ce), (st), (really)) #define ce_match_stat(ce, st, options) ie_match_stat(&the_index, (ce), (st), (options))
#define ce_modified(ce, st, really) ie_modified(&the_index, (ce), (st), (really)) #define ce_modified(ce, st, options) ie_modified(&the_index, (ce), (st), (options))
#endif #endif


enum object_type { enum object_type {
Expand Down Expand Up @@ -266,8 +266,14 @@ extern int remove_file_from_index(struct index_state *, const char *path);
extern int add_file_to_index(struct index_state *, const char *path, int verbose); extern int add_file_to_index(struct index_state *, const char *path, int verbose);
extern struct cache_entry *make_cache_entry(unsigned int mode, const unsigned char *sha1, const char *path, int stage, int refresh); extern struct cache_entry *make_cache_entry(unsigned int mode, const unsigned char *sha1, const char *path, int stage, int refresh);
extern int ce_same_name(struct cache_entry *a, struct cache_entry *b); extern int ce_same_name(struct cache_entry *a, struct cache_entry *b);
extern int ie_match_stat(struct index_state *, struct cache_entry *, struct stat *, int);
extern int ie_modified(struct index_state *, struct cache_entry *, struct stat *, int); /* do stat comparison even if CE_VALID is true */
#define CE_MATCH_IGNORE_VALID 01
/* do not check the contents but report dirty on racily-clean entries */
#define CE_MATCH_RACY_IS_DIRTY 02
extern int ie_match_stat(struct index_state *, struct cache_entry *, struct stat *, unsigned int);
extern int ie_modified(struct index_state *, struct cache_entry *, struct stat *, unsigned int);

extern int ce_path_match(const struct cache_entry *ce, const char **pathspec); extern int ce_path_match(const struct cache_entry *ce, const char **pathspec);
extern int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, enum object_type type, const char *path); extern int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, enum object_type type, const char *path);
extern int read_fd(int fd, char **return_buf, unsigned long *return_size); extern int read_fd(int fd, char **return_buf, unsigned long *return_size);
Expand Down
2 changes: 1 addition & 1 deletion check-racy.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int main(int ac, char **av)


if (ce_match_stat(ce, &st, 0)) if (ce_match_stat(ce, &st, 0))
dirty++; dirty++;
else if (ce_match_stat(ce, &st, 2)) else if (ce_match_stat(ce, &st, CE_MATCH_RACY_IS_DIRTY))
racy++; racy++;
else else
clean++; clean++;
Expand Down
16 changes: 9 additions & 7 deletions diff-lib.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -173,9 +173,10 @@ static int is_in_index(const char *path)
} }


static int handle_diff_files_args(struct rev_info *revs, static int handle_diff_files_args(struct rev_info *revs,
int argc, const char **argv, int *silent) int argc, const char **argv,
unsigned int *options)
{ {
*silent = 0; *options = 0;


/* revs->max_count == -2 means --no-index */ /* revs->max_count == -2 means --no-index */
while (1 < argc && argv[1][0] == '-') { while (1 < argc && argv[1][0] == '-') {
Expand All @@ -192,7 +193,7 @@ static int handle_diff_files_args(struct rev_info *revs,
revs->diffopt.no_index = 1; revs->diffopt.no_index = 1;
} }
else if (!strcmp(argv[1], "-q")) else if (!strcmp(argv[1], "-q"))
*silent = 1; *options |= DIFF_SILENT_ON_REMOVED;
else else
return error("invalid option: %s", argv[1]); return error("invalid option: %s", argv[1]);
argv++; argc--; argv++; argc--;
Expand Down Expand Up @@ -305,9 +306,9 @@ int setup_diff_no_index(struct rev_info *revs,


int run_diff_files_cmd(struct rev_info *revs, int argc, const char **argv) int run_diff_files_cmd(struct rev_info *revs, int argc, const char **argv)
{ {
int silent_on_removed; unsigned int options;


if (handle_diff_files_args(revs, argc, argv, &silent_on_removed)) if (handle_diff_files_args(revs, argc, argv, &options))
return -1; return -1;


if (revs->diffopt.no_index) { if (revs->diffopt.no_index) {
Expand All @@ -329,13 +330,14 @@ int run_diff_files_cmd(struct rev_info *revs, int argc, const char **argv)
perror("read_cache"); perror("read_cache");
return -1; return -1;
} }
return run_diff_files(revs, silent_on_removed); return run_diff_files(revs, options);
} }


int run_diff_files(struct rev_info *revs, int silent_on_removed) int run_diff_files(struct rev_info *revs, unsigned int option)
{ {
int entries, i; int entries, i;
int diff_unmerged_stage = revs->max_count; int diff_unmerged_stage = revs->max_count;
int silent_on_removed = option & DIFF_SILENT_ON_REMOVED;


if (diff_unmerged_stage < 0) if (diff_unmerged_stage < 0)
diff_unmerged_stage = 2; diff_unmerged_stage = 2;
Expand Down
4 changes: 3 additions & 1 deletion diff.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ extern void diff_flush(struct diff_options*);


extern const char *diff_unique_abbrev(const unsigned char *, int); extern const char *diff_unique_abbrev(const unsigned char *, int);


extern int run_diff_files(struct rev_info *revs, int silent_on_removed); /* do not report anything on removed paths */
#define DIFF_SILENT_ON_REMOVED 01
extern int run_diff_files(struct rev_info *revs, unsigned int option);
extern int setup_diff_no_index(struct rev_info *revs, extern int setup_diff_no_index(struct rev_info *revs,
int argc, const char ** argv, int nongit, const char *prefix); int argc, const char ** argv, int nongit, const char *prefix);
extern int run_diff_files_cmd(struct rev_info *revs, int argc, const char **argv); extern int run_diff_files_cmd(struct rev_info *revs, int argc, const char **argv);
Expand Down
2 changes: 1 addition & 1 deletion entry.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ int checkout_entry(struct cache_entry *ce, const struct checkout *state, char *t
strcpy(path + len, ce->name); strcpy(path + len, ce->name);


if (!lstat(path, &st)) { if (!lstat(path, &st)) {
unsigned changed = ce_match_stat(ce, &st, 1); unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID);
if (!changed) if (!changed)
return 0; return 0;
if (!state->force) { if (!state->force) {
Expand Down
47 changes: 30 additions & 17 deletions read-cache.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -194,11 +194,12 @@ static int ce_match_stat_basic(struct cache_entry *ce, struct stat *st)
} }


int ie_match_stat(struct index_state *istate, int ie_match_stat(struct index_state *istate,
struct cache_entry *ce, struct stat *st, int options) struct cache_entry *ce, struct stat *st,
unsigned int options)
{ {
unsigned int changed; unsigned int changed;
int ignore_valid = options & 01; int ignore_valid = options & CE_MATCH_IGNORE_VALID;
int assume_racy_is_modified = options & 02; int assume_racy_is_modified = options & CE_MATCH_RACY_IS_DIRTY;


/* /*
* If it's marked as always valid in the index, it's * If it's marked as always valid in the index, it's
Expand Down Expand Up @@ -238,10 +239,11 @@ int ie_match_stat(struct index_state *istate,
} }


int ie_modified(struct index_state *istate, int ie_modified(struct index_state *istate,
struct cache_entry *ce, struct stat *st, int really) struct cache_entry *ce, struct stat *st, unsigned int options)
{ {
int changed, changed_fs; int changed, changed_fs;
changed = ie_match_stat(istate, ce, st, really);
changed = ie_match_stat(istate, ce, st, options);
if (!changed) if (!changed)
return 0; return 0;
/* /*
Expand Down Expand Up @@ -420,7 +422,7 @@ int add_file_to_index(struct index_state *istate, const char *path, int verbose)
pos = index_name_pos(istate, ce->name, namelen); pos = index_name_pos(istate, ce->name, namelen);
if (0 <= pos && if (0 <= pos &&
!ce_stage(istate->cache[pos]) && !ce_stage(istate->cache[pos]) &&
!ie_modified(istate, istate->cache[pos], &st, 1)) { !ie_modified(istate, istate->cache[pos], &st, CE_MATCH_IGNORE_VALID)) {
/* Nothing changed, really */ /* Nothing changed, really */
free(ce); free(ce);
return 0; return 0;
Expand Down Expand Up @@ -782,28 +784,37 @@ int add_index_entry(struct index_state *istate, struct cache_entry *ce, int opti
* to link up the stat cache details with the proper files. * to link up the stat cache details with the proper files.
*/ */
static struct cache_entry *refresh_cache_ent(struct index_state *istate, static struct cache_entry *refresh_cache_ent(struct index_state *istate,
struct cache_entry *ce, int really, int *err) struct cache_entry *ce,
unsigned int options, int *err)
{ {
struct stat st; struct stat st;
struct cache_entry *updated; struct cache_entry *updated;
int changed, size; int changed, size;
int ignore_valid = options & CE_MATCH_IGNORE_VALID;


if (lstat(ce->name, &st) < 0) { if (lstat(ce->name, &st) < 0) {
if (err) if (err)
*err = errno; *err = errno;
return NULL; return NULL;
} }


changed = ie_match_stat(istate, ce, &st, really); changed = ie_match_stat(istate, ce, &st, options);
if (!changed) { if (!changed) {
if (really && assume_unchanged && /*
* The path is unchanged. If we were told to ignore
* valid bit, then we did the actual stat check and
* found that the entry is unmodified. If the entry
* is not marked VALID, this is the place to mark it
* valid again, under "assume unchanged" mode.
*/
if (ignore_valid && assume_unchanged &&
!(ce->ce_flags & htons(CE_VALID))) !(ce->ce_flags & htons(CE_VALID)))
; /* mark this one VALID again */ ; /* mark this one VALID again */
else else
return ce; return ce;
} }


if (ie_modified(istate, ce, &st, really)) { if (ie_modified(istate, ce, &st, options)) {
if (err) if (err)
*err = EINVAL; *err = EINVAL;
return NULL; return NULL;
Expand All @@ -814,13 +825,14 @@ static struct cache_entry *refresh_cache_ent(struct index_state *istate,
memcpy(updated, ce, size); memcpy(updated, ce, size);
fill_stat_cache_info(updated, &st); fill_stat_cache_info(updated, &st);


/* In this case, if really is not set, we should leave /*
* CE_VALID bit alone. Otherwise, paths marked with * If ignore_valid is not set, we should leave CE_VALID bit
* --no-assume-unchanged (i.e. things to be edited) will * alone. Otherwise, paths marked with --no-assume-unchanged
* reacquire CE_VALID bit automatically, which is not * (i.e. things to be edited) will reacquire CE_VALID bit
* really what we want. * automatically, which is not really what we want.
*/ */
if (!really && assume_unchanged && !(ce->ce_flags & htons(CE_VALID))) if (!ignore_valid && assume_unchanged &&
!(ce->ce_flags & htons(CE_VALID)))
updated->ce_flags &= ~htons(CE_VALID); updated->ce_flags &= ~htons(CE_VALID);


return updated; return updated;
Expand All @@ -834,6 +846,7 @@ int refresh_index(struct index_state *istate, unsigned int flags, const char **p
int allow_unmerged = (flags & REFRESH_UNMERGED) != 0; int allow_unmerged = (flags & REFRESH_UNMERGED) != 0;
int quiet = (flags & REFRESH_QUIET) != 0; int quiet = (flags & REFRESH_QUIET) != 0;
int not_new = (flags & REFRESH_IGNORE_MISSING) != 0; int not_new = (flags & REFRESH_IGNORE_MISSING) != 0;
unsigned int options = really ? CE_MATCH_IGNORE_VALID : 0;


for (i = 0; i < istate->cache_nr; i++) { for (i = 0; i < istate->cache_nr; i++) {
struct cache_entry *ce, *new; struct cache_entry *ce, *new;
Expand All @@ -855,7 +868,7 @@ int refresh_index(struct index_state *istate, unsigned int flags, const char **p
if (pathspec && !match_pathspec(pathspec, ce->name, strlen(ce->name), 0, seen)) if (pathspec && !match_pathspec(pathspec, ce->name, strlen(ce->name), 0, seen))
continue; continue;


new = refresh_cache_ent(istate, ce, really, &cache_errno); new = refresh_cache_ent(istate, ce, options, &cache_errno);
if (new == ce) if (new == ce)
continue; continue;
if (!new) { if (!new) {
Expand Down
4 changes: 2 additions & 2 deletions unpack-trees.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ static void verify_uptodate(struct cache_entry *ce,
return; return;


if (!lstat(ce->name, &st)) { if (!lstat(ce->name, &st)) {
unsigned changed = ce_match_stat(ce, &st, 1); unsigned changed = ce_match_stat(ce, &st, CE_MATCH_IGNORE_VALID);
if (!changed) if (!changed)
return; return;
/* /*
Expand Down Expand Up @@ -927,7 +927,7 @@ int oneway_merge(struct cache_entry **src,
if (o->reset) { if (o->reset) {
struct stat st; struct stat st;
if (lstat(old->name, &st) || if (lstat(old->name, &st) ||
ce_match_stat(old, &st, 1)) ce_match_stat(old, &st, CE_MATCH_IGNORE_VALID))
old->ce_flags |= htons(CE_UPDATE); old->ce_flags |= htons(CE_UPDATE);
} }
return keep_entry(old, o); return keep_entry(old, o);
Expand Down

0 comments on commit 4bd5b7d

Please sign in to comment.