2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
12 * Default to not allowing changes to the list of files. The
13 * tool doesn't actually care, but this makes it harder to add
14 * files to the revision control by mistake by doing something
15 * like "git-update-index *" and suddenly having all the object
16 * files be revision controlled.
19 static int allow_remove
;
20 static int allow_replace
;
22 static int force_remove
;
24 static int mark_valid_only
= 0;
26 #define UNMARK_VALID 2
28 static void report(const char *fmt
, ...)
41 static int mark_valid(const char *path
)
43 int namelen
= strlen(path
);
44 int pos
= cache_name_pos(path
, namelen
);
46 switch (mark_valid_only
) {
48 active_cache
[pos
]->ce_flags
|= htons(CE_VALID
);
51 active_cache
[pos
]->ce_flags
&= ~htons(CE_VALID
);
54 active_cache_changed
= 1;
60 static int add_file_to_cache(const char *path
)
62 int size
, namelen
, option
, status
;
63 struct cache_entry
*ce
;
66 status
= lstat(path
, &st
);
67 if (status
< 0 || S_ISDIR(st
.st_mode
)) {
68 /* When we used to have "path" and now we want to add
69 * "path/file", we need a way to remove "path" before
70 * being able to add "path/file". However,
71 * "git-update-index --remove path" would not work.
72 * --force-remove can be used but this is more user
73 * friendly, especially since we can do the opposite
74 * case just fine without --force-remove.
76 if (status
== 0 || (errno
== ENOENT
|| errno
== ENOTDIR
)) {
78 if (remove_file_from_cache(path
))
79 return error("%s: cannot remove from the index",
83 } else if (status
< 0) {
84 return error("%s: does not exist and --remove not passed",
89 return error("%s: is a directory - add files inside instead",
92 return error("lstat(\"%s\"): %s", path
,
96 namelen
= strlen(path
);
97 size
= cache_entry_size(namelen
);
98 ce
= xcalloc(1, size
);
99 memcpy(ce
->name
, path
, namelen
);
100 ce
->ce_flags
= htons(namelen
);
101 fill_stat_cache_info(ce
, &st
);
103 ce
->ce_mode
= create_ce_mode(st
.st_mode
);
104 if (!trust_executable_bit
) {
105 /* If there is an existing entry, pick the mode bits
108 int pos
= cache_name_pos(path
, namelen
);
110 ce
->ce_mode
= active_cache
[pos
]->ce_mode
;
113 if (index_path(ce
->sha1
, path
, &st
, !info_only
))
115 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
116 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
117 if (add_cache_entry(ce
, option
))
118 return error("%s: cannot add to the index - missing --add option?",
123 static int add_cacheinfo(unsigned int mode
, const unsigned char *sha1
,
124 const char *path
, int stage
)
126 int size
, len
, option
;
127 struct cache_entry
*ce
;
129 if (!verify_path(path
))
133 size
= cache_entry_size(len
);
134 ce
= xcalloc(1, size
);
136 memcpy(ce
->sha1
, sha1
, 20);
137 memcpy(ce
->name
, path
, len
);
138 ce
->ce_flags
= create_ce_flags(len
, stage
);
139 ce
->ce_mode
= create_ce_mode(mode
);
140 if (assume_unchanged
)
141 ce
->ce_flags
|= htons(CE_VALID
);
142 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
143 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
144 if (add_cache_entry(ce
, option
))
145 return error("%s: cannot add to the index - missing --add option?",
147 report("add '%s'", path
);
151 static void chmod_path(int flip
, const char *path
)
154 struct cache_entry
*ce
;
157 pos
= cache_name_pos(path
, strlen(path
));
160 ce
= active_cache
[pos
];
161 mode
= ntohl(ce
->ce_mode
);
166 ce
->ce_mode
|= htonl(0111); break;
168 ce
->ce_mode
&= htonl(~0111); break;
172 active_cache_changed
= 1;
173 report("chmod %cx '%s'", flip
, path
);
176 die("git-update-index: cannot chmod %cx '%s'", flip
, path
);
179 static struct cache_file cache_file
;
181 static void update_one(const char *path
, const char *prefix
, int prefix_length
)
183 const char *p
= prefix_path(prefix
, prefix_length
, path
);
184 if (!verify_path(p
)) {
185 fprintf(stderr
, "Ignoring path %s\n", path
);
188 if (mark_valid_only
) {
190 die("Unable to mark file %s", path
);
195 if (remove_file_from_cache(p
))
196 die("git-update-index: unable to remove %s", path
);
197 report("remove '%s'", path
);
200 if (add_file_to_cache(p
))
201 die("Unable to process file %s", path
);
202 report("add '%s'", path
);
204 if (p
< path
|| p
> path
+ strlen(path
))
208 static void read_index_info(int line_termination
)
215 unsigned char sha1
[20];
219 /* This reads lines formatted in one of three formats:
221 * (1) mode SP sha1 TAB path
222 * The first format is what "git-apply --index-info"
223 * reports, and used to reconstruct a partial tree
224 * that is used for phony merge base tree when falling
225 * back on 3-way merge.
227 * (2) mode SP type SP sha1 TAB path
228 * The second format is to stuff git-ls-tree output
229 * into the index file.
231 * (3) mode SP sha1 SP stage TAB path
232 * This format is to put higher order stages into the
233 * index file and matches git-ls-files --stage output.
235 read_line(&buf
, stdin
, line_termination
);
239 mode
= strtoul(buf
.buf
, &ptr
, 8);
240 if (ptr
== buf
.buf
|| *ptr
!= ' ')
243 tab
= strchr(ptr
, '\t');
244 if (!tab
|| tab
- ptr
< 41)
247 if (tab
[-2] == ' ' && '0' <= tab
[-1] && tab
[-1] <= '3') {
248 stage
= tab
[-1] - '0';
249 ptr
= tab
+ 1; /* point at the head of path */
250 tab
= tab
- 2; /* point at tail of sha1 */
254 ptr
= tab
+ 1; /* point at the head of path */
257 if (get_sha1_hex(tab
- 40, sha1
) || tab
[-41] != ' ')
260 if (line_termination
&& ptr
[0] == '"')
261 path_name
= unquote_c_style(ptr
, NULL
);
265 if (!verify_path(path_name
)) {
266 fprintf(stderr
, "Ignoring path %s\n", path_name
);
267 if (path_name
!= ptr
)
273 /* mode == 0 means there is no such path -- remove */
274 if (remove_file_from_cache(path_name
))
275 die("git-update-index: unable to remove %s",
279 /* mode ' ' sha1 '\t' name
280 * ptr[-1] points at tab,
281 * ptr[-41] is at the beginning of sha1
283 ptr
[-42] = ptr
[-1] = 0;
284 if (add_cacheinfo(mode
, sha1
, path_name
, stage
))
285 die("git-update-index: unable to update %s",
288 if (path_name
!= ptr
)
293 die("malformed index info %s", buf
.buf
);
297 static const char update_index_usage
[] =
298 "git-update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--really-refresh] [--cacheinfo] [--chmod=(+|-)x] [--assume-unchanged] [--info-only] [--force-remove] [--stdin] [--index-info] [--unresolve] [--again] [--ignore-missing] [-z] [--verbose] [--] <file>...";
300 static unsigned char head_sha1
[20];
301 static unsigned char merge_head_sha1
[20];
303 static struct cache_entry
*read_one_ent(const char *which
,
304 unsigned char *ent
, const char *path
,
305 int namelen
, int stage
)
308 unsigned char sha1
[20];
310 struct cache_entry
*ce
;
312 if (get_tree_entry(ent
, path
, sha1
, &mode
)) {
314 error("%s: not in %s branch.", path
, which
);
317 if (mode
== S_IFDIR
) {
319 error("%s: not a blob in %s branch.", path
, which
);
322 size
= cache_entry_size(namelen
);
323 ce
= xcalloc(1, size
);
325 memcpy(ce
->sha1
, sha1
, 20);
326 memcpy(ce
->name
, path
, namelen
);
327 ce
->ce_flags
= create_ce_flags(namelen
, stage
);
328 ce
->ce_mode
= create_ce_mode(mode
);
332 static int unresolve_one(const char *path
)
334 int namelen
= strlen(path
);
337 struct cache_entry
*ce_2
= NULL
, *ce_3
= NULL
;
339 /* See if there is such entry in the index. */
340 pos
= cache_name_pos(path
, namelen
);
342 /* If there isn't, either it is unmerged, or
343 * resolved as "removed" by mistake. We do not
344 * want to do anything in the former case.
347 if (pos
< active_nr
) {
348 struct cache_entry
*ce
= active_cache
[pos
];
349 if (ce_namelen(ce
) == namelen
&&
350 !memcmp(ce
->name
, path
, namelen
)) {
352 "%s: skipping still unmerged path.\n",
359 /* Grab blobs from given path from HEAD and MERGE_HEAD,
360 * stuff HEAD version in stage #2,
361 * stuff MERGE_HEAD version in stage #3.
363 ce_2
= read_one_ent("our", head_sha1
, path
, namelen
, 2);
364 ce_3
= read_one_ent("their", merge_head_sha1
, path
, namelen
, 3);
366 if (!ce_2
|| !ce_3
) {
370 if (!memcmp(ce_2
->sha1
, ce_3
->sha1
, 20) &&
371 ce_2
->ce_mode
== ce_3
->ce_mode
) {
372 fprintf(stderr
, "%s: identical in both, skipping.\n",
377 remove_file_from_cache(path
);
378 if (add_cache_entry(ce_2
, ADD_CACHE_OK_TO_ADD
)) {
379 error("%s: cannot add our version to the index.", path
);
383 if (!add_cache_entry(ce_3
, ADD_CACHE_OK_TO_ADD
))
385 error("%s: cannot add their version to the index.", path
);
393 static void read_head_pointers(void)
395 if (read_ref(git_path("HEAD"), head_sha1
))
396 die("No HEAD -- no initial commit yet?\n");
397 if (read_ref(git_path("MERGE_HEAD"), merge_head_sha1
)) {
398 fprintf(stderr
, "Not in the middle of a merge.\n");
403 static int do_unresolve(int ac
, const char **av
,
404 const char *prefix
, int prefix_length
)
409 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
410 * are not doing a merge, so exit with success status.
412 read_head_pointers();
414 for (i
= 1; i
< ac
; i
++) {
415 const char *arg
= av
[i
];
416 const char *p
= prefix_path(prefix
, prefix_length
, arg
);
417 err
|= unresolve_one(p
);
418 if (p
< arg
|| p
> arg
+ strlen(arg
))
424 static int do_reupdate(int ac
, const char **av
,
425 const char *prefix
, int prefix_length
)
427 /* Read HEAD and run update-index on paths that are
428 * merged and already different between index and HEAD.
432 const char **pathspec
= get_pathspec(prefix
, av
+ 1);
434 if (read_ref(git_path("HEAD"), head_sha1
))
435 /* If there is no HEAD, that means it is an initial
436 * commit. Update everything in the index.
440 for (pos
= 0; pos
< active_nr
; pos
++) {
441 struct cache_entry
*ce
= active_cache
[pos
];
442 struct cache_entry
*old
= NULL
;
445 if (ce_stage(ce
) || !ce_path_match(ce
, pathspec
))
448 old
= read_one_ent(NULL
, head_sha1
,
449 ce
->name
, ce_namelen(ce
), 0);
450 if (old
&& ce
->ce_mode
== old
->ce_mode
&&
451 !memcmp(ce
->sha1
, old
->sha1
, 20)) {
453 continue; /* unchanged */
455 /* Be careful. The working tree may not have the
456 * path anymore, in which case, under 'allow_remove',
457 * or worse yet 'allow_replace', active_nr may decrease.
460 update_one(ce
->name
+ prefix_length
, prefix
, prefix_length
);
461 if (save_nr
!= active_nr
)
467 int main(int argc
, const char **argv
)
469 int i
, newfd
, entries
, has_errors
= 0, line_termination
= '\n';
470 int allow_options
= 1;
471 int read_from_stdin
= 0;
472 const char *prefix
= setup_git_directory();
473 int prefix_length
= prefix
? strlen(prefix
) : 0;
474 char set_executable_bit
= 0;
475 unsigned int refresh_flags
= 0;
477 git_config(git_default_config
);
479 newfd
= hold_index_file_for_update(&cache_file
, get_index_file());
481 die("unable to create new cachefile");
483 entries
= read_cache();
485 die("cache corrupted");
487 for (i
= 1 ; i
< argc
; i
++) {
488 const char *path
= argv
[i
];
490 if (allow_options
&& *path
== '-') {
491 if (!strcmp(path
, "--")) {
495 if (!strcmp(path
, "-q")) {
496 refresh_flags
|= REFRESH_QUIET
;
499 if (!strcmp(path
, "--add")) {
503 if (!strcmp(path
, "--replace")) {
507 if (!strcmp(path
, "--remove")) {
511 if (!strcmp(path
, "--unmerged")) {
512 refresh_flags
|= REFRESH_UNMERGED
;
515 if (!strcmp(path
, "--refresh")) {
516 has_errors
|= refresh_cache(refresh_flags
);
519 if (!strcmp(path
, "--really-refresh")) {
520 has_errors
|= refresh_cache(REFRESH_REALLY
| refresh_flags
);
523 if (!strcmp(path
, "--cacheinfo")) {
524 unsigned char sha1
[20];
528 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
530 if ((sscanf(argv
[i
+1], "%o", &mode
) != 1) ||
531 get_sha1_hex(argv
[i
+2], sha1
) ||
532 add_cacheinfo(mode
, sha1
, argv
[i
+3], 0))
533 die("git-update-index: --cacheinfo"
534 " cannot add %s", argv
[i
+3]);
538 if (!strcmp(path
, "--chmod=-x") ||
539 !strcmp(path
, "--chmod=+x")) {
541 die("git-update-index: %s <path>", path
);
542 set_executable_bit
= path
[8];
545 if (!strcmp(path
, "--assume-unchanged")) {
546 mark_valid_only
= MARK_VALID
;
549 if (!strcmp(path
, "--no-assume-unchanged")) {
550 mark_valid_only
= UNMARK_VALID
;
553 if (!strcmp(path
, "--info-only")) {
557 if (!strcmp(path
, "--force-remove")) {
561 if (!strcmp(path
, "-z")) {
562 line_termination
= 0;
565 if (!strcmp(path
, "--stdin")) {
567 die("--stdin must be at the end");
571 if (!strcmp(path
, "--index-info")) {
573 die("--index-info must be at the end");
574 allow_add
= allow_replace
= allow_remove
= 1;
575 read_index_info(line_termination
);
578 if (!strcmp(path
, "--unresolve")) {
579 has_errors
= do_unresolve(argc
- i
, argv
+ i
,
580 prefix
, prefix_length
);
582 active_cache_changed
= 0;
585 if (!strcmp(path
, "--again")) {
586 has_errors
= do_reupdate(argc
- i
, argv
+ i
,
587 prefix
, prefix_length
);
589 active_cache_changed
= 0;
592 if (!strcmp(path
, "--ignore-missing")) {
593 refresh_flags
|= REFRESH_IGNORE_MISSING
;
596 if (!strcmp(path
, "--verbose")) {
600 if (!strcmp(path
, "-h") || !strcmp(path
, "--help"))
601 usage(update_index_usage
);
602 die("unknown option %s", path
);
604 update_one(path
, prefix
, prefix_length
);
605 if (set_executable_bit
)
606 chmod_path(set_executable_bit
, path
);
608 if (read_from_stdin
) {
614 read_line(&buf
, stdin
, line_termination
);
617 if (line_termination
&& buf
.buf
[0] == '"')
618 path_name
= unquote_c_style(buf
.buf
, NULL
);
621 p
= prefix_path(prefix
, prefix_length
, path_name
);
622 update_one(p
, NULL
, 0);
623 if (set_executable_bit
)
624 chmod_path(set_executable_bit
, p
);
625 if (p
< path_name
|| p
> path_name
+ strlen(path_name
))
627 if (path_name
!= buf
.buf
)
633 if (active_cache_changed
) {
634 if (write_cache(newfd
, active_cache
, active_nr
) ||
635 commit_index_file(&cache_file
))
636 die("Unable to write new cachefile");
639 return has_errors
? 1 : 0;