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