Linus Torvalds | 8bc9a0c | 2005-04-07 15:16:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * GIT - The information manager from hell |
| 3 | * |
| 4 | * Copyright (C) Linus Torvalds, 2005 |
| 5 | */ |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 6 | #include "cache.h" |
Junio C Hamano | ee1bec3 | 2005-09-26 18:13:08 -0700 | [diff] [blame] | 7 | #include "strbuf.h" |
Junio C Hamano | 973d6a2 | 2005-10-16 00:39:07 -0700 | [diff] [blame] | 8 | #include "quote.h" |
Junio C Hamano | a6e5642 | 2006-04-24 00:23:54 -0700 | [diff] [blame] | 9 | #include "cache-tree.h" |
Junio C Hamano | 2bd452d | 2006-04-19 23:52:05 -0700 | [diff] [blame] | 10 | #include "tree-walk.h" |
Lukas Sandström | fefe81c | 2006-06-13 22:21:57 +0200 | [diff] [blame] | 11 | #include "builtin.h" |
Linus Torvalds | e011054 | 2007-04-12 12:29:40 -0700 | [diff] [blame] | 12 | #include "refs.h" |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 13 | |
Linus Torvalds | 121481a | 2005-04-10 11:32:54 -0700 | [diff] [blame] | 14 | /* |
| 15 | * Default to not allowing changes to the list of files. The |
| 16 | * tool doesn't actually care, but this makes it harder to add |
| 17 | * files to the revision control by mistake by doing something |
Junio C Hamano | 215a7ad | 2005-09-07 17:26:23 -0700 | [diff] [blame] | 18 | * like "git-update-index *" and suddenly having all the object |
Linus Torvalds | 121481a | 2005-04-10 11:32:54 -0700 | [diff] [blame] | 19 | * files be revision controlled. |
| 20 | */ |
Junio C Hamano | caf4f58 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 21 | static int allow_add; |
| 22 | static int allow_remove; |
| 23 | static int allow_replace; |
Junio C Hamano | caf4f58 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 24 | static int info_only; |
Petr Baudis | 9b63f50 | 2005-05-31 18:52:43 +0200 | [diff] [blame] | 25 | static int force_remove; |
Junio C Hamano | caf4f58 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 26 | static int verbose; |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 27 | static int mark_valid_only; |
Junio C Hamano | 5f73076 | 2006-02-08 21:15:24 -0800 | [diff] [blame] | 28 | #define MARK_VALID 1 |
| 29 | #define UNMARK_VALID 2 |
| 30 | |
Junio C Hamano | caf4f58 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 31 | static void report(const char *fmt, ...) |
| 32 | { |
| 33 | va_list vp; |
| 34 | |
| 35 | if (!verbose) |
| 36 | return; |
| 37 | |
| 38 | va_start(vp, fmt); |
| 39 | vprintf(fmt, vp); |
| 40 | putchar('\n'); |
| 41 | va_end(vp); |
| 42 | } |
| 43 | |
Junio C Hamano | 5f73076 | 2006-02-08 21:15:24 -0800 | [diff] [blame] | 44 | static int mark_valid(const char *path) |
| 45 | { |
| 46 | int namelen = strlen(path); |
| 47 | int pos = cache_name_pos(path, namelen); |
| 48 | if (0 <= pos) { |
| 49 | switch (mark_valid_only) { |
| 50 | case MARK_VALID: |
| 51 | active_cache[pos]->ce_flags |= htons(CE_VALID); |
| 52 | break; |
| 53 | case UNMARK_VALID: |
| 54 | active_cache[pos]->ce_flags &= ~htons(CE_VALID); |
| 55 | break; |
| 56 | } |
Junio C Hamano | a6e5642 | 2006-04-24 00:23:54 -0700 | [diff] [blame] | 57 | cache_tree_invalidate_path(active_cache_tree, path); |
Junio C Hamano | 5f73076 | 2006-02-08 21:15:24 -0800 | [diff] [blame] | 58 | active_cache_changed = 1; |
| 59 | return 0; |
| 60 | } |
| 61 | return -1; |
| 62 | } |
| 63 | |
Linus Torvalds | e011054 | 2007-04-12 12:29:40 -0700 | [diff] [blame] | 64 | static int remove_one_path(const char *path) |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 65 | { |
Linus Torvalds | e011054 | 2007-04-12 12:29:40 -0700 | [diff] [blame] | 66 | if (!allow_remove) |
| 67 | return error("%s: does not exist and --remove not passed", path); |
| 68 | if (remove_file_from_cache(path)) |
| 69 | return error("%s: cannot remove from the index", path); |
| 70 | return 0; |
| 71 | } |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 72 | |
Linus Torvalds | e011054 | 2007-04-12 12:29:40 -0700 | [diff] [blame] | 73 | /* |
| 74 | * Handle a path that couldn't be lstat'ed. It's either: |
| 75 | * - missing file (ENOENT or ENOTDIR). That's ok if we're |
| 76 | * supposed to be removing it and the removal actually |
| 77 | * succeeds. |
| 78 | * - permission error. That's never ok. |
| 79 | */ |
| 80 | static int process_lstat_error(const char *path, int err) |
| 81 | { |
| 82 | if (err == ENOENT || err == ENOTDIR) |
| 83 | return remove_one_path(path); |
| 84 | return error("lstat(\"%s\"): %s", path, strerror(errno)); |
| 85 | } |
| 86 | |
| 87 | static int add_one_path(struct cache_entry *old, const char *path, int len, struct stat *st) |
| 88 | { |
Linus Torvalds | 2263147 | 2007-08-10 09:51:58 -0700 | [diff] [blame] | 89 | int option, size; |
| 90 | struct cache_entry *ce; |
Linus Torvalds | e011054 | 2007-04-12 12:29:40 -0700 | [diff] [blame] | 91 | |
Linus Torvalds | 2263147 | 2007-08-10 09:51:58 -0700 | [diff] [blame] | 92 | /* Was the old index entry already up-to-date? */ |
| 93 | if (old && !ce_stage(old) && !ce_match_stat(old, st, 0)) |
| 94 | return 0; |
| 95 | |
| 96 | size = cache_entry_size(len); |
| 97 | ce = xcalloc(1, size); |
Linus Torvalds | e011054 | 2007-04-12 12:29:40 -0700 | [diff] [blame] | 98 | memcpy(ce->name, path, len); |
| 99 | ce->ce_flags = htons(len); |
| 100 | fill_stat_cache_info(ce, st); |
| 101 | ce->ce_mode = ce_mode_from_stat(old, st->st_mode); |
| 102 | |
| 103 | if (index_path(ce->sha1, path, st, !info_only)) |
| 104 | return -1; |
| 105 | option = allow_add ? ADD_CACHE_OK_TO_ADD : 0; |
| 106 | option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0; |
| 107 | if (add_cache_entry(ce, option)) |
| 108 | return error("%s: cannot add to the index - missing --add option?", path); |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Handle a path that was a directory. Four cases: |
| 114 | * |
| 115 | * - it's already a gitlink in the index, and we keep it that |
| 116 | * way, and update it if we can (if we cannot find the HEAD, |
| 117 | * we're going to keep it unchanged in the index!) |
| 118 | * |
| 119 | * - it's a *file* in the index, in which case it should be |
| 120 | * removed as a file if removal is allowed, since it doesn't |
| 121 | * exist as such any more. If removal isn't allowed, it's |
| 122 | * an error. |
| 123 | * |
| 124 | * (NOTE! This is old and arguably fairly strange behaviour. |
| 125 | * We might want to make this an error unconditionally, and |
| 126 | * use "--force-remove" if you actually want to force removal). |
| 127 | * |
| 128 | * - it used to exist as a subdirectory (ie multiple files with |
| 129 | * this particular prefix) in the index, in which case it's wrong |
| 130 | * to try to update it as a directory. |
| 131 | * |
| 132 | * - it doesn't exist at all in the index, but it is a valid |
| 133 | * git directory, and it should be *added* as a gitlink. |
| 134 | */ |
| 135 | static int process_directory(const char *path, int len, struct stat *st) |
| 136 | { |
| 137 | unsigned char sha1[20]; |
| 138 | int pos = cache_name_pos(path, len); |
| 139 | |
| 140 | /* Exact match: file or existing gitlink */ |
| 141 | if (pos >= 0) { |
| 142 | struct cache_entry *ce = active_cache[pos]; |
Martin Waitz | 302b928 | 2007-05-21 22:08:28 +0200 | [diff] [blame] | 143 | if (S_ISGITLINK(ntohl(ce->ce_mode))) { |
Linus Torvalds | e011054 | 2007-04-12 12:29:40 -0700 | [diff] [blame] | 144 | |
| 145 | /* Do nothing to the index if there is no HEAD! */ |
| 146 | if (resolve_gitlink_ref(path, "HEAD", sha1) < 0) |
| 147 | return 0; |
| 148 | |
| 149 | return add_one_path(ce, path, len, st); |
| 150 | } |
| 151 | /* Should this be an unconditional error? */ |
| 152 | return remove_one_path(path); |
| 153 | } |
| 154 | |
| 155 | /* Inexact match: is there perhaps a subdirectory match? */ |
| 156 | pos = -pos-1; |
| 157 | while (pos < active_nr) { |
| 158 | struct cache_entry *ce = active_cache[pos++]; |
| 159 | |
| 160 | if (strncmp(ce->name, path, len)) |
| 161 | break; |
| 162 | if (ce->name[len] > '/') |
| 163 | break; |
| 164 | if (ce->name[len] < '/') |
| 165 | continue; |
| 166 | |
| 167 | /* Subdirectory match - error out */ |
| 168 | return error("%s: is a directory - add individual files instead", path); |
| 169 | } |
| 170 | |
| 171 | /* No match - should we add it as a gitlink? */ |
| 172 | if (!resolve_gitlink_ref(path, "HEAD", sha1)) |
| 173 | return add_one_path(NULL, path, len, st); |
| 174 | |
| 175 | /* Error out. */ |
| 176 | return error("%s: is a directory - add files inside instead", path); |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * Process a regular file |
| 181 | */ |
| 182 | static int process_file(const char *path, int len, struct stat *st) |
| 183 | { |
| 184 | int pos = cache_name_pos(path, len); |
| 185 | struct cache_entry *ce = pos < 0 ? NULL : active_cache[pos]; |
| 186 | |
Martin Waitz | 302b928 | 2007-05-21 22:08:28 +0200 | [diff] [blame] | 187 | if (ce && S_ISGITLINK(ntohl(ce->ce_mode))) |
Linus Torvalds | e011054 | 2007-04-12 12:29:40 -0700 | [diff] [blame] | 188 | return error("%s is already a gitlink, not replacing", path); |
| 189 | |
| 190 | return add_one_path(ce, path, len, st); |
| 191 | } |
| 192 | |
| 193 | static int process_path(const char *path) |
| 194 | { |
| 195 | int len; |
| 196 | struct stat st; |
Junio C Hamano | a6e5642 | 2006-04-24 00:23:54 -0700 | [diff] [blame] | 197 | |
Linus Torvalds | e011054 | 2007-04-12 12:29:40 -0700 | [diff] [blame] | 198 | /* |
| 199 | * First things first: get the stat information, to decide |
| 200 | * what to do about the pathname! |
| 201 | */ |
| 202 | if (lstat(path, &st) < 0) |
| 203 | return process_lstat_error(path, errno); |
Junio C Hamano | 3e09cdf | 2005-10-11 18:45:33 -0700 | [diff] [blame] | 204 | |
Linus Torvalds | e011054 | 2007-04-12 12:29:40 -0700 | [diff] [blame] | 205 | len = strlen(path); |
| 206 | if (S_ISDIR(st.st_mode)) |
| 207 | return process_directory(path, len, &st); |
Junio C Hamano | 3e09cdf | 2005-10-11 18:45:33 -0700 | [diff] [blame] | 208 | |
Linus Torvalds | e011054 | 2007-04-12 12:29:40 -0700 | [diff] [blame] | 209 | return process_file(path, len, &st); |
Linus Torvalds | 121481a | 2005-04-10 11:32:54 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Junio C Hamano | d23748a | 2005-12-07 01:45:38 -0800 | [diff] [blame] | 212 | static int add_cacheinfo(unsigned int mode, const unsigned char *sha1, |
| 213 | const char *path, int stage) |
Linus Torvalds | 9945d98 | 2005-04-15 11:08:33 -0700 | [diff] [blame] | 214 | { |
Junio C Hamano | 192268c | 2005-05-07 21:55:21 -0700 | [diff] [blame] | 215 | int size, len, option; |
Linus Torvalds | 9945d98 | 2005-04-15 11:08:33 -0700 | [diff] [blame] | 216 | struct cache_entry *ce; |
| 217 | |
Junio C Hamano | d23748a | 2005-12-07 01:45:38 -0800 | [diff] [blame] | 218 | if (!verify_path(path)) |
Linus Torvalds | 9945d98 | 2005-04-15 11:08:33 -0700 | [diff] [blame] | 219 | return -1; |
Linus Torvalds | 9945d98 | 2005-04-15 11:08:33 -0700 | [diff] [blame] | 220 | |
Junio C Hamano | d23748a | 2005-12-07 01:45:38 -0800 | [diff] [blame] | 221 | len = strlen(path); |
Linus Torvalds | 9945d98 | 2005-04-15 11:08:33 -0700 | [diff] [blame] | 222 | size = cache_entry_size(len); |
Peter Eriksen | 90321c1 | 2006-04-03 19:30:46 +0100 | [diff] [blame] | 223 | ce = xcalloc(1, size); |
Linus Torvalds | 9945d98 | 2005-04-15 11:08:33 -0700 | [diff] [blame] | 224 | |
Shawn Pearce | e702496 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 225 | hashcpy(ce->sha1, sha1); |
Junio C Hamano | d23748a | 2005-12-07 01:45:38 -0800 | [diff] [blame] | 226 | memcpy(ce->name, path, len); |
| 227 | ce->ce_flags = create_ce_flags(len, stage); |
Linus Torvalds | e447947 | 2005-04-16 22:26:31 -0700 | [diff] [blame] | 228 | ce->ce_mode = create_ce_mode(mode); |
Junio C Hamano | 5f73076 | 2006-02-08 21:15:24 -0800 | [diff] [blame] | 229 | if (assume_unchanged) |
| 230 | ce->ce_flags |= htons(CE_VALID); |
Junio C Hamano | 192268c | 2005-05-07 21:55:21 -0700 | [diff] [blame] | 231 | option = allow_add ? ADD_CACHE_OK_TO_ADD : 0; |
| 232 | option |= allow_replace ? ADD_CACHE_OK_TO_REPLACE : 0; |
Junio C Hamano | caf4f58 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 233 | if (add_cache_entry(ce, option)) |
| 234 | return error("%s: cannot add to the index - missing --add option?", |
Junio C Hamano | d23748a | 2005-12-07 01:45:38 -0800 | [diff] [blame] | 235 | path); |
| 236 | report("add '%s'", path); |
Junio C Hamano | caf4f58 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 237 | return 0; |
Linus Torvalds | 9945d98 | 2005-04-15 11:08:33 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Alex Riesen | 227bdb1 | 2006-04-23 09:01:29 +0200 | [diff] [blame] | 240 | static void chmod_path(int flip, const char *path) |
Junio C Hamano | 3e09cdf | 2005-10-11 18:45:33 -0700 | [diff] [blame] | 241 | { |
| 242 | int pos; |
| 243 | struct cache_entry *ce; |
| 244 | unsigned int mode; |
Linus Torvalds | f2a1934 | 2005-04-26 11:55:42 -0700 | [diff] [blame] | 245 | |
Junio C Hamano | 3e09cdf | 2005-10-11 18:45:33 -0700 | [diff] [blame] | 246 | pos = cache_name_pos(path, strlen(path)); |
| 247 | if (pos < 0) |
Alex Riesen | 227bdb1 | 2006-04-23 09:01:29 +0200 | [diff] [blame] | 248 | goto fail; |
Junio C Hamano | 3e09cdf | 2005-10-11 18:45:33 -0700 | [diff] [blame] | 249 | ce = active_cache[pos]; |
| 250 | mode = ntohl(ce->ce_mode); |
| 251 | if (!S_ISREG(mode)) |
Alex Riesen | 227bdb1 | 2006-04-23 09:01:29 +0200 | [diff] [blame] | 252 | goto fail; |
Junio C Hamano | 3e09cdf | 2005-10-11 18:45:33 -0700 | [diff] [blame] | 253 | switch (flip) { |
| 254 | case '+': |
| 255 | ce->ce_mode |= htonl(0111); break; |
| 256 | case '-': |
| 257 | ce->ce_mode &= htonl(~0111); break; |
| 258 | default: |
Alex Riesen | 227bdb1 | 2006-04-23 09:01:29 +0200 | [diff] [blame] | 259 | goto fail; |
Junio C Hamano | 3e09cdf | 2005-10-11 18:45:33 -0700 | [diff] [blame] | 260 | } |
Junio C Hamano | a6e5642 | 2006-04-24 00:23:54 -0700 | [diff] [blame] | 261 | cache_tree_invalidate_path(active_cache_tree, path); |
Junio C Hamano | 3e09cdf | 2005-10-11 18:45:33 -0700 | [diff] [blame] | 262 | active_cache_changed = 1; |
Alex Riesen | 227bdb1 | 2006-04-23 09:01:29 +0200 | [diff] [blame] | 263 | report("chmod %cx '%s'", flip, path); |
| 264 | return; |
| 265 | fail: |
| 266 | die("git-update-index: cannot chmod %cx '%s'", flip, path); |
Junio C Hamano | 3e09cdf | 2005-10-11 18:45:33 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Junio C Hamano | ee1bec3 | 2005-09-26 18:13:08 -0700 | [diff] [blame] | 269 | static void update_one(const char *path, const char *prefix, int prefix_length) |
| 270 | { |
| 271 | const char *p = prefix_path(prefix, prefix_length, path); |
| 272 | if (!verify_path(p)) { |
| 273 | fprintf(stderr, "Ignoring path %s\n", path); |
Junio C Hamano | fb69a76 | 2006-05-05 22:53:56 -0700 | [diff] [blame] | 274 | goto free_return; |
Junio C Hamano | ee1bec3 | 2005-09-26 18:13:08 -0700 | [diff] [blame] | 275 | } |
Junio C Hamano | 5f73076 | 2006-02-08 21:15:24 -0800 | [diff] [blame] | 276 | if (mark_valid_only) { |
| 277 | if (mark_valid(p)) |
| 278 | die("Unable to mark file %s", path); |
Junio C Hamano | fb69a76 | 2006-05-05 22:53:56 -0700 | [diff] [blame] | 279 | goto free_return; |
Junio C Hamano | 5f73076 | 2006-02-08 21:15:24 -0800 | [diff] [blame] | 280 | } |
| 281 | |
Junio C Hamano | ee1bec3 | 2005-09-26 18:13:08 -0700 | [diff] [blame] | 282 | if (force_remove) { |
| 283 | if (remove_file_from_cache(p)) |
| 284 | die("git-update-index: unable to remove %s", path); |
Junio C Hamano | caf4f58 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 285 | report("remove '%s'", path); |
Junio C Hamano | fb69a76 | 2006-05-05 22:53:56 -0700 | [diff] [blame] | 286 | goto free_return; |
Junio C Hamano | ee1bec3 | 2005-09-26 18:13:08 -0700 | [diff] [blame] | 287 | } |
Linus Torvalds | e011054 | 2007-04-12 12:29:40 -0700 | [diff] [blame] | 288 | if (process_path(p)) |
| 289 | die("Unable to process path %s", path); |
Junio C Hamano | caf4f58 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 290 | report("add '%s'", path); |
Junio C Hamano | fb69a76 | 2006-05-05 22:53:56 -0700 | [diff] [blame] | 291 | free_return: |
Johannes Schindelin | 0cc9e70 | 2006-05-07 00:02:53 +0200 | [diff] [blame] | 292 | if (p < path || p > path + strlen(path)) |
Junio C Hamano | fb69a76 | 2006-05-05 22:53:56 -0700 | [diff] [blame] | 293 | free((char*)p); |
Junio C Hamano | ee1bec3 | 2005-09-26 18:13:08 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Junio C Hamano | d4dbf36d | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 296 | static void read_index_info(int line_termination) |
| 297 | { |
| 298 | struct strbuf buf; |
| 299 | strbuf_init(&buf); |
| 300 | while (1) { |
Junio C Hamano | 9c20a47 | 2005-11-21 21:46:57 -0800 | [diff] [blame] | 301 | char *ptr, *tab; |
Junio C Hamano | 973d6a2 | 2005-10-16 00:39:07 -0700 | [diff] [blame] | 302 | char *path_name; |
Junio C Hamano | d4dbf36d | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 303 | unsigned char sha1[20]; |
| 304 | unsigned int mode; |
Jim Meyering | 6aead43 | 2007-04-10 01:01:44 +0200 | [diff] [blame] | 305 | unsigned long ul; |
Junio C Hamano | d23748a | 2005-12-07 01:45:38 -0800 | [diff] [blame] | 306 | int stage; |
Junio C Hamano | d4dbf36d | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 307 | |
Junio C Hamano | d23748a | 2005-12-07 01:45:38 -0800 | [diff] [blame] | 308 | /* This reads lines formatted in one of three formats: |
| 309 | * |
| 310 | * (1) mode SP sha1 TAB path |
| 311 | * The first format is what "git-apply --index-info" |
| 312 | * reports, and used to reconstruct a partial tree |
| 313 | * that is used for phony merge base tree when falling |
| 314 | * back on 3-way merge. |
| 315 | * |
| 316 | * (2) mode SP type SP sha1 TAB path |
| 317 | * The second format is to stuff git-ls-tree output |
| 318 | * into the index file. |
Lukas Sandström | fefe81c | 2006-06-13 22:21:57 +0200 | [diff] [blame] | 319 | * |
Junio C Hamano | d23748a | 2005-12-07 01:45:38 -0800 | [diff] [blame] | 320 | * (3) mode SP sha1 SP stage TAB path |
| 321 | * This format is to put higher order stages into the |
| 322 | * index file and matches git-ls-files --stage output. |
| 323 | */ |
Junio C Hamano | d4dbf36d | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 324 | read_line(&buf, stdin, line_termination); |
| 325 | if (buf.eof) |
| 326 | break; |
| 327 | |
Jim Meyering | 6aead43 | 2007-04-10 01:01:44 +0200 | [diff] [blame] | 328 | errno = 0; |
| 329 | ul = strtoul(buf.buf, &ptr, 8); |
| 330 | if (ptr == buf.buf || *ptr != ' ' |
| 331 | || errno || (unsigned int) ul != ul) |
Junio C Hamano | d4dbf36d | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 332 | goto bad_line; |
Jim Meyering | 6aead43 | 2007-04-10 01:01:44 +0200 | [diff] [blame] | 333 | mode = ul; |
Junio C Hamano | d4dbf36d | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 334 | |
Junio C Hamano | 9c20a47 | 2005-11-21 21:46:57 -0800 | [diff] [blame] | 335 | tab = strchr(ptr, '\t'); |
| 336 | if (!tab || tab - ptr < 41) |
| 337 | goto bad_line; |
Junio C Hamano | d23748a | 2005-12-07 01:45:38 -0800 | [diff] [blame] | 338 | |
Junio C Hamano | 2d49711 | 2006-01-31 18:03:37 -0800 | [diff] [blame] | 339 | if (tab[-2] == ' ' && '0' <= tab[-1] && tab[-1] <= '3') { |
Junio C Hamano | d23748a | 2005-12-07 01:45:38 -0800 | [diff] [blame] | 340 | stage = tab[-1] - '0'; |
| 341 | ptr = tab + 1; /* point at the head of path */ |
| 342 | tab = tab - 2; /* point at tail of sha1 */ |
| 343 | } |
| 344 | else { |
| 345 | stage = 0; |
| 346 | ptr = tab + 1; /* point at the head of path */ |
| 347 | } |
| 348 | |
Junio C Hamano | 9c20a47 | 2005-11-21 21:46:57 -0800 | [diff] [blame] | 349 | if (get_sha1_hex(tab - 40, sha1) || tab[-41] != ' ') |
| 350 | goto bad_line; |
Junio C Hamano | 973d6a2 | 2005-10-16 00:39:07 -0700 | [diff] [blame] | 351 | |
| 352 | if (line_termination && ptr[0] == '"') |
| 353 | path_name = unquote_c_style(ptr, NULL); |
| 354 | else |
| 355 | path_name = ptr; |
| 356 | |
| 357 | if (!verify_path(path_name)) { |
| 358 | fprintf(stderr, "Ignoring path %s\n", path_name); |
| 359 | if (path_name != ptr) |
| 360 | free(path_name); |
Junio C Hamano | d4dbf36d | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 361 | continue; |
| 362 | } |
| 363 | |
| 364 | if (!mode) { |
| 365 | /* mode == 0 means there is no such path -- remove */ |
Junio C Hamano | 973d6a2 | 2005-10-16 00:39:07 -0700 | [diff] [blame] | 366 | if (remove_file_from_cache(path_name)) |
Junio C Hamano | d4dbf36d | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 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; |
Junio C Hamano | d23748a | 2005-12-07 01:45:38 -0800 | [diff] [blame] | 376 | if (add_cacheinfo(mode, sha1, path_name, stage)) |
Junio C Hamano | d4dbf36d | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 377 | die("git-update-index: unable to update %s", |
Junio C Hamano | 973d6a2 | 2005-10-16 00:39:07 -0700 | [diff] [blame] | 378 | path_name); |
Junio C Hamano | d4dbf36d | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 379 | } |
Junio C Hamano | 973d6a2 | 2005-10-16 00:39:07 -0700 | [diff] [blame] | 380 | if (path_name != ptr) |
| 381 | free(path_name); |
Junio C Hamano | d4dbf36d | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 382 | continue; |
| 383 | |
| 384 | bad_line: |
| 385 | die("malformed index info %s", buf.buf); |
| 386 | } |
| 387 | } |
| 388 | |
Petr Baudis | f6ab5bb | 2005-10-25 17:26:25 +0200 | [diff] [blame] | 389 | static const char update_index_usage[] = |
Junio C Hamano | 7099c9c | 2006-08-23 21:24:47 -0700 | [diff] [blame] | 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>..."; |
Petr Baudis | f6ab5bb | 2005-10-25 17:26:25 +0200 | [diff] [blame] | 391 | |
Junio C Hamano | 2bd452d | 2006-04-19 23:52:05 -0700 | [diff] [blame] | 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)) { |
Junio C Hamano | 83e77a2 | 2006-05-05 17:40:47 -0700 | [diff] [blame] | 405 | if (which) |
| 406 | error("%s: not in %s branch.", path, which); |
Junio C Hamano | 2bd452d | 2006-04-19 23:52:05 -0700 | [diff] [blame] | 407 | return NULL; |
| 408 | } |
| 409 | if (mode == S_IFDIR) { |
Junio C Hamano | 83e77a2 | 2006-05-05 17:40:47 -0700 | [diff] [blame] | 410 | if (which) |
| 411 | error("%s: not a blob in %s branch.", path, which); |
Junio C Hamano | 2bd452d | 2006-04-19 23:52:05 -0700 | [diff] [blame] | 412 | return NULL; |
| 413 | } |
| 414 | size = cache_entry_size(namelen); |
| 415 | ce = xcalloc(1, size); |
| 416 | |
Shawn Pearce | e702496 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 417 | hashcpy(ce->sha1, sha1); |
Junio C Hamano | 2bd452d | 2006-04-19 23:52:05 -0700 | [diff] [blame] | 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 | } |
David Rientjes | a89fccd | 2006-08-17 11:54:57 -0700 | [diff] [blame] | 462 | if (!hashcmp(ce_2->sha1, ce_3->sha1) && |
Junio C Hamano | 2bd452d | 2006-04-19 23:52:05 -0700 | [diff] [blame] | 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 | { |
Johannes Schindelin | 913c983 | 2006-09-21 23:29:59 +0200 | [diff] [blame] | 487 | if (read_ref("HEAD", head_sha1)) |
Junio C Hamano | 2bd452d | 2006-04-19 23:52:05 -0700 | [diff] [blame] | 488 | die("No HEAD -- no initial commit yet?\n"); |
Johannes Schindelin | 913c983 | 2006-09-21 23:29:59 +0200 | [diff] [blame] | 489 | if (read_ref("MERGE_HEAD", merge_head_sha1)) { |
Junio C Hamano | 2bd452d | 2006-04-19 23:52:05 -0700 | [diff] [blame] | 490 | fprintf(stderr, "Not in the middle of a merge.\n"); |
| 491 | exit(0); |
| 492 | } |
| 493 | } |
| 494 | |
Junio C Hamano | 09895c1 | 2006-05-05 17:50:06 -0700 | [diff] [blame] | 495 | static int do_unresolve(int ac, const char **av, |
| 496 | const char *prefix, int prefix_length) |
Junio C Hamano | 2bd452d | 2006-04-19 23:52:05 -0700 | [diff] [blame] | 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]; |
Junio C Hamano | 09895c1 | 2006-05-05 17:50:06 -0700 | [diff] [blame] | 508 | const char *p = prefix_path(prefix, prefix_length, arg); |
| 509 | err |= unresolve_one(p); |
Johannes Schindelin | 0cc9e70 | 2006-05-07 00:02:53 +0200 | [diff] [blame] | 510 | if (p < arg || p > arg + strlen(arg)) |
Junio C Hamano | 09895c1 | 2006-05-05 17:50:06 -0700 | [diff] [blame] | 511 | free((char*)p); |
Junio C Hamano | 2bd452d | 2006-04-19 23:52:05 -0700 | [diff] [blame] | 512 | } |
| 513 | return err; |
| 514 | } |
| 515 | |
Junio C Hamano | 83e77a2 | 2006-05-05 17:40:47 -0700 | [diff] [blame] | 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; |
Johannes Schindelin | 0cc9e70 | 2006-05-07 00:02:53 +0200 | [diff] [blame] | 524 | const char **pathspec = get_pathspec(prefix, av + 1); |
Junio C Hamano | 83e77a2 | 2006-05-05 17:40:47 -0700 | [diff] [blame] | 525 | |
Johannes Schindelin | 913c983 | 2006-09-21 23:29:59 +0200 | [diff] [blame] | 526 | if (read_ref("HEAD", head_sha1)) |
Junio C Hamano | 83e77a2 | 2006-05-05 17:40:47 -0700 | [diff] [blame] | 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; |
Junio C Hamano | 22293b9 | 2006-05-05 23:09:05 -0700 | [diff] [blame] | 536 | |
| 537 | if (ce_stage(ce) || !ce_path_match(ce, pathspec)) |
Junio C Hamano | 83e77a2 | 2006-05-05 17:40:47 -0700 | [diff] [blame] | 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 && |
David Rientjes | a89fccd | 2006-08-17 11:54:57 -0700 | [diff] [blame] | 543 | !hashcmp(ce->sha1, old->sha1)) { |
Junio C Hamano | 83e77a2 | 2006-05-05 17:40:47 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 559 | int cmd_update_index(int argc, const char **argv, const char *prefix) |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 560 | { |
Junio C Hamano | ee1bec3 | 2005-09-26 18:13:08 -0700 | [diff] [blame] | 561 | int i, newfd, entries, has_errors = 0, line_termination = '\n'; |
Linus Torvalds | 121481a | 2005-04-10 11:32:54 -0700 | [diff] [blame] | 562 | int allow_options = 1; |
Junio C Hamano | ee1bec3 | 2005-09-26 18:13:08 -0700 | [diff] [blame] | 563 | int read_from_stdin = 0; |
Junio C Hamano | ee1bec3 | 2005-09-26 18:13:08 -0700 | [diff] [blame] | 564 | int prefix_length = prefix ? strlen(prefix) : 0; |
Alex Riesen | 227bdb1 | 2006-04-23 09:01:29 +0200 | [diff] [blame] | 565 | char set_executable_bit = 0; |
Linus Torvalds | 405e5b2 | 2006-05-19 09:56:35 -0700 | [diff] [blame] | 566 | unsigned int refresh_flags = 0; |
Junio C Hamano | 7b802b8 | 2007-02-22 00:30:45 -0800 | [diff] [blame] | 567 | int lock_error = 0; |
Lukas Sandström | fefe81c | 2006-06-13 22:21:57 +0200 | [diff] [blame] | 568 | struct lock_file *lock_file; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 569 | |
Junio C Hamano | 3e09cdf | 2005-10-11 18:45:33 -0700 | [diff] [blame] | 570 | git_config(git_default_config); |
| 571 | |
Lukas Sandström | fefe81c | 2006-06-13 22:21:57 +0200 | [diff] [blame] | 572 | /* We can't free this memory, it becomes part of a linked list parsed atexit() */ |
Johannes Schindelin | 928e47e | 2006-06-20 00:55:20 +0200 | [diff] [blame] | 573 | lock_file = xcalloc(1, sizeof(struct lock_file)); |
Lukas Sandström | fefe81c | 2006-06-13 22:21:57 +0200 | [diff] [blame] | 574 | |
Junio C Hamano | 30ca07a | 2007-03-31 23:09:02 -0700 | [diff] [blame] | 575 | newfd = hold_locked_index(lock_file, 0); |
Junio C Hamano | 7b802b8 | 2007-02-22 00:30:45 -0800 | [diff] [blame] | 576 | if (newfd < 0) |
| 577 | lock_error = errno; |
Linus Torvalds | 9614b8d | 2005-04-11 15:39:26 -0700 | [diff] [blame] | 578 | |
Linus Torvalds | 9614b8d | 2005-04-11 15:39:26 -0700 | [diff] [blame] | 579 | entries = read_cache(); |
| 580 | if (entries < 0) |
Petr Baudis | 2de381f | 2005-04-13 02:28:48 -0700 | [diff] [blame] | 581 | die("cache corrupted"); |
Linus Torvalds | 9614b8d | 2005-04-11 15:39:26 -0700 | [diff] [blame] | 582 | |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 583 | for (i = 1 ; i < argc; i++) { |
Junio C Hamano | 6b5ee13 | 2005-09-21 00:00:47 -0700 | [diff] [blame] | 584 | const char *path = argv[i]; |
Alex Riesen | 9ebe6cf | 2007-01-31 14:34:17 +0100 | [diff] [blame] | 585 | const char *p; |
Linus Torvalds | 121481a | 2005-04-10 11:32:54 -0700 | [diff] [blame] | 586 | |
| 587 | if (allow_options && *path == '-') { |
| 588 | if (!strcmp(path, "--")) { |
| 589 | allow_options = 0; |
| 590 | continue; |
| 591 | } |
Linus Torvalds | 0ed3715 | 2005-06-20 21:18:54 -0700 | [diff] [blame] | 592 | if (!strcmp(path, "-q")) { |
Linus Torvalds | 405e5b2 | 2006-05-19 09:56:35 -0700 | [diff] [blame] | 593 | refresh_flags |= REFRESH_QUIET; |
Linus Torvalds | 0ed3715 | 2005-06-20 21:18:54 -0700 | [diff] [blame] | 594 | continue; |
| 595 | } |
Linus Torvalds | 121481a | 2005-04-10 11:32:54 -0700 | [diff] [blame] | 596 | if (!strcmp(path, "--add")) { |
| 597 | allow_add = 1; |
| 598 | continue; |
| 599 | } |
Junio C Hamano | 192268c | 2005-05-07 21:55:21 -0700 | [diff] [blame] | 600 | if (!strcmp(path, "--replace")) { |
| 601 | allow_replace = 1; |
| 602 | continue; |
| 603 | } |
Linus Torvalds | 121481a | 2005-04-10 11:32:54 -0700 | [diff] [blame] | 604 | if (!strcmp(path, "--remove")) { |
| 605 | allow_remove = 1; |
| 606 | continue; |
| 607 | } |
Linus Torvalds | 5d1a5c0 | 2005-10-01 13:24:27 -0700 | [diff] [blame] | 608 | if (!strcmp(path, "--unmerged")) { |
Linus Torvalds | 405e5b2 | 2006-05-19 09:56:35 -0700 | [diff] [blame] | 609 | refresh_flags |= REFRESH_UNMERGED; |
Linus Torvalds | 5d1a5c0 | 2005-10-01 13:24:27 -0700 | [diff] [blame] | 610 | continue; |
| 611 | } |
Linus Torvalds | 121481a | 2005-04-10 11:32:54 -0700 | [diff] [blame] | 612 | if (!strcmp(path, "--refresh")) { |
Linus Torvalds | 405e5b2 | 2006-05-19 09:56:35 -0700 | [diff] [blame] | 613 | has_errors |= refresh_cache(refresh_flags); |
Junio C Hamano | 5f73076 | 2006-02-08 21:15:24 -0800 | [diff] [blame] | 614 | continue; |
| 615 | } |
| 616 | if (!strcmp(path, "--really-refresh")) { |
Linus Torvalds | 405e5b2 | 2006-05-19 09:56:35 -0700 | [diff] [blame] | 617 | has_errors |= refresh_cache(REFRESH_REALLY | refresh_flags); |
Linus Torvalds | 121481a | 2005-04-10 11:32:54 -0700 | [diff] [blame] | 618 | continue; |
| 619 | } |
Linus Torvalds | 9945d98 | 2005-04-15 11:08:33 -0700 | [diff] [blame] | 620 | if (!strcmp(path, "--cacheinfo")) { |
Junio C Hamano | d23748a | 2005-12-07 01:45:38 -0800 | [diff] [blame] | 621 | unsigned char sha1[20]; |
| 622 | unsigned int mode; |
| 623 | |
Junio C Hamano | b3f94c4 | 2005-05-08 15:31:33 -0700 | [diff] [blame] | 624 | if (i+3 >= argc) |
Junio C Hamano | 215a7ad | 2005-09-07 17:26:23 -0700 | [diff] [blame] | 625 | die("git-update-index: --cacheinfo <mode> <sha1> <path>"); |
Junio C Hamano | d23748a | 2005-12-07 01:45:38 -0800 | [diff] [blame] | 626 | |
Andy Whitcroft | 6e6db39 | 2007-04-19 03:08:15 +0100 | [diff] [blame] | 627 | if (strtoul_ui(argv[i+1], 8, &mode) || |
Junio C Hamano | d23748a | 2005-12-07 01:45:38 -0800 | [diff] [blame] | 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]); |
Linus Torvalds | 9945d98 | 2005-04-15 11:08:33 -0700 | [diff] [blame] | 632 | i += 3; |
| 633 | continue; |
| 634 | } |
Junio C Hamano | 3e09cdf | 2005-10-11 18:45:33 -0700 | [diff] [blame] | 635 | if (!strcmp(path, "--chmod=-x") || |
| 636 | !strcmp(path, "--chmod=+x")) { |
| 637 | if (argc <= i+1) |
| 638 | die("git-update-index: %s <path>", path); |
Alex Riesen | 227bdb1 | 2006-04-23 09:01:29 +0200 | [diff] [blame] | 639 | set_executable_bit = path[8]; |
Junio C Hamano | 3e09cdf | 2005-10-11 18:45:33 -0700 | [diff] [blame] | 640 | continue; |
| 641 | } |
Junio C Hamano | 5f73076 | 2006-02-08 21:15:24 -0800 | [diff] [blame] | 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 | } |
Bryan Larsen | df6e151 | 2005-07-08 16:52:12 -0700 | [diff] [blame] | 650 | if (!strcmp(path, "--info-only")) { |
| 651 | info_only = 1; |
| 652 | continue; |
| 653 | } |
Junio C Hamano | 0ff5bf7 | 2005-05-01 23:50:51 -0700 | [diff] [blame] | 654 | if (!strcmp(path, "--force-remove")) { |
Petr Baudis | 9b63f50 | 2005-05-31 18:52:43 +0200 | [diff] [blame] | 655 | force_remove = 1; |
Junio C Hamano | 0ff5bf7 | 2005-05-01 23:50:51 -0700 | [diff] [blame] | 656 | continue; |
| 657 | } |
Junio C Hamano | ee1bec3 | 2005-09-26 18:13:08 -0700 | [diff] [blame] | 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 | } |
Junio C Hamano | d4dbf36d | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 668 | if (!strcmp(path, "--index-info")) { |
Shawn Pearce | a41c175 | 2006-03-02 12:21:33 -0500 | [diff] [blame] | 669 | if (i != argc - 1) |
| 670 | die("--index-info must be at the end"); |
Junio C Hamano | d4dbf36d | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 671 | allow_add = allow_replace = allow_remove = 1; |
| 672 | read_index_info(line_termination); |
Shawn Pearce | a41c175 | 2006-03-02 12:21:33 -0500 | [diff] [blame] | 673 | break; |
Junio C Hamano | d4dbf36d | 2005-10-07 03:42:00 -0700 | [diff] [blame] | 674 | } |
Junio C Hamano | 2bd452d | 2006-04-19 23:52:05 -0700 | [diff] [blame] | 675 | if (!strcmp(path, "--unresolve")) { |
Junio C Hamano | 09895c1 | 2006-05-05 17:50:06 -0700 | [diff] [blame] | 676 | has_errors = do_unresolve(argc - i, argv + i, |
| 677 | prefix, prefix_length); |
Junio C Hamano | 2bd452d | 2006-04-19 23:52:05 -0700 | [diff] [blame] | 678 | if (has_errors) |
| 679 | active_cache_changed = 0; |
| 680 | goto finish; |
| 681 | } |
Junio C Hamano | 7099c9c | 2006-08-23 21:24:47 -0700 | [diff] [blame] | 682 | if (!strcmp(path, "--again") || !strcmp(path, "-g")) { |
Junio C Hamano | 83e77a2 | 2006-05-05 17:40:47 -0700 | [diff] [blame] | 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 | } |
James Bottomley | c6e007b | 2005-04-24 15:14:16 -0700 | [diff] [blame] | 689 | if (!strcmp(path, "--ignore-missing")) { |
Linus Torvalds | 405e5b2 | 2006-05-19 09:56:35 -0700 | [diff] [blame] | 690 | refresh_flags |= REFRESH_IGNORE_MISSING; |
James Bottomley | c6e007b | 2005-04-24 15:14:16 -0700 | [diff] [blame] | 691 | continue; |
| 692 | } |
Junio C Hamano | caf4f58 | 2005-10-14 21:56:46 -0700 | [diff] [blame] | 693 | if (!strcmp(path, "--verbose")) { |
| 694 | verbose = 1; |
| 695 | continue; |
| 696 | } |
Petr Baudis | f6ab5bb | 2005-10-25 17:26:25 +0200 | [diff] [blame] | 697 | if (!strcmp(path, "-h") || !strcmp(path, "--help")) |
| 698 | usage(update_index_usage); |
Petr Baudis | 2de381f | 2005-04-13 02:28:48 -0700 | [diff] [blame] | 699 | die("unknown option %s", path); |
Linus Torvalds | 121481a | 2005-04-10 11:32:54 -0700 | [diff] [blame] | 700 | } |
Alex Riesen | 9ebe6cf | 2007-01-31 14:34:17 +0100 | [diff] [blame] | 701 | p = prefix_path(prefix, prefix_length, path); |
| 702 | update_one(p, NULL, 0); |
Alex Riesen | 227bdb1 | 2006-04-23 09:01:29 +0200 | [diff] [blame] | 703 | if (set_executable_bit) |
Alex Riesen | 9ebe6cf | 2007-01-31 14:34:17 +0100 | [diff] [blame] | 704 | chmod_path(set_executable_bit, p); |
| 705 | if (p < path || p > path + strlen(path)) |
| 706 | free((char*)p); |
Junio C Hamano | ee1bec3 | 2005-09-26 18:13:08 -0700 | [diff] [blame] | 707 | } |
| 708 | if (read_from_stdin) { |
| 709 | struct strbuf buf; |
| 710 | strbuf_init(&buf); |
| 711 | while (1) { |
Junio C Hamano | a94d994 | 2006-01-11 13:36:45 -0800 | [diff] [blame] | 712 | char *path_name; |
Junio C Hamano | fb69a76 | 2006-05-05 22:53:56 -0700 | [diff] [blame] | 713 | const char *p; |
Junio C Hamano | ee1bec3 | 2005-09-26 18:13:08 -0700 | [diff] [blame] | 714 | read_line(&buf, stdin, line_termination); |
| 715 | if (buf.eof) |
| 716 | break; |
Junio C Hamano | a94d994 | 2006-01-11 13:36:45 -0800 | [diff] [blame] | 717 | if (line_termination && buf.buf[0] == '"') |
| 718 | path_name = unquote_c_style(buf.buf, NULL); |
| 719 | else |
| 720 | path_name = buf.buf; |
Junio C Hamano | fb69a76 | 2006-05-05 22:53:56 -0700 | [diff] [blame] | 721 | p = prefix_path(prefix, prefix_length, path_name); |
| 722 | update_one(p, NULL, 0); |
| 723 | if (set_executable_bit) |
Alex Riesen | 227bdb1 | 2006-04-23 09:01:29 +0200 | [diff] [blame] | 724 | chmod_path(set_executable_bit, p); |
Johannes Schindelin | 0cc9e70 | 2006-05-07 00:02:53 +0200 | [diff] [blame] | 725 | if (p < path_name || p > path_name + strlen(path_name)) |
Junio C Hamano | fb69a76 | 2006-05-05 22:53:56 -0700 | [diff] [blame] | 726 | free((char*) p); |
Junio C Hamano | a94d994 | 2006-01-11 13:36:45 -0800 | [diff] [blame] | 727 | if (path_name != buf.buf) |
| 728 | free(path_name); |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 729 | } |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 730 | } |
Junio C Hamano | 2bd452d | 2006-04-19 23:52:05 -0700 | [diff] [blame] | 731 | |
| 732 | finish: |
Linus Torvalds | 5cd5ace | 2005-10-01 13:39:47 -0700 | [diff] [blame] | 733 | if (active_cache_changed) { |
Junio C Hamano | 7b802b8 | 2007-02-22 00:30:45 -0800 | [diff] [blame] | 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 | } |
Linus Torvalds | 5cd5ace | 2005-10-01 13:39:47 -0700 | [diff] [blame] | 740 | if (write_cache(newfd, active_cache, active_nr) || |
Junio C Hamano | 30ca07a | 2007-03-31 23:09:02 -0700 | [diff] [blame] | 741 | close(newfd) || commit_locked_index(lock_file)) |
Junio C Hamano | 021b6e4 | 2006-06-06 12:51:49 -0700 | [diff] [blame] | 742 | die("Unable to write new index file"); |
Linus Torvalds | 5cd5ace | 2005-10-01 13:39:47 -0700 | [diff] [blame] | 743 | } |
Linus Torvalds | 9614b8d | 2005-04-11 15:39:26 -0700 | [diff] [blame] | 744 | |
Lukas Sandström | fefe81c | 2006-06-13 22:21:57 +0200 | [diff] [blame] | 745 | rollback_lock_file(lock_file); |
| 746 | |
Junio C Hamano | c4b83e6 | 2005-05-05 15:29:06 -0700 | [diff] [blame] | 747 | return has_errors ? 1 : 0; |
Linus Torvalds | e83c516 | 2005-04-07 15:13:13 -0700 | [diff] [blame] | 748 | } |