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?",
124 * We fundamentally don't like some paths: we don't want
125 * dot or dot-dot anywhere, and for obvious reasons don't
126 * want to recurse into ".git" either.
128 * Also, we don't want double slashes or slashes at the
129 * end that can make pathnames ambiguous.
131 static int verify_dotfile(const char *rest
)
134 * The first character was '.', but that
135 * has already been discarded, we now test
139 /* "." is not allowed */
144 * ".git" followed by NUL or slash is bad. This
145 * shares the path end test with the ".." case.
155 if (rest
[1] == '\0' || rest
[1] == '/')
161 static int verify_path(const char *path
)
178 if (verify_dotfile(path
))
187 static int add_cacheinfo(unsigned int mode
, const unsigned char *sha1
,
188 const char *path
, int stage
)
190 int size
, len
, option
;
191 struct cache_entry
*ce
;
193 if (!verify_path(path
))
197 size
= cache_entry_size(len
);
198 ce
= xcalloc(1, size
);
200 memcpy(ce
->sha1
, sha1
, 20);
201 memcpy(ce
->name
, path
, len
);
202 ce
->ce_flags
= create_ce_flags(len
, stage
);
203 ce
->ce_mode
= create_ce_mode(mode
);
204 if (assume_unchanged
)
205 ce
->ce_flags
|= htons(CE_VALID
);
206 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
207 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
208 if (add_cache_entry(ce
, option
))
209 return error("%s: cannot add to the index - missing --add option?",
211 report("add '%s'", path
);
215 static void chmod_path(int flip
, const char *path
)
218 struct cache_entry
*ce
;
221 pos
= cache_name_pos(path
, strlen(path
));
224 ce
= active_cache
[pos
];
225 mode
= ntohl(ce
->ce_mode
);
230 ce
->ce_mode
|= htonl(0111); break;
232 ce
->ce_mode
&= htonl(~0111); break;
236 active_cache_changed
= 1;
237 report("chmod %cx '%s'", flip
, path
);
240 die("git-update-index: cannot chmod %cx '%s'", flip
, path
);
243 static struct cache_file cache_file
;
245 static void update_one(const char *path
, const char *prefix
, int prefix_length
)
247 const char *p
= prefix_path(prefix
, prefix_length
, path
);
248 if (!verify_path(p
)) {
249 fprintf(stderr
, "Ignoring path %s\n", path
);
252 if (mark_valid_only
) {
254 die("Unable to mark file %s", path
);
259 if (remove_file_from_cache(p
))
260 die("git-update-index: unable to remove %s", path
);
261 report("remove '%s'", path
);
264 if (add_file_to_cache(p
))
265 die("Unable to process file %s", path
);
266 report("add '%s'", path
);
268 if (p
< path
|| p
> path
+ strlen(path
))
272 static void read_index_info(int line_termination
)
279 unsigned char sha1
[20];
283 /* This reads lines formatted in one of three formats:
285 * (1) mode SP sha1 TAB path
286 * The first format is what "git-apply --index-info"
287 * reports, and used to reconstruct a partial tree
288 * that is used for phony merge base tree when falling
289 * back on 3-way merge.
291 * (2) mode SP type SP sha1 TAB path
292 * The second format is to stuff git-ls-tree output
293 * into the index file.
295 * (3) mode SP sha1 SP stage TAB path
296 * This format is to put higher order stages into the
297 * index file and matches git-ls-files --stage output.
299 read_line(&buf
, stdin
, line_termination
);
303 mode
= strtoul(buf
.buf
, &ptr
, 8);
304 if (ptr
== buf
.buf
|| *ptr
!= ' ')
307 tab
= strchr(ptr
, '\t');
308 if (!tab
|| tab
- ptr
< 41)
311 if (tab
[-2] == ' ' && '0' <= tab
[-1] && tab
[-1] <= '3') {
312 stage
= tab
[-1] - '0';
313 ptr
= tab
+ 1; /* point at the head of path */
314 tab
= tab
- 2; /* point at tail of sha1 */
318 ptr
= tab
+ 1; /* point at the head of path */
321 if (get_sha1_hex(tab
- 40, sha1
) || tab
[-41] != ' ')
324 if (line_termination
&& ptr
[0] == '"')
325 path_name
= unquote_c_style(ptr
, NULL
);
329 if (!verify_path(path_name
)) {
330 fprintf(stderr
, "Ignoring path %s\n", path_name
);
331 if (path_name
!= ptr
)
337 /* mode == 0 means there is no such path -- remove */
338 if (remove_file_from_cache(path_name
))
339 die("git-update-index: unable to remove %s",
343 /* mode ' ' sha1 '\t' name
344 * ptr[-1] points at tab,
345 * ptr[-41] is at the beginning of sha1
347 ptr
[-42] = ptr
[-1] = 0;
348 if (add_cacheinfo(mode
, sha1
, path_name
, stage
))
349 die("git-update-index: unable to update %s",
352 if (path_name
!= ptr
)
357 die("malformed index info %s", buf
.buf
);
361 static const char update_index_usage
[] =
362 "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>...";
364 static unsigned char head_sha1
[20];
365 static unsigned char merge_head_sha1
[20];
367 static struct cache_entry
*read_one_ent(const char *which
,
368 unsigned char *ent
, const char *path
,
369 int namelen
, int stage
)
372 unsigned char sha1
[20];
374 struct cache_entry
*ce
;
376 if (get_tree_entry(ent
, path
, sha1
, &mode
)) {
378 error("%s: not in %s branch.", path
, which
);
381 if (mode
== S_IFDIR
) {
383 error("%s: not a blob in %s branch.", path
, which
);
386 size
= cache_entry_size(namelen
);
387 ce
= xcalloc(1, size
);
389 memcpy(ce
->sha1
, sha1
, 20);
390 memcpy(ce
->name
, path
, namelen
);
391 ce
->ce_flags
= create_ce_flags(namelen
, stage
);
392 ce
->ce_mode
= create_ce_mode(mode
);
396 static int unresolve_one(const char *path
)
398 int namelen
= strlen(path
);
401 struct cache_entry
*ce_2
= NULL
, *ce_3
= NULL
;
403 /* See if there is such entry in the index. */
404 pos
= cache_name_pos(path
, namelen
);
406 /* If there isn't, either it is unmerged, or
407 * resolved as "removed" by mistake. We do not
408 * want to do anything in the former case.
411 if (pos
< active_nr
) {
412 struct cache_entry
*ce
= active_cache
[pos
];
413 if (ce_namelen(ce
) == namelen
&&
414 !memcmp(ce
->name
, path
, namelen
)) {
416 "%s: skipping still unmerged path.\n",
423 /* Grab blobs from given path from HEAD and MERGE_HEAD,
424 * stuff HEAD version in stage #2,
425 * stuff MERGE_HEAD version in stage #3.
427 ce_2
= read_one_ent("our", head_sha1
, path
, namelen
, 2);
428 ce_3
= read_one_ent("their", merge_head_sha1
, path
, namelen
, 3);
430 if (!ce_2
|| !ce_3
) {
434 if (!memcmp(ce_2
->sha1
, ce_3
->sha1
, 20) &&
435 ce_2
->ce_mode
== ce_3
->ce_mode
) {
436 fprintf(stderr
, "%s: identical in both, skipping.\n",
441 remove_file_from_cache(path
);
442 if (add_cache_entry(ce_2
, ADD_CACHE_OK_TO_ADD
)) {
443 error("%s: cannot add our version to the index.", path
);
447 if (!add_cache_entry(ce_3
, ADD_CACHE_OK_TO_ADD
))
449 error("%s: cannot add their version to the index.", path
);
457 static void read_head_pointers(void)
459 if (read_ref(git_path("HEAD"), head_sha1
))
460 die("No HEAD -- no initial commit yet?\n");
461 if (read_ref(git_path("MERGE_HEAD"), merge_head_sha1
)) {
462 fprintf(stderr
, "Not in the middle of a merge.\n");
467 static int do_unresolve(int ac
, const char **av
,
468 const char *prefix
, int prefix_length
)
473 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
474 * are not doing a merge, so exit with success status.
476 read_head_pointers();
478 for (i
= 1; i
< ac
; i
++) {
479 const char *arg
= av
[i
];
480 const char *p
= prefix_path(prefix
, prefix_length
, arg
);
481 err
|= unresolve_one(p
);
482 if (p
< arg
|| p
> arg
+ strlen(arg
))
488 static int do_reupdate(int ac
, const char **av
,
489 const char *prefix
, int prefix_length
)
491 /* Read HEAD and run update-index on paths that are
492 * merged and already different between index and HEAD.
496 const char **pathspec
= get_pathspec(prefix
, av
+ 1);
498 if (read_ref(git_path("HEAD"), head_sha1
))
499 /* If there is no HEAD, that means it is an initial
500 * commit. Update everything in the index.
504 for (pos
= 0; pos
< active_nr
; pos
++) {
505 struct cache_entry
*ce
= active_cache
[pos
];
506 struct cache_entry
*old
= NULL
;
509 if (ce_stage(ce
) || !ce_path_match(ce
, pathspec
))
512 old
= read_one_ent(NULL
, head_sha1
,
513 ce
->name
, ce_namelen(ce
), 0);
514 if (old
&& ce
->ce_mode
== old
->ce_mode
&&
515 !memcmp(ce
->sha1
, old
->sha1
, 20)) {
517 continue; /* unchanged */
519 /* Be careful. The working tree may not have the
520 * path anymore, in which case, under 'allow_remove',
521 * or worse yet 'allow_replace', active_nr may decrease.
524 update_one(ce
->name
+ prefix_length
, prefix
, prefix_length
);
525 if (save_nr
!= active_nr
)
531 int main(int argc
, const char **argv
)
533 int i
, newfd
, entries
, has_errors
= 0, line_termination
= '\n';
534 int allow_options
= 1;
535 int read_from_stdin
= 0;
536 const char *prefix
= setup_git_directory();
537 int prefix_length
= prefix
? strlen(prefix
) : 0;
538 char set_executable_bit
= 0;
539 unsigned int refresh_flags
= 0;
541 git_config(git_default_config
);
543 newfd
= hold_index_file_for_update(&cache_file
, get_index_file());
545 die("unable to create new cachefile");
547 entries
= read_cache();
549 die("cache corrupted");
551 for (i
= 1 ; i
< argc
; i
++) {
552 const char *path
= argv
[i
];
554 if (allow_options
&& *path
== '-') {
555 if (!strcmp(path
, "--")) {
559 if (!strcmp(path
, "-q")) {
560 refresh_flags
|= REFRESH_QUIET
;
563 if (!strcmp(path
, "--add")) {
567 if (!strcmp(path
, "--replace")) {
571 if (!strcmp(path
, "--remove")) {
575 if (!strcmp(path
, "--unmerged")) {
576 refresh_flags
|= REFRESH_UNMERGED
;
579 if (!strcmp(path
, "--refresh")) {
580 has_errors
|= refresh_cache(refresh_flags
);
583 if (!strcmp(path
, "--really-refresh")) {
584 has_errors
|= refresh_cache(REFRESH_REALLY
| refresh_flags
);
587 if (!strcmp(path
, "--cacheinfo")) {
588 unsigned char sha1
[20];
592 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
594 if ((sscanf(argv
[i
+1], "%o", &mode
) != 1) ||
595 get_sha1_hex(argv
[i
+2], sha1
) ||
596 add_cacheinfo(mode
, sha1
, argv
[i
+3], 0))
597 die("git-update-index: --cacheinfo"
598 " cannot add %s", argv
[i
+3]);
602 if (!strcmp(path
, "--chmod=-x") ||
603 !strcmp(path
, "--chmod=+x")) {
605 die("git-update-index: %s <path>", path
);
606 set_executable_bit
= path
[8];
609 if (!strcmp(path
, "--assume-unchanged")) {
610 mark_valid_only
= MARK_VALID
;
613 if (!strcmp(path
, "--no-assume-unchanged")) {
614 mark_valid_only
= UNMARK_VALID
;
617 if (!strcmp(path
, "--info-only")) {
621 if (!strcmp(path
, "--force-remove")) {
625 if (!strcmp(path
, "-z")) {
626 line_termination
= 0;
629 if (!strcmp(path
, "--stdin")) {
631 die("--stdin must be at the end");
635 if (!strcmp(path
, "--index-info")) {
637 die("--index-info must be at the end");
638 allow_add
= allow_replace
= allow_remove
= 1;
639 read_index_info(line_termination
);
642 if (!strcmp(path
, "--unresolve")) {
643 has_errors
= do_unresolve(argc
- i
, argv
+ i
,
644 prefix
, prefix_length
);
646 active_cache_changed
= 0;
649 if (!strcmp(path
, "--again")) {
650 has_errors
= do_reupdate(argc
- i
, argv
+ i
,
651 prefix
, prefix_length
);
653 active_cache_changed
= 0;
656 if (!strcmp(path
, "--ignore-missing")) {
657 refresh_flags
|= REFRESH_IGNORE_MISSING
;
660 if (!strcmp(path
, "--verbose")) {
664 if (!strcmp(path
, "-h") || !strcmp(path
, "--help"))
665 usage(update_index_usage
);
666 die("unknown option %s", path
);
668 update_one(path
, prefix
, prefix_length
);
669 if (set_executable_bit
)
670 chmod_path(set_executable_bit
, path
);
672 if (read_from_stdin
) {
678 read_line(&buf
, stdin
, line_termination
);
681 if (line_termination
&& buf
.buf
[0] == '"')
682 path_name
= unquote_c_style(buf
.buf
, NULL
);
685 p
= prefix_path(prefix
, prefix_length
, path_name
);
686 update_one(p
, NULL
, 0);
687 if (set_executable_bit
)
688 chmod_path(set_executable_bit
, p
);
689 if (p
< path_name
|| p
> path_name
+ strlen(path_name
))
691 if (path_name
!= buf
.buf
)
697 if (active_cache_changed
) {
698 if (write_cache(newfd
, active_cache
, active_nr
) ||
699 commit_index_file(&cache_file
))
700 die("Unable to write new cachefile");
703 return has_errors
? 1 : 0;