]>
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" |
e83c5163 | 13 | |
121481ab LT |
14 | /* |
15 | * Default to not allowing changes to the list of files. The | |
16 | * tool doesn't actually care, but this makes it harder to add | |
17 | * files to the revision control by mistake by doing something | |
f18d244a | 18 | * like "git update-index *" and suddenly having all the object |
121481ab LT |
19 | * files be revision controlled. |
20 | */ | |
caf4f582 JH |
21 | static int allow_add; |
22 | static int allow_remove; | |
23 | static int allow_replace; | |
caf4f582 | 24 | static int info_only; |
9b63f501 | 25 | static int force_remove; |
caf4f582 | 26 | static int verbose; |
96f1e58f | 27 | static int mark_valid_only; |
44a36913 | 28 | static int mark_skip_worktree_only; |
83b327ba NTND |
29 | #define MARK_FLAG 1 |
30 | #define UNMARK_FLAG 2 | |
5f73076c | 31 | |
28bea9e5 | 32 | __attribute__((format (printf, 1, 2))) |
caf4f582 JH |
33 | static void report(const char *fmt, ...) |
34 | { | |
35 | va_list vp; | |
36 | ||
37 | if (!verbose) | |
38 | return; | |
39 | ||
40 | va_start(vp, fmt); | |
41 | vprintf(fmt, vp); | |
42 | putchar('\n'); | |
43 | va_end(vp); | |
44 | } | |
45 | ||
83b327ba | 46 | static int mark_ce_flags(const char *path, int flag, int mark) |
5f73076c JH |
47 | { |
48 | int namelen = strlen(path); | |
49 | int pos = cache_name_pos(path, namelen); | |
50 | if (0 <= pos) { | |
83b327ba NTND |
51 | if (mark) |
52 | active_cache[pos]->ce_flags |= flag; | |
53 | else | |
54 | active_cache[pos]->ce_flags &= ~flag; | |
a6e5642f | 55 | cache_tree_invalidate_path(active_cache_tree, path); |
5f73076c JH |
56 | active_cache_changed = 1; |
57 | return 0; | |
58 | } | |
59 | return -1; | |
60 | } | |
61 | ||
e011054b | 62 | static int remove_one_path(const char *path) |
e83c5163 | 63 | { |
e011054b LT |
64 | if (!allow_remove) |
65 | return error("%s: does not exist and --remove not passed", path); | |
66 | if (remove_file_from_cache(path)) | |
67 | return error("%s: cannot remove from the index", path); | |
68 | return 0; | |
69 | } | |
a6e5642f | 70 | |
e011054b LT |
71 | /* |
72 | * Handle a path that couldn't be lstat'ed. It's either: | |
73 | * - missing file (ENOENT or ENOTDIR). That's ok if we're | |
74 | * supposed to be removing it and the removal actually | |
75 | * succeeds. | |
76 | * - permission error. That's never ok. | |
77 | */ | |
78 | static int process_lstat_error(const char *path, int err) | |
79 | { | |
80 | if (err == ENOENT || err == ENOTDIR) | |
81 | return remove_one_path(path); | |
82 | return error("lstat(\"%s\"): %s", path, strerror(errno)); | |
83 | } | |
a6e5642f | 84 | |
e011054b LT |
85 | static int add_one_path(struct cache_entry *old, const char *path, int len, struct stat *st) |
86 | { | |
22631473 LT |
87 | int option, size; |
88 | struct cache_entry *ce; | |
3e09cdfd | 89 | |
22631473 LT |
90 | /* Was the old index entry already up-to-date? */ |
91 | if (old && !ce_stage(old) && !ce_match_stat(old, st, 0)) | |
92 | return 0; | |
93 | ||
94 | size = cache_entry_size(len); | |
95 | ce = xcalloc(1, size); | |
e011054b | 96 | memcpy(ce->name, path, len); |
7a51ed66 | 97 | ce->ce_flags = len; |
e011054b LT |
98 | fill_stat_cache_info(ce, st); |
99 | ce->ce_mode = ce_mode_from_stat(old, st->st_mode); | |
ec1fcc16 | 100 | |
e011054b | 101 | if (index_path(ce->sha1, path, st, !info_only)) |
ec1fcc16 | 102 | return -1; |
192268c1 JH |
103 | option = allow_add ? ADD_CACHE_OK_TO_ADD : 0; |
104 | option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0; | |
34143b26 | 105 | if (add_cache_entry(ce, option)) |
e011054b | 106 | return error("%s: cannot add to the index - missing --add option?", path); |
34143b26 | 107 | return 0; |
121481ab LT |
108 | } |
109 | ||
e011054b LT |
110 | /* |
111 | * Handle a path that was a directory. Four cases: | |
112 | * | |
113 | * - it's already a gitlink in the index, and we keep it that | |
114 | * way, and update it if we can (if we cannot find the HEAD, | |
115 | * we're going to keep it unchanged in the index!) | |
116 | * | |
117 | * - it's a *file* in the index, in which case it should be | |
118 | * removed as a file if removal is allowed, since it doesn't | |
119 | * exist as such any more. If removal isn't allowed, it's | |
120 | * an error. | |
121 | * | |
122 | * (NOTE! This is old and arguably fairly strange behaviour. | |
123 | * We might want to make this an error unconditionally, and | |
124 | * use "--force-remove" if you actually want to force removal). | |
125 | * | |
126 | * - it used to exist as a subdirectory (ie multiple files with | |
127 | * this particular prefix) in the index, in which case it's wrong | |
128 | * to try to update it as a directory. | |
129 | * | |
130 | * - it doesn't exist at all in the index, but it is a valid | |
131 | * git directory, and it should be *added* as a gitlink. | |
132 | */ | |
133 | static int process_directory(const char *path, int len, struct stat *st) | |
134 | { | |
135 | unsigned char sha1[20]; | |
136 | int pos = cache_name_pos(path, len); | |
137 | ||
138 | /* Exact match: file or existing gitlink */ | |
139 | if (pos >= 0) { | |
140 | struct cache_entry *ce = active_cache[pos]; | |
7a51ed66 | 141 | if (S_ISGITLINK(ce->ce_mode)) { |
e011054b LT |
142 | |
143 | /* Do nothing to the index if there is no HEAD! */ | |
144 | if (resolve_gitlink_ref(path, "HEAD", sha1) < 0) | |
145 | return 0; | |
146 | ||
147 | return add_one_path(ce, path, len, st); | |
148 | } | |
149 | /* Should this be an unconditional error? */ | |
150 | return remove_one_path(path); | |
151 | } | |
152 | ||
153 | /* Inexact match: is there perhaps a subdirectory match? */ | |
154 | pos = -pos-1; | |
155 | while (pos < active_nr) { | |
156 | struct cache_entry *ce = active_cache[pos++]; | |
157 | ||
158 | if (strncmp(ce->name, path, len)) | |
159 | break; | |
160 | if (ce->name[len] > '/') | |
161 | break; | |
162 | if (ce->name[len] < '/') | |
163 | continue; | |
164 | ||
165 | /* Subdirectory match - error out */ | |
166 | return error("%s: is a directory - add individual files instead", path); | |
167 | } | |
168 | ||
169 | /* No match - should we add it as a gitlink? */ | |
170 | if (!resolve_gitlink_ref(path, "HEAD", sha1)) | |
171 | return add_one_path(NULL, path, len, st); | |
172 | ||
173 | /* Error out. */ | |
174 | return error("%s: is a directory - add files inside instead", path); | |
175 | } | |
176 | ||
e011054b LT |
177 | static int process_path(const char *path) |
178 | { | |
b4d1690d | 179 | int pos, len; |
e011054b | 180 | struct stat st; |
b4d1690d | 181 | struct cache_entry *ce; |
e011054b | 182 | |
806d13b1 | 183 | len = strlen(path); |
57199892 | 184 | if (has_symlink_leading_path(path, len)) |
806d13b1 JH |
185 | return error("'%s' is beyond a symbolic link", path); |
186 | ||
b4d1690d NTND |
187 | pos = cache_name_pos(path, len); |
188 | ce = pos < 0 ? NULL : active_cache[pos]; | |
189 | if (ce && ce_skip_worktree(ce)) { | |
190 | /* | |
191 | * working directory version is assumed "good" | |
192 | * so updating it does not make sense. | |
193 | * On the other hand, removing it from index should work | |
194 | */ | |
195 | if (allow_remove && remove_file_from_cache(path)) | |
196 | return error("%s: cannot remove from the index", path); | |
197 | return 0; | |
198 | } | |
199 | ||
e011054b LT |
200 | /* |
201 | * First things first: get the stat information, to decide | |
202 | * what to do about the pathname! | |
203 | */ | |
204 | if (lstat(path, &st) < 0) | |
205 | return process_lstat_error(path, errno); | |
206 | ||
e011054b LT |
207 | if (S_ISDIR(st.st_mode)) |
208 | return process_directory(path, len, &st); | |
209 | ||
b4d1690d NTND |
210 | /* |
211 | * Process a regular file | |
212 | */ | |
213 | if (ce && S_ISGITLINK(ce->ce_mode)) | |
214 | return error("%s is already a gitlink, not replacing", path); | |
215 | ||
216 | return add_one_path(ce, path, len, &st); | |
e011054b LT |
217 | } |
218 | ||
d23748a6 JH |
219 | static int add_cacheinfo(unsigned int mode, const unsigned char *sha1, |
220 | const char *path, int stage) | |
9945d980 | 221 | { |
192268c1 | 222 | int size, len, option; |
9945d980 LT |
223 | struct cache_entry *ce; |
224 | ||
d23748a6 | 225 | if (!verify_path(path)) |
7e7abea9 | 226 | return error("Invalid path '%s'", path); |
9945d980 | 227 | |
d23748a6 | 228 | len = strlen(path); |
9945d980 | 229 | size = cache_entry_size(len); |
90321c10 | 230 | ce = xcalloc(1, size); |
9945d980 | 231 | |
e702496e | 232 | hashcpy(ce->sha1, sha1); |
d23748a6 JH |
233 | memcpy(ce->name, path, len); |
234 | ce->ce_flags = create_ce_flags(len, stage); | |
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 | ||
f6ab5bb2 | 400 | static const char update_index_usage[] = |
44a36913 | 401 | "git update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--really-refresh] [--cacheinfo] [--chmod=(+|-)x] [--assume-unchanged] [--skip-worktree|--no-skip-worktree] [--info-only] [--force-remove] [--stdin] [--index-info] [--unresolve] [--again | -g] [--ignore-missing] [-z] [--verbose] [--] <file>..."; |
f6ab5bb2 | 402 | |
2bd452d3 JH |
403 | static unsigned char head_sha1[20]; |
404 | static unsigned char merge_head_sha1[20]; | |
405 | ||
406 | static struct cache_entry *read_one_ent(const char *which, | |
407 | unsigned char *ent, const char *path, | |
408 | int namelen, int stage) | |
409 | { | |
410 | unsigned mode; | |
411 | unsigned char sha1[20]; | |
412 | int size; | |
413 | struct cache_entry *ce; | |
414 | ||
415 | if (get_tree_entry(ent, path, sha1, &mode)) { | |
83e77a25 JH |
416 | if (which) |
417 | error("%s: not in %s branch.", path, which); | |
2bd452d3 JH |
418 | return NULL; |
419 | } | |
420 | if (mode == S_IFDIR) { | |
83e77a25 JH |
421 | if (which) |
422 | error("%s: not a blob in %s branch.", path, which); | |
2bd452d3 JH |
423 | return NULL; |
424 | } | |
425 | size = cache_entry_size(namelen); | |
426 | ce = xcalloc(1, size); | |
427 | ||
e702496e | 428 | hashcpy(ce->sha1, sha1); |
2bd452d3 JH |
429 | memcpy(ce->name, path, namelen); |
430 | ce->ce_flags = create_ce_flags(namelen, stage); | |
431 | ce->ce_mode = create_ce_mode(mode); | |
432 | return ce; | |
433 | } | |
434 | ||
435 | static int unresolve_one(const char *path) | |
436 | { | |
437 | int namelen = strlen(path); | |
438 | int pos; | |
439 | int ret = 0; | |
440 | struct cache_entry *ce_2 = NULL, *ce_3 = NULL; | |
441 | ||
442 | /* See if there is such entry in the index. */ | |
443 | pos = cache_name_pos(path, namelen); | |
8aa38563 JH |
444 | if (0 <= pos) { |
445 | /* already merged */ | |
446 | pos = unmerge_cache_entry_at(pos); | |
447 | if (pos < active_nr) { | |
448 | struct cache_entry *ce = active_cache[pos]; | |
449 | if (ce_stage(ce) && | |
450 | ce_namelen(ce) == namelen && | |
451 | !memcmp(ce->name, path, namelen)) | |
452 | return 0; | |
453 | } | |
454 | /* no resolve-undo information; fall back */ | |
455 | } else { | |
2bd452d3 JH |
456 | /* If there isn't, either it is unmerged, or |
457 | * resolved as "removed" by mistake. We do not | |
458 | * want to do anything in the former case. | |
459 | */ | |
460 | pos = -pos-1; | |
461 | if (pos < active_nr) { | |
462 | struct cache_entry *ce = active_cache[pos]; | |
463 | if (ce_namelen(ce) == namelen && | |
464 | !memcmp(ce->name, path, namelen)) { | |
465 | fprintf(stderr, | |
466 | "%s: skipping still unmerged path.\n", | |
467 | path); | |
468 | goto free_return; | |
469 | } | |
470 | } | |
471 | } | |
472 | ||
473 | /* Grab blobs from given path from HEAD and MERGE_HEAD, | |
474 | * stuff HEAD version in stage #2, | |
475 | * stuff MERGE_HEAD version in stage #3. | |
476 | */ | |
477 | ce_2 = read_one_ent("our", head_sha1, path, namelen, 2); | |
478 | ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3); | |
479 | ||
480 | if (!ce_2 || !ce_3) { | |
481 | ret = -1; | |
482 | goto free_return; | |
483 | } | |
a89fccd2 | 484 | if (!hashcmp(ce_2->sha1, ce_3->sha1) && |
2bd452d3 JH |
485 | ce_2->ce_mode == ce_3->ce_mode) { |
486 | fprintf(stderr, "%s: identical in both, skipping.\n", | |
487 | path); | |
488 | goto free_return; | |
489 | } | |
490 | ||
491 | remove_file_from_cache(path); | |
492 | if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) { | |
493 | error("%s: cannot add our version to the index.", path); | |
494 | ret = -1; | |
495 | goto free_return; | |
496 | } | |
497 | if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD)) | |
498 | return 0; | |
499 | error("%s: cannot add their version to the index.", path); | |
500 | ret = -1; | |
501 | free_return: | |
502 | free(ce_2); | |
503 | free(ce_3); | |
504 | return ret; | |
505 | } | |
506 | ||
507 | static void read_head_pointers(void) | |
508 | { | |
913c983e | 509 | if (read_ref("HEAD", head_sha1)) |
d7530708 | 510 | die("No HEAD -- no initial commit yet?"); |
913c983e | 511 | if (read_ref("MERGE_HEAD", merge_head_sha1)) { |
2bd452d3 JH |
512 | fprintf(stderr, "Not in the middle of a merge.\n"); |
513 | exit(0); | |
514 | } | |
515 | } | |
516 | ||
09895c1f JH |
517 | static int do_unresolve(int ac, const char **av, |
518 | const char *prefix, int prefix_length) | |
2bd452d3 JH |
519 | { |
520 | int i; | |
521 | int err = 0; | |
522 | ||
523 | /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we | |
524 | * are not doing a merge, so exit with success status. | |
525 | */ | |
526 | read_head_pointers(); | |
527 | ||
528 | for (i = 1; i < ac; i++) { | |
529 | const char *arg = av[i]; | |
09895c1f JH |
530 | const char *p = prefix_path(prefix, prefix_length, arg); |
531 | err |= unresolve_one(p); | |
0cc9e70c | 532 | if (p < arg || p > arg + strlen(arg)) |
4b25d091 | 533 | free((char *)p); |
2bd452d3 JH |
534 | } |
535 | return err; | |
536 | } | |
537 | ||
83e77a25 JH |
538 | static int do_reupdate(int ac, const char **av, |
539 | const char *prefix, int prefix_length) | |
540 | { | |
541 | /* Read HEAD and run update-index on paths that are | |
542 | * merged and already different between index and HEAD. | |
543 | */ | |
544 | int pos; | |
545 | int has_head = 1; | |
0cc9e70c | 546 | const char **pathspec = get_pathspec(prefix, av + 1); |
83e77a25 | 547 | |
913c983e | 548 | if (read_ref("HEAD", head_sha1)) |
83e77a25 JH |
549 | /* If there is no HEAD, that means it is an initial |
550 | * commit. Update everything in the index. | |
551 | */ | |
552 | has_head = 0; | |
553 | redo: | |
554 | for (pos = 0; pos < active_nr; pos++) { | |
555 | struct cache_entry *ce = active_cache[pos]; | |
556 | struct cache_entry *old = NULL; | |
557 | int save_nr; | |
22293b9c JH |
558 | |
559 | if (ce_stage(ce) || !ce_path_match(ce, pathspec)) | |
83e77a25 JH |
560 | continue; |
561 | if (has_head) | |
562 | old = read_one_ent(NULL, head_sha1, | |
563 | ce->name, ce_namelen(ce), 0); | |
564 | if (old && ce->ce_mode == old->ce_mode && | |
a89fccd2 | 565 | !hashcmp(ce->sha1, old->sha1)) { |
83e77a25 JH |
566 | free(old); |
567 | continue; /* unchanged */ | |
568 | } | |
569 | /* Be careful. The working tree may not have the | |
570 | * path anymore, in which case, under 'allow_remove', | |
571 | * or worse yet 'allow_replace', active_nr may decrease. | |
572 | */ | |
573 | save_nr = active_nr; | |
574 | update_one(ce->name + prefix_length, prefix, prefix_length); | |
575 | if (save_nr != active_nr) | |
576 | goto redo; | |
577 | } | |
578 | return 0; | |
579 | } | |
580 | ||
a633fca0 | 581 | int cmd_update_index(int argc, const char **argv, const char *prefix) |
e83c5163 | 582 | { |
ee1bec3d | 583 | int i, newfd, entries, has_errors = 0, line_termination = '\n'; |
121481ab | 584 | int allow_options = 1; |
ee1bec3d | 585 | int read_from_stdin = 0; |
ee1bec3d | 586 | int prefix_length = prefix ? strlen(prefix) : 0; |
227bdb18 | 587 | char set_executable_bit = 0; |
405e5b2f | 588 | unsigned int refresh_flags = 0; |
7b802b86 | 589 | int lock_error = 0; |
fefe81c9 | 590 | struct lock_file *lock_file; |
bb233d69 | 591 | |
ef90d6d4 | 592 | git_config(git_default_config, NULL); |
3e09cdfd | 593 | |
fefe81c9 | 594 | /* We can't free this memory, it becomes part of a linked list parsed atexit() */ |
928e47e3 | 595 | lock_file = xcalloc(1, sizeof(struct lock_file)); |
fefe81c9 | 596 | |
30ca07a2 | 597 | newfd = hold_locked_index(lock_file, 0); |
7b802b86 JH |
598 | if (newfd < 0) |
599 | lock_error = errno; | |
9614b8dc | 600 | |
e83c5163 | 601 | entries = read_cache(); |
9614b8dc | 602 | if (entries < 0) |
2de381f9 | 603 | die("cache corrupted"); |
e83c5163 | 604 | |
e83c5163 | 605 | for (i = 1 ; i < argc; i++) { |
6b5ee137 | 606 | const char *path = argv[i]; |
9ebe6cf9 | 607 | const char *p; |
121481ab LT |
608 | |
609 | if (allow_options && *path == '-') { | |
610 | if (!strcmp(path, "--")) { | |
611 | allow_options = 0; | |
612 | continue; | |
613 | } | |
0ed3715f | 614 | if (!strcmp(path, "-q")) { |
405e5b2f | 615 | refresh_flags |= REFRESH_QUIET; |
0ed3715f LT |
616 | continue; |
617 | } | |
5fdeacb0 JS |
618 | if (!strcmp(path, "--ignore-submodules")) { |
619 | refresh_flags |= REFRESH_IGNORE_SUBMODULES; | |
620 | continue; | |
621 | } | |
121481ab LT |
622 | if (!strcmp(path, "--add")) { |
623 | allow_add = 1; | |
624 | continue; | |
625 | } | |
192268c1 JH |
626 | if (!strcmp(path, "--replace")) { |
627 | allow_replace = 1; | |
628 | continue; | |
629 | } | |
121481ab LT |
630 | if (!strcmp(path, "--remove")) { |
631 | allow_remove = 1; | |
632 | continue; | |
633 | } | |
5d1a5c02 | 634 | if (!strcmp(path, "--unmerged")) { |
405e5b2f | 635 | refresh_flags |= REFRESH_UNMERGED; |
5d1a5c02 LT |
636 | continue; |
637 | } | |
121481ab | 638 | if (!strcmp(path, "--refresh")) { |
f83eafdd | 639 | setup_work_tree(); |
405e5b2f | 640 | has_errors |= refresh_cache(refresh_flags); |
5f73076c JH |
641 | continue; |
642 | } | |
643 | if (!strcmp(path, "--really-refresh")) { | |
f83eafdd | 644 | setup_work_tree(); |
405e5b2f | 645 | has_errors |= refresh_cache(REFRESH_REALLY | refresh_flags); |
121481ab LT |
646 | continue; |
647 | } | |
9945d980 | 648 | if (!strcmp(path, "--cacheinfo")) { |
d23748a6 JH |
649 | unsigned char sha1[20]; |
650 | unsigned int mode; | |
651 | ||
b3f94c4b | 652 | if (i+3 >= argc) |
7e44c935 | 653 | die("git update-index: --cacheinfo <mode> <sha1> <path>"); |
d23748a6 | 654 | |
6e6db39a | 655 | if (strtoul_ui(argv[i+1], 8, &mode) || |
d23748a6 JH |
656 | get_sha1_hex(argv[i+2], sha1) || |
657 | add_cacheinfo(mode, sha1, argv[i+3], 0)) | |
7e44c935 | 658 | die("git update-index: --cacheinfo" |
d23748a6 | 659 | " cannot add %s", argv[i+3]); |
9945d980 LT |
660 | i += 3; |
661 | continue; | |
662 | } | |
3e09cdfd JH |
663 | if (!strcmp(path, "--chmod=-x") || |
664 | !strcmp(path, "--chmod=+x")) { | |
665 | if (argc <= i+1) | |
7e44c935 | 666 | die("git update-index: %s <path>", path); |
227bdb18 | 667 | set_executable_bit = path[8]; |
3e09cdfd JH |
668 | continue; |
669 | } | |
5f73076c | 670 | if (!strcmp(path, "--assume-unchanged")) { |
83b327ba | 671 | mark_valid_only = MARK_FLAG; |
5f73076c JH |
672 | continue; |
673 | } | |
674 | if (!strcmp(path, "--no-assume-unchanged")) { | |
83b327ba | 675 | mark_valid_only = UNMARK_FLAG; |
5f73076c JH |
676 | continue; |
677 | } | |
44a36913 NTND |
678 | if (!strcmp(path, "--no-skip-worktree")) { |
679 | mark_skip_worktree_only = UNMARK_FLAG; | |
680 | continue; | |
681 | } | |
682 | if (!strcmp(path, "--skip-worktree")) { | |
683 | mark_skip_worktree_only = MARK_FLAG; | |
5f73076c JH |
684 | continue; |
685 | } | |
df6e1516 BL |
686 | if (!strcmp(path, "--info-only")) { |
687 | info_only = 1; | |
688 | continue; | |
689 | } | |
0ff5bf7c | 690 | if (!strcmp(path, "--force-remove")) { |
9b63f501 | 691 | force_remove = 1; |
0ff5bf7c JH |
692 | continue; |
693 | } | |
ee1bec3d JH |
694 | if (!strcmp(path, "-z")) { |
695 | line_termination = 0; | |
696 | continue; | |
697 | } | |
698 | if (!strcmp(path, "--stdin")) { | |
699 | if (i != argc - 1) | |
700 | die("--stdin must be at the end"); | |
701 | read_from_stdin = 1; | |
702 | break; | |
703 | } | |
d4dbf36d | 704 | if (!strcmp(path, "--index-info")) { |
a41c175d SP |
705 | if (i != argc - 1) |
706 | die("--index-info must be at the end"); | |
d4dbf36d JH |
707 | allow_add = allow_replace = allow_remove = 1; |
708 | read_index_info(line_termination); | |
a41c175d | 709 | break; |
d4dbf36d | 710 | } |
2bd452d3 | 711 | if (!strcmp(path, "--unresolve")) { |
09895c1f JH |
712 | has_errors = do_unresolve(argc - i, argv + i, |
713 | prefix, prefix_length); | |
2bd452d3 JH |
714 | if (has_errors) |
715 | active_cache_changed = 0; | |
716 | goto finish; | |
717 | } | |
7099c9c7 | 718 | if (!strcmp(path, "--again") || !strcmp(path, "-g")) { |
f83eafdd | 719 | setup_work_tree(); |
83e77a25 JH |
720 | has_errors = do_reupdate(argc - i, argv + i, |
721 | prefix, prefix_length); | |
722 | if (has_errors) | |
723 | active_cache_changed = 0; | |
724 | goto finish; | |
725 | } | |
c6e007b0 | 726 | if (!strcmp(path, "--ignore-missing")) { |
405e5b2f | 727 | refresh_flags |= REFRESH_IGNORE_MISSING; |
c6e007b0 JB |
728 | continue; |
729 | } | |
caf4f582 JH |
730 | if (!strcmp(path, "--verbose")) { |
731 | verbose = 1; | |
732 | continue; | |
733 | } | |
4a39f79d JH |
734 | if (!strcmp(path, "--clear-resolve-undo")) { |
735 | resolve_undo_clear(); | |
736 | continue; | |
737 | } | |
f6ab5bb2 PB |
738 | if (!strcmp(path, "-h") || !strcmp(path, "--help")) |
739 | usage(update_index_usage); | |
2de381f9 | 740 | die("unknown option %s", path); |
121481ab | 741 | } |
f83eafdd | 742 | setup_work_tree(); |
9ebe6cf9 AR |
743 | p = prefix_path(prefix, prefix_length, path); |
744 | update_one(p, NULL, 0); | |
227bdb18 | 745 | if (set_executable_bit) |
9ebe6cf9 AR |
746 | chmod_path(set_executable_bit, p); |
747 | if (p < path || p > path + strlen(path)) | |
4b25d091 | 748 | free((char *)p); |
ee1bec3d JH |
749 | } |
750 | if (read_from_stdin) { | |
f285a2d7 | 751 | struct strbuf buf = STRBUF_INIT, nbuf = STRBUF_INIT; |
7fb1011e | 752 | |
f83eafdd | 753 | setup_work_tree(); |
7fb1011e | 754 | while (strbuf_getline(&buf, stdin, line_termination) != EOF) { |
fb69a760 | 755 | const char *p; |
7fb1011e PH |
756 | if (line_termination && buf.buf[0] == '"') { |
757 | strbuf_reset(&nbuf); | |
758 | if (unquote_c_style(&nbuf, buf.buf, NULL)) | |
759 | die("line is badly quoted"); | |
760 | strbuf_swap(&buf, &nbuf); | |
761 | } | |
762 | p = prefix_path(prefix, prefix_length, buf.buf); | |
fb69a760 JH |
763 | update_one(p, NULL, 0); |
764 | if (set_executable_bit) | |
227bdb18 | 765 | chmod_path(set_executable_bit, p); |
7fb1011e PH |
766 | if (p < buf.buf || p > buf.buf + buf.len) |
767 | free((char *)p); | |
9b63f501 | 768 | } |
7fb1011e | 769 | strbuf_release(&nbuf); |
e6c019d0 | 770 | strbuf_release(&buf); |
e83c5163 | 771 | } |
2bd452d3 JH |
772 | |
773 | finish: | |
5cd5ace7 | 774 | if (active_cache_changed) { |
7b802b86 JH |
775 | if (newfd < 0) { |
776 | if (refresh_flags & REFRESH_QUIET) | |
777 | exit(128); | |
e43a6fd3 | 778 | unable_to_lock_index_die(get_index_file(), lock_error); |
7b802b86 | 779 | } |
5cd5ace7 | 780 | if (write_cache(newfd, active_cache, active_nr) || |
4ed7cd3a | 781 | commit_locked_index(lock_file)) |
021b6e45 | 782 | die("Unable to write new index file"); |
5cd5ace7 | 783 | } |
9614b8dc | 784 | |
fefe81c9 LS |
785 | rollback_lock_file(lock_file); |
786 | ||
c4b83e61 | 787 | return has_errors ? 1 : 0; |
e83c5163 | 788 | } |