2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
10 * Default to not allowing changes to the list of files. The
11 * tool doesn't actually care, but this makes it harder to add
12 * files to the revision control by mistake by doing something
13 * like "git-update-index *" and suddenly having all the object
14 * files be revision controlled.
17 static int allow_remove
;
18 static int allow_replace
;
19 static int allow_unmerged
; /* --refresh needing merge is not error */
20 static int not_new
; /* --refresh not having working tree files is not error */
21 static int quiet
; /* --refresh needing update is not error */
23 static int force_remove
;
26 /* Three functions to allow overloaded pointer return; see linux/err.h */
27 static inline void *ERR_PTR(long error
)
29 return (void *) error
;
32 static inline long PTR_ERR(const void *ptr
)
37 static inline long IS_ERR(const void *ptr
)
39 return (unsigned long)ptr
> (unsigned long)-1000L;
42 static void report(const char *fmt
, ...)
55 static int add_file_to_cache(const char *path
)
57 int size
, namelen
, option
, status
;
58 struct cache_entry
*ce
;
61 status
= lstat(path
, &st
);
62 if (status
< 0 || S_ISDIR(st
.st_mode
)) {
63 /* When we used to have "path" and now we want to add
64 * "path/file", we need a way to remove "path" before
65 * being able to add "path/file". However,
66 * "git-update-index --remove path" would not work.
67 * --force-remove can be used but this is more user
68 * friendly, especially since we can do the opposite
69 * case just fine without --force-remove.
71 if (status
== 0 || (errno
== ENOENT
|| errno
== ENOTDIR
)) {
73 if (remove_file_from_cache(path
))
74 return error("%s: cannot remove from the index",
78 } else if (status
< 0) {
79 return error("%s: does not exist and --remove not passed",
84 return error("%s: is a directory - add files inside instead",
87 return error("lstat(\"%s\"): %s", path
,
91 namelen
= strlen(path
);
92 size
= cache_entry_size(namelen
);
95 memcpy(ce
->name
, path
, namelen
);
96 fill_stat_cache_info(ce
, &st
);
98 ce
->ce_mode
= create_ce_mode(st
.st_mode
);
99 if (!trust_executable_bit
) {
100 /* If there is an existing entry, pick the mode bits
103 int pos
= cache_name_pos(path
, namelen
);
105 ce
->ce_mode
= active_cache
[pos
]->ce_mode
;
107 ce
->ce_flags
= htons(namelen
);
109 if (index_path(ce
->sha1
, path
, &st
, !info_only
))
111 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
112 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
113 if (add_cache_entry(ce
, option
))
114 return error("%s: cannot add to the index - missing --add option?",
120 * "refresh" does not calculate a new sha1 file or bring the
121 * cache up-to-date for mode/content changes. But what it
122 * _does_ do is to "re-match" the stat information of a file
123 * with the cache, so that you can refresh the cache for a
124 * file that hasn't been changed but where the stat entry is
127 * For example, you'd want to do this after doing a "git-read-tree",
128 * to link up the stat cache details with the proper files.
130 static struct cache_entry
*refresh_entry(struct cache_entry
*ce
)
133 struct cache_entry
*updated
;
136 if (lstat(ce
->name
, &st
) < 0)
137 return ERR_PTR(-errno
);
139 changed
= ce_match_stat(ce
, &st
);
143 if (ce_modified(ce
, &st
))
144 return ERR_PTR(-EINVAL
);
147 updated
= xmalloc(size
);
148 memcpy(updated
, ce
, size
);
149 fill_stat_cache_info(updated
, &st
);
153 static int refresh_cache(void)
158 for (i
= 0; i
< active_nr
; i
++) {
159 struct cache_entry
*ce
, *new;
160 ce
= active_cache
[i
];
162 while ((i
< active_nr
) &&
163 ! strcmp(active_cache
[i
]->name
, ce
->name
))
168 printf("%s: needs merge\n", ce
->name
);
173 new = refresh_entry(ce
);
177 if (not_new
&& PTR_ERR(new) == -ENOENT
)
181 printf("%s: needs update\n", ce
->name
);
185 active_cache_changed
= 1;
186 /* You can NOT just free active_cache[i] here, since it
187 * might not be necessarily malloc()ed but can also come
189 active_cache
[i
] = new;
195 * We fundamentally don't like some paths: we don't want
196 * dot or dot-dot anywhere, and for obvious reasons don't
197 * want to recurse into ".git" either.
199 * Also, we don't want double slashes or slashes at the
200 * end that can make pathnames ambiguous.
202 static int verify_dotfile(const char *rest
)
205 * The first character was '.', but that
206 * has already been discarded, we now test
210 /* "." is not allowed */
215 * ".git" followed by NUL or slash is bad. This
216 * shares the path end test with the ".." case.
226 if (rest
[1] == '\0' || rest
[1] == '/')
232 static int verify_path(const char *path
)
249 if (verify_dotfile(path
))
258 static int add_cacheinfo(const char *arg1
, const char *arg2
, const char *arg3
)
260 int size
, len
, option
;
262 unsigned char sha1
[20];
263 struct cache_entry
*ce
;
265 if (sscanf(arg1
, "%o", &mode
) != 1)
267 if (get_sha1_hex(arg2
, sha1
))
269 if (!verify_path(arg3
))
273 size
= cache_entry_size(len
);
277 memcpy(ce
->sha1
, sha1
, 20);
278 memcpy(ce
->name
, arg3
, len
);
279 ce
->ce_flags
= htons(len
);
280 ce
->ce_mode
= create_ce_mode(mode
);
281 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
282 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
283 if (add_cache_entry(ce
, option
))
284 return error("%s: cannot add to the index - missing --add option?",
286 report("add '%s'", arg3
);
290 static int chmod_path(int flip
, const char *path
)
293 struct cache_entry
*ce
;
296 pos
= cache_name_pos(path
, strlen(path
));
299 ce
= active_cache
[pos
];
300 mode
= ntohl(ce
->ce_mode
);
305 ce
->ce_mode
|= htonl(0111); break;
307 ce
->ce_mode
&= htonl(~0111); break;
311 active_cache_changed
= 1;
315 static struct cache_file cache_file
;
317 static void update_one(const char *path
, const char *prefix
, int prefix_length
)
319 const char *p
= prefix_path(prefix
, prefix_length
, path
);
320 if (!verify_path(p
)) {
321 fprintf(stderr
, "Ignoring path %s\n", path
);
325 if (remove_file_from_cache(p
))
326 die("git-update-index: unable to remove %s", path
);
327 report("remove '%s'", path
);
330 if (add_file_to_cache(p
))
331 die("Unable to process file %s", path
);
332 report("add '%s'", path
);
335 static void read_index_info(int line_termination
)
341 unsigned char sha1
[20];
344 read_line(&buf
, stdin
, line_termination
);
348 mode
= strtoul(buf
.buf
, &ptr
, 8);
349 if (ptr
== buf
.buf
|| *ptr
!= ' ' ||
350 get_sha1_hex(ptr
+ 1, sha1
) ||
355 if (!verify_path(ptr
)) {
356 fprintf(stderr
, "Ignoring path %s\n", ptr
);
361 /* mode == 0 means there is no such path -- remove */
362 if (remove_file_from_cache(ptr
))
363 die("git-update-index: unable to remove %s",
367 /* mode ' ' sha1 '\t' name
368 * ptr[-1] points at tab,
369 * ptr[-41] is at the beginning of sha1
371 ptr
[-42] = ptr
[-1] = 0;
372 if (add_cacheinfo(buf
.buf
, ptr
-41, ptr
))
373 die("git-update-index: unable to update %s",
379 die("malformed index info %s", buf
.buf
);
383 int main(int argc
, const char **argv
)
385 int i
, newfd
, entries
, has_errors
= 0, line_termination
= '\n';
386 int allow_options
= 1;
387 int read_from_stdin
= 0;
388 const char *prefix
= setup_git_directory();
389 int prefix_length
= prefix
? strlen(prefix
) : 0;
391 git_config(git_default_config
);
393 newfd
= hold_index_file_for_update(&cache_file
, get_index_file());
395 die("unable to create new cachefile");
397 entries
= read_cache();
399 die("cache corrupted");
401 for (i
= 1 ; i
< argc
; i
++) {
402 const char *path
= argv
[i
];
404 if (allow_options
&& *path
== '-') {
405 if (!strcmp(path
, "--")) {
409 if (!strcmp(path
, "-q")) {
413 if (!strcmp(path
, "--add")) {
417 if (!strcmp(path
, "--replace")) {
421 if (!strcmp(path
, "--remove")) {
425 if (!strcmp(path
, "--unmerged")) {
429 if (!strcmp(path
, "--refresh")) {
430 has_errors
|= refresh_cache();
433 if (!strcmp(path
, "--cacheinfo")) {
435 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
436 if (add_cacheinfo(argv
[i
+1], argv
[i
+2], argv
[i
+3]))
437 die("git-update-index: --cacheinfo cannot add %s", argv
[i
+3]);
441 if (!strcmp(path
, "--chmod=-x") ||
442 !strcmp(path
, "--chmod=+x")) {
444 die("git-update-index: %s <path>", path
);
445 if (chmod_path(path
[8], argv
[++i
]))
446 die("git-update-index: %s cannot chmod %s", path
, argv
[i
]);
449 if (!strcmp(path
, "--info-only")) {
453 if (!strcmp(path
, "--force-remove")) {
457 if (!strcmp(path
, "-z")) {
458 line_termination
= 0;
461 if (!strcmp(path
, "--stdin")) {
463 die("--stdin must be at the end");
467 if (!strcmp(path
, "--index-info")) {
468 allow_add
= allow_replace
= allow_remove
= 1;
469 read_index_info(line_termination
);
472 if (!strcmp(path
, "--ignore-missing")) {
476 if (!strcmp(path
, "--verbose")) {
480 die("unknown option %s", path
);
482 update_one(path
, prefix
, prefix_length
);
484 if (read_from_stdin
) {
488 read_line(&buf
, stdin
, line_termination
);
491 update_one(buf
.buf
, prefix
, prefix_length
);
494 if (active_cache_changed
) {
495 if (write_cache(newfd
, active_cache
, active_nr
) ||
496 commit_index_file(&cache_file
))
497 die("Unable to write new cachefile");
500 return has_errors
? 1 : 0;