Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 749 lines (673 sloc) 19.336 kb
8bc9a0c Add copyright notices.
Linus Torvalds authored
1 /*
2 * GIT - The information manager from hell
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
e83c516 Initial revision of "git", the information manager from hell
Linus Torvalds authored
6 #include "cache.h"
973d6a2 update-index --index-info: adjust for funny-path quoting.
Junio C Hamano authored
7 #include "quote.h"
a6e5642 Use cache-tree in update-index.
Junio C Hamano authored
8 #include "cache-tree.h"
2bd452d git-update-index --unresolve
Junio C Hamano authored
9 #include "tree-walk.h"
fefe81c Make git-update-index a builtin
Lukas Sandström authored
10 #include "builtin.h"
e011054 @torvalds Teach git-update-index about gitlinks
torvalds authored
11 #include "refs.h"
e83c516 Initial revision of "git", the information manager from hell
Linus Torvalds authored
12
121481a Make "update-cache" a bit friendlier to use (and harder to mis-use).
Linus Torvalds authored
13 /*
14 * Default to not allowing changes to the list of files. The
15 * tool doesn't actually care, but this makes it harder to add
16 * files to the revision control by mistake by doing something
215a7ad Big tool rename.
Junio C Hamano authored
17 * like "git-update-index *" and suddenly having all the object
121481a Make "update-cache" a bit friendlier to use (and harder to mis-use).
Linus Torvalds authored
18 * files be revision controlled.
19 */
caf4f58 Improve "git add" again.
Junio C Hamano authored
20 static int allow_add;
21 static int allow_remove;
22 static int allow_replace;
23 static int info_only;
9b63f50 @pasky [PATCH] Make git-update-cache --force-remove regular
pasky authored
24 static int force_remove;
caf4f58 Improve "git add" again.
Junio C Hamano authored
25 static int verbose;
96f1e58 remove unnecessary initializations
David Rientjes authored
26 static int mark_valid_only;
5f73076 "Assume unchanged" git
Junio C Hamano authored
27 #define MARK_VALID 1
28 #define UNMARK_VALID 2
29
caf4f58 Improve "git add" again.
Junio C Hamano authored
30 static void report(const char *fmt, ...)
31 {
32 va_list vp;
33
34 if (!verbose)
35 return;
36
37 va_start(vp, fmt);
38 vprintf(fmt, vp);
39 putchar('\n');
40 va_end(vp);
41 }
42
5f73076 "Assume unchanged" git
Junio C Hamano authored
43 static int mark_valid(const char *path)
44 {
45 int namelen = strlen(path);
46 int pos = cache_name_pos(path, namelen);
47 if (0 <= pos) {
48 switch (mark_valid_only) {
49 case MARK_VALID:
7a51ed6 @torvalds Make on-disk index representation separate from in-core one
torvalds authored
50 active_cache[pos]->ce_flags |= CE_VALID;
5f73076 "Assume unchanged" git
Junio C Hamano authored
51 break;
52 case UNMARK_VALID:
7a51ed6 @torvalds Make on-disk index representation separate from in-core one
torvalds authored
53 active_cache[pos]->ce_flags &= ~CE_VALID;
5f73076 "Assume unchanged" git
Junio C Hamano authored
54 break;
55 }
a6e5642 Use cache-tree in update-index.
Junio C Hamano authored
56 cache_tree_invalidate_path(active_cache_tree, path);
5f73076 "Assume unchanged" git
Junio C Hamano authored
57 active_cache_changed = 1;
58 return 0;
59 }
60 return -1;
61 }
62
e011054 @torvalds Teach git-update-index about gitlinks
torvalds authored
63 static int remove_one_path(const char *path)
e83c516 Initial revision of "git", the information manager from hell
Linus Torvalds authored
64 {
e011054 @torvalds Teach git-update-index about gitlinks
torvalds authored
65 if (!allow_remove)
66 return error("%s: does not exist and --remove not passed", path);
67 if (remove_file_from_cache(path))
68 return error("%s: cannot remove from the index", path);
69 return 0;
70 }
a6e5642 Use cache-tree in update-index.
Junio C Hamano authored
71
e011054 @torvalds Teach git-update-index about gitlinks
torvalds authored
72 /*
73 * Handle a path that couldn't be lstat'ed. It's either:
74 * - missing file (ENOENT or ENOTDIR). That's ok if we're
75 * supposed to be removing it and the removal actually
76 * succeeds.
77 * - permission error. That's never ok.
78 */
79 static int process_lstat_error(const char *path, int err)
80 {
81 if (err == ENOENT || err == ENOTDIR)
82 return remove_one_path(path);
83 return error("lstat(\"%s\"): %s", path, strerror(errno));
84 }
a6e5642 Use cache-tree in update-index.
Junio C Hamano authored
85
e011054 @torvalds Teach git-update-index about gitlinks
torvalds authored
86 static int add_one_path(struct cache_entry *old, const char *path, int len, struct stat *st)
87 {
2263147 @torvalds Fix "git commit directory/" performance anomaly
torvalds authored
88 int option, size;
89 struct cache_entry *ce;
3e09cdf Use core.filemode.
Junio C Hamano authored
90
2263147 @torvalds Fix "git commit directory/" performance anomaly
torvalds authored
91 /* Was the old index entry already up-to-date? */
92 if (old && !ce_stage(old) && !ce_match_stat(old, st, 0))
93 return 0;
94
95 size = cache_entry_size(len);
96 ce = xcalloc(1, size);
e011054 @torvalds Teach git-update-index about gitlinks
torvalds authored
97 memcpy(ce->name, path, len);
7a51ed6 @torvalds Make on-disk index representation separate from in-core one
torvalds authored
98 ce->ce_flags = len;
e011054 @torvalds Teach git-update-index about gitlinks
torvalds authored
99 fill_stat_cache_info(ce, st);
100 ce->ce_mode = ce_mode_from_stat(old, st->st_mode);
ec1fcc1 Show original and resulting blob object info in diff output.
Junio C Hamano authored
101
e011054 @torvalds Teach git-update-index about gitlinks
torvalds authored
102 if (index_path(ce->sha1, path, st, !info_only))
ec1fcc1 Show original and resulting blob object info in diff output.
Junio C Hamano authored
103 return -1;
192268c Add git-update-cache --replace option.
Junio C Hamano authored
104 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
105 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
34143b2 [PATCH] Improve git-update-index error reporting
Petr Baudis authored
106 if (add_cache_entry(ce, option))
e011054 @torvalds Teach git-update-index about gitlinks
torvalds authored
107 return error("%s: cannot add to the index - missing --add option?", path);
34143b2 [PATCH] Improve git-update-index error reporting
Petr Baudis authored
108 return 0;
121481a Make "update-cache" a bit friendlier to use (and harder to mis-use).
Linus Torvalds authored
109 }
110
e011054 @torvalds Teach git-update-index about gitlinks
torvalds authored
111 /*
112 * Handle a path that was a directory. Four cases:
113 *
114 * - it's already a gitlink in the index, and we keep it that
115 * way, and update it if we can (if we cannot find the HEAD,
116 * we're going to keep it unchanged in the index!)
117 *
118 * - it's a *file* in the index, in which case it should be
119 * removed as a file if removal is allowed, since it doesn't
120 * exist as such any more. If removal isn't allowed, it's
121 * an error.
122 *
123 * (NOTE! This is old and arguably fairly strange behaviour.
124 * We might want to make this an error unconditionally, and
125 * use "--force-remove" if you actually want to force removal).
126 *
127 * - it used to exist as a subdirectory (ie multiple files with
128 * this particular prefix) in the index, in which case it's wrong
129 * to try to update it as a directory.
130 *
131 * - it doesn't exist at all in the index, but it is a valid
132 * git directory, and it should be *added* as a gitlink.
133 */
134 static int process_directory(const char *path, int len, struct stat *st)
135 {
136 unsigned char sha1[20];
137 int pos = cache_name_pos(path, len);
138
139 /* Exact match: file or existing gitlink */
140 if (pos >= 0) {
141 struct cache_entry *ce = active_cache[pos];
7a51ed6 @torvalds Make on-disk index representation separate from in-core one
torvalds authored
142 if (S_ISGITLINK(ce->ce_mode)) {
e011054 @torvalds Teach git-update-index about gitlinks
torvalds authored
143
144 /* Do nothing to the index if there is no HEAD! */
145 if (resolve_gitlink_ref(path, "HEAD", sha1) < 0)
146 return 0;
147
148 return add_one_path(ce, path, len, st);
149 }
150 /* Should this be an unconditional error? */
151 return remove_one_path(path);
152 }
153
154 /* Inexact match: is there perhaps a subdirectory match? */
155 pos = -pos-1;
156 while (pos < active_nr) {
157 struct cache_entry *ce = active_cache[pos++];
158
159 if (strncmp(ce->name, path, len))
160 break;
161 if (ce->name[len] > '/')
162 break;
163 if (ce->name[len] < '/')
164 continue;
165
166 /* Subdirectory match - error out */
167 return error("%s: is a directory - add individual files instead", path);
168 }
169
170 /* No match - should we add it as a gitlink? */
171 if (!resolve_gitlink_ref(path, "HEAD", sha1))
172 return add_one_path(NULL, path, len, st);
173
174 /* Error out. */
175 return error("%s: is a directory - add files inside instead", path);
176 }
177
178 /*
179 * Process a regular file
180 */
181 static int process_file(const char *path, int len, struct stat *st)
182 {
183 int pos = cache_name_pos(path, len);
184 struct cache_entry *ce = pos < 0 ? NULL : active_cache[pos];
185
7a51ed6 @torvalds Make on-disk index representation separate from in-core one
torvalds authored
186 if (ce && S_ISGITLINK(ce->ce_mode))
e011054 @torvalds Teach git-update-index about gitlinks
torvalds authored
187 return error("%s is already a gitlink, not replacing", path);
188
189 return add_one_path(ce, path, len, st);
190 }
191
192 static int process_path(const char *path)
193 {
194 int len;
195 struct stat st;
196
197 /*
198 * First things first: get the stat information, to decide
199 * what to do about the pathname!
200 */
201 if (lstat(path, &st) < 0)
202 return process_lstat_error(path, errno);
203
204 len = strlen(path);
205 if (S_ISDIR(st.st_mode))
206 return process_directory(path, len, &st);
207
208 return process_file(path, len, &st);
209 }
210
d23748a update-index: allow --index-info to add higher stages.
Junio C Hamano authored
211 static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
212 const char *path, int stage)
9945d98 Add "--cacheinfo" option to update-cache.
Linus Torvalds authored
213 {
192268c Add git-update-cache --replace option.
Junio C Hamano authored
214 int size, len, option;
9945d98 Add "--cacheinfo" option to update-cache.
Linus Torvalds authored
215 struct cache_entry *ce;
216
d23748a update-index: allow --index-info to add higher stages.
Junio C Hamano authored
217 if (!verify_path(path))
9945d98 Add "--cacheinfo" option to update-cache.
Linus Torvalds authored
218 return -1;
219
d23748a update-index: allow --index-info to add higher stages.
Junio C Hamano authored
220 len = strlen(path);
9945d98 Add "--cacheinfo" option to update-cache.
Linus Torvalds authored
221 size = cache_entry_size(len);
90321c1 Replace xmalloc+memset(0) with xcalloc.
Peter Eriksen authored
222 ce = xcalloc(1, size);
9945d98 Add "--cacheinfo" option to update-cache.
Linus Torvalds authored
223
e702496 @spearce Convert memcpy(a,b,20) to hashcpy(a,b).
spearce authored
224 hashcpy(ce->sha1, sha1);
d23748a update-index: allow --index-info to add higher stages.
Junio C Hamano authored
225 memcpy(ce->name, path, len);
226 ce->ce_flags = create_ce_flags(len, stage);
e447947 Be much more liberal about the file mode bits.
Linus Torvalds authored
227 ce->ce_mode = create_ce_mode(mode);
5f73076 "Assume unchanged" git
Junio C Hamano authored
228 if (assume_unchanged)
7a51ed6 @torvalds Make on-disk index representation separate from in-core one
torvalds authored
229 ce->ce_flags |= CE_VALID;
192268c Add git-update-cache --replace option.
Junio C Hamano authored
230 option = allow_add ? ADD_CACHE_OK_TO_ADD : 0;
231 option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0;
caf4f58 Improve "git add" again.
Junio C Hamano authored
232 if (add_cache_entry(ce, option))
233 return error("%s: cannot add to the index - missing --add option?",
d23748a update-index: allow --index-info to add higher stages.
Junio C Hamano authored
234 path);
235 report("add '%s'", path);
caf4f58 Improve "git add" again.
Junio C Hamano authored
236 return 0;
9945d98 Add "--cacheinfo" option to update-cache.
Linus Torvalds authored
237 }
238
227bdb1 @raalkml make update-index --chmod work with multiple files and --stdin
raalkml authored
239 static void chmod_path(int flip, const char *path)
3e09cdf Use core.filemode.
Junio C Hamano authored
240 {
241 int pos;
242 struct cache_entry *ce;
243 unsigned int mode;
f2a1934 update-cache: remove index lock file on SIGINT
Linus Torvalds authored
244
3e09cdf Use core.filemode.
Junio C Hamano authored
245 pos = cache_name_pos(path, strlen(path));
246 if (pos < 0)
227bdb1 @raalkml make update-index --chmod work with multiple files and --stdin
raalkml authored
247 goto fail;
3e09cdf Use core.filemode.
Junio C Hamano authored
248 ce = active_cache[pos];
7a51ed6 @torvalds Make on-disk index representation separate from in-core one
torvalds authored
249 mode = ce->ce_mode;
3e09cdf Use core.filemode.
Junio C Hamano authored
250 if (!S_ISREG(mode))
227bdb1 @raalkml make update-index --chmod work with multiple files and --stdin
raalkml authored
251 goto fail;
3e09cdf Use core.filemode.
Junio C Hamano authored
252 switch (flip) {
253 case '+':
7a51ed6 @torvalds Make on-disk index representation separate from in-core one
torvalds authored
254 ce->ce_mode |= 0111; break;
3e09cdf Use core.filemode.
Junio C Hamano authored
255 case '-':
7a51ed6 @torvalds Make on-disk index representation separate from in-core one
torvalds authored
256 ce->ce_mode &= ~0111; break;
3e09cdf Use core.filemode.
Junio C Hamano authored
257 default:
227bdb1 @raalkml make update-index --chmod work with multiple files and --stdin
raalkml authored
258 goto fail;
3e09cdf Use core.filemode.
Junio C Hamano authored
259 }
a6e5642 Use cache-tree in update-index.
Junio C Hamano authored
260 cache_tree_invalidate_path(active_cache_tree, path);
3e09cdf Use core.filemode.
Junio C Hamano authored
261 active_cache_changed = 1;
227bdb1 @raalkml make update-index --chmod work with multiple files and --stdin
raalkml authored
262 report("chmod %cx '%s'", flip, path);
263 return;
264 fail:
265 die("git-update-index: cannot chmod %cx '%s'", flip, path);
3e09cdf Use core.filemode.
Junio C Hamano authored
266 }
267
ee1bec3 update-index: --stdin and -z
Junio C Hamano authored
268 static void update_one(const char *path, const char *prefix, int prefix_length)
269 {
270 const char *p = prefix_path(prefix, prefix_length, path);
271 if (!verify_path(p)) {
272 fprintf(stderr, "Ignoring path %s\n", path);
fb69a76 update-index: plug memory leak from prefix_path()
Junio C Hamano authored
273 goto free_return;
ee1bec3 update-index: --stdin and -z
Junio C Hamano authored
274 }
5f73076 "Assume unchanged" git
Junio C Hamano authored
275 if (mark_valid_only) {
276 if (mark_valid(p))
277 die("Unable to mark file %s", path);
fb69a76 update-index: plug memory leak from prefix_path()
Junio C Hamano authored
278 goto free_return;
5f73076 "Assume unchanged" git
Junio C Hamano authored
279 }
280
ee1bec3 update-index: --stdin and -z
Junio C Hamano authored
281 if (force_remove) {
282 if (remove_file_from_cache(p))
283 die("git-update-index: unable to remove %s", path);
caf4f58 Improve "git add" again.
Junio C Hamano authored
284 report("remove '%s'", path);
fb69a76 update-index: plug memory leak from prefix_path()
Junio C Hamano authored
285 goto free_return;
ee1bec3 update-index: --stdin and -z
Junio C Hamano authored
286 }
e011054 @torvalds Teach git-update-index about gitlinks
torvalds authored
287 if (process_path(p))
288 die("Unable to process path %s", path);
caf4f58 Improve "git add" again.
Junio C Hamano authored
289 report("add '%s'", path);
fb69a76 update-index: plug memory leak from prefix_path()
Junio C Hamano authored
290 free_return:
0cc9e70 @dscho Fix users of prefix_path() to free() only when necessary
dscho authored
291 if (p < path || p > path + strlen(path))
fb69a76 update-index: plug memory leak from prefix_path()
Junio C Hamano authored
292 free((char*)p);
ee1bec3 update-index: --stdin and -z
Junio C Hamano authored
293 }
294
d4dbf36 update-index: read --show-index-info output from standard input.
Junio C Hamano authored
295 static void read_index_info(int line_termination)
296 {
297 struct strbuf buf;
7fb1011 @MadCoder Rework unquote_c_style to work on a strbuf.
MadCoder authored
298 struct strbuf uq;
299
f1696ee @MadCoder Strbuf API extensions and fixes.
MadCoder authored
300 strbuf_init(&buf, 0);
7fb1011 @MadCoder Rework unquote_c_style to work on a strbuf.
MadCoder authored
301 strbuf_init(&uq, 0);
302 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
9c20a47 Teach update-index to read from ls-tree.
Junio C Hamano authored
303 char *ptr, *tab;
973d6a2 update-index --index-info: adjust for funny-path quoting.
Junio C Hamano authored
304 char *path_name;
d4dbf36 update-index: read --show-index-info output from standard input.
Junio C Hamano authored
305 unsigned char sha1[20];
306 unsigned int mode;
6aead43 @meyering sscanf/strtoul: parse integers robustly
meyering authored
307 unsigned long ul;
d23748a update-index: allow --index-info to add higher stages.
Junio C Hamano authored
308 int stage;
309
310 /* This reads lines formatted in one of three formats:
311 *
312 * (1) mode SP sha1 TAB path
313 * The first format is what "git-apply --index-info"
314 * reports, and used to reconstruct a partial tree
315 * that is used for phony merge base tree when falling
316 * back on 3-way merge.
317 *
318 * (2) mode SP type SP sha1 TAB path
319 * The second format is to stuff git-ls-tree output
320 * into the index file.
fefe81c Make git-update-index a builtin
Lukas Sandström authored
321 *
d23748a update-index: allow --index-info to add higher stages.
Junio C Hamano authored
322 * (3) mode SP sha1 SP stage TAB path
323 * This format is to put higher order stages into the
324 * index file and matches git-ls-files --stage output.
325 */
6aead43 @meyering sscanf/strtoul: parse integers robustly
meyering authored
326 errno = 0;
327 ul = strtoul(buf.buf, &ptr, 8);
328 if (ptr == buf.buf || *ptr != ' '
329 || errno || (unsigned int) ul != ul)
d4dbf36 update-index: read --show-index-info output from standard input.
Junio C Hamano authored
330 goto bad_line;
6aead43 @meyering sscanf/strtoul: parse integers robustly
meyering authored
331 mode = ul;
d4dbf36 update-index: read --show-index-info output from standard input.
Junio C Hamano authored
332
9c20a47 Teach update-index to read from ls-tree.
Junio C Hamano authored
333 tab = strchr(ptr, '\t');
334 if (!tab || tab - ptr < 41)
335 goto bad_line;
d23748a update-index: allow --index-info to add higher stages.
Junio C Hamano authored
336
2d49711 update-index --index-info: allow stage 0 entries.
Junio C Hamano authored
337 if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') {
d23748a update-index: allow --index-info to add higher stages.
Junio C Hamano authored
338 stage = tab[-1] - '0';
339 ptr = tab + 1; /* point at the head of path */
340 tab = tab - 2; /* point at tail of sha1 */
341 }
342 else {
343 stage = 0;
344 ptr = tab + 1; /* point at the head of path */
345 }
346
9c20a47 Teach update-index to read from ls-tree.
Junio C Hamano authored
347 if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ')
348 goto bad_line;
973d6a2 update-index --index-info: adjust for funny-path quoting.
Junio C Hamano authored
349
7fb1011 @MadCoder Rework unquote_c_style to work on a strbuf.
MadCoder authored
350 path_name = ptr;
351 if (line_termination && path_name[0] == '"') {
352 strbuf_reset(&uq);
353 if (unquote_c_style(&uq, path_name, NULL)) {
354 die("git-update-index: bad quoting of path name");
355 }
356 path_name = uq.buf;
357 }
973d6a2 update-index --index-info: adjust for funny-path quoting.
Junio C Hamano authored
358
359 if (!verify_path(path_name)) {
360 fprintf(stderr, "Ignoring path %s\n", path_name);
d4dbf36 update-index: read --show-index-info output from standard input.
Junio C Hamano authored
361 continue;
362 }
363
364 if (!mode) {
365 /* mode == 0 means there is no such path -- remove */
973d6a2 update-index --index-info: adjust for funny-path quoting.
Junio C Hamano authored
366 if (remove_file_from_cache(path_name))
d4dbf36 update-index: read --show-index-info output from standard input.
Junio C Hamano authored
367 die("git-update-index: unable to remove %s",
368 ptr);
369 }
370 else {
371 /* mode ' ' sha1 '\t' name
372 * ptr[-1] points at tab,
373 * ptr[-41] is at the beginning of sha1
374 */
375 ptr[-42] = ptr[-1] = 0;
d23748a update-index: allow --index-info to add higher stages.
Junio C Hamano authored
376 if (add_cacheinfo(mode, sha1, path_name, stage))
d4dbf36 update-index: read --show-index-info output from standard input.
Junio C Hamano authored
377 die("git-update-index: unable to update %s",
973d6a2 update-index --index-info: adjust for funny-path quoting.
Junio C Hamano authored
378 path_name);
d4dbf36 update-index: read --show-index-info output from standard input.
Junio C Hamano authored
379 }
380 continue;
381
382 bad_line:
383 die("malformed index info %s", buf.buf);
384 }
e6c019d @MadCoder Drop strbuf's 'eof' marker, and make read_line a first class citizen.
MadCoder authored
385 strbuf_release(&buf);
7fb1011 @MadCoder Rework unquote_c_style to work on a strbuf.
MadCoder authored
386 strbuf_release(&uq);
d4dbf36 update-index: read --show-index-info output from standard input.
Junio C Hamano authored
387 }
388
f6ab5bb Add usage string to git-update-index
Petr Baudis authored
389 static const char update_index_usage[] =
7099c9c update-index -g
Junio C Hamano authored
390 "git-update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--really-refresh] [--cacheinfo] [--chmod=(+|-)x] [--assume-unchanged] [--info-only] [--force-remove] [--stdin] [--index-info] [--unresolve] [--again | -g] [--ignore-missing] [-z] [--verbose] [--] <file>...";
f6ab5bb Add usage string to git-update-index
Petr Baudis authored
391
2bd452d git-update-index --unresolve
Junio C Hamano authored
392 static unsigned char head_sha1[20];
393 static unsigned char merge_head_sha1[20];
394
395 static struct cache_entry *read_one_ent(const char *which,
396 unsigned char *ent, const char *path,
397 int namelen, int stage)
398 {
399 unsigned mode;
400 unsigned char sha1[20];
401 int size;
402 struct cache_entry *ce;
403
404 if (get_tree_entry(ent, path, sha1, &mode)) {
83e77a2 update-index --again
Junio C Hamano authored
405 if (which)
406 error("%s: not in %s branch.", path, which);
2bd452d git-update-index --unresolve
Junio C Hamano authored
407 return NULL;
408 }
409 if (mode == S_IFDIR) {
83e77a2 update-index --again
Junio C Hamano authored
410 if (which)
411 error("%s: not a blob in %s branch.", path, which);
2bd452d git-update-index --unresolve
Junio C Hamano authored
412 return NULL;
413 }
414 size = cache_entry_size(namelen);
415 ce = xcalloc(1, size);
416
e702496 @spearce Convert memcpy(a,b,20) to hashcpy(a,b).
spearce authored
417 hashcpy(ce->sha1, sha1);
2bd452d git-update-index --unresolve
Junio C Hamano authored
418 memcpy(ce->name, path, namelen);
419 ce->ce_flags = create_ce_flags(namelen, stage);
420 ce->ce_mode = create_ce_mode(mode);
421 return ce;
422 }
423
424 static int unresolve_one(const char *path)
425 {
426 int namelen = strlen(path);
427 int pos;
428 int ret = 0;
429 struct cache_entry *ce_2 = NULL, *ce_3 = NULL;
430
431 /* See if there is such entry in the index. */
432 pos = cache_name_pos(path, namelen);
433 if (pos < 0) {
434 /* If there isn't, either it is unmerged, or
435 * resolved as "removed" by mistake. We do not
436 * want to do anything in the former case.
437 */
438 pos = -pos-1;
439 if (pos < active_nr) {
440 struct cache_entry *ce = active_cache[pos];
441 if (ce_namelen(ce) == namelen &&
442 !memcmp(ce->name, path, namelen)) {
443 fprintf(stderr,
444 "%s: skipping still unmerged path.\n",
445 path);
446 goto free_return;
447 }
448 }
449 }
450
451 /* Grab blobs from given path from HEAD and MERGE_HEAD,
452 * stuff HEAD version in stage #2,
453 * stuff MERGE_HEAD version in stage #3.
454 */
455 ce_2 = read_one_ent("our", head_sha1, path, namelen, 2);
456 ce_3 = read_one_ent("their", merge_head_sha1, path, namelen, 3);
457
458 if (!ce_2 || !ce_3) {
459 ret = -1;
460 goto free_return;
461 }
a89fccd Do not use memcmp(sha1_1, sha1_2, 20) with hardcoded length.
David Rientjes authored
462 if (!hashcmp(ce_2->sha1, ce_3->sha1) &&
2bd452d git-update-index --unresolve
Junio C Hamano authored
463 ce_2->ce_mode == ce_3->ce_mode) {
464 fprintf(stderr, "%s: identical in both, skipping.\n",
465 path);
466 goto free_return;
467 }
468
469 remove_file_from_cache(path);
470 if (add_cache_entry(ce_2, ADD_CACHE_OK_TO_ADD)) {
471 error("%s: cannot add our version to the index.", path);
472 ret = -1;
473 goto free_return;
474 }
475 if (!add_cache_entry(ce_3, ADD_CACHE_OK_TO_ADD))
476 return 0;
477 error("%s: cannot add their version to the index.", path);
478 ret = -1;
479 free_return:
480 free(ce_2);
481 free(ce_3);
482 return ret;
483 }
484
485 static void read_head_pointers(void)
486 {
913c983 @dscho Fix git-update-index --again
dscho authored
487 if (read_ref("HEAD", head_sha1))
2bd452d git-update-index --unresolve
Junio C Hamano authored
488 die("No HEAD -- no initial commit yet?\n");
913c983 @dscho Fix git-update-index --again
dscho authored
489 if (read_ref("MERGE_HEAD", merge_head_sha1)) {
2bd452d git-update-index --unresolve
Junio C Hamano authored
490 fprintf(stderr, "Not in the middle of a merge.\n");
491 exit(0);
492 }
493 }
494
09895c1 update-index --unresolve: work from a subdirectory.
Junio C Hamano authored
495 static int do_unresolve(int ac, const char **av,
496 const char *prefix, int prefix_length)
2bd452d git-update-index --unresolve
Junio C Hamano authored
497 {
498 int i;
499 int err = 0;
500
501 /* Read HEAD and MERGE_HEAD; if MERGE_HEAD does not exist, we
502 * are not doing a merge, so exit with success status.
503 */
504 read_head_pointers();
505
506 for (i = 1; i < ac; i++) {
507 const char *arg = av[i];
09895c1 update-index --unresolve: work from a subdirectory.
Junio C Hamano authored
508 const char *p = prefix_path(prefix, prefix_length, arg);
509 err |= unresolve_one(p);
0cc9e70 @dscho Fix users of prefix_path() to free() only when necessary
dscho authored
510 if (p < arg || p > arg + strlen(arg))
09895c1 update-index --unresolve: work from a subdirectory.
Junio C Hamano authored
511 free((char*)p);
2bd452d git-update-index --unresolve
Junio C Hamano authored
512 }
513 return err;
514 }
515
83e77a2 update-index --again
Junio C Hamano authored
516 static int do_reupdate(int ac, const char **av,
517 const char *prefix, int prefix_length)
518 {
519 /* Read HEAD and run update-index on paths that are
520 * merged and already different between index and HEAD.
521 */
522 int pos;
523 int has_head = 1;
0cc9e70 @dscho Fix users of prefix_path() to free() only when necessary
dscho authored
524 const char **pathspec = get_pathspec(prefix, av + 1);
83e77a2 update-index --again
Junio C Hamano authored
525
913c983 @dscho Fix git-update-index --again
dscho authored
526 if (read_ref("HEAD", head_sha1))
83e77a2 update-index --again
Junio C Hamano authored
527 /* If there is no HEAD, that means it is an initial
528 * commit. Update everything in the index.
529 */
530 has_head = 0;
531 redo:
532 for (pos = 0; pos < active_nr; pos++) {
533 struct cache_entry *ce = active_cache[pos];
534 struct cache_entry *old = NULL;
535 int save_nr;
22293b9 update-index --again: take optional pathspecs
Junio C Hamano authored
536
537 if (ce_stage(ce) || !ce_path_match(ce, pathspec))
83e77a2 update-index --again
Junio C Hamano authored
538 continue;
539 if (has_head)
540 old = read_one_ent(NULL, head_sha1,
541 ce->name, ce_namelen(ce), 0);
542 if (old && ce->ce_mode == old->ce_mode &&
a89fccd Do not use memcmp(sha1_1, sha1_2, 20) with hardcoded length.
David Rientjes authored
543 !hashcmp(ce->sha1, old->sha1)) {
83e77a2 update-index --again
Junio C Hamano authored
544 free(old);
545 continue; /* unchanged */
546 }
547 /* Be careful. The working tree may not have the
548 * path anymore, in which case, under 'allow_remove',
549 * or worse yet 'allow_replace', active_nr may decrease.
550 */
551 save_nr = active_nr;
552 update_one(ce->name + prefix_length, prefix, prefix_length);
553 if (save_nr != active_nr)
554 goto redo;
555 }
556 return 0;
557 }
558
a633fca Call setup_git_directory() much earlier
Linus Torvalds authored
559 int cmd_update_index(int argc, const char **argv, const char *prefix)
e83c516 Initial revision of "git", the information manager from hell
Linus Torvalds authored
560 {
ee1bec3 update-index: --stdin and -z
Junio C Hamano authored
561 int i, newfd, entries, has_errors = 0, line_termination = '\n';
121481a Make "update-cache" a bit friendlier to use (and harder to mis-use).
Linus Torvalds authored
562 int allow_options = 1;
ee1bec3 update-index: --stdin and -z
Junio C Hamano authored
563 int read_from_stdin = 0;
564 int prefix_length = prefix ? strlen(prefix) : 0;
227bdb1 @raalkml make update-index --chmod work with multiple files and --stdin
raalkml authored
565 char set_executable_bit = 0;
405e5b2 Libify the index refresh logic
Linus Torvalds authored
566 unsigned int refresh_flags = 0;
7b802b8 update-index: do not die too early in a read-only repository.
Junio C Hamano authored
567 int lock_error = 0;
fefe81c Make git-update-index a builtin
Lukas Sandström authored
568 struct lock_file *lock_file;
bb233d6 Add support for a "GIT_INDEX_FILE" environment variable.
Linus Torvalds authored
569
3e09cdf Use core.filemode.
Junio C Hamano authored
570 git_config(git_default_config);
571
fefe81c Make git-update-index a builtin
Lukas Sandström authored
572 /* We can't free this memory, it becomes part of a linked list parsed atexit() */
928e47e @dscho Initialize lock_file struct to all zero.
dscho authored
573 lock_file = xcalloc(1, sizeof(struct lock_file));
fefe81c Make git-update-index a builtin
Lukas Sandström authored
574
30ca07a _GIT_INDEX_OUTPUT: allow plumbing to output to an alternative index file...
Junio C Hamano authored
575 newfd = hold_locked_index(lock_file, 0);
7b802b8 update-index: do not die too early in a read-only repository.
Junio C Hamano authored
576 if (newfd < 0)
577 lock_error = errno;
9614b8d Fix stale index.lock file removal using "atexit()".
Linus Torvalds authored
578
e83c516 Initial revision of "git", the information manager from hell
Linus Torvalds authored
579 entries = read_cache();
9614b8d Fix stale index.lock file removal using "atexit()".
Linus Torvalds authored
580 if (entries < 0)
2de381f @pasky [PATCH] Consolidate the error handling
pasky authored
581 die("cache corrupted");
e83c516 Initial revision of "git", the information manager from hell
Linus Torvalds authored
582
583 for (i = 1 ; i < argc; i++) {
6b5ee13 Diff clean-up.
Junio C Hamano authored
584 const char *path = argv[i];
9ebe6cf @raalkml Fix git-update-index to work with relative pathnames.
raalkml authored
585 const char *p;
121481a Make "update-cache" a bit friendlier to use (and harder to mis-use).
Linus Torvalds authored
586
587 if (allow_options && *path == '-') {
588 if (!strcmp(path, "--")) {
589 allow_options = 0;
590 continue;
591 }
0ed3715 Add "-q" flag to git-update-cache
Linus Torvalds authored
592 if (!strcmp(path, "-q")) {
405e5b2 Libify the index refresh logic
Linus Torvalds authored
593 refresh_flags |= REFRESH_QUIET;
0ed3715 Add "-q" flag to git-update-cache
Linus Torvalds authored
594 continue;
595 }
121481a Make "update-cache" a bit friendlier to use (and harder to mis-use).
Linus Torvalds authored
596 if (!strcmp(path, "--add")) {
597 allow_add = 1;
598 continue;
599 }
192268c Add git-update-cache --replace option.
Junio C Hamano authored
600 if (!strcmp(path, "--replace")) {
601 allow_replace = 1;
602 continue;
603 }
121481a Make "update-cache" a bit friendlier to use (and harder to mis-use).
Linus Torvalds authored
604 if (!strcmp(path, "--remove")) {
605 allow_remove = 1;
606 continue;
607 }
5d1a5c0 [PATCH] Better error reporting for "git status"
Linus Torvalds authored
608 if (!strcmp(path, "--unmerged")) {
405e5b2 Libify the index refresh logic
Linus Torvalds authored
609 refresh_flags |= REFRESH_UNMERGED;
5d1a5c0 [PATCH] Better error reporting for "git status"
Linus Torvalds authored
610 continue;
611 }
121481a Make "update-cache" a bit friendlier to use (and harder to mis-use).
Linus Torvalds authored
612 if (!strcmp(path, "--refresh")) {
405e5b2 Libify the index refresh logic
Linus Torvalds authored
613 has_errors |= refresh_cache(refresh_flags);
5f73076 "Assume unchanged" git
Junio C Hamano authored
614 continue;
615 }
616 if (!strcmp(path, "--really-refresh")) {
405e5b2 Libify the index refresh logic
Linus Torvalds authored
617 has_errors |= refresh_cache(REFRESH_REALLY | refresh_flags);
121481a Make "update-cache" a bit friendlier to use (and harder to mis-use).
Linus Torvalds authored
618 continue;
619 }
9945d98 Add "--cacheinfo" option to update-cache.
Linus Torvalds authored
620 if (!strcmp(path, "--cacheinfo")) {
d23748a update-index: allow --index-info to add higher stages.
Junio C Hamano authored
621 unsigned char sha1[20];
622 unsigned int mode;
623
b3f94c4 Fix git-update-cache --cacheinfo error message.
Junio C Hamano authored
624 if (i+3 >= argc)
215a7ad Big tool rename.
Junio C Hamano authored
625 die("git-update-index: --cacheinfo <mode> <sha1> <path>");
d23748a update-index: allow --index-info to add higher stages.
Junio C Hamano authored
626
6e6db39 @awhitcroft fix up strtoul_ui error handling
awhitcroft authored
627 if (strtoul_ui(argv[i+1], 8, &mode) ||
d23748a update-index: allow --index-info to add higher stages.
Junio C Hamano authored
628 get_sha1_hex(argv[i+2], sha1) ||
629 add_cacheinfo(mode, sha1, argv[i+3], 0))
630 die("git-update-index: --cacheinfo"
631 " cannot add %s", argv[i+3]);
9945d98 Add "--cacheinfo" option to update-cache.
Linus Torvalds authored
632 i += 3;
633 continue;
634 }
3e09cdf Use core.filemode.
Junio C Hamano authored
635 if (!strcmp(path, "--chmod=-x") ||
636 !strcmp(path, "--chmod=+x")) {
637 if (argc <= i+1)
638 die("git-update-index: %s <path>", path);
227bdb1 @raalkml make update-index --chmod work with multiple files and --stdin
raalkml authored
639 set_executable_bit = path[8];
3e09cdf Use core.filemode.
Junio C Hamano authored
640 continue;
641 }
5f73076 "Assume unchanged" git
Junio C Hamano authored
642 if (!strcmp(path, "--assume-unchanged")) {
643 mark_valid_only = MARK_VALID;
644 continue;
645 }
646 if (!strcmp(path, "--no-assume-unchanged")) {
647 mark_valid_only = UNMARK_VALID;
648 continue;
649 }
df6e151 @bryanlarsen [PATCH] Add --info-only option to git-update-cache.
bryanlarsen authored
650 if (!strcmp(path, "--info-only")) {
651 info_only = 1;
652 continue;
653 }
0ff5bf7 Implement git-update-cache --force-remove <path>
Junio C Hamano authored
654 if (!strcmp(path, "--force-remove")) {
9b63f50 @pasky [PATCH] Make git-update-cache --force-remove regular
pasky authored
655 force_remove = 1;
0ff5bf7 Implement git-update-cache --force-remove <path>
Junio C Hamano authored
656 continue;
657 }
ee1bec3 update-index: --stdin and -z
Junio C Hamano authored
658 if (!strcmp(path, "-z")) {
659 line_termination = 0;
660 continue;
661 }
662 if (!strcmp(path, "--stdin")) {
663 if (i != argc - 1)
664 die("--stdin must be at the end");
665 read_from_stdin = 1;
666 break;
667 }
d4dbf36 update-index: read --show-index-info output from standard input.
Junio C Hamano authored
668 if (!strcmp(path, "--index-info")) {
a41c175 @spearce Prevent --index-info from ignoring -z.
spearce authored
669 if (i != argc - 1)
670 die("--index-info must be at the end");
d4dbf36 update-index: read --show-index-info output from standard input.
Junio C Hamano authored
671 allow_add = allow_replace = allow_remove = 1;
672 read_index_info(line_termination);
a41c175 @spearce Prevent --index-info from ignoring -z.
spearce authored
673 break;
d4dbf36 update-index: read --show-index-info output from standard input.
Junio C Hamano authored
674 }
2bd452d git-update-index --unresolve
Junio C Hamano authored
675 if (!strcmp(path, "--unresolve")) {
09895c1 update-index --unresolve: work from a subdirectory.
Junio C Hamano authored
676 has_errors = do_unresolve(argc - i, argv + i,
677 prefix, prefix_length);
2bd452d git-update-index --unresolve
Junio C Hamano authored
678 if (has_errors)
679 active_cache_changed = 0;
680 goto finish;
681 }
7099c9c update-index -g
Junio C Hamano authored
682 if (!strcmp(path, "--again") || !strcmp(path, "-g")) {
83e77a2 update-index --again
Junio C Hamano authored
683 has_errors = do_reupdate(argc - i, argv + i,
684 prefix, prefix_length);
685 if (has_errors)
686 active_cache_changed = 0;
687 goto finish;
688 }
c6e007b [PATCH] update-cache: add "--ignore-missing" option
James Bottomley authored
689 if (!strcmp(path, "--ignore-missing")) {
405e5b2 Libify the index refresh logic
Linus Torvalds authored
690 refresh_flags |= REFRESH_IGNORE_MISSING;
c6e007b [PATCH] update-cache: add "--ignore-missing" option
James Bottomley authored
691 continue;
692 }
caf4f58 Improve "git add" again.
Junio C Hamano authored
693 if (!strcmp(path, "--verbose")) {
694 verbose = 1;
695 continue;
696 }
f6ab5bb Add usage string to git-update-index
Petr Baudis authored
697 if (!strcmp(path, "-h") || !strcmp(path, "--help"))
698 usage(update_index_usage);
2de381f @pasky [PATCH] Consolidate the error handling
pasky authored
699 die("unknown option %s", path);
121481a Make "update-cache" a bit friendlier to use (and harder to mis-use).
Linus Torvalds authored
700 }
9ebe6cf @raalkml Fix git-update-index to work with relative pathnames.
raalkml authored
701 p = prefix_path(prefix, prefix_length, path);
702 update_one(p, NULL, 0);
227bdb1 @raalkml make update-index --chmod work with multiple files and --stdin
raalkml authored
703 if (set_executable_bit)
9ebe6cf @raalkml Fix git-update-index to work with relative pathnames.
raalkml authored
704 chmod_path(set_executable_bit, p);
705 if (p < path || p > path + strlen(path))
706 free((char*)p);
ee1bec3 update-index: --stdin and -z
Junio C Hamano authored
707 }
708 if (read_from_stdin) {
7fb1011 @MadCoder Rework unquote_c_style to work on a strbuf.
MadCoder authored
709 struct strbuf buf, nbuf;
710
f1696ee @MadCoder Strbuf API extensions and fixes.
MadCoder authored
711 strbuf_init(&buf, 0);
7fb1011 @MadCoder Rework unquote_c_style to work on a strbuf.
MadCoder authored
712 strbuf_init(&nbuf, 0);
713 while (strbuf_getline(&buf, stdin, line_termination) != EOF) {
fb69a76 update-index: plug memory leak from prefix_path()
Junio C Hamano authored
714 const char *p;
7fb1011 @MadCoder Rework unquote_c_style to work on a strbuf.
MadCoder authored
715 if (line_termination && buf.buf[0] == '"') {
716 strbuf_reset(&nbuf);
717 if (unquote_c_style(&nbuf, buf.buf, NULL))
718 die("line is badly quoted");
719 strbuf_swap(&buf, &nbuf);
720 }
721 p = prefix_path(prefix, prefix_length, buf.buf);
fb69a76 update-index: plug memory leak from prefix_path()
Junio C Hamano authored
722 update_one(p, NULL, 0);
723 if (set_executable_bit)
227bdb1 @raalkml make update-index --chmod work with multiple files and --stdin
raalkml authored
724 chmod_path(set_executable_bit, p);
7fb1011 @MadCoder Rework unquote_c_style to work on a strbuf.
MadCoder authored
725 if (p < buf.buf || p > buf.buf + buf.len)
726 free((char *)p);
9b63f50 @pasky [PATCH] Make git-update-cache --force-remove regular
pasky authored
727 }
7fb1011 @MadCoder Rework unquote_c_style to work on a strbuf.
MadCoder authored
728 strbuf_release(&nbuf);
e6c019d @MadCoder Drop strbuf's 'eof' marker, and make read_line a first class citizen.
MadCoder authored
729 strbuf_release(&buf);
e83c516 Initial revision of "git", the information manager from hell
Linus Torvalds authored
730 }
2bd452d git-update-index --unresolve
Junio C Hamano authored
731
732 finish:
5cd5ace [PATCH] Re-instate index file write optimization
Linus Torvalds authored
733 if (active_cache_changed) {
7b802b8 update-index: do not die too early in a read-only repository.
Junio C Hamano authored
734 if (newfd < 0) {
735 if (refresh_flags & REFRESH_QUIET)
736 exit(128);
737 die("unable to create '%s.lock': %s",
738 get_index_file(), strerror(lock_error));
739 }
5cd5ace [PATCH] Re-instate index file write optimization
Linus Torvalds authored
740 if (write_cache(newfd, active_cache, active_nr) ||
4ed7cd3 Improve use of lockfile API
Brandon Casey authored
741 commit_locked_index(lock_file))
021b6e4 Make index file locking code reusable to others.
Junio C Hamano authored
742 die("Unable to write new index file");
5cd5ace [PATCH] Re-instate index file write optimization
Linus Torvalds authored
743 }
9614b8d Fix stale index.lock file removal using "atexit()".
Linus Torvalds authored
744
fefe81c Make git-update-index a builtin
Lukas Sandström authored
745 rollback_lock_file(lock_file);
746
c4b83e6 Do not write out new index if nothing has changed.
Junio C Hamano authored
747 return has_errors ? 1 : 0;
e83c516 Initial revision of "git", the information manager from hell
Linus Torvalds authored
748 }
Something went wrong with that request. Please try again.