@matheusbsbs / matheus-bagliano / commits / 5f73076c1a
Fork 0

"Assume unchanged" git

This adds "assume unchanged" logic, started by this message in the list discussion recently: <Pine.LNX.4.64.0601311807470.7301@g5.osdl.org> This is a workaround for filesystems that do not have lstat() that is quick enough for the index mechanism to take advantage of. On the paths marked as "assumed to be unchanged", the user needs to explicitly use update-index to register the object name to be in the next commit. You can use two new options to update-index to set and reset the CE_VALID bit: git-update-index --assume-unchanged path... git-update-index --no-assume-unchanged path... These forms manipulate only the CE_VALID bit; it does not change the object name recorded in the index file. Nor they add a new entry to the index. When the configuration variable "core.ignorestat = true" is set, the index entries are marked with CE_VALID bit automatically after: - update-index to explicitly register the current object name to the index file. - when update-index --refresh finds the path to be up-to-date. - when tools like read-tree -u and apply --index update the working tree file and register the current object name to the index file. The flag is dropped upon read-tree that does not check out the index entry. This happens regardless of the core.ignorestat settings. Index entries marked with CE_VALID bit are assumed to be unchanged most of the time. However, there are cases that CE_VALID bit is ignored for the sake of safety and usability: - while "git-read-tree -m" or git-apply need to make sure that the paths involved in the merge do not have local modifications. This sacrifices performance for safety. - when git-checkout-index -f -q -u -a tries to see if it needs to checkout the paths. Otherwise you can never check anything out ;-). - when git-update-index --really-refresh (a new flag) tries to see if the index entry is up to date. You can start with everything marked as CE_VALID and run this once to drop CE_VALID bit for paths that are modified. Most notably, "update-index --refresh" honours CE_VALID and does not actively stat, so after you modified a file in the working tree, update-index --refresh would not notice until you tell the index about it with "git-update-index path" or "git-update-index --no-assume-unchanged path". This version is not expected to be perfect. I think diff between index and/or tree and working files may need some adjustment, and there probably needs other cases we should automatically unmark paths that are marked to be CE_VALID. But the basics seem to work, and ready to be tested by people who asked for this feature. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Feb 8, 2006 at 21:15 UTC 5f73076c1a9b4b8dc94f77eac98eb558d25e33c0
13 files changed +99 -21
apply.c
+1 -1
@@ -1309,7 +1309,7 @@ static int check_patch(struct patch *patch)
1309 return -1;
1310 }
1311
1312 - changed = ce_match_stat(active_cache[pos], &st);
1312 + changed = ce_match_stat(active_cache[pos], &st, 1);
1313 if (changed)
1314 return error("%s: does not match index",
1315 old_name);
cache.h
+4 -2
@@ -91,6 +91,7 @@ struct cache_entry {
91 #define CE_NAMEMASK (0x0fff)
92 #define CE_STAGEMASK (0x3000)
93 #define CE_UPDATE (0x4000)
94 +#define CE_VALID (0x8000)
95 #define CE_STAGESHIFT 12
96
97 #define create_ce_flags(len, stage) htons((len) | ((stage) << CE_STAGESHIFT))
@@ -144,8 +145,8 @@ extern int add_cache_entry(struct cache_entry *ce, int option);
145 extern int remove_cache_entry_at(int pos);
146 extern int remove_file_from_cache(const char *path);
147 extern int ce_same_name(struct cache_entry *a, struct cache_entry *b);
147 -extern int ce_match_stat(struct cache_entry *ce, struct stat *st);
148 -extern int ce_modified(struct cache_entry *ce, struct stat *st);
148 +extern int ce_match_stat(struct cache_entry *ce, struct stat *st, int);
149 +extern int ce_modified(struct cache_entry *ce, struct stat *st, int);
150 extern int ce_path_match(const struct cache_entry *ce, const char **pathspec);
151 extern int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, const char *type);
152 extern int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object);
@@ -161,6 +162,7 @@ extern int commit_index_file(struct cache_file *);
162 extern void rollback_index_file(struct cache_file *);
163
164 extern int trust_executable_bit;
165 +extern int assume_unchanged;
166 extern int only_use_symrefs;
167 extern int diff_rename_limit_default;
168 extern int shared_repository;
checkout-index.c
+1
@@ -116,6 +116,7 @@ int main(int argc, char **argv)
116 int all = 0;
117
118 prefix = setup_git_directory();
119 + git_config(git_default_config);
120 prefix_length = prefix ? strlen(prefix) : 0;
121
122 if (read_cache() < 0) {
config.c
+5
@@ -222,6 +222,11 @@ int git_default_config(const char *var, const char *value)
222 return 0;
223 }
224
225 + if (!strcmp(var, "core.ignorestat")) {
226 + assume_unchanged = git_config_bool(var, value);
227 + return 0;
228 + }
229 +
230 if (!strcmp(var, "core.symrefsonly")) {
231 only_use_symrefs = git_config_bool(var, value);
232 return 0;
diff-files.c
+1 -1
@@ -191,7 +191,7 @@ int main(int argc, const char **argv)
191 show_file('-', ce);
192 continue;
193 }
194 - changed = ce_match_stat(ce, &st);
194 + changed = ce_match_stat(ce, &st, 0);
195 if (!changed && !diff_options.find_copies_harder)
196 continue;
197 oldmode = ntohl(ce->ce_mode);
diff-index.c
+1 -1
@@ -33,7 +33,7 @@ static int get_stat_data(struct cache_entry *ce,
33 }
34 return -1;
35 }
36 - changed = ce_match_stat(ce, &st);
36 + changed = ce_match_stat(ce, &st, 0);
37 if (changed) {
38 mode = create_ce_mode(st.st_mode);
39 if (!trust_executable_bit &&
diff.c
+1 -1
@@ -311,7 +311,7 @@ static int work_tree_matches(const char *name, const unsigned char *sha1)
311 ce = active_cache[pos];
312 if ((lstat(name, &st) < 0) ||
313 !S_ISREG(st.st_mode) || /* careful! */
314 - ce_match_stat(ce, &st) ||
314 + ce_match_stat(ce, &st, 0) ||
315 memcmp(sha1, ce->sha1, 20))
316 return 0;
317 /* we return 1 only when we can stat, it is a regular file,
entry.c
+1 -1
@@ -123,7 +123,7 @@ int checkout_entry(struct cache_entry *ce, struct checkout *state)
123 strcpy(path + len, ce->name);
124
125 if (!lstat(path, &st)) {
126 - unsigned changed = ce_match_stat(ce, &st);
126 + unsigned changed = ce_match_stat(ce, &st, 1);
127 if (!changed)
128 return 0;
129 if (!state->force) {
environment.c
+1
@@ -12,6 +12,7 @@
12 char git_default_email[MAX_GITNAME];
13 char git_default_name[MAX_GITNAME];
14 int trust_executable_bit = 1;
15 +int assume_unchanged = 0;
16 int only_use_symrefs = 0;
17 int repository_format_version = 0;
18 char git_commit_encoding[MAX_ENCODING_LENGTH] = "utf-8";
read-cache.c
+23 -5
@@ -27,6 +27,9 @@ void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
27 ce->ce_uid = htonl(st->st_uid);
28 ce->ce_gid = htonl(st->st_gid);
29 ce->ce_size = htonl(st->st_size);
30 +
31 + if (assume_unchanged)
32 + ce->ce_flags |= htons(CE_VALID);
33 }
34
35 static int ce_compare_data(struct cache_entry *ce, struct stat *st)
@@ -146,9 +149,18 @@ static int ce_match_stat_basic(struct cache_entry *ce, struct stat *st)
149 return changed;
150 }
151
149 -int ce_match_stat(struct cache_entry *ce, struct stat *st)
152 +int ce_match_stat(struct cache_entry *ce, struct stat *st, int ignore_valid)
153 {
151 - unsigned int changed = ce_match_stat_basic(ce, st);
154 + unsigned int changed;
155 +
156 + /*
157 + * If it's marked as always valid in the index, it's
158 + * valid whatever the checked-out copy says.
159 + */
160 + if (!ignore_valid && (ce->ce_flags & htons(CE_VALID)))
161 + return 0;
162 +
163 + changed = ce_match_stat_basic(ce, st);
164
165 /*
166 * Within 1 second of this sequence:
@@ -164,7 +176,7 @@ int ce_match_stat(struct cache_entry *ce, struct stat *st)
176 * effectively mean we can make at most one commit per second,
177 * which is not acceptable. Instead, we check cache entries
178 * whose mtime are the same as the index file timestamp more
167 - * careful than others.
179 + * carefully than others.
180 */
181 if (!changed &&
182 index_file_timestamp &&
@@ -174,10 +186,10 @@ int ce_match_stat(struct cache_entry *ce, struct stat *st)
186 return changed;
187 }
188
177 -int ce_modified(struct cache_entry *ce, struct stat *st)
189 +int ce_modified(struct cache_entry *ce, struct stat *st, int really)
190 {
191 int changed, changed_fs;
180 - changed = ce_match_stat(ce, st);
192 + changed = ce_match_stat(ce, st, really);
193 if (!changed)
194 return 0;
195 /*
@@ -233,6 +245,11 @@ int cache_name_compare(const char *name1, int flags1, const char *name2, int fla
245 return -1;
246 if (len1 > len2)
247 return 1;
248 +
249 + /* Differences between "assume up-to-date" should not matter. */
250 + flags1 &= ~CE_VALID;
251 + flags2 &= ~CE_VALID;
252 +
253 if (flags1 < flags2)
254 return -1;
255 if (flags1 > flags2)
@@ -430,6 +447,7 @@ int add_cache_entry(struct cache_entry *ce, int option)
447 int ok_to_add = option & ADD_CACHE_OK_TO_ADD;
448 int ok_to_replace = option & ADD_CACHE_OK_TO_REPLACE;
449 int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK;
450 +
451 pos = cache_name_pos(ce->name, ntohs(ce->ce_flags));
452
453 /* existing match? Just replace it. */
read-tree.c
+1 -1
@@ -349,7 +349,7 @@ static void verify_uptodate(struct cache_entry *ce)
349 return;
350
351 if (!lstat(ce->name, &st)) {
352 - unsigned changed = ce_match_stat(ce, &st);
352 + unsigned changed = ce_match_stat(ce, &st, 1);
353 if (!changed)
354 return;
355 errno = 0;
update-index.c
+58 -7
@@ -23,6 +23,10 @@ static int quiet; /* --refresh needing update is not error */
23 static int info_only;
24 static int force_remove;
25 static int verbose;
26 +static int mark_valid_only = 0;
27 +#define MARK_VALID 1
28 +#define UNMARK_VALID 2
29 +
30
31 /* Three functions to allow overloaded pointer return; see linux/err.h */
32 static inline void *ERR_PTR(long error)
@@ -53,6 +57,25 @@ static void report(const char *fmt, ...)
57 va_end(vp);
58 }
59
60 +static int mark_valid(const char *path)
61 +{
62 + int namelen = strlen(path);
63 + int pos = cache_name_pos(path, namelen);
64 + if (0 <= pos) {
65 + switch (mark_valid_only) {
66 + case MARK_VALID:
67 + active_cache[pos]->ce_flags |= htons(CE_VALID);
68 + break;
69 + case UNMARK_VALID:
70 + active_cache[pos]->ce_flags &= ~htons(CE_VALID);
71 + break;
72 + }
73 + active_cache_changed = 1;
74 + return 0;
75 + }
76 + return -1;
77 +}
78 +
79 static int add_file_to_cache(const char *path)
80 {
81 int size, namelen, option, status;
@@ -94,6 +117,7 @@ static int add_file_to_cache(const char *path)
117 ce = xmalloc(size);
118 memset(ce, 0, size);
119 memcpy(ce->name, path, namelen);
120 + ce->ce_flags = htons(namelen);
121 fill_stat_cache_info(ce, &st);
122
123 ce->ce_mode = create_ce_mode(st.st_mode);
@@ -105,7 +129,6 @@ static int add_file_to_cache(const char *path)
129 if (0 <= pos)
130 ce->ce_mode = active_cache[pos]->ce_mode;
131 }
108 - ce->ce_flags = htons(namelen);
132
133 if (index_path(ce->sha1, path, &st, !info_only))
134 return -1;
@@ -128,7 +151,7 @@ static int add_file_to_cache(const char *path)
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 */
131 -static struct cache_entry *refresh_entry(struct cache_entry *ce)
154 +static struct cache_entry *refresh_entry(struct cache_entry *ce, int really)
155 {
156 struct stat st;
157 struct cache_entry *updated;
@@ -137,21 +160,22 @@ static struct cache_entry *refresh_entry(struct cache_entry *ce)
160 if (lstat(ce->name, &st) < 0)
161 return ERR_PTR(-errno);
162
140 - changed = ce_match_stat(ce, &st);
163 + changed = ce_match_stat(ce, &st, really);
164 if (!changed)
165 return NULL;
166
144 - if (ce_modified(ce, &st))
167 + if (ce_modified(ce, &st, really))
168 return ERR_PTR(-EINVAL);
169
170 size = ce_size(ce);
171 updated = xmalloc(size);
172 memcpy(updated, ce, size);
173 fill_stat_cache_info(updated, &st);
174 +
175 return updated;
176 }
177
154 -static int refresh_cache(void)
178 +static int refresh_cache(int really)
179 {
180 int i;
181 int has_errors = 0;
@@ -171,12 +195,19 @@ static int refresh_cache(void)
195 continue;
196 }
197
174 - new = refresh_entry(ce);
198 + new = refresh_entry(ce, really);
199 if (!new)
200 continue;
201 if (IS_ERR(new)) {
202 if (not_new && PTR_ERR(new) == -ENOENT)
203 continue;
204 + if (really && PTR_ERR(new) == -EINVAL) {
205 + /* If we are doing --really-refresh that
206 + * means the index is not valid anymore.
207 + */
208 + ce->ce_flags &= ~htons(CE_VALID);
209 + active_cache_changed = 1;
210 + }
211 if (quiet)
212 continue;
213 printf("%s: needs update\n", ce->name);
@@ -274,6 +305,8 @@ static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
305 memcpy(ce->name, path, len);
306 ce->ce_flags = create_ce_flags(len, stage);
307 ce->ce_mode = create_ce_mode(mode);
308 + if (assume_unchanged)
309 + ce->ce_flags |= htons(CE_VALID);
310 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
311 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
312 if (add_cache_entry(ce, option))
@@ -317,6 +350,12 @@ static void update_one(const char *path, const char *prefix, int prefix_length)
350 fprintf(stderr, "Ignoring path %s\n", path);
351 return;
352 }
353 + if (mark_valid_only) {
354 + if (mark_valid(p))
355 + die("Unable to mark file %s", path);
356 + return;
357 + }
358 +
359 if (force_remove) {
360 if (remove_file_from_cache(p))
361 die("git-update-index: unable to remove %s", path);
@@ -467,7 +506,11 @@ int main(int argc, const char **argv)
506 continue;
507 }
508 if (!strcmp(path, "--refresh")) {
470 - has_errors |= refresh_cache();
509 + has_errors |= refresh_cache(0);
510 + continue;
511 + }
512 + if (!strcmp(path, "--really-refresh")) {
513 + has_errors |= refresh_cache(1);
514 continue;
515 }
516 if (!strcmp(path, "--cacheinfo")) {
@@ -493,6 +536,14 @@ int main(int argc, const char **argv)
536 die("git-update-index: %s cannot chmod %s", path, argv[i]);
537 continue;
538 }
539 + if (!strcmp(path, "--assume-unchanged")) {
540 + mark_valid_only = MARK_VALID;
541 + continue;
542 + }
543 + if (!strcmp(path, "--no-assume-unchanged")) {
544 + mark_valid_only = UNMARK_VALID;
545 + continue;
546 + }
547 if (!strcmp(path, "--info-only")) {
548 info_only = 1;
549 continue;
write-tree.c
+1 -1
@@ -111,7 +111,7 @@ int main(int argc, char **argv)
111 funny = 0;
112 for (i = 0; i < entries; i++) {
113 struct cache_entry *ce = active_cache[i];
114 - if (ntohs(ce->ce_flags) & ~CE_NAMEMASK) {
114 + if (ce_stage(ce)) {
115 if (10 < ++funny) {
116 fprintf(stderr, "...\n");
117 break;