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