]>
Commit | Line | Data |
---|---|---|
8bc9a0c7 LT |
1 | /* |
2 | * GIT - The information manager from hell | |
3 | * | |
4 | * Copyright (C) Linus Torvalds, 2005 | |
5 | */ | |
e83c5163 | 6 | #include "cache.h" |
973d6a20 | 7 | #include "quote.h" |
a6e5642f | 8 | #include "cache-tree.h" |
2bd452d3 | 9 | #include "tree-walk.h" |
fefe81c9 | 10 | #include "builtin.h" |
e011054b | 11 | #include "refs.h" |
4a39f79d | 12 | #include "resolve-undo.h" |
309be813 | 13 | #include "parse-options.h" |
e83c5163 | 14 | |
121481ab LT |
15 | /* |
16 | * Default to not allowing changes to the list of files. The | |
17 | * tool doesn't actually care, but this makes it harder to add | |
18 | * files to the revision control by mistake by doing something | |
f18d244a | 19 | * like "git update-index *" and suddenly having all the object |
121481ab LT |
20 | * files be revision controlled. |
21 | */ | |
caf4f582 JH |
22 | static int allow_add; |
23 | static int allow_remove; | |
24 | static int allow_replace; | |
caf4f582 | 25 | static int info_only; |
9b63f501 | 26 | static int force_remove; |
caf4f582 | 27 | static int verbose; |
96f1e58f | 28 | static int mark_valid_only; |
44a36913 | 29 | static int mark_skip_worktree_only; |
83b327ba NTND |
30 | #define MARK_FLAG 1 |
31 | #define UNMARK_FLAG 2 | |
5f73076c | 32 | |
28bea9e5 | 33 | __attribute__((format (printf, 1, 2))) |
caf4f582 JH |
34 | static void report(const char *fmt, ...) |
35 | { | |
36 | va_list vp; | |
37 | ||
38 | if (!verbose) | |
39 | return; | |
40 | ||
41 | va_start(vp, fmt); | |
42 | vprintf(fmt, vp); | |
43 | putchar('\n'); | |
44 | va_end(vp); | |
45 | } | |
46 | ||
83b327ba | 47 | static int mark_ce_flags(const char *path, int flag, int mark) |
5f73076c JH |
48 | { |
49 | int namelen = strlen(path); | |
50 | int pos = cache_name_pos(path, namelen); | |
51 | if (0 <= pos) { | |
83b327ba NTND |
52 | if (mark) |
53 | active_cache[pos]->ce_flags |= flag; | |
54 | else | |
55 | active_cache[pos]->ce_flags &= ~flag; | |
a6e5642f | 56 | cache_tree_invalidate_path(active_cache_tree, path); |
5f73076c JH |
57 | active_cache_changed = 1; |
58 | return 0; | |
59 | } | |
60 | return -1; | |
61 | } | |
62 | ||
e011054b | 63 | static int remove_one_path(const char *path) |
e83c5163 | 64 | { |
e011054b LT |
65 | if (!allow_remove) |
66 | return error("%s: does not exist and --remove not passed", path); | |
67 | if (remove_file_from_cache(path)) | |
68 | return error("%s: cannot remove from the index", path); | |
69 | return 0; | |
70 | } | |
a6e5642f | 71 | |
e011054b LT |
72 | /* |
73 | * Handle a path that couldn't be lstat'ed. It's either: | |
74 | * - missing file (ENOENT or ENOTDIR). That's ok if we're | |
75 | * supposed to be removing it and the removal actually | |
76 | * succeeds. | |
77 | * - permission error. That's never ok. | |
78 | */ | |
79 | static int process_lstat_error(const char *path, int err) | |
80 | { | |
81 | if (err == ENOENT || err == ENOTDIR) | |
82 | return remove_one_path(path); | |
83 | return error("lstat(\"%s\"): %s", path, strerror(errno)); | |
84 | } | |
a6e5642f | 85 | |
e011054b LT |
86 | static int add_one_path(struct cache_entry *old, const char *path, int len, struct stat *st) |
87 | { | |
22631473 LT |
88 | int option, size; |
89 | struct cache_entry *ce; | |
3e09cdfd | 90 | |
22631473 LT |
91 | /* Was the old index entry already up-to-date? */ |
92 | if (old && !ce_stage(old) && !ce_match_stat(old, st, 0)) | |
93 | return 0; | |
94 | ||
95 | size = cache_entry_size(len); | |
96 | ce = xcalloc(1, size); | |
e011054b | 97 | memcpy(ce->name, path, len); |
7a51ed66 | 98 | ce->ce_flags = len; |
e011054b LT |
99 | fill_stat_cache_info(ce, st); |
100 | ce->ce_mode = ce_mode_from_stat(old, st->st_mode); | |
ec1fcc16 | 101 | |
c4ce46fc | 102 | if (index_path(ce->sha1, path, st, |
dc4cd767 JM |
103 | info_only ? 0 : HASH_WRITE_OBJECT)) { |
104 | free(ce); | |
ec1fcc16 | 105 | return -1; |
dc4cd767 | 106 | } |
192268c1 JH |
107 | option = allow_add ? ADD_CACHE_OK_TO_ADD : 0; |
108 | option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0; | |
34143b26 | 109 | if (add_cache_entry(ce, option)) |
e011054b | 110 | return error("%s: cannot add to the index - missing --add option?", path); |
34143b26 | 111 | return 0; |
121481ab LT |
112 | } |
113 | ||
e011054b LT |
114 | /* |
115 | * Handle a path that was a directory. Four cases: | |
116 | * | |
117 | * - it's already a gitlink in the index, and we keep it that | |
118 | * way, and update it if we can (if we cannot find the HEAD, | |
119 | * we're going to keep it unchanged in the index!) | |
120 | * | |
121 | * - it's a *file* in the index, in which case it should be | |
122 | * removed as a file if removal is allowed, since it doesn't | |
123 | * exist as such any more. If removal isn't allowed, it's | |
124 | * an error. | |
125 | * | |
126 | * (NOTE! This is old and arguably fairly strange behaviour. | |
127 | * We might want to make this an error unconditionally, and | |
128 | * use "--force-remove" if you actually want to force removal). | |
129 | * | |
130 | * - it used to exist as a subdirectory (ie multiple files with | |
131 | * this particular prefix) in the index, in which case it's wrong | |
132 | * to try to update it as a directory. | |
133 | * | |
134 | * - it doesn't exist at all in the index, but it is a valid | |
135 | * git directory, and it should be *added* as a gitlink. | |
136 | */ | |
137 | static int process_directory(const char *path, int len, struct stat *st) | |
138 | { | |
139 | unsigned char sha1[20]; | |
140 | int pos = cache_name_pos(path, len); | |
141 | ||
142 | /* Exact match: file or existing gitlink */ | |
143 | if (pos >= 0) { | |
144 | struct cache_entry *ce = active_cache[pos]; | |
7a51ed66 | 145 | if (S_ISGITLINK(ce->ce_mode)) { |
e011054b LT |
146 | |
147 | /* Do nothing to the index if there is no HEAD! */ | |
148 | if (resolve_gitlink_ref(path, "HEAD", sha1) < 0) | |
149 | return 0; | |
150 | ||
151 | return add_one_path(ce, path, len, st); | |
152 | } | |
153 | /* Should this be an unconditional error? */ | |
154 | return remove_one_path(path); | |
155 | } | |
156 | ||
157 | /* Inexact match: is there perhaps a subdirectory match? */ | |
158 | pos = -pos-1; | |
159 | while (pos < active_nr) { | |
160 | struct cache_entry *ce = active_cache[pos++]; | |
161 | ||
162 | if (strncmp(ce->name, path, len)) | |
163 | break; | |
164 | if (ce->name[len] > '/') | |
165 | break; | |
166 | if (ce->name[len] < '/') | |
167 | continue; | |
168 | ||
169 | /* Subdirectory match - error out */ | |
170 | return error("%s: is a directory - add individual files instead", path); | |
171 | } | |
172 | ||
173 | /* No match - should we add it as a gitlink? */ | |
174 | if (!resolve_gitlink_ref(path, "HEAD", sha1)) | |
175 | return add_one_path(NULL, path, len, st); | |
176 | ||
177 | /* Error out. */ | |
178 | return error("%s: is a directory - add files inside instead", path); | |
179 | } | |
180 | ||
e011054b LT |
181 | static int process_path(const char *path) |
182 | { | |
b4d1690d | 183 | int pos, len; |
e011054b | 184 | struct stat st; |
b4d1690d | 185 | struct cache_entry *ce; |
e011054b | 186 | |
806d13b1 | 187 | len = strlen(path); |
57199892 | 188 | if (has_symlink_leading_path(path, len)) |
806d13b1 JH |
189 | return error("'%s' is beyond a symbolic link", path); |
190 | ||
b4d1690d NTND |
191 | pos = cache_name_pos(path, len); |
192 | ce = pos < 0 ? NULL : active_cache[pos]; | |
193 | if (ce && ce_skip_worktree(ce)) { | |
194 | /* | |
195 | * working directory version is assumed "good" | |
196 | * so updating it does not make sense. | |
197 | * On the other hand, removing it from index should work | |
198 | */ | |
199 | if (allow_remove && remove_file_from_cache(path)) | |
200 | return error("%s: cannot remove from the index", path); | |
201 | return 0; | |
202 | } | |
203 | ||
e011054b LT |
204 | /* |
205 | * First things first: get the stat information, to decide | |
206 | * what to do about the pathname! | |
207 | */ | |
208 | if (lstat(path, &st) < 0) | |
209 | return process_lstat_error(path, errno); | |
210 | ||
e011054b LT |
211 | if (S_ISDIR(st.st_mode)) |
212 | return process_directory(path, len, &st); | |
213 | ||
b4d1690d NTND |
214 | /* |
215 | * Process a regular file | |
216 | */ | |
217 | if (ce && S_ISGITLINK(ce->ce_mode)) | |
218 | return error("%s is already a gitlink, not replacing", path); | |
219 | ||
220 | return add_one_path(ce, path, len, &st); | |
e011054b LT |
221 | } |
222 | ||
d23748a6 JH |
223 | static int add_cacheinfo(unsigned int mode, const unsigned char *sha1, |
224 | const char *path, int stage) | |
9945d980 | 225 | { |
192268c1 | 226 | int size, len, option; |
9945d980 LT |
227 | struct cache_entry *ce; |
228 | ||
d23748a6 | 229 | if (!verify_path(path)) |
7e7abea9 | 230 | return error("Invalid path '%s'", path); |
9945d980 | 231 | |
d23748a6 | 232 | len = strlen(path); |
9945d980 | 233 | size = cache_entry_size(len); |
90321c10 | 234 | ce = xcalloc(1, size); |
9945d980 | 235 | |
e702496e | 236 | hashcpy(ce->sha1, sha1); |
d23748a6 JH |
237 | memcpy(ce->name, path, len); |
238 | ce->ce_flags = create_ce_flags(len, stage); | |
e4479470 | 239 | ce->ce_mode = create_ce_mode(mode); |
5f73076c | 240 | if (assume_unchanged) |
7a51ed66 | 241 | ce->ce_flags |= CE_VALID; |
192268c1 JH |
242 | option = allow_add ? ADD_CACHE_OK_TO_ADD : 0; |
243 | option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0; | |
caf4f582 JH |
244 | if (add_cache_entry(ce, option)) |
245 | return error("%s: cannot add to the index - missing --add option?", | |
d23748a6 JH |
246 | path); |
247 | report("add '%s'", path); | |
caf4f582 | 248 | return 0; |
9945d980 LT |
249 | } |
250 | ||
227bdb18 | 251 | static void chmod_path(int flip, const char *path) |
3e09cdfd JH |
252 | { |
253 | int pos; | |
254 | struct cache_entry *ce; | |
255 | unsigned int mode; | |
f2a19340 | 256 | |
3e09cdfd JH |
257 | pos = cache_name_pos(path, strlen(path)); |
258 | if (pos < 0) | |
227bdb18 | 259 | goto fail; |
3e09cdfd | 260 | ce = active_cache[pos]; |
7a51ed66 | 261 | mode = ce->ce_mode; |
3e09cdfd | 262 | if (!S_ISREG(mode)) |
227bdb18 | 263 | goto fail; |
3e09cdfd JH |
264 | switch (flip) { |
265 | case '+': | |
7a51ed66 | 266 | ce->ce_mode |= 0111; break; |
3e09cdfd | 267 | case '-': |
7a51ed66 | 268 | ce->ce_mode &= ~0111; break; |
3e09cdfd | 269 | default: |
227bdb18 | 270 | goto fail; |
3e09cdfd | 271 | } |
a6e5642f | 272 | cache_tree_invalidate_path(active_cache_tree, path); |
3e09cdfd | 273 | active_cache_changed = 1; |
227bdb18 AR |
274 | report("chmod %cx '%s'", flip, path); |
275 | return; | |
276 | fail: | |
7e44c935 | 277 | die("git update-index: cannot chmod %cx '%s'", flip, path); |
3e09cdfd JH |
278 | } |
279 | ||
ee1bec3d JH |
280 | static void update_one(const char *path, const char *prefix, int prefix_length) |
281 | { | |
282 | const char *p = prefix_path(prefix, prefix_length, path); | |
283 | if (!verify_path(p)) { | |
284 | fprintf(stderr, "Ignoring path %s\n", path); | |
fb69a760 | 285 | goto free_return; |
ee1bec3d | 286 | } |
5f73076c | 287 | if (mark_valid_only) { |
83b327ba | 288 | if (mark_ce_flags(p, CE_VALID, mark_valid_only == MARK_FLAG)) |
5f73076c | 289 | die("Unable to mark file %s", path); |
fb69a760 | 290 | goto free_return; |
5f73076c | 291 | } |
44a36913 NTND |
292 | if (mark_skip_worktree_only) { |
293 | if (mark_ce_flags(p, CE_SKIP_WORKTREE, mark_skip_worktree_only == MARK_FLAG)) | |
5f73076c | 294 | die("Unable to mark file %s", path); |
fb69a760 | 295 | goto free_return; |
5f73076c JH |
296 | } |
297 | ||
ee1bec3d JH |
298 | if (force_remove) { |
299 | if (remove_file_from_cache(p)) | |
7e44c935 | 300 | die("git update-index: unable to remove %s", path); |
caf4f582 | 301 | report("remove '%s'", path); |
fb69a760 | 302 | goto free_return; |
ee1bec3d | 303 | } |
e011054b LT |
304 | if (process_path(p)) |
305 | die("Unable to process path %s", path); | |
caf4f582 | 306 | report("add '%s'", path); |
fb69a760 | 307 | free_return: |
0cc9e70c | 308 | if (p < path || p > path + strlen(path)) |
4b25d091 | 309 | free((char *)p); |
ee1bec3d JH |
310 | } |
311 | ||
d4dbf36d JH |
312 | static void read_index_info(int line_termination) |
313 | { | |
f285a2d7 BC |
314 | struct strbuf buf = STRBUF_INIT; |
315 | struct strbuf uq = STRBUF_INIT; | |
7fb1011e | 316 | |
7fb1011e | 317 | while (strbuf_getline(&buf, stdin, line_termination) != EOF) { |
9c20a470 | 318 | char *ptr, *tab; |
973d6a20 | 319 | char *path_name; |
d4dbf36d JH |
320 | unsigned char sha1[20]; |
321 | unsigned int mode; | |
6aead43d | 322 | unsigned long ul; |
d23748a6 JH |
323 | int stage; |
324 | ||
325 | /* This reads lines formatted in one of three formats: | |
326 | * | |
327 | * (1) mode SP sha1 TAB path | |
f18d244a | 328 | * The first format is what "git apply --index-info" |
d23748a6 JH |
329 | * reports, and used to reconstruct a partial tree |
330 | * that is used for phony merge base tree when falling | |
331 | * back on 3-way merge. | |
332 | * | |
333 | * (2) mode SP type SP sha1 TAB path | |
f18d244a | 334 | * The second format is to stuff "git ls-tree" output |
d23748a6 | 335 | * into the index file. |
fefe81c9 | 336 | * |
d23748a6 JH |
337 | * (3) mode SP sha1 SP stage TAB path |
338 | * This format is to put higher order stages into the | |
f18d244a | 339 | * index file and matches "git ls-files --stage" output. |
d23748a6 | 340 | */ |
6aead43d JM |
341 | errno = 0; |
342 | ul = strtoul(buf.buf, &ptr, 8); | |
343 | if (ptr == buf.buf || *ptr != ' ' | |
344 | || errno || (unsigned int) ul != ul) | |
d4dbf36d | 345 | goto bad_line; |
6aead43d | 346 | mode = ul; |
d4dbf36d | 347 | |
9c20a470 JH |
348 | tab = strchr(ptr, '\t'); |
349 | if (!tab || tab - ptr < 41) | |
350 | goto bad_line; | |
d23748a6 | 351 | |
2d497115 | 352 | if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') { |
d23748a6 JH |
353 | stage = tab[-1] - '0'; |
354 | ptr = tab + 1; /* point at the head of path */ | |
355 | tab = tab - 2; /* point at tail of sha1 */ | |
356 | } | |
357 | else { | |
358 | stage = 0; | |
359 | ptr = tab + 1; /* point at the head of path */ | |
360 | } | |
361 | ||
9c20a470 JH |
362 | if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ') |
363 | goto bad_line; | |
973d6a20 | 364 | |
7fb1011e PH |
365 | path_name = ptr; |
366 | if (line_termination && path_name[0] == '"') { | |
367 | strbuf_reset(&uq); | |
368 | if (unquote_c_style(&uq, path_name, NULL)) { | |
7e44c935 | 369 | die("git update-index: bad quoting of path name"); |
7fb1011e PH |
370 | } |
371 | path_name = uq.buf; | |
372 | } | |
973d6a20 JH |
373 | |
374 | if (!verify_path(path_name)) { | |
375 | fprintf(stderr, "Ignoring path %s\n", path_name); | |
d4dbf36d JH |
376 | continue; |
377 | } | |
378 | ||
379 | if (!mode) { | |
380 | /* mode == 0 means there is no such path -- remove */ | |
973d6a20 | 381 | if (remove_file_from_cache(path_name)) |
7e44c935 | 382 | die("git update-index: unable to remove %s", |
d4dbf36d JH |
383 | ptr); |
384 | } | |
385 | else { | |
386 | /* mode ' ' sha1 '\t' name | |
387 | * ptr[-1] points at tab, | |
388 | * ptr[-41] is at the beginning of sha1 | |
389 | */ | |
390 | ptr[-42] = ptr[-1] = 0; | |
d23748a6 | 391 | if (add_cacheinfo(mode, sha1, path_name, stage)) |
7e44c935 | 392 | die("git update-index: unable to update %s", |
973d6a20 | 393 | path_name); |
d4dbf36d JH |
394 | } |
395 | continue; | |
396 | ||
397 | bad_line: | |
398 | die("malformed index info %s", buf.buf); | |
399 | } | |
e6c019d0 | 400 | strbuf_release(&buf); |
7fb1011e | 401 | strbuf_release(&uq); |
d4dbf36d JH |
402 | } |
403 | ||
309be813 JN |
404 | static const char * const update_index_usage[] = { |
405 | "git update-index [options] [--] [<file>...]", | |
406 | NULL | |
407 | }; | |
f6ab5bb2 | 408 | |
2bd452d3 JH |
409 | static unsigned char head_sha1[20]; |
410 | static unsigned char merge_head_sha1[20]; | |
411 | ||
412 | static struct cache_entry *read_one_ent(const char *which, | |
413 | unsigned char *ent, const char *path, | |
414 | int namelen, int stage) | |
415 | { | |
416 | unsigned mode; | |
417 | unsigned char sha1[20]; | |
418 | int size; | |
419 | struct cache_entry *ce; | |
420 | ||
421 | if (get_tree_entry(ent, path, sha1, &mode)) { | |
83e77a25 JH |
422 | if (which) |
423 | error("%s: not in %s branch.", path, which); | |
2bd452d3 JH |
424 | return NULL; |
425 | } | |
426 | if (mode == S_IFDIR) { | |
83e77a25 JH |
427 | if (which) |
428 | error("%s: not a blob in %s branch.", path, which); | |
2bd452d3 JH |
429 | return NULL; |
430 | } | |
431 | size = cache_entry_size(namelen); | |
432 | ce = xcalloc(1, size); | |
433 | ||
e702496e | 434 | hashcpy(ce->sha1, sha1); |
2bd452d3 JH |
435 | memcpy(ce->name, path, namelen); |
436 | ce->ce_flags = create_ce_flags(namelen, stage); | |
437 | ce->ce_mode = create_ce_mode(mode); | |
438 | return ce; | |
439 | } | |
440 | ||
441 | static int unresolve_one(const char *path) | |
442 | { | |
443 | int namelen = strlen(path); | |
444 | int pos; | |
445 | int ret = 0; | |
446 | struct cache_entry *ce_2 = NULL, *ce_3 = NULL; | |
447 | ||
448 | /* See if there is such entry in the index. */ | |
449 | pos = cache_name_pos(path, namelen); | |
8aa38563 JH |
450 | if (0 <= pos) { |
451 | /* already merged */ | |
452 | pos = unmerge_cache_entry_at(pos); | |
453 | if (pos < active_nr) { | |
454 | struct cache_entry *ce = active_cache[pos]; | |
455 | if (ce_stage(ce) && | |
456 | ce_namelen(ce) == namelen && | |
457 | !memcmp(ce->name, path, namelen)) | |
458 | return 0; | |
459 | } | |
460 | /* no resolve-undo information; fall back */ | |
461 | } else { | |
2bd452d3 JH |
462 | /* If there isn't, either it is unmerged, or |
463 | * resolved as "removed" by mistake. We do not | |
464 | * want to do anything in the former case. | |
465 | */ | |
466 | pos = -pos-1; | |
467 | if (pos < active_nr) { | |
468 | struct cache_entry *ce = active_cache[pos]; | |
469 | if (ce_namelen(ce) == namelen && | |
470 | !memcmp(ce->name, path, namelen)) { | |
471 | fprintf(stderr, | |
472 | "%s: skipping still unmerged path.\n", | |
473 | path); | |
474 | goto free_return; | |
475 | } | |
476 | } | |
477 | } | |
478 | ||
479 | /* Grab blobs from given path from HEAD and MERGE_HEAD, | |
480 | * stuff HEAD version in stage #2, | |
481 | * stuff MERGE_HEAD version in stage #3. | |
482 | */ | |
483 | ce_2 = read_one_ent("our", head_sha1, path, namelen, 2); | |
484 | ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3); | |
485 | ||
486 | if (!ce_2 || !ce_3) { | |
487 | ret = -1; | |
488 | goto free_return; | |
489 | } | |
a89fccd2 | 490 | if (!hashcmp(ce_2->sha1, ce_3->sha1) && |
2bd452d3 JH |
491 | ce_2->ce_mode == ce_3->ce_mode) { |
492 | fprintf(stderr, "%s: identical in both, skipping.\n", | |
493 | path); | |
494 | goto free_return; | |
495 | } | |
496 | ||
497 | remove_file_from_cache(path); | |
498 | if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) { | |
499 | error("%s: cannot add our version to the index.", path); | |
500 | ret = -1; | |
501 | goto free_return; | |
502 | } | |
503 | if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD)) | |
504 | return 0; | |
505 | error("%s: cannot add their version to the index.", path); | |
506 | ret = -1; | |
507 | free_return: | |
508 | free(ce_2); | |
509 | free(ce_3); | |
510 | return ret; | |
511 | } | |
512 | ||
513 | static void read_head_pointers(void) | |
514 | { | |
913c983e | 515 | if (read_ref("HEAD", head_sha1)) |
d7530708 | 516 | die("No HEAD -- no initial commit yet?"); |
913c983e | 517 | if (read_ref("MERGE_HEAD", merge_head_sha1)) { |
2bd452d3 JH |
518 | fprintf(stderr, "Not in the middle of a merge.\n"); |
519 | exit(0); | |
520 | } | |
521 | } | |
522 | ||
09895c1f JH |
523 | static int do_unresolve(int ac, const char **av, |
524 | const char *prefix, int prefix_length) | |
2bd452d3 JH |
525 | { |
526 | int i; | |
527 | int err = 0; | |
528 | ||
529 | /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we | |
530 | * are not doing a merge, so exit with success status. | |
531 | */ | |
532 | read_head_pointers(); | |
533 | ||
534 | for (i = 1; i < ac; i++) { | |
535 | const char *arg = av[i]; | |
09895c1f JH |
536 | const char *p = prefix_path(prefix, prefix_length, arg); |
537 | err |= unresolve_one(p); | |
0cc9e70c | 538 | if (p < arg || p > arg + strlen(arg)) |
4b25d091 | 539 | free((char *)p); |
2bd452d3 JH |
540 | } |
541 | return err; | |
542 | } | |
543 | ||
83e77a25 JH |
544 | static int do_reupdate(int ac, const char **av, |
545 | const char *prefix, int prefix_length) | |
546 | { | |
547 | /* Read HEAD and run update-index on paths that are | |
548 | * merged and already different between index and HEAD. | |
549 | */ | |
550 | int pos; | |
551 | int has_head = 1; | |
eb9cb55b NTND |
552 | const char **paths = get_pathspec(prefix, av + 1); |
553 | struct pathspec pathspec; | |
554 | ||
555 | init_pathspec(&pathspec, paths); | |
83e77a25 | 556 | |
913c983e | 557 | if (read_ref("HEAD", head_sha1)) |
83e77a25 JH |
558 | /* If there is no HEAD, that means it is an initial |
559 | * commit. Update everything in the index. | |
560 | */ | |
561 | has_head = 0; | |
562 | redo: | |
563 | for (pos = 0; pos < active_nr; pos++) { | |
564 | struct cache_entry *ce = active_cache[pos]; | |
565 | struct cache_entry *old = NULL; | |
566 | int save_nr; | |
22293b9c | 567 | |
eb9cb55b | 568 | if (ce_stage(ce) || !ce_path_match(ce, &pathspec)) |
83e77a25 JH |
569 | continue; |
570 | if (has_head) | |
571 | old = read_one_ent(NULL, head_sha1, | |
572 | ce->name, ce_namelen(ce), 0); | |
573 | if (old && ce->ce_mode == old->ce_mode && | |
a89fccd2 | 574 | !hashcmp(ce->sha1, old->sha1)) { |
83e77a25 JH |
575 | free(old); |
576 | continue; /* unchanged */ | |
577 | } | |
578 | /* Be careful. The working tree may not have the | |
579 | * path anymore, in which case, under 'allow_remove', | |
580 | * or worse yet 'allow_replace', active_nr may decrease. | |
581 | */ | |
582 | save_nr = active_nr; | |
583 | update_one(ce->name + prefix_length, prefix, prefix_length); | |
584 | if (save_nr != active_nr) | |
585 | goto redo; | |
586 | } | |
eb9cb55b | 587 | free_pathspec(&pathspec); |
83e77a25 JH |
588 | return 0; |
589 | } | |
590 | ||
309be813 JN |
591 | struct refresh_params { |
592 | unsigned int flags; | |
593 | int *has_errors; | |
594 | }; | |
595 | ||
596 | static int refresh(struct refresh_params *o, unsigned int flag) | |
597 | { | |
598 | setup_work_tree(); | |
599 | *o->has_errors |= refresh_cache(o->flags | flag); | |
600 | return 0; | |
601 | } | |
602 | ||
603 | static int refresh_callback(const struct option *opt, | |
604 | const char *arg, int unset) | |
605 | { | |
606 | return refresh(opt->value, 0); | |
607 | } | |
608 | ||
609 | static int really_refresh_callback(const struct option *opt, | |
610 | const char *arg, int unset) | |
611 | { | |
612 | return refresh(opt->value, REFRESH_REALLY); | |
613 | } | |
614 | ||
615 | static int chmod_callback(const struct option *opt, | |
616 | const char *arg, int unset) | |
617 | { | |
618 | char *flip = opt->value; | |
619 | if ((arg[0] != '-' && arg[0] != '+') || arg[1] != 'x' || arg[2]) | |
620 | return error("option 'chmod' expects \"+x\" or \"-x\""); | |
621 | *flip = arg[0]; | |
622 | return 0; | |
623 | } | |
624 | ||
625 | static int resolve_undo_clear_callback(const struct option *opt, | |
626 | const char *arg, int unset) | |
627 | { | |
628 | resolve_undo_clear(); | |
629 | return 0; | |
630 | } | |
631 | ||
632 | static int cacheinfo_callback(struct parse_opt_ctx_t *ctx, | |
633 | const struct option *opt, int unset) | |
634 | { | |
635 | unsigned char sha1[20]; | |
636 | unsigned int mode; | |
637 | ||
638 | if (ctx->argc <= 3) | |
639 | return error("option 'cacheinfo' expects three arguments"); | |
640 | if (strtoul_ui(*++ctx->argv, 8, &mode) || | |
641 | get_sha1_hex(*++ctx->argv, sha1) || | |
642 | add_cacheinfo(mode, sha1, *++ctx->argv, 0)) | |
643 | die("git update-index: --cacheinfo cannot add %s", *ctx->argv); | |
644 | ctx->argc -= 3; | |
645 | return 0; | |
646 | } | |
647 | ||
648 | static int stdin_cacheinfo_callback(struct parse_opt_ctx_t *ctx, | |
649 | const struct option *opt, int unset) | |
650 | { | |
651 | int *line_termination = opt->value; | |
652 | ||
653 | if (ctx->argc != 1) | |
654 | return error("option '%s' must be the last argument", opt->long_name); | |
655 | allow_add = allow_replace = allow_remove = 1; | |
656 | read_index_info(*line_termination); | |
657 | return 0; | |
658 | } | |
659 | ||
660 | static int stdin_callback(struct parse_opt_ctx_t *ctx, | |
661 | const struct option *opt, int unset) | |
662 | { | |
663 | int *read_from_stdin = opt->value; | |
664 | ||
665 | if (ctx->argc != 1) | |
666 | return error("option '%s' must be the last argument", opt->long_name); | |
667 | *read_from_stdin = 1; | |
668 | return 0; | |
669 | } | |
670 | ||
671 | static int unresolve_callback(struct parse_opt_ctx_t *ctx, | |
672 | const struct option *opt, int flags) | |
673 | { | |
674 | int *has_errors = opt->value; | |
675 | const char *prefix = startup_info->prefix; | |
676 | ||
677 | /* consume remaining arguments. */ | |
678 | *has_errors = do_unresolve(ctx->argc, ctx->argv, | |
679 | prefix, prefix ? strlen(prefix) : 0); | |
680 | if (*has_errors) | |
681 | active_cache_changed = 0; | |
682 | ||
683 | ctx->argv += ctx->argc - 1; | |
684 | ctx->argc = 1; | |
685 | return 0; | |
686 | } | |
687 | ||
688 | static int reupdate_callback(struct parse_opt_ctx_t *ctx, | |
689 | const struct option *opt, int flags) | |
690 | { | |
691 | int *has_errors = opt->value; | |
692 | const char *prefix = startup_info->prefix; | |
693 | ||
694 | /* consume remaining arguments. */ | |
695 | setup_work_tree(); | |
696 | *has_errors = do_reupdate(ctx->argc, ctx->argv, | |
697 | prefix, prefix ? strlen(prefix) : 0); | |
698 | if (*has_errors) | |
699 | active_cache_changed = 0; | |
700 | ||
701 | ctx->argv += ctx->argc - 1; | |
702 | ctx->argc = 1; | |
703 | return 0; | |
704 | } | |
705 | ||
a633fca0 | 706 | int cmd_update_index(int argc, const char **argv, const char *prefix) |
e83c5163 | 707 | { |
309be813 | 708 | int newfd, entries, has_errors = 0, line_termination = '\n'; |
ee1bec3d | 709 | int read_from_stdin = 0; |
ee1bec3d | 710 | int prefix_length = prefix ? strlen(prefix) : 0; |
227bdb18 | 711 | char set_executable_bit = 0; |
309be813 | 712 | struct refresh_params refresh_args = {0, &has_errors}; |
7b802b86 | 713 | int lock_error = 0; |
fefe81c9 | 714 | struct lock_file *lock_file; |
309be813 JN |
715 | struct parse_opt_ctx_t ctx; |
716 | int parseopt_state = PARSE_OPT_UNKNOWN; | |
717 | struct option options[] = { | |
718 | OPT_BIT('q', NULL, &refresh_args.flags, | |
719 | "continue refresh even when index needs update", | |
720 | REFRESH_QUIET), | |
721 | OPT_BIT(0, "ignore-submodules", &refresh_args.flags, | |
722 | "refresh: ignore submodules", | |
723 | REFRESH_IGNORE_SUBMODULES), | |
724 | OPT_SET_INT(0, "add", &allow_add, | |
725 | "do not ignore new files", 1), | |
726 | OPT_SET_INT(0, "replace", &allow_replace, | |
727 | "let files replace directories and vice-versa", 1), | |
728 | OPT_SET_INT(0, "remove", &allow_remove, | |
729 | "notice files missing from worktree", 1), | |
730 | OPT_BIT(0, "unmerged", &refresh_args.flags, | |
731 | "refresh even if index contains unmerged entries", | |
732 | REFRESH_UNMERGED), | |
733 | {OPTION_CALLBACK, 0, "refresh", &refresh_args, NULL, | |
734 | "refresh stat information", | |
735 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, | |
736 | refresh_callback}, | |
737 | {OPTION_CALLBACK, 0, "really-refresh", &refresh_args, NULL, | |
738 | "like --refresh, but ignore assume-unchanged setting", | |
739 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, | |
740 | really_refresh_callback}, | |
741 | {OPTION_LOWLEVEL_CALLBACK, 0, "cacheinfo", NULL, | |
742 | "<mode> <object> <path>", | |
743 | "add the specified entry to the index", | |
744 | PARSE_OPT_NOARG | /* disallow --cacheinfo=<mode> form */ | |
745 | PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP, | |
746 | (parse_opt_cb *) cacheinfo_callback}, | |
747 | {OPTION_CALLBACK, 0, "chmod", &set_executable_bit, "(+/-)x", | |
748 | "override the executable bit of the listed files", | |
749 | PARSE_OPT_NONEG | PARSE_OPT_LITERAL_ARGHELP, | |
750 | chmod_callback}, | |
751 | {OPTION_SET_INT, 0, "assume-unchanged", &mark_valid_only, NULL, | |
752 | "mark files as \"not changing\"", | |
753 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG}, | |
754 | {OPTION_SET_INT, 0, "no-assume-unchanged", &mark_valid_only, NULL, | |
755 | "clear assumed-unchanged bit", | |
756 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG}, | |
757 | {OPTION_SET_INT, 0, "skip-worktree", &mark_skip_worktree_only, NULL, | |
758 | "mark files as \"index-only\"", | |
759 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, MARK_FLAG}, | |
760 | {OPTION_SET_INT, 0, "no-skip-worktree", &mark_skip_worktree_only, NULL, | |
761 | "clear skip-worktree bit", | |
762 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, UNMARK_FLAG}, | |
763 | OPT_SET_INT(0, "info-only", &info_only, | |
764 | "add to index only; do not add content to object database", 1), | |
765 | OPT_SET_INT(0, "force-remove", &force_remove, | |
766 | "remove named paths even if present in worktree", 1), | |
767 | OPT_SET_INT('z', NULL, &line_termination, | |
768 | "with --stdin: input lines are terminated by null bytes", '\0'), | |
769 | {OPTION_LOWLEVEL_CALLBACK, 0, "stdin", &read_from_stdin, NULL, | |
770 | "read list of paths to be updated from standard input", | |
771 | PARSE_OPT_NONEG | PARSE_OPT_NOARG, | |
772 | (parse_opt_cb *) stdin_callback}, | |
773 | {OPTION_LOWLEVEL_CALLBACK, 0, "index-info", &line_termination, NULL, | |
774 | "add entries from standard input to the index", | |
775 | PARSE_OPT_NONEG | PARSE_OPT_NOARG, | |
776 | (parse_opt_cb *) stdin_cacheinfo_callback}, | |
777 | {OPTION_LOWLEVEL_CALLBACK, 0, "unresolve", &has_errors, NULL, | |
778 | "repopulate stages #2 and #3 for the listed paths", | |
779 | PARSE_OPT_NONEG | PARSE_OPT_NOARG, | |
780 | (parse_opt_cb *) unresolve_callback}, | |
781 | {OPTION_LOWLEVEL_CALLBACK, 'g', "again", &has_errors, NULL, | |
782 | "only update entries that differ from HEAD", | |
783 | PARSE_OPT_NONEG | PARSE_OPT_NOARG, | |
784 | (parse_opt_cb *) reupdate_callback}, | |
785 | OPT_BIT(0, "ignore-missing", &refresh_args.flags, | |
786 | "ignore files missing from worktree", | |
787 | REFRESH_IGNORE_MISSING), | |
788 | OPT_SET_INT(0, "verbose", &verbose, | |
789 | "report actions to standard output", 1), | |
790 | {OPTION_CALLBACK, 0, "clear-resolve-undo", NULL, NULL, | |
791 | "(for porcelains) forget saved unresolved conflicts", | |
792 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, | |
793 | resolve_undo_clear_callback}, | |
794 | OPT_END() | |
795 | }; | |
bb233d69 | 796 | |
9c7c27ee | 797 | if (argc == 2 && !strcmp(argv[1], "-h")) |
6e67619d | 798 | usage(update_index_usage[0]); |
9c7c27ee | 799 | |
ef90d6d4 | 800 | git_config(git_default_config, NULL); |
3e09cdfd | 801 | |
fefe81c9 | 802 | /* We can't free this memory, it becomes part of a linked list parsed atexit() */ |
928e47e3 | 803 | lock_file = xcalloc(1, sizeof(struct lock_file)); |
fefe81c9 | 804 | |
30ca07a2 | 805 | newfd = hold_locked_index(lock_file, 0); |
7b802b86 JH |
806 | if (newfd < 0) |
807 | lock_error = errno; | |
9614b8dc | 808 | |
e83c5163 | 809 | entries = read_cache(); |
9614b8dc | 810 | if (entries < 0) |
2de381f9 | 811 | die("cache corrupted"); |
e83c5163 | 812 | |
309be813 JN |
813 | /* |
814 | * Custom copy of parse_options() because we want to handle | |
815 | * filename arguments as they come. | |
816 | */ | |
817 | parse_options_start(&ctx, argc, argv, prefix, | |
818 | options, PARSE_OPT_STOP_AT_NON_OPTION); | |
819 | while (ctx.argc) { | |
820 | if (parseopt_state != PARSE_OPT_DONE) | |
821 | parseopt_state = parse_options_step(&ctx, options, | |
822 | update_index_usage); | |
823 | if (!ctx.argc) | |
824 | break; | |
825 | switch (parseopt_state) { | |
826 | case PARSE_OPT_HELP: | |
827 | exit(129); | |
828 | case PARSE_OPT_NON_OPTION: | |
829 | case PARSE_OPT_DONE: | |
830 | { | |
831 | const char *path = ctx.argv[0]; | |
832 | const char *p; | |
121481ab | 833 | |
309be813 JN |
834 | setup_work_tree(); |
835 | p = prefix_path(prefix, prefix_length, path); | |
836 | update_one(p, NULL, 0); | |
837 | if (set_executable_bit) | |
838 | chmod_path(set_executable_bit, p); | |
839 | if (p < path || p > path + strlen(path)) | |
840 | free((char *)p); | |
841 | ctx.argc--; | |
842 | ctx.argv++; | |
843 | break; | |
844 | } | |
845 | case PARSE_OPT_UNKNOWN: | |
846 | if (ctx.argv[0][1] == '-') | |
847 | error("unknown option '%s'", ctx.argv[0] + 2); | |
848 | else | |
849 | error("unknown switch '%c'", *ctx.opt); | |
850 | usage_with_options(update_index_usage, options); | |
121481ab | 851 | } |
ee1bec3d | 852 | } |
309be813 JN |
853 | argc = parse_options_end(&ctx); |
854 | ||
ee1bec3d | 855 | if (read_from_stdin) { |
f285a2d7 | 856 | struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT; |
7fb1011e | 857 | |
f83eafdd | 858 | setup_work_tree(); |
7fb1011e | 859 | while (strbuf_getline(&buf, stdin, line_termination) != EOF) { |
fb69a760 | 860 | const char *p; |
7fb1011e PH |
861 | if (line_termination && buf.buf[0] == '"') { |
862 | strbuf_reset(&nbuf); | |
863 | if (unquote_c_style(&nbuf, buf.buf, NULL)) | |
864 | die("line is badly quoted"); | |
865 | strbuf_swap(&buf, &nbuf); | |
866 | } | |
867 | p = prefix_path(prefix, prefix_length, buf.buf); | |
fb69a760 JH |
868 | update_one(p, NULL, 0); |
869 | if (set_executable_bit) | |
227bdb18 | 870 | chmod_path(set_executable_bit, p); |
7fb1011e PH |
871 | if (p < buf.buf || p > buf.buf + buf.len) |
872 | free((char *)p); | |
9b63f501 | 873 | } |
7fb1011e | 874 | strbuf_release(&nbuf); |
e6c019d0 | 875 | strbuf_release(&buf); |
e83c5163 | 876 | } |
2bd452d3 | 877 | |
5cd5ace7 | 878 | if (active_cache_changed) { |
7b802b86 | 879 | if (newfd < 0) { |
309be813 | 880 | if (refresh_args.flags & REFRESH_QUIET) |
7b802b86 | 881 | exit(128); |
e43a6fd3 | 882 | unable_to_lock_index_die(get_index_file(), lock_error); |
7b802b86 | 883 | } |
5cd5ace7 | 884 | if (write_cache(newfd, active_cache, active_nr) || |
4ed7cd3a | 885 | commit_locked_index(lock_file)) |
021b6e45 | 886 | die("Unable to write new index file"); |
5cd5ace7 | 887 | } |
9614b8dc | 888 | |
fefe81c9 LS |
889 | rollback_lock_file(lock_file); |
890 | ||
c4b83e61 | 891 | return has_errors ? 1 : 0; |
e83c5163 | 892 | } |