Git fork
at reftables-rust 350 lines 9.3 kB view raw
1/* 2 * Check-out files from the "current cache directory" 3 * 4 * Copyright (C) 2005 Linus Torvalds 5 * 6 */ 7 8#define DISABLE_SIGN_COMPARE_WARNINGS 9 10#include "builtin.h" 11#include "config.h" 12#include "environment.h" 13#include "gettext.h" 14#include "lockfile.h" 15#include "quote.h" 16#include "cache-tree.h" 17#include "parse-options.h" 18#include "entry.h" 19#include "parallel-checkout.h" 20#include "read-cache-ll.h" 21#include "setup.h" 22#include "sparse-index.h" 23 24#define CHECKOUT_ALL 4 25static int nul_term_line; 26static int checkout_stage; /* default to checkout stage0 */ 27static int ignore_skip_worktree; /* default to 0 */ 28static int to_tempfile = -1; 29static char topath[4][TEMPORARY_FILENAME_LENGTH + 1]; 30 31static struct checkout state = CHECKOUT_INIT; 32 33static void write_tempfile_record(const char *name, const char *prefix) 34{ 35 int i; 36 int have_tempname = 0; 37 38 if (CHECKOUT_ALL == checkout_stage) { 39 for (i = 1; i < 4; i++) 40 if (topath[i][0]) { 41 have_tempname = 1; 42 break; 43 } 44 45 if (have_tempname) { 46 for (i = 1; i < 4; i++) { 47 if (i > 1) 48 putchar(' '); 49 if (topath[i][0]) 50 fputs(topath[i], stdout); 51 else 52 putchar('.'); 53 } 54 } 55 } else if (topath[checkout_stage][0]) { 56 have_tempname = 1; 57 fputs(topath[checkout_stage], stdout); 58 } 59 60 if (have_tempname) { 61 putchar('\t'); 62 write_name_quoted_relative(name, prefix, stdout, 63 nul_term_line ? '\0' : '\n'); 64 } 65 66 for (i = 0; i < 4; i++) { 67 topath[i][0] = 0; 68 } 69} 70 71static int checkout_file(struct index_state *index, const char *name, const char *prefix) 72{ 73 int namelen = strlen(name); 74 int pos = index_name_pos(index, name, namelen); 75 int has_same_name = 0; 76 int is_file = 0; 77 int is_skipped = 1; 78 int did_checkout = 0; 79 int errs = 0; 80 81 if (pos < 0) 82 pos = -pos - 1; 83 84 while (pos < index->cache_nr) { 85 struct cache_entry *ce = index->cache[pos]; 86 if (ce_namelen(ce) != namelen || 87 memcmp(ce->name, name, namelen)) 88 break; 89 has_same_name = 1; 90 pos++; 91 if (S_ISSPARSEDIR(ce->ce_mode)) 92 break; 93 is_file = 1; 94 if (!ignore_skip_worktree && ce_skip_worktree(ce)) 95 break; 96 is_skipped = 0; 97 if (ce_stage(ce) != checkout_stage 98 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce))) 99 continue; 100 did_checkout = 1; 101 if (checkout_entry(ce, &state, 102 to_tempfile ? topath[ce_stage(ce)] : NULL, 103 NULL) < 0) 104 errs++; 105 } 106 107 if (did_checkout) { 108 if (to_tempfile) 109 write_tempfile_record(name, prefix); 110 return errs > 0 ? -1 : 0; 111 } 112 113 /* 114 * At this point we know we didn't try to check anything out. If it was 115 * because we did find an entry but it was stage 0, that's not an 116 * error. 117 */ 118 if (has_same_name && checkout_stage == CHECKOUT_ALL) 119 return 0; 120 121 if (!state.quiet) { 122 fprintf(stderr, "git checkout-index: %s ", name); 123 if (!has_same_name) 124 fprintf(stderr, "is not in the cache"); 125 else if (!is_file) 126 fprintf(stderr, "is a sparse directory"); 127 else if (is_skipped) 128 fprintf(stderr, "has skip-worktree enabled; " 129 "use '--ignore-skip-worktree-bits' to checkout"); 130 else if (checkout_stage) 131 fprintf(stderr, "does not exist at stage %d", 132 checkout_stage); 133 else 134 fprintf(stderr, "is unmerged"); 135 fputc('\n', stderr); 136 } 137 return -1; 138} 139 140static int checkout_all(struct index_state *index, const char *prefix, int prefix_length) 141{ 142 int i, errs = 0; 143 struct cache_entry *last_ce = NULL; 144 145 for (i = 0; i < index->cache_nr ; i++) { 146 struct cache_entry *ce = index->cache[i]; 147 148 if (S_ISSPARSEDIR(ce->ce_mode)) { 149 if (!ce_skip_worktree(ce)) 150 BUG("sparse directory '%s' does not have skip-worktree set", ce->name); 151 152 /* 153 * If the current entry is a sparse directory and skip-worktree 154 * entries are being checked out, expand the index and continue 155 * the loop on the current index position (now pointing to the 156 * first entry inside the expanded sparse directory). 157 */ 158 if (ignore_skip_worktree) { 159 ensure_full_index(index); 160 ce = index->cache[i]; 161 } 162 } 163 164 if (!ignore_skip_worktree && ce_skip_worktree(ce)) 165 continue; 166 if (ce_stage(ce) != checkout_stage 167 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce))) 168 continue; 169 if (prefix && *prefix && 170 (ce_namelen(ce) <= prefix_length || 171 memcmp(prefix, ce->name, prefix_length))) 172 continue; 173 if (last_ce && to_tempfile) { 174 if (ce_namelen(last_ce) != ce_namelen(ce) 175 || memcmp(last_ce->name, ce->name, ce_namelen(ce))) 176 write_tempfile_record(last_ce->name, prefix); 177 } 178 if (checkout_entry(ce, &state, 179 to_tempfile ? topath[ce_stage(ce)] : NULL, 180 NULL) < 0) 181 errs++; 182 last_ce = ce; 183 } 184 if (last_ce && to_tempfile) 185 write_tempfile_record(last_ce->name, prefix); 186 return !!errs; 187} 188 189static const char * const builtin_checkout_index_usage[] = { 190 N_("git checkout-index [<options>] [--] [<file>...]"), 191 NULL 192}; 193 194static int option_parse_stage(const struct option *opt, 195 const char *arg, int unset) 196{ 197 int *stage = opt->value; 198 199 BUG_ON_OPT_NEG(unset); 200 201 if (!strcmp(arg, "all")) { 202 *stage = CHECKOUT_ALL; 203 } else { 204 int ch = arg[0]; 205 if ('1' <= ch && ch <= '3') 206 *stage = arg[0] - '0'; 207 else 208 die(_("stage should be between 1 and 3 or all")); 209 } 210 return 0; 211} 212 213int cmd_checkout_index(int argc, 214 const char **argv, 215 const char *prefix, 216 struct repository *repo) 217{ 218 int i; 219 struct lock_file lock_file = LOCK_INIT; 220 int all = 0; 221 int read_from_stdin = 0; 222 int prefix_length; 223 int force = 0, quiet = 0, not_new = 0; 224 int index_opt = 0; 225 int err = 0; 226 int pc_workers, pc_threshold; 227 struct option builtin_checkout_index_options[] = { 228 OPT_BOOL('a', "all", &all, 229 N_("check out all files in the index")), 230 OPT_BOOL(0, "ignore-skip-worktree-bits", &ignore_skip_worktree, 231 N_("do not skip files with skip-worktree set")), 232 OPT__FORCE(&force, N_("force overwrite of existing files"), 0), 233 OPT__QUIET(&quiet, 234 N_("no warning for existing files and files not in index")), 235 OPT_BOOL('n', "no-create", &not_new, 236 N_("don't checkout new files")), 237 OPT_BOOL('u', "index", &index_opt, 238 N_("update stat information in the index file")), 239 OPT_BOOL('z', NULL, &nul_term_line, 240 N_("paths are separated with NUL character")), 241 OPT_BOOL(0, "stdin", &read_from_stdin, 242 N_("read list of paths from the standard input")), 243 OPT_BOOL(0, "temp", &to_tempfile, 244 N_("write the content to temporary files")), 245 OPT_STRING(0, "prefix", &state.base_dir, N_("string"), 246 N_("when creating files, prepend <string>")), 247 OPT_CALLBACK_F(0, "stage", &checkout_stage, "(1|2|3|all)", 248 N_("copy out the files from named stage"), 249 PARSE_OPT_NONEG, option_parse_stage), 250 OPT_END() 251 }; 252 253 show_usage_with_options_if_asked(argc, argv, 254 builtin_checkout_index_usage, 255 builtin_checkout_index_options); 256 repo_config(repo, git_default_config, NULL); 257 prefix_length = prefix ? strlen(prefix) : 0; 258 259 prepare_repo_settings(repo); 260 repo->settings.command_requires_full_index = 0; 261 262 if (repo_read_index(repo) < 0) { 263 die("invalid cache"); 264 } 265 266 argc = parse_options(argc, argv, prefix, builtin_checkout_index_options, 267 builtin_checkout_index_usage, 0); 268 state.istate = repo->index; 269 state.force = force; 270 state.quiet = quiet; 271 state.not_new = not_new; 272 273 if (!state.base_dir) 274 state.base_dir = ""; 275 state.base_dir_len = strlen(state.base_dir); 276 277 if (to_tempfile < 0) 278 to_tempfile = (checkout_stage == CHECKOUT_ALL); 279 if (!to_tempfile && checkout_stage == CHECKOUT_ALL) 280 die(_("options '%s' and '%s' cannot be used together"), 281 "--stage=all", "--no-temp"); 282 283 /* 284 * when --prefix is specified we do not want to update cache. 285 */ 286 if (index_opt && !state.base_dir_len && !to_tempfile) { 287 state.refresh_cache = 1; 288 state.istate = repo->index; 289 repo_hold_locked_index(repo, &lock_file, 290 LOCK_DIE_ON_ERROR); 291 } 292 293 get_parallel_checkout_configs(&pc_workers, &pc_threshold); 294 if (pc_workers > 1) 295 init_parallel_checkout(); 296 297 /* Check out named files first */ 298 for (i = 0; i < argc; i++) { 299 const char *arg = argv[i]; 300 char *p; 301 302 if (all) 303 die("git checkout-index: don't mix '--all' and explicit filenames"); 304 if (read_from_stdin) 305 die("git checkout-index: don't mix '--stdin' and explicit filenames"); 306 p = prefix_path(prefix, prefix_length, arg); 307 err |= checkout_file(repo->index, p, prefix); 308 free(p); 309 } 310 311 if (read_from_stdin) { 312 struct strbuf buf = STRBUF_INIT; 313 struct strbuf unquoted = STRBUF_INIT; 314 strbuf_getline_fn getline_fn; 315 316 if (all) 317 die("git checkout-index: don't mix '--all' and '--stdin'"); 318 319 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf; 320 while (getline_fn(&buf, stdin) != EOF) { 321 char *p; 322 if (!nul_term_line && buf.buf[0] == '"') { 323 strbuf_reset(&unquoted); 324 if (unquote_c_style(&unquoted, buf.buf, NULL)) 325 die("line is badly quoted"); 326 strbuf_swap(&buf, &unquoted); 327 } 328 p = prefix_path(prefix, prefix_length, buf.buf); 329 err |= checkout_file(repo->index, p, prefix); 330 free(p); 331 } 332 strbuf_release(&unquoted); 333 strbuf_release(&buf); 334 } 335 336 if (all) 337 err |= checkout_all(repo->index, prefix, prefix_length); 338 339 if (pc_workers > 1) 340 err |= run_parallel_checkout(&state, pc_workers, pc_threshold, 341 NULL, NULL); 342 343 if (err) 344 return 1; 345 346 if (is_lock_file_locked(&lock_file) && 347 write_locked_index(repo->index, &lock_file, COMMIT_LOCK)) 348 die("Unable to write new index file"); 349 return 0; 350}