]> git.ipfire.org Git - thirdparty/git.git/blob - update-index.c
Improve "git add" again.
[thirdparty/git.git] / update-index.c
1 /*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "strbuf.h"
8
9 /*
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.
15 */
16 static int allow_add;
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 */
22 static int info_only;
23 static int force_remove;
24 static int verbose;
25
26 /* Three functions to allow overloaded pointer return; see linux/err.h */
27 static inline void *ERR_PTR(long error)
28 {
29 return (void *) error;
30 }
31
32 static inline long PTR_ERR(const void *ptr)
33 {
34 return (long) ptr;
35 }
36
37 static inline long IS_ERR(const void *ptr)
38 {
39 return (unsigned long)ptr > (unsigned long)-1000L;
40 }
41
42 static void report(const char *fmt, ...)
43 {
44 va_list vp;
45
46 if (!verbose)
47 return;
48
49 va_start(vp, fmt);
50 vprintf(fmt, vp);
51 putchar('\n');
52 va_end(vp);
53 }
54
55 static int add_file_to_cache(const char *path)
56 {
57 int size, namelen, option, status;
58 struct cache_entry *ce;
59 struct stat st;
60
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.
70 */
71 if (status == 0 || (errno == ENOENT || errno == ENOTDIR)) {
72 if (allow_remove) {
73 if (remove_file_from_cache(path))
74 return error("%s: cannot remove from the index",
75 path);
76 else
77 return 0;
78 } else if (status < 0) {
79 return error("%s: does not exist and --remove not passed",
80 path);
81 }
82 }
83 if (0 == status)
84 return error("%s: is a directory - add files inside instead",
85 path);
86 else
87 return error("lstat(\"%s\"): %s", path,
88 strerror(errno));
89 }
90
91 namelen = strlen(path);
92 size = cache_entry_size(namelen);
93 ce = xmalloc(size);
94 memset(ce, 0, size);
95 memcpy(ce->name, path, namelen);
96 fill_stat_cache_info(ce, &st);
97
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
101 * from it.
102 */
103 int pos = cache_name_pos(path, namelen);
104 if (0 <= pos)
105 ce->ce_mode = active_cache[pos]->ce_mode;
106 }
107 ce->ce_flags = htons(namelen);
108
109 if (index_path(ce->sha1, path, &st, !info_only))
110 return -1;
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?",
115 path);
116 return 0;
117 }
118
119 /*
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
125 * out of date.
126 *
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.
129 */
130 static struct cache_entry *refresh_entry(struct cache_entry *ce)
131 {
132 struct stat st;
133 struct cache_entry *updated;
134 int changed, size;
135
136 if (lstat(ce->name, &st) < 0)
137 return ERR_PTR(-errno);
138
139 changed = ce_match_stat(ce, &st);
140 if (!changed)
141 return NULL;
142
143 if (ce_modified(ce, &st))
144 return ERR_PTR(-EINVAL);
145
146 size = ce_size(ce);
147 updated = xmalloc(size);
148 memcpy(updated, ce, size);
149 fill_stat_cache_info(updated, &st);
150 return updated;
151 }
152
153 static int refresh_cache(void)
154 {
155 int i;
156 int has_errors = 0;
157
158 for (i = 0; i < active_nr; i++) {
159 struct cache_entry *ce, *new;
160 ce = active_cache[i];
161 if (ce_stage(ce)) {
162 while ((i < active_nr) &&
163 ! strcmp(active_cache[i]->name, ce->name))
164 i++;
165 i--;
166 if (allow_unmerged)
167 continue;
168 printf("%s: needs merge\n", ce->name);
169 has_errors = 1;
170 continue;
171 }
172
173 new = refresh_entry(ce);
174 if (!new)
175 continue;
176 if (IS_ERR(new)) {
177 if (not_new && PTR_ERR(new) == -ENOENT)
178 continue;
179 if (quiet)
180 continue;
181 printf("%s: needs update\n", ce->name);
182 has_errors = 1;
183 continue;
184 }
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
188 * from mmap(). */
189 active_cache[i] = new;
190 }
191 return has_errors;
192 }
193
194 /*
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.
198 *
199 * Also, we don't want double slashes or slashes at the
200 * end that can make pathnames ambiguous.
201 */
202 static int verify_dotfile(const char *rest)
203 {
204 /*
205 * The first character was '.', but that
206 * has already been discarded, we now test
207 * the rest.
208 */
209 switch (*rest) {
210 /* "." is not allowed */
211 case '\0': case '/':
212 return 0;
213
214 /*
215 * ".git" followed by NUL or slash is bad. This
216 * shares the path end test with the ".." case.
217 */
218 case 'g':
219 if (rest[1] != 'i')
220 break;
221 if (rest[2] != 't')
222 break;
223 rest += 2;
224 /* fallthrough */
225 case '.':
226 if (rest[1] == '\0' || rest[1] == '/')
227 return 0;
228 }
229 return 1;
230 }
231
232 static int verify_path(const char *path)
233 {
234 char c;
235
236 goto inside;
237 for (;;) {
238 if (!c)
239 return 1;
240 if (c == '/') {
241 inside:
242 c = *path++;
243 switch (c) {
244 default:
245 continue;
246 case '/': case '\0':
247 break;
248 case '.':
249 if (verify_dotfile(path))
250 continue;
251 }
252 return 0;
253 }
254 c = *path++;
255 }
256 }
257
258 static int add_cacheinfo(const char *arg1, const char *arg2, const char *arg3)
259 {
260 int size, len, option;
261 unsigned int mode;
262 unsigned char sha1[20];
263 struct cache_entry *ce;
264
265 if (sscanf(arg1, "%o", &mode) != 1)
266 return -1;
267 if (get_sha1_hex(arg2, sha1))
268 return -1;
269 if (!verify_path(arg3))
270 return -1;
271
272 len = strlen(arg3);
273 size = cache_entry_size(len);
274 ce = xmalloc(size);
275 memset(ce, 0, size);
276
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?",
285 arg3);
286 report("add '%s'", arg3);
287 return 0;
288 }
289
290 static int chmod_path(int flip, const char *path)
291 {
292 int pos;
293 struct cache_entry *ce;
294 unsigned int mode;
295
296 pos = cache_name_pos(path, strlen(path));
297 if (pos < 0)
298 return -1;
299 ce = active_cache[pos];
300 mode = ntohl(ce->ce_mode);
301 if (!S_ISREG(mode))
302 return -1;
303 switch (flip) {
304 case '+':
305 ce->ce_mode |= htonl(0111); break;
306 case '-':
307 ce->ce_mode &= htonl(~0111); break;
308 default:
309 return -1;
310 }
311 active_cache_changed = 1;
312 return 0;
313 }
314
315 static struct cache_file cache_file;
316
317 static void update_one(const char *path, const char *prefix, int prefix_length)
318 {
319 const char *p = prefix_path(prefix, prefix_length, path);
320 if (!verify_path(p)) {
321 fprintf(stderr, "Ignoring path %s\n", path);
322 return;
323 }
324 if (force_remove) {
325 if (remove_file_from_cache(p))
326 die("git-update-index: unable to remove %s", path);
327 report("remove '%s'", path);
328 return;
329 }
330 if (add_file_to_cache(p))
331 die("Unable to process file %s", path);
332 report("add '%s'", path);
333 }
334
335 static void read_index_info(int line_termination)
336 {
337 struct strbuf buf;
338 strbuf_init(&buf);
339 while (1) {
340 char *ptr;
341 unsigned char sha1[20];
342 unsigned int mode;
343
344 read_line(&buf, stdin, line_termination);
345 if (buf.eof)
346 break;
347
348 mode = strtoul(buf.buf, &ptr, 8);
349 if (ptr == buf.buf || *ptr != ' ' ||
350 get_sha1_hex(ptr + 1, sha1) ||
351 ptr[41] != '\t')
352 goto bad_line;
353
354 ptr += 42;
355 if (!verify_path(ptr)) {
356 fprintf(stderr, "Ignoring path %s\n", ptr);
357 continue;
358 }
359
360 if (!mode) {
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",
364 ptr);
365 }
366 else {
367 /* mode ' ' sha1 '\t' name
368 * ptr[-1] points at tab,
369 * ptr[-41] is at the beginning of sha1
370 */
371 ptr[-42] = ptr[-1] = 0;
372 if (add_cacheinfo(buf.buf, ptr-41, ptr))
373 die("git-update-index: unable to update %s",
374 ptr);
375 }
376 continue;
377
378 bad_line:
379 die("malformed index info %s", buf.buf);
380 }
381 }
382
383 int main(int argc, const char **argv)
384 {
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;
390
391 git_config(git_default_config);
392
393 newfd = hold_index_file_for_update(&cache_file, get_index_file());
394 if (newfd < 0)
395 die("unable to create new cachefile");
396
397 entries = read_cache();
398 if (entries < 0)
399 die("cache corrupted");
400
401 for (i = 1 ; i < argc; i++) {
402 const char *path = argv[i];
403
404 if (allow_options && *path == '-') {
405 if (!strcmp(path, "--")) {
406 allow_options = 0;
407 continue;
408 }
409 if (!strcmp(path, "-q")) {
410 quiet = 1;
411 continue;
412 }
413 if (!strcmp(path, "--add")) {
414 allow_add = 1;
415 continue;
416 }
417 if (!strcmp(path, "--replace")) {
418 allow_replace = 1;
419 continue;
420 }
421 if (!strcmp(path, "--remove")) {
422 allow_remove = 1;
423 continue;
424 }
425 if (!strcmp(path, "--unmerged")) {
426 allow_unmerged = 1;
427 continue;
428 }
429 if (!strcmp(path, "--refresh")) {
430 has_errors |= refresh_cache();
431 continue;
432 }
433 if (!strcmp(path, "--cacheinfo")) {
434 if (i+3 >= argc)
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]);
438 i += 3;
439 continue;
440 }
441 if (!strcmp(path, "--chmod=-x") ||
442 !strcmp(path, "--chmod=+x")) {
443 if (argc <= i+1)
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]);
447 continue;
448 }
449 if (!strcmp(path, "--info-only")) {
450 info_only = 1;
451 continue;
452 }
453 if (!strcmp(path, "--force-remove")) {
454 force_remove = 1;
455 continue;
456 }
457 if (!strcmp(path, "-z")) {
458 line_termination = 0;
459 continue;
460 }
461 if (!strcmp(path, "--stdin")) {
462 if (i != argc - 1)
463 die("--stdin must be at the end");
464 read_from_stdin = 1;
465 break;
466 }
467 if (!strcmp(path, "--index-info")) {
468 allow_add = allow_replace = allow_remove = 1;
469 read_index_info(line_termination);
470 continue;
471 }
472 if (!strcmp(path, "--ignore-missing")) {
473 not_new = 1;
474 continue;
475 }
476 if (!strcmp(path, "--verbose")) {
477 verbose = 1;
478 continue;
479 }
480 die("unknown option %s", path);
481 }
482 update_one(path, prefix, prefix_length);
483 }
484 if (read_from_stdin) {
485 struct strbuf buf;
486 strbuf_init(&buf);
487 while (1) {
488 read_line(&buf, stdin, line_termination);
489 if (buf.eof)
490 break;
491 update_one(buf.buf, prefix, prefix_length);
492 }
493 }
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");
498 }
499
500 return has_errors ? 1 : 0;
501 }