2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
9 * Default to not allowing changes to the list of files. The
10 * tool doesn't actually care, but this makes it harder to add
11 * files to the revision control by mistake by doing something
12 * like "git-update-index *" and suddenly having all the object
13 * files be revision controlled.
15 static int allow_add
= 0, allow_remove
= 0, allow_replace
= 0, not_new
= 0, quiet
= 0, info_only
= 0;
16 static int force_remove
;
18 /* Three functions to allow overloaded pointer return; see linux/err.h */
19 static inline void *ERR_PTR(long error
)
21 return (void *) error
;
24 static inline long PTR_ERR(const void *ptr
)
29 static inline long IS_ERR(const void *ptr
)
31 return (unsigned long)ptr
> (unsigned long)-1000L;
34 static int add_file_to_cache(char *path
)
36 int size
, namelen
, option
, status
;
37 struct cache_entry
*ce
;
42 status
= lstat(path
, &st
);
43 if (status
< 0 || S_ISDIR(st
.st_mode
)) {
44 /* When we used to have "path" and now we want to add
45 * "path/file", we need a way to remove "path" before
46 * being able to add "path/file". However,
47 * "git-update-index --remove path" would not work.
48 * --force-remove can be used but this is more user
49 * friendly, especially since we can do the opposite
50 * case just fine without --force-remove.
52 if (status
== 0 || (errno
== ENOENT
|| errno
== ENOTDIR
)) {
54 return remove_file_from_cache(path
);
57 return error("%s: is a directory", path
);
59 return error("lstat(\"%s\"): %s", path
,
62 namelen
= strlen(path
);
63 size
= cache_entry_size(namelen
);
66 memcpy(ce
->name
, path
, namelen
);
67 fill_stat_cache_info(ce
, &st
);
68 ce
->ce_mode
= create_ce_mode(st
.st_mode
);
69 ce
->ce_flags
= htons(namelen
);
70 switch (st
.st_mode
& S_IFMT
) {
72 fd
= open(path
, O_RDONLY
);
75 if (index_fd(ce
->sha1
, fd
, &st
, !info_only
, NULL
) < 0)
79 target
= xmalloc(st
.st_size
+1);
80 if (readlink(path
, target
, st
.st_size
+1) != st
.st_size
) {
85 unsigned char hdr
[50];
87 write_sha1_file_prepare(target
, st
.st_size
, "blob",
88 ce
->sha1
, hdr
, &hdrlen
);
89 } else if (write_sha1_file(target
, st
.st_size
, "blob", ce
->sha1
))
96 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
97 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
98 return add_cache_entry(ce
, option
);
101 static int compare_data(struct cache_entry
*ce
, struct stat
*st
)
104 int fd
= open(ce
->name
, O_RDONLY
);
107 unsigned char sha1
[20];
108 if (!index_fd(sha1
, fd
, st
, 0, NULL
))
109 match
= memcmp(sha1
, ce
->sha1
, 20);
115 static int compare_link(struct cache_entry
*ce
, unsigned long expected_size
)
124 target
= xmalloc(expected_size
);
125 len
= readlink(ce
->name
, target
, expected_size
);
126 if (len
!= expected_size
) {
130 buffer
= read_sha1_file(ce
->sha1
, type
, &size
);
135 if (size
== expected_size
)
136 match
= memcmp(buffer
, target
, size
);
143 * "refresh" does not calculate a new sha1 file or bring the
144 * cache up-to-date for mode/content changes. But what it
145 * _does_ do is to "re-match" the stat information of a file
146 * with the cache, so that you can refresh the cache for a
147 * file that hasn't been changed but where the stat entry is
150 * For example, you'd want to do this after doing a "git-read-tree",
151 * to link up the stat cache details with the proper files.
153 static struct cache_entry
*refresh_entry(struct cache_entry
*ce
)
156 struct cache_entry
*updated
;
159 if (lstat(ce
->name
, &st
) < 0)
160 return ERR_PTR(-errno
);
162 changed
= ce_match_stat(ce
, &st
);
167 * If the mode or type has changed, there's no point in trying
168 * to refresh the entry - it's not going to match
170 if (changed
& (MODE_CHANGED
| TYPE_CHANGED
))
171 return ERR_PTR(-EINVAL
);
173 switch (st
.st_mode
& S_IFMT
) {
175 if (compare_data(ce
, &st
))
176 return ERR_PTR(-EINVAL
);
179 if (compare_link(ce
, st
.st_size
))
180 return ERR_PTR(-EINVAL
);
183 return ERR_PTR(-EINVAL
);
187 updated
= xmalloc(size
);
188 memcpy(updated
, ce
, size
);
189 fill_stat_cache_info(updated
, &st
);
193 static int refresh_cache(void)
198 for (i
= 0; i
< active_nr
; i
++) {
199 struct cache_entry
*ce
, *new;
200 ce
= active_cache
[i
];
202 printf("%s: needs merge\n", ce
->name
);
204 while ((i
< active_nr
) &&
205 ! strcmp(active_cache
[i
]->name
, ce
->name
))
211 new = refresh_entry(ce
);
213 if (not_new
&& PTR_ERR(new) == -ENOENT
)
217 printf("%s: needs update\n", ce
->name
);
221 active_cache_changed
= 1;
222 /* You can NOT just free active_cache[i] here, since it
223 * might not be necessarily malloc()ed but can also come
225 active_cache
[i
] = new;
231 * We fundamentally don't like some paths: we don't want
232 * dot or dot-dot anywhere, and for obvious reasons don't
233 * want to recurse into ".git" either.
235 * Also, we don't want double slashes or slashes at the
236 * end that can make pathnames ambiguous.
238 static int verify_dotfile(const char *rest
)
241 * The first character was '.', but that
242 * has already been discarded, we now test
246 /* "." is not allowed */
251 * ".git" followed by NUL or slash is bad. This
252 * shares the path end test with the ".." case.
262 if (rest
[1] == '\0' || rest
[1] == '/')
268 static int verify_path(char *path
)
285 if (verify_dotfile(path
))
294 static int add_cacheinfo(char *arg1
, char *arg2
, char *arg3
)
296 int size
, len
, option
;
298 unsigned char sha1
[20];
299 struct cache_entry
*ce
;
301 if (sscanf(arg1
, "%o", &mode
) != 1)
303 if (get_sha1_hex(arg2
, sha1
))
305 if (!verify_path(arg3
))
309 size
= cache_entry_size(len
);
313 memcpy(ce
->sha1
, sha1
, 20);
314 memcpy(ce
->name
, arg3
, len
);
315 ce
->ce_flags
= htons(len
);
316 ce
->ce_mode
= create_ce_mode(mode
);
317 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
318 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
319 return add_cache_entry(ce
, option
);
322 static struct cache_file cache_file
;
324 int main(int argc
, char **argv
)
326 int i
, newfd
, entries
, has_errors
= 0;
327 int allow_options
= 1;
328 const char *prefix
= setup_git_directory();
330 newfd
= hold_index_file_for_update(&cache_file
, get_index_file());
332 die("unable to create new cachefile");
334 entries
= read_cache();
336 die("cache corrupted");
338 for (i
= 1 ; i
< argc
; i
++) {
339 char *path
= argv
[i
];
341 if (allow_options
&& *path
== '-') {
342 if (!strcmp(path
, "--")) {
346 if (!strcmp(path
, "-q")) {
350 if (!strcmp(path
, "--add")) {
354 if (!strcmp(path
, "--replace")) {
358 if (!strcmp(path
, "--remove")) {
362 if (!strcmp(path
, "--refresh")) {
363 has_errors
|= refresh_cache();
366 if (!strcmp(path
, "--cacheinfo")) {
368 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
369 if (add_cacheinfo(argv
[i
+1], argv
[i
+2], argv
[i
+3]))
370 die("git-update-index: --cacheinfo cannot add %s", argv
[i
+3]);
374 if (!strcmp(path
, "--info-only")) {
378 if (!strcmp(path
, "--force-remove")) {
383 if (!strcmp(path
, "--ignore-missing")) {
387 die("unknown option %s", path
);
389 path
= prefix_path(prefix
, prefix
? strlen(prefix
) : 0, path
);
390 if (!verify_path(path
)) {
391 fprintf(stderr
, "Ignoring path %s\n", argv
[i
]);
395 if (remove_file_from_cache(path
))
396 die("git-update-index: --force-remove cannot remove %s", path
);
399 if (add_file_to_cache(path
))
400 die("Unable to add %s to database; maybe you want to use --add option?", path
);
402 if (write_cache(newfd
, active_cache
, active_nr
) ||
403 commit_index_file(&cache_file
))
404 die("Unable to write new cachefile");
406 return has_errors
? 1 : 0;