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.
16 static int allow_add
= 0, allow_remove
= 0, allow_replace
= 0, allow_unmerged
= 0, not_new
= 0, quiet
= 0, info_only
= 0;
17 static int force_remove
;
19 /* Three functions to allow overloaded pointer return; see linux/err.h */
20 static inline void *ERR_PTR(long error
)
22 return (void *) error
;
25 static inline long PTR_ERR(const void *ptr
)
30 static inline long IS_ERR(const void *ptr
)
32 return (unsigned long)ptr
> (unsigned long)-1000L;
35 static int add_file_to_cache(const char *path
)
37 int size
, namelen
, option
, status
;
38 struct cache_entry
*ce
;
43 status
= lstat(path
, &st
);
44 if (status
< 0 || S_ISDIR(st
.st_mode
)) {
45 /* When we used to have "path" and now we want to add
46 * "path/file", we need a way to remove "path" before
47 * being able to add "path/file". However,
48 * "git-update-index --remove path" would not work.
49 * --force-remove can be used but this is more user
50 * friendly, especially since we can do the opposite
51 * case just fine without --force-remove.
53 if (status
== 0 || (errno
== ENOENT
|| errno
== ENOTDIR
)) {
55 if (remove_file_from_cache(path
))
56 return error("%s: cannot remove from the index",
60 } else if (status
< 0) {
61 return error("%s: does not exist and --remove not passed",
66 return error("%s: is a directory - add files inside instead",
69 return error("lstat(\"%s\"): %s", path
,
72 namelen
= strlen(path
);
73 size
= cache_entry_size(namelen
);
76 memcpy(ce
->name
, path
, namelen
);
77 fill_stat_cache_info(ce
, &st
);
78 ce
->ce_mode
= create_ce_mode(st
.st_mode
);
79 ce
->ce_flags
= htons(namelen
);
80 switch (st
.st_mode
& S_IFMT
) {
82 fd
= open(path
, O_RDONLY
);
84 return error("open(\"%s\"): %s", path
, strerror(errno
));
85 if (index_fd(ce
->sha1
, fd
, &st
, !info_only
, NULL
) < 0)
86 return error("%s: failed to insert into database", path
);
89 target
= xmalloc(st
.st_size
+1);
90 if (readlink(path
, target
, st
.st_size
+1) != st
.st_size
) {
91 char *errstr
= strerror(errno
);
93 return error("readlink(\"%s\"): %s", path
,
97 unsigned char hdr
[50];
99 write_sha1_file_prepare(target
, st
.st_size
, "blob",
100 ce
->sha1
, hdr
, &hdrlen
);
101 } else if (write_sha1_file(target
, st
.st_size
, "blob", ce
->sha1
))
102 return error("%s: failed to insert into database", path
);
106 return error("%s: unsupported file type", path
);
108 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
109 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
110 if (add_cache_entry(ce
, option
))
111 return error("%s: cannot add to the index - missing --add option?",
117 * "refresh" does not calculate a new sha1 file or bring the
118 * cache up-to-date for mode/content changes. But what it
119 * _does_ do is to "re-match" the stat information of a file
120 * with the cache, so that you can refresh the cache for a
121 * file that hasn't been changed but where the stat entry is
124 * For example, you'd want to do this after doing a "git-read-tree",
125 * to link up the stat cache details with the proper files.
127 static struct cache_entry
*refresh_entry(struct cache_entry
*ce
)
130 struct cache_entry
*updated
;
133 if (lstat(ce
->name
, &st
) < 0)
134 return ERR_PTR(-errno
);
136 changed
= ce_match_stat(ce
, &st
);
140 if (ce_modified(ce
, &st
))
141 return ERR_PTR(-EINVAL
);
144 updated
= xmalloc(size
);
145 memcpy(updated
, ce
, size
);
146 fill_stat_cache_info(updated
, &st
);
150 static int refresh_cache(void)
155 for (i
= 0; i
< active_nr
; i
++) {
156 struct cache_entry
*ce
, *new;
157 ce
= active_cache
[i
];
159 while ((i
< active_nr
) &&
160 ! strcmp(active_cache
[i
]->name
, ce
->name
))
165 printf("%s: needs merge\n", ce
->name
);
170 new = refresh_entry(ce
);
174 if (not_new
&& PTR_ERR(new) == -ENOENT
)
178 printf("%s: needs update\n", ce
->name
);
182 active_cache_changed
= 1;
183 /* You can NOT just free active_cache[i] here, since it
184 * might not be necessarily malloc()ed but can also come
186 active_cache
[i
] = new;
192 * We fundamentally don't like some paths: we don't want
193 * dot or dot-dot anywhere, and for obvious reasons don't
194 * want to recurse into ".git" either.
196 * Also, we don't want double slashes or slashes at the
197 * end that can make pathnames ambiguous.
199 static int verify_dotfile(const char *rest
)
202 * The first character was '.', but that
203 * has already been discarded, we now test
207 /* "." is not allowed */
212 * ".git" followed by NUL or slash is bad. This
213 * shares the path end test with the ".." case.
223 if (rest
[1] == '\0' || rest
[1] == '/')
229 static int verify_path(const char *path
)
246 if (verify_dotfile(path
))
255 static int add_cacheinfo(const char *arg1
, const char *arg2
, const char *arg3
)
257 int size
, len
, option
;
259 unsigned char sha1
[20];
260 struct cache_entry
*ce
;
262 if (sscanf(arg1
, "%o", &mode
) != 1)
264 if (get_sha1_hex(arg2
, sha1
))
266 if (!verify_path(arg3
))
270 size
= cache_entry_size(len
);
274 memcpy(ce
->sha1
, sha1
, 20);
275 memcpy(ce
->name
, arg3
, len
);
276 ce
->ce_flags
= htons(len
);
277 ce
->ce_mode
= create_ce_mode(mode
);
278 option
= allow_add
? ADD_CACHE_OK_TO_ADD
: 0;
279 option
|= allow_replace
? ADD_CACHE_OK_TO_REPLACE
: 0;
280 return add_cache_entry(ce
, option
);
283 static struct cache_file cache_file
;
286 static void update_one(const char *path
, const char *prefix
, int prefix_length
)
288 const char *p
= prefix_path(prefix
, prefix_length
, path
);
289 if (!verify_path(p
)) {
290 fprintf(stderr
, "Ignoring path %s\n", path
);
294 if (remove_file_from_cache(p
))
295 die("git-update-index: unable to remove %s", path
);
298 if (add_file_to_cache(p
))
299 die("Unable to process file %s", path
);
302 int main(int argc
, const char **argv
)
304 int i
, newfd
, entries
, has_errors
= 0, line_termination
= '\n';
305 int allow_options
= 1;
306 int read_from_stdin
= 0;
307 const char *prefix
= setup_git_directory();
308 int prefix_length
= prefix
? strlen(prefix
) : 0;
310 newfd
= hold_index_file_for_update(&cache_file
, get_index_file());
312 die("unable to create new cachefile");
314 entries
= read_cache();
316 die("cache corrupted");
318 for (i
= 1 ; i
< argc
; i
++) {
319 const char *path
= argv
[i
];
321 if (allow_options
&& *path
== '-') {
322 if (!strcmp(path
, "--")) {
326 if (!strcmp(path
, "-q")) {
330 if (!strcmp(path
, "--add")) {
334 if (!strcmp(path
, "--replace")) {
338 if (!strcmp(path
, "--remove")) {
342 if (!strcmp(path
, "--unmerged")) {
346 if (!strcmp(path
, "--refresh")) {
347 has_errors
|= refresh_cache();
350 if (!strcmp(path
, "--cacheinfo")) {
352 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
353 if (add_cacheinfo(argv
[i
+1], argv
[i
+2], argv
[i
+3]))
354 die("git-update-index: --cacheinfo cannot add %s", argv
[i
+3]);
358 if (!strcmp(path
, "--info-only")) {
362 if (!strcmp(path
, "--force-remove")) {
366 if (!strcmp(path
, "-z")) {
367 line_termination
= 0;
370 if (!strcmp(path
, "--stdin")) {
372 die("--stdin must be at the end");
376 if (!strcmp(path
, "--ignore-missing")) {
380 die("unknown option %s", path
);
382 update_one(path
, prefix
, prefix_length
);
384 if (read_from_stdin
) {
388 read_line(&buf
, stdin
, line_termination
);
391 update_one(buf
.buf
, prefix
, prefix_length
);
394 if (write_cache(newfd
, active_cache
, active_nr
) ||
395 commit_index_file(&cache_file
))
396 die("Unable to write new cachefile");
398 return has_errors
? 1 : 0;