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