@matheusbsbs / matheus-bagliano / commits / 405e5b2fe0
Fork 0

Libify the index refresh logic

This cleans up and libifies the "git update-index --[really-]refresh" functionality. This will be eventually required for eventually doing the "commit" and "status" commands as built-ins. It really just moves "refresh_index()" from update-index.c to read-cache.c, but it also has to change the calling convention so that the function uses a "unsigned int flags" argument instead of various static flags variables for passing down the information about whether to be quiet or not, and allow unmerged entries etc. That actually cleans up update-index.c too, since it turns out that all those flags were really specific to that one function of the index update, so they shouldn't have had file-scope visibility even before. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed May 19, 2006 at 09:56 UTC 405e5b2fe0cf20b0eea41ca892f416c218b49f59
3 files changed +129 -122
cache.h
+6
@@ -158,6 +158,12 @@ extern int index_pipe(unsigned char *sha1, int fd, const char *type, int write_o
158 extern int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object);
159 extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
160
161 +#define REFRESH_REALLY 0x0001 /* ignore_valid */
162 +#define REFRESH_UNMERGED 0x0002 /* allow unmerged */
163 +#define REFRESH_QUIET 0x0004 /* be quiet about it */
164 +#define REFRESH_IGNORE_MISSING 0x0008 /* ignore non-existent */
165 +extern int refresh_cache(unsigned int flags);
166 +
167 struct cache_file {
168 struct cache_file *next;
169 char lockfile[PATH_MAX];
read-cache.c
+117
@@ -496,6 +496,123 @@ int add_cache_entry(struct cache_entry *ce, int option)
496 return 0;
497 }
498
499 +/* Three functions to allow overloaded pointer return; see linux/err.h */
500 +static inline void *ERR_PTR(long error)
501 +{
502 + return (void *) error;
503 +}
504 +
505 +static inline long PTR_ERR(const void *ptr)
506 +{
507 + return (long) ptr;
508 +}
509 +
510 +static inline long IS_ERR(const void *ptr)
511 +{
512 + return (unsigned long)ptr > (unsigned long)-1000L;
513 +}
514 +
515 +/*
516 + * "refresh" does not calculate a new sha1 file or bring the
517 + * cache up-to-date for mode/content changes. But what it
518 + * _does_ do is to "re-match" the stat information of a file
519 + * with the cache, so that you can refresh the cache for a
520 + * file that hasn't been changed but where the stat entry is
521 + * out of date.
522 + *
523 + * For example, you'd want to do this after doing a "git-read-tree",
524 + * to link up the stat cache details with the proper files.
525 + */
526 +static struct cache_entry *refresh_entry(struct cache_entry *ce, int really)
527 +{
528 + struct stat st;
529 + struct cache_entry *updated;
530 + int changed, size;
531 +
532 + if (lstat(ce->name, &st) < 0)
533 + return ERR_PTR(-errno);
534 +
535 + changed = ce_match_stat(ce, &st, really);
536 + if (!changed) {
537 + if (really && assume_unchanged &&
538 + !(ce->ce_flags & htons(CE_VALID)))
539 + ; /* mark this one VALID again */
540 + else
541 + return NULL;
542 + }
543 +
544 + if (ce_modified(ce, &st, really))
545 + return ERR_PTR(-EINVAL);
546 +
547 + size = ce_size(ce);
548 + updated = xmalloc(size);
549 + memcpy(updated, ce, size);
550 + fill_stat_cache_info(updated, &st);
551 +
552 + /* In this case, if really is not set, we should leave
553 + * CE_VALID bit alone. Otherwise, paths marked with
554 + * --no-assume-unchanged (i.e. things to be edited) will
555 + * reacquire CE_VALID bit automatically, which is not
556 + * really what we want.
557 + */
558 + if (!really && assume_unchanged && !(ce->ce_flags & htons(CE_VALID)))
559 + updated->ce_flags &= ~htons(CE_VALID);
560 +
561 + return updated;
562 +}
563 +
564 +int refresh_cache(unsigned int flags)
565 +{
566 + int i;
567 + int has_errors = 0;
568 + int really = (flags & REFRESH_REALLY) != 0;
569 + int allow_unmerged = (flags & REFRESH_UNMERGED) != 0;
570 + int quiet = (flags & REFRESH_QUIET) != 0;
571 + int not_new = (flags & REFRESH_IGNORE_MISSING) != 0;
572 +
573 + for (i = 0; i < active_nr; i++) {
574 + struct cache_entry *ce, *new;
575 + ce = active_cache[i];
576 + if (ce_stage(ce)) {
577 + while ((i < active_nr) &&
578 + ! strcmp(active_cache[i]->name, ce->name))
579 + i++;
580 + i--;
581 + if (allow_unmerged)
582 + continue;
583 + printf("%s: needs merge\n", ce->name);
584 + has_errors = 1;
585 + continue;
586 + }
587 +
588 + new = refresh_entry(ce, really);
589 + if (!new)
590 + continue;
591 + if (IS_ERR(new)) {
592 + if (not_new && PTR_ERR(new) == -ENOENT)
593 + continue;
594 + if (really && PTR_ERR(new) == -EINVAL) {
595 + /* If we are doing --really-refresh that
596 + * means the index is not valid anymore.
597 + */
598 + ce->ce_flags &= ~htons(CE_VALID);
599 + active_cache_changed = 1;
600 + }
601 + if (quiet)
602 + continue;
603 + printf("%s: needs update\n", ce->name);
604 + has_errors = 1;
605 + continue;
606 + }
607 + active_cache_changed = 1;
608 + /* You can NOT just free active_cache[i] here, since it
609 + * might not be necessarily malloc()ed but can also come
610 + * from mmap(). */
611 + active_cache[i] = new;
612 + }
613 + return has_errors;
614 +}
615 +
616 static int verify_hdr(struct cache_header *hdr, unsigned long size)
617 {
618 SHA_CTX c;
update-index.c
+6 -122
@@ -18,9 +18,6 @@
18 static int allow_add;
19 static int allow_remove;
20 static int allow_replace;
21 -static int allow_unmerged; /* --refresh needing merge is not error */
22 -static int not_new; /* --refresh not having working tree files is not error */
23 -static int quiet; /* --refresh needing update is not error */
21 static int info_only;
22 static int force_remove;
23 static int verbose;
@@ -28,23 +25,6 @@ static int mark_valid_only = 0;
25 #define MARK_VALID 1
26 #define UNMARK_VALID 2
27
31 -
32 -/* Three functions to allow overloaded pointer return; see linux/err.h */
33 -static inline void *ERR_PTR(long error)
34 -{
35 - return (void *) error;
36 -}
37 -
38 -static inline long PTR_ERR(const void *ptr)
39 -{
40 - return (long) ptr;
41 -}
42 -
43 -static inline long IS_ERR(const void *ptr)
44 -{
45 - return (unsigned long)ptr > (unsigned long)-1000L;
46 -}
47 -
28 static void report(const char *fmt, ...)
29 {
30 va_list vp;
@@ -140,103 +120,6 @@ static int add_file_to_cache(const char *path)
120 return 0;
121 }
122
143 -/*
144 - * "refresh" does not calculate a new sha1 file or bring the
145 - * cache up-to-date for mode/content changes. But what it
146 - * _does_ do is to "re-match" the stat information of a file
147 - * with the cache, so that you can refresh the cache for a
148 - * file that hasn't been changed but where the stat entry is
149 - * out of date.
150 - *
151 - * For example, you'd want to do this after doing a "git-read-tree",
152 - * to link up the stat cache details with the proper files.
153 - */
154 -static struct cache_entry *refresh_entry(struct cache_entry *ce, int really)
155 -{
156 - struct stat st;
157 - struct cache_entry *updated;
158 - int changed, size;
159 -
160 - if (lstat(ce->name, &st) < 0)
161 - return ERR_PTR(-errno);
162 -
163 - changed = ce_match_stat(ce, &st, really);
164 - if (!changed) {
165 - if (really && assume_unchanged &&
166 - !(ce->ce_flags & htons(CE_VALID)))
167 - ; /* mark this one VALID again */
168 - else
169 - return NULL;
170 - }
171 -
172 - if (ce_modified(ce, &st, really))
173 - return ERR_PTR(-EINVAL);
174 -
175 - size = ce_size(ce);
176 - updated = xmalloc(size);
177 - memcpy(updated, ce, size);
178 - fill_stat_cache_info(updated, &st);
179 -
180 - /* In this case, if really is not set, we should leave
181 - * CE_VALID bit alone. Otherwise, paths marked with
182 - * --no-assume-unchanged (i.e. things to be edited) will
183 - * reacquire CE_VALID bit automatically, which is not
184 - * really what we want.
185 - */
186 - if (!really && assume_unchanged && !(ce->ce_flags & htons(CE_VALID)))
187 - updated->ce_flags &= ~htons(CE_VALID);
188 -
189 - return updated;
190 -}
191 -
192 -static int refresh_cache(int really)
193 -{
194 - int i;
195 - int has_errors = 0;
196 -
197 - for (i = 0; i < active_nr; i++) {
198 - struct cache_entry *ce, *new;
199 - ce = active_cache[i];
200 - if (ce_stage(ce)) {
201 - while ((i < active_nr) &&
202 - ! strcmp(active_cache[i]->name, ce->name))
203 - i++;
204 - i--;
205 - if (allow_unmerged)
206 - continue;
207 - printf("%s: needs merge\n", ce->name);
208 - has_errors = 1;
209 - continue;
210 - }
211 -
212 - new = refresh_entry(ce, really);
213 - if (!new)
214 - continue;
215 - if (IS_ERR(new)) {
216 - if (not_new && PTR_ERR(new) == -ENOENT)
217 - continue;
218 - if (really && PTR_ERR(new) == -EINVAL) {
219 - /* If we are doing --really-refresh that
220 - * means the index is not valid anymore.
221 - */
222 - ce->ce_flags &= ~htons(CE_VALID);
223 - active_cache_changed = 1;
224 - }
225 - if (quiet)
226 - continue;
227 - printf("%s: needs update\n", ce->name);
228 - has_errors = 1;
229 - continue;
230 - }
231 - active_cache_changed = 1;
232 - /* You can NOT just free active_cache[i] here, since it
233 - * might not be necessarily malloc()ed but can also come
234 - * from mmap(). */
235 - active_cache[i] = new;
236 - }
237 - return has_errors;
238 -}
239 -
123 /*
124 * We fundamentally don't like some paths: we don't want
125 * dot or dot-dot anywhere, and for obvious reasons don't
@@ -653,6 +536,7 @@ int main(int argc, const char **argv)
536 const char *prefix = setup_git_directory();
537 int prefix_length = prefix ? strlen(prefix) : 0;
538 char set_executable_bit = 0;
539 + unsigned int refresh_flags = 0;
540
541 git_config(git_default_config);
542
@@ -673,7 +557,7 @@ int main(int argc, const char **argv)
557 continue;
558 }
559 if (!strcmp(path, "-q")) {
676 - quiet = 1;
560 + refresh_flags |= REFRESH_QUIET;
561 continue;
562 }
563 if (!strcmp(path, "--add")) {
@@ -689,15 +573,15 @@ int main(int argc, const char **argv)
573 continue;
574 }
575 if (!strcmp(path, "--unmerged")) {
692 - allow_unmerged = 1;
576 + refresh_flags |= REFRESH_UNMERGED;
577 continue;
578 }
579 if (!strcmp(path, "--refresh")) {
696 - has_errors |= refresh_cache(0);
580 + has_errors |= refresh_cache(refresh_flags);
581 continue;
582 }
583 if (!strcmp(path, "--really-refresh")) {
700 - has_errors |= refresh_cache(1);
584 + has_errors |= refresh_cache(REFRESH_REALLY | refresh_flags);
585 continue;
586 }
587 if (!strcmp(path, "--cacheinfo")) {
@@ -770,7 +654,7 @@ int main(int argc, const char **argv)
654 goto finish;
655 }
656 if (!strcmp(path, "--ignore-missing")) {
773 - not_new = 1;
657 + refresh_flags |= REFRESH_IGNORE_MISSING;
658 continue;
659 }
660 if (!strcmp(path, "--verbose")) {