[PATCH] Improved "git add"
[git/git.git] / update-index.c
CommitLineData
8bc9a0c7
LT
1/*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
e83c5163
LT
6#include "cache.h"
7
121481ab
LT
8/*
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
215a7ad1 12 * like "git-update-index *" and suddenly having all the object
121481ab
LT
13 * files be revision controlled.
14 */
df6e1516 15static int allow_add = 0, allow_remove = 0, allow_replace = 0, not_new = 0, quiet = 0, info_only = 0;
9b63f501 16static int force_remove;
c6e007b0
JB
17
18/* Three functions to allow overloaded pointer return; see linux/err.h */
19static inline void *ERR_PTR(long error)
20{
21 return (void *) error;
22}
23
24static inline long PTR_ERR(const void *ptr)
25{
26 return (long) ptr;
27}
28
29static inline long IS_ERR(const void *ptr)
30{
31 return (unsigned long)ptr > (unsigned long)-1000L;
32}
121481ab 33
ee267527 34static int add_file_to_cache(char *path)
e83c5163 35{
4c5abf42 36 int size, namelen, option, status;
e83c5163
LT
37 struct cache_entry *ce;
38 struct stat st;
39 int fd;
ffbe1add 40 char *target;
e83c5163 41
4c5abf42
JH
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,
215a7ad1 47 * "git-update-index --remove path" would not work.
4c5abf42
JH
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.
51 */
52 if (status == 0 || (errno == ENOENT || errno == ENOTDIR)) {
121481ab
LT
53 if (allow_remove)
54 return remove_file_from_cache(path);
55 }
89bc8c78
AW
56 if (0 == status)
57 return error("%s: is a directory", path);
58 else
59 return error("lstat(\"%s\"): %s", path,
60 strerror(errno));
e83c5163 61 }
e83c5163
LT
62 namelen = strlen(path);
63 size = cache_entry_size(namelen);
812666c8 64 ce = xmalloc(size);
e83c5163
LT
65 memset(ce, 0, size);
66 memcpy(ce->name, path, namelen);
711cf3a0 67 fill_stat_cache_info(ce, &st);
e4479470 68 ce->ce_mode = create_ce_mode(st.st_mode);
f5cabd13 69 ce->ce_flags = htons(namelen);
8ae0a8c5
KS
70 switch (st.st_mode & S_IFMT) {
71 case S_IFREG:
72 fd = open(path, O_RDONLY);
73 if (fd < 0)
74 return -1;
df6e1516 75 if (index_fd(ce->sha1, fd, &st, !info_only, NULL) < 0)
8ae0a8c5
KS
76 return -1;
77 break;
78 case S_IFLNK:
ffbe1add
KS
79 target = xmalloc(st.st_size+1);
80 if (readlink(path, target, st.st_size+1) != st.st_size) {
81 free(target);
8ae0a8c5 82 return -1;
ffbe1add 83 }
df6e1516
BL
84 if (info_only) {
85 unsigned char hdr[50];
86 int hdrlen;
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))
8ae0a8c5 90 return -1;
ffbe1add 91 free(target);
8ae0a8c5
KS
92 break;
93 default:
e83c5163 94 return -1;
8ae0a8c5 95 }
192268c1
JH
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);
121481ab
LT
99}
100
69a97f1d 101static int compare_data(struct cache_entry *ce, struct stat *st)
121481ab 102{
711cf3a0
LT
103 int match = -1;
104 int fd = open(ce->name, O_RDONLY);
105
106 if (fd >= 0) {
69a97f1d
BL
107 unsigned char sha1[20];
108 if (!index_fd(sha1, fd, st, 0, NULL))
109 match = memcmp(sha1, ce->sha1, 20);
711cf3a0
LT
110 close(fd);
111 }
112 return match;
113}
114
ffbe1add
KS
115static int compare_link(struct cache_entry *ce, unsigned long expected_size)
116{
117 int match = -1;
118 char *target;
119 void *buffer;
120 unsigned long size;
121 char type[10];
122 int len;
123
124 target = xmalloc(expected_size);
125 len = readlink(ce->name, target, expected_size);
126 if (len != expected_size) {
127 free(target);
128 return -1;
129 }
130 buffer = read_sha1_file(ce->sha1, type, &size);
131 if (!buffer) {
132 free(target);
133 return -1;
134 }
135 if (size == expected_size)
136 match = memcmp(buffer, target, size);
137 free(buffer);
138 free(target);
139 return match;
140}
141
711cf3a0
LT
142/*
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
148 * out of date.
149 *
667bb59b 150 * For example, you'd want to do this after doing a "git-read-tree",
711cf3a0
LT
151 * to link up the stat cache details with the proper files.
152 */
153static struct cache_entry *refresh_entry(struct cache_entry *ce)
154{
155 struct stat st;
156 struct cache_entry *updated;
157 int changed, size;
158
8ae0a8c5 159 if (lstat(ce->name, &st) < 0)
c6e007b0 160 return ERR_PTR(-errno);
711cf3a0 161
5d728c84 162 changed = ce_match_stat(ce, &st);
711cf3a0
LT
163 if (!changed)
164 return ce;
165
121481ab 166 /*
8ae0a8c5 167 * If the mode or type has changed, there's no point in trying
711cf3a0 168 * to refresh the entry - it's not going to match
121481ab 169 */
8ae0a8c5 170 if (changed & (MODE_CHANGED | TYPE_CHANGED))
c6e007b0 171 return ERR_PTR(-EINVAL);
711cf3a0 172
ffbe1add
KS
173 switch (st.st_mode & S_IFMT) {
174 case S_IFREG:
69a97f1d 175 if (compare_data(ce, &st))
ffbe1add
KS
176 return ERR_PTR(-EINVAL);
177 break;
178 case S_IFLNK:
179 if (compare_link(ce, st.st_size))
180 return ERR_PTR(-EINVAL);
181 break;
182 default:
c6e007b0 183 return ERR_PTR(-EINVAL);
ffbe1add 184 }
711cf3a0
LT
185
186 size = ce_size(ce);
812666c8 187 updated = xmalloc(size);
711cf3a0
LT
188 memcpy(updated, ce, size);
189 fill_stat_cache_info(updated, &st);
190 return updated;
121481ab
LT
191}
192
90535218 193static int refresh_cache(void)
121481ab
LT
194{
195 int i;
90535218 196 int has_errors = 0;
121481ab 197
711cf3a0 198 for (i = 0; i < active_nr; i++) {
1bc992ac
JH
199 struct cache_entry *ce, *new;
200 ce = active_cache[i];
201 if (ce_stage(ce)) {
202 printf("%s: needs merge\n", ce->name);
90535218 203 has_errors = 1;
1bc992ac
JH
204 while ((i < active_nr) &&
205 ! strcmp(active_cache[i]->name, ce->name))
206 i++;
207 i--;
208 continue;
209 }
711cf3a0 210
1bc992ac 211 new = refresh_entry(ce);
c6e007b0 212 if (IS_ERR(new)) {
0ed3715f
LT
213 if (not_new && PTR_ERR(new) == -ENOENT)
214 continue;
215 if (quiet)
216 continue;
217 printf("%s: needs update\n", ce->name);
218 has_errors = 1;
711cf3a0
LT
219 continue;
220 }
ee267527 221 active_cache_changed = 1;
62d046a0
PB
222 /* You can NOT just free active_cache[i] here, since it
223 * might not be necessarily malloc()ed but can also come
224 * from mmap(). */
711cf3a0
LT
225 active_cache[i] = new;
226 }
90535218 227 return has_errors;
e83c5163
LT
228}
229
e83c5163
LT
230/*
231 * We fundamentally don't like some paths: we don't want
320d3a1b
LT
232 * dot or dot-dot anywhere, and for obvious reasons don't
233 * want to recurse into ".git" either.
e83c5163
LT
234 *
235 * Also, we don't want double slashes or slashes at the
aebb2679 236 * end that can make pathnames ambiguous.
e83c5163 237 */
320d3a1b
LT
238static int verify_dotfile(const char *rest)
239{
240 /*
241 * The first character was '.', but that
242 * has already been discarded, we now test
243 * the rest.
244 */
245 switch (*rest) {
246 /* "." is not allowed */
247 case '\0': case '/':
248 return 0;
249
250 /*
251 * ".git" followed by NUL or slash is bad. This
252 * shares the path end test with the ".." case.
253 */
254 case 'g':
255 if (rest[1] != 'i')
256 break;
257 if (rest[2] != 't')
258 break;
259 rest += 2;
260 /* fallthrough */
261 case '.':
262 if (rest[1] == '\0' || rest[1] == '/')
263 return 0;
264 }
265 return 1;
266}
267
e83c5163
LT
268static int verify_path(char *path)
269{
270 char c;
271
272 goto inside;
273 for (;;) {
274 if (!c)
275 return 1;
276 if (c == '/') {
277inside:
278 c = *path++;
320d3a1b
LT
279 switch (c) {
280 default:
e83c5163 281 continue;
320d3a1b
LT
282 case '/': case '\0':
283 break;
284 case '.':
285 if (verify_dotfile(path))
286 continue;
287 }
e83c5163
LT
288 return 0;
289 }
290 c = *path++;
291 }
292}
293
9945d980
LT
294static int add_cacheinfo(char *arg1, char *arg2, char *arg3)
295{
192268c1 296 int size, len, option;
9945d980
LT
297 unsigned int mode;
298 unsigned char sha1[20];
299 struct cache_entry *ce;
300
301 if (sscanf(arg1, "%o", &mode) != 1)
302 return -1;
9945d980
LT
303 if (get_sha1_hex(arg2, sha1))
304 return -1;
9945d980
LT
305 if (!verify_path(arg3))
306 return -1;
9945d980
LT
307
308 len = strlen(arg3);
309 size = cache_entry_size(len);
812666c8 310 ce = xmalloc(size);
9945d980
LT
311 memset(ce, 0, size);
312
313 memcpy(ce->sha1, sha1, 20);
314 memcpy(ce->name, arg3, len);
f5cabd13 315 ce->ce_flags = htons(len);
e4479470 316 ce->ce_mode = create_ce_mode(mode);
192268c1
JH
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);
9945d980
LT
320}
321
e99d59ff 322static struct cache_file cache_file;
f2a19340 323
e83c5163
LT
324int main(int argc, char **argv)
325{
90535218 326 int i, newfd, entries, has_errors = 0;
121481ab 327 int allow_options = 1;
cfb0af1d 328 const char *prefix = setup_git_directory();
bb233d69 329
415e96c8 330 newfd = hold_index_file_for_update(&cache_file, get_index_file());
9614b8dc 331 if (newfd < 0)
2de381f9 332 die("unable to create new cachefile");
9614b8dc 333
e83c5163 334 entries = read_cache();
9614b8dc 335 if (entries < 0)
2de381f9 336 die("cache corrupted");
e83c5163 337
e83c5163
LT
338 for (i = 1 ; i < argc; i++) {
339 char *path = argv[i];
121481ab
LT
340
341 if (allow_options && *path == '-') {
342 if (!strcmp(path, "--")) {
343 allow_options = 0;
344 continue;
345 }
0ed3715f
LT
346 if (!strcmp(path, "-q")) {
347 quiet = 1;
348 continue;
349 }
121481ab
LT
350 if (!strcmp(path, "--add")) {
351 allow_add = 1;
352 continue;
353 }
192268c1
JH
354 if (!strcmp(path, "--replace")) {
355 allow_replace = 1;
356 continue;
357 }
121481ab
LT
358 if (!strcmp(path, "--remove")) {
359 allow_remove = 1;
360 continue;
361 }
362 if (!strcmp(path, "--refresh")) {
90535218 363 has_errors |= refresh_cache();
121481ab
LT
364 continue;
365 }
9945d980 366 if (!strcmp(path, "--cacheinfo")) {
b3f94c4b 367 if (i+3 >= argc)
215a7ad1 368 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
b3f94c4b 369 if (add_cacheinfo(argv[i+1], argv[i+2], argv[i+3]))
215a7ad1 370 die("git-update-index: --cacheinfo cannot add %s", argv[i+3]);
9945d980
LT
371 i += 3;
372 continue;
373 }
df6e1516
BL
374 if (!strcmp(path, "--info-only")) {
375 info_only = 1;
376 continue;
377 }
0ff5bf7c 378 if (!strcmp(path, "--force-remove")) {
9b63f501 379 force_remove = 1;
0ff5bf7c
JH
380 continue;
381 }
382
c6e007b0
JB
383 if (!strcmp(path, "--ignore-missing")) {
384 not_new = 1;
385 continue;
386 }
2de381f9 387 die("unknown option %s", path);
121481ab 388 }
cfb0af1d 389 path = prefix_path(prefix, prefix ? strlen(prefix) : 0, path);
e83c5163
LT
390 if (!verify_path(path)) {
391 fprintf(stderr, "Ignoring path %s\n", argv[i]);
392 continue;
393 }
9b63f501
PB
394 if (force_remove) {
395 if (remove_file_from_cache(path))
215a7ad1 396 die("git-update-index: --force-remove cannot remove %s", path);
9b63f501
PB
397 continue;
398 }
9614b8dc 399 if (add_file_to_cache(path))
89bc8c78 400 die("Unable to add %s to database; maybe you want to use --add option?", path);
e83c5163 401 }
415e96c8
JH
402 if (write_cache(newfd, active_cache, active_nr) ||
403 commit_index_file(&cache_file))
2de381f9 404 die("Unable to write new cachefile");
9614b8dc 405
c4b83e61 406 return has_errors ? 1 : 0;
e83c5163 407}