Linus Torvalds | 33db5f4 | 2005-04-09 09:53:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Check-out files from the "current cache directory" |
| 3 | * |
| 4 | * Copyright (C) 2005 Linus Torvalds |
| 5 | * |
Linus Torvalds | 33db5f4 | 2005-04-09 09:53:05 -0700 | [diff] [blame] | 6 | */ |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 7 | #define USE_THE_INDEX_VARIABLE |
Peter Hagervall | baffc0e | 2007-07-15 01:14:45 +0200 | [diff] [blame] | 8 | #include "builtin.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 9 | #include "config.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 10 | #include "gettext.h" |
Michael Haggerty | 697cc8e | 2014-10-01 12:28:42 +0200 | [diff] [blame] | 11 | #include "lockfile.h" |
Shawn Pearce | 9debe63 | 2006-02-28 21:43:33 -0500 | [diff] [blame] | 12 | #include "quote.h" |
Elijah Newren | d1cbe1e | 2023-04-22 20:17:20 +0000 | [diff] [blame] | 13 | #include "repository.h" |
Junio C Hamano | bad68ec | 2006-04-24 21:18:58 -0700 | [diff] [blame] | 14 | #include "cache-tree.h" |
Miklos Vajna | a6c7db1 | 2008-10-18 03:17:23 +0200 | [diff] [blame] | 15 | #include "parse-options.h" |
Matheus Tavares | d052cc0 | 2021-03-23 11:19:32 -0300 | [diff] [blame] | 16 | #include "entry.h" |
Matheus Tavares | 70b052b | 2021-05-04 13:27:30 -0300 | [diff] [blame] | 17 | #include "parallel-checkout.h" |
Elijah Newren | 08c46a4 | 2023-05-16 06:33:56 +0000 | [diff] [blame] | 18 | #include "read-cache-ll.h" |
Elijah Newren | e38da48 | 2023-03-21 06:26:05 +0000 | [diff] [blame] | 19 | #include "setup.h" |
Elijah Newren | baf889c | 2023-05-16 06:33:51 +0000 | [diff] [blame] | 20 | #include "sparse-index.h" |
Linus Torvalds | 33db5f4 | 2005-04-09 09:53:05 -0700 | [diff] [blame] | 21 | |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 22 | #define CHECKOUT_ALL 4 |
Junio C Hamano | a392f57 | 2016-01-13 16:14:48 -0800 | [diff] [blame] | 23 | static int nul_term_line; |
Junio C Hamano | 3bd348a | 2005-12-07 00:29:51 -0800 | [diff] [blame] | 24 | static int checkout_stage; /* default to checkout stage0 */ |
Victoria Dye | 88078f5 | 2022-01-11 18:05:02 +0000 | [diff] [blame] | 25 | static int ignore_skip_worktree; /* default to 0 */ |
Jeff King | 9b40386 | 2023-09-05 03:12:59 -0400 | [diff] [blame] | 26 | static int to_tempfile = -1; |
Junio C Hamano | af2a651 | 2013-10-23 10:52:42 -0700 | [diff] [blame] | 27 | static char topath[4][TEMPORARY_FILENAME_LENGTH + 1]; |
Junio C Hamano | c3e9a65 | 2005-11-26 00:22:48 -0800 | [diff] [blame] | 28 | |
René Scharfe | 68e3d62 | 2016-09-22 18:11:33 +0200 | [diff] [blame] | 29 | static struct checkout state = CHECKOUT_INIT; |
Linus Torvalds | 33db5f4 | 2005-04-09 09:53:05 -0700 | [diff] [blame] | 30 | |
Eric Sunshine | 74c4de5 | 2014-12-24 04:43:16 -0500 | [diff] [blame] | 31 | static void write_tempfile_record(const char *name, const char *prefix) |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 32 | { |
| 33 | int i; |
Matheus Tavares | 3f7ba60 | 2021-02-16 11:06:52 -0300 | [diff] [blame] | 34 | int have_tempname = 0; |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 35 | |
| 36 | if (CHECKOUT_ALL == checkout_stage) { |
Matheus Tavares | 3f7ba60 | 2021-02-16 11:06:52 -0300 | [diff] [blame] | 37 | for (i = 1; i < 4; i++) |
| 38 | if (topath[i][0]) { |
| 39 | have_tempname = 1; |
| 40 | break; |
| 41 | } |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 42 | |
Matheus Tavares | 3f7ba60 | 2021-02-16 11:06:52 -0300 | [diff] [blame] | 43 | if (have_tempname) { |
| 44 | for (i = 1; i < 4; i++) { |
| 45 | if (i > 1) |
| 46 | putchar(' '); |
| 47 | if (topath[i][0]) |
| 48 | fputs(topath[i], stdout); |
| 49 | else |
| 50 | putchar('.'); |
| 51 | } |
| 52 | } |
| 53 | } else if (topath[checkout_stage][0]) { |
| 54 | have_tempname = 1; |
| 55 | fputs(topath[checkout_stage], stdout); |
| 56 | } |
| 57 | |
| 58 | if (have_tempname) { |
| 59 | putchar('\t'); |
| 60 | write_name_quoted_relative(name, prefix, stdout, |
| 61 | nul_term_line ? '\0' : '\n'); |
| 62 | } |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 63 | |
| 64 | for (i = 0; i < 4; i++) { |
| 65 | topath[i][0] = 0; |
| 66 | } |
| 67 | } |
| 68 | |
Eric Sunshine | 74c4de5 | 2014-12-24 04:43:16 -0500 | [diff] [blame] | 69 | static int checkout_file(const char *name, const char *prefix) |
Linus Torvalds | 33db5f4 | 2005-04-09 09:53:05 -0700 | [diff] [blame] | 70 | { |
Junio C Hamano | 3bd348a | 2005-12-07 00:29:51 -0800 | [diff] [blame] | 71 | int namelen = strlen(name); |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 72 | int pos = index_name_pos(&the_index, name, namelen); |
Junio C Hamano | 3bd348a | 2005-12-07 00:29:51 -0800 | [diff] [blame] | 73 | int has_same_name = 0; |
Victoria Dye | 35682ad | 2022-01-11 18:05:03 +0000 | [diff] [blame] | 74 | int is_file = 0; |
Victoria Dye | 88078f5 | 2022-01-11 18:05:02 +0000 | [diff] [blame] | 75 | int is_skipped = 1; |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 76 | int did_checkout = 0; |
| 77 | int errs = 0; |
Junio C Hamano | 3bd348a | 2005-12-07 00:29:51 -0800 | [diff] [blame] | 78 | |
| 79 | if (pos < 0) |
| 80 | pos = -pos - 1; |
| 81 | |
Ævar Arnfjörð Bjarmason | dc59418 | 2022-11-19 14:07:34 +0100 | [diff] [blame] | 82 | while (pos < the_index.cache_nr) { |
| 83 | struct cache_entry *ce = the_index.cache[pos]; |
Junio C Hamano | f4f9ada | 2005-12-13 21:39:56 -0800 | [diff] [blame] | 84 | if (ce_namelen(ce) != namelen || |
Junio C Hamano | 3bd348a | 2005-12-07 00:29:51 -0800 | [diff] [blame] | 85 | memcmp(ce->name, name, namelen)) |
| 86 | break; |
| 87 | has_same_name = 1; |
Junio C Hamano | 3bd348a | 2005-12-07 00:29:51 -0800 | [diff] [blame] | 88 | pos++; |
Victoria Dye | 35682ad | 2022-01-11 18:05:03 +0000 | [diff] [blame] | 89 | if (S_ISSPARSEDIR(ce->ce_mode)) |
| 90 | break; |
| 91 | is_file = 1; |
Victoria Dye | 88078f5 | 2022-01-11 18:05:02 +0000 | [diff] [blame] | 92 | if (!ignore_skip_worktree && ce_skip_worktree(ce)) |
| 93 | break; |
| 94 | is_skipped = 0; |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 95 | if (ce_stage(ce) != checkout_stage |
| 96 | && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce))) |
| 97 | continue; |
| 98 | did_checkout = 1; |
| 99 | if (checkout_entry(ce, &state, |
Nguyễn Thái Ngọc Duy | 0f086e6 | 2018-11-13 19:28:00 +0100 | [diff] [blame] | 100 | to_tempfile ? topath[ce_stage(ce)] : NULL, |
| 101 | NULL) < 0) |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 102 | errs++; |
| 103 | } |
| 104 | |
| 105 | if (did_checkout) { |
| 106 | if (to_tempfile) |
Eric Sunshine | 74c4de5 | 2014-12-24 04:43:16 -0500 | [diff] [blame] | 107 | write_tempfile_record(name, prefix); |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 108 | return errs > 0 ? -1 : 0; |
Linus Torvalds | 33db5f4 | 2005-04-09 09:53:05 -0700 | [diff] [blame] | 109 | } |
Junio C Hamano | 3bd348a | 2005-12-07 00:29:51 -0800 | [diff] [blame] | 110 | |
Jeff King | 0b809c8 | 2020-10-27 03:36:02 -0400 | [diff] [blame] | 111 | /* |
| 112 | * At this point we know we didn't try to check anything out. If it was |
| 113 | * because we did find an entry but it was stage 0, that's not an |
| 114 | * error. |
| 115 | */ |
| 116 | if (has_same_name && checkout_stage == CHECKOUT_ALL) |
| 117 | return 0; |
| 118 | |
Junio C Hamano | 3bd348a | 2005-12-07 00:29:51 -0800 | [diff] [blame] | 119 | if (!state.quiet) { |
Heikki Orsila | 05207a2 | 2008-09-09 13:28:30 +0300 | [diff] [blame] | 120 | fprintf(stderr, "git checkout-index: %s ", name); |
Junio C Hamano | 3bd348a | 2005-12-07 00:29:51 -0800 | [diff] [blame] | 121 | if (!has_same_name) |
| 122 | fprintf(stderr, "is not in the cache"); |
Victoria Dye | 35682ad | 2022-01-11 18:05:03 +0000 | [diff] [blame] | 123 | else if (!is_file) |
| 124 | fprintf(stderr, "is a sparse directory"); |
Victoria Dye | 88078f5 | 2022-01-11 18:05:02 +0000 | [diff] [blame] | 125 | else if (is_skipped) |
| 126 | fprintf(stderr, "has skip-worktree enabled; " |
| 127 | "use '--ignore-skip-worktree-bits' to checkout"); |
Junio C Hamano | 3bd348a | 2005-12-07 00:29:51 -0800 | [diff] [blame] | 128 | else if (checkout_stage) |
| 129 | fprintf(stderr, "does not exist at stage %d", |
| 130 | checkout_stage); |
| 131 | else |
| 132 | fprintf(stderr, "is unmerged"); |
| 133 | fputc('\n', stderr); |
| 134 | } |
| 135 | return -1; |
Linus Torvalds | 33db5f4 | 2005-04-09 09:53:05 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Matheus Tavares | 70b052b | 2021-05-04 13:27:30 -0300 | [diff] [blame] | 138 | static int checkout_all(const char *prefix, int prefix_length) |
Linus Torvalds | 33db5f4 | 2005-04-09 09:53:05 -0700 | [diff] [blame] | 139 | { |
Junio C Hamano | 4b12dae | 2005-10-03 12:44:48 -0700 | [diff] [blame] | 140 | int i, errs = 0; |
Felipe Contreras | 4b25d09 | 2009-05-01 12:06:36 +0300 | [diff] [blame] | 141 | struct cache_entry *last_ce = NULL; |
Linus Torvalds | 33db5f4 | 2005-04-09 09:53:05 -0700 | [diff] [blame] | 142 | |
Ævar Arnfjörð Bjarmason | dc59418 | 2022-11-19 14:07:34 +0100 | [diff] [blame] | 143 | for (i = 0; i < the_index.cache_nr ; i++) { |
| 144 | struct cache_entry *ce = the_index.cache[i]; |
Victoria Dye | 35682ad | 2022-01-11 18:05:03 +0000 | [diff] [blame] | 145 | |
| 146 | if (S_ISSPARSEDIR(ce->ce_mode)) { |
| 147 | if (!ce_skip_worktree(ce)) |
| 148 | BUG("sparse directory '%s' does not have skip-worktree set", ce->name); |
| 149 | |
| 150 | /* |
| 151 | * If the current entry is a sparse directory and skip-worktree |
| 152 | * entries are being checked out, expand the index and continue |
| 153 | * the loop on the current index position (now pointing to the |
| 154 | * first entry inside the expanded sparse directory). |
| 155 | */ |
| 156 | if (ignore_skip_worktree) { |
| 157 | ensure_full_index(&the_index); |
Ævar Arnfjörð Bjarmason | dc59418 | 2022-11-19 14:07:34 +0100 | [diff] [blame] | 158 | ce = the_index.cache[i]; |
Victoria Dye | 35682ad | 2022-01-11 18:05:03 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
Victoria Dye | 88078f5 | 2022-01-11 18:05:02 +0000 | [diff] [blame] | 162 | if (!ignore_skip_worktree && ce_skip_worktree(ce)) |
| 163 | continue; |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 164 | if (ce_stage(ce) != checkout_stage |
| 165 | && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce))) |
Linus Torvalds | d9f98ee | 2005-04-17 18:39:14 -0700 | [diff] [blame] | 166 | continue; |
Junio C Hamano | c3e9a65 | 2005-11-26 00:22:48 -0800 | [diff] [blame] | 167 | if (prefix && *prefix && |
Junio C Hamano | 3bd348a | 2005-12-07 00:29:51 -0800 | [diff] [blame] | 168 | (ce_namelen(ce) <= prefix_length || |
| 169 | memcmp(prefix, ce->name, prefix_length))) |
Junio C Hamano | c3e9a65 | 2005-11-26 00:22:48 -0800 | [diff] [blame] | 170 | continue; |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 171 | if (last_ce && to_tempfile) { |
| 172 | if (ce_namelen(last_ce) != ce_namelen(ce) |
| 173 | || memcmp(last_ce->name, ce->name, ce_namelen(ce))) |
Eric Sunshine | 74c4de5 | 2014-12-24 04:43:16 -0500 | [diff] [blame] | 174 | write_tempfile_record(last_ce->name, prefix); |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 175 | } |
| 176 | if (checkout_entry(ce, &state, |
Nguyễn Thái Ngọc Duy | 0f086e6 | 2018-11-13 19:28:00 +0100 | [diff] [blame] | 177 | to_tempfile ? topath[ce_stage(ce)] : NULL, |
| 178 | NULL) < 0) |
Junio C Hamano | 4b12dae | 2005-10-03 12:44:48 -0700 | [diff] [blame] | 179 | errs++; |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 180 | last_ce = ce; |
Linus Torvalds | 33db5f4 | 2005-04-09 09:53:05 -0700 | [diff] [blame] | 181 | } |
Shawn Pearce | de84f99 | 2006-03-05 03:24:15 -0500 | [diff] [blame] | 182 | if (last_ce && to_tempfile) |
Eric Sunshine | 74c4de5 | 2014-12-24 04:43:16 -0500 | [diff] [blame] | 183 | write_tempfile_record(last_ce->name, prefix); |
Matheus Tavares | 70b052b | 2021-05-04 13:27:30 -0300 | [diff] [blame] | 184 | return !!errs; |
Linus Torvalds | 33db5f4 | 2005-04-09 09:53:05 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Miklos Vajna | a6c7db1 | 2008-10-18 03:17:23 +0200 | [diff] [blame] | 187 | static const char * const builtin_checkout_index_usage[] = { |
Alex Henrie | 9c9b4f2 | 2015-01-13 00:44:47 -0700 | [diff] [blame] | 188 | N_("git checkout-index [<options>] [--] [<file>...]"), |
Miklos Vajna | a6c7db1 | 2008-10-18 03:17:23 +0200 | [diff] [blame] | 189 | NULL |
| 190 | }; |
Junio C Hamano | d46ad9c | 2005-07-13 20:25:07 -0700 | [diff] [blame] | 191 | |
Miklos Vajna | a6c7db1 | 2008-10-18 03:17:23 +0200 | [diff] [blame] | 192 | static int option_parse_stage(const struct option *opt, |
| 193 | const char *arg, int unset) |
| 194 | { |
Jeff King | 66e3309 | 2023-08-31 17:21:07 -0400 | [diff] [blame] | 195 | int *stage = opt->value; |
| 196 | |
Jeff King | 517fe80 | 2018-11-05 01:45:42 -0500 | [diff] [blame] | 197 | BUG_ON_OPT_NEG(unset); |
| 198 | |
Miklos Vajna | a6c7db1 | 2008-10-18 03:17:23 +0200 | [diff] [blame] | 199 | if (!strcmp(arg, "all")) { |
Jeff King | 66e3309 | 2023-08-31 17:21:07 -0400 | [diff] [blame] | 200 | *stage = CHECKOUT_ALL; |
Miklos Vajna | a6c7db1 | 2008-10-18 03:17:23 +0200 | [diff] [blame] | 201 | } else { |
| 202 | int ch = arg[0]; |
| 203 | if ('1' <= ch && ch <= '3') |
Jeff King | 66e3309 | 2023-08-31 17:21:07 -0400 | [diff] [blame] | 204 | *stage = arg[0] - '0'; |
Miklos Vajna | a6c7db1 | 2008-10-18 03:17:23 +0200 | [diff] [blame] | 205 | else |
Jeff King | 2239617 | 2016-01-31 22:18:24 -0500 | [diff] [blame] | 206 | die(_("stage should be between 1 and 3 or all")); |
Miklos Vajna | a6c7db1 | 2008-10-18 03:17:23 +0200 | [diff] [blame] | 207 | } |
| 208 | return 0; |
| 209 | } |
| 210 | |
Junio C Hamano | e414156 | 2006-08-04 01:23:19 -0700 | [diff] [blame] | 211 | int cmd_checkout_index(int argc, const char **argv, const char *prefix) |
Linus Torvalds | 33db5f4 | 2005-04-09 09:53:05 -0700 | [diff] [blame] | 212 | { |
Linus Torvalds | a65a686 | 2005-10-17 17:32:12 -0700 | [diff] [blame] | 213 | int i; |
Martin Ågren | 02ae242 | 2017-10-05 22:32:07 +0200 | [diff] [blame] | 214 | struct lock_file lock_file = LOCK_INIT; |
Linus Torvalds | a65a686 | 2005-10-17 17:32:12 -0700 | [diff] [blame] | 215 | int all = 0; |
Shawn Pearce | 9debe63 | 2006-02-28 21:43:33 -0500 | [diff] [blame] | 216 | int read_from_stdin = 0; |
Junio C Hamano | e414156 | 2006-08-04 01:23:19 -0700 | [diff] [blame] | 217 | int prefix_length; |
Miklos Vajna | a6c7db1 | 2008-10-18 03:17:23 +0200 | [diff] [blame] | 218 | int force = 0, quiet = 0, not_new = 0; |
Jeff King | 6a6df8a | 2016-01-31 06:29:36 -0500 | [diff] [blame] | 219 | int index_opt = 0; |
Jeff King | 7e41061 | 2020-10-27 03:37:14 -0400 | [diff] [blame] | 220 | int err = 0; |
Matheus Tavares | 70b052b | 2021-05-04 13:27:30 -0300 | [diff] [blame] | 221 | int pc_workers, pc_threshold; |
Miklos Vajna | a6c7db1 | 2008-10-18 03:17:23 +0200 | [diff] [blame] | 222 | struct option builtin_checkout_index_options[] = { |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 223 | OPT_BOOL('a', "all", &all, |
Nguyễn Thái Ngọc Duy | f63cf8c | 2012-08-20 19:32:55 +0700 | [diff] [blame] | 224 | N_("check out all files in the index")), |
Victoria Dye | 88078f5 | 2022-01-11 18:05:02 +0000 | [diff] [blame] | 225 | OPT_BOOL(0, "ignore-skip-worktree-bits", &ignore_skip_worktree, |
| 226 | N_("do not skip files with skip-worktree set")), |
Nguyễn Thái Ngọc Duy | 1224781 | 2018-02-09 18:01:42 +0700 | [diff] [blame] | 227 | OPT__FORCE(&force, N_("force overwrite of existing files"), 0), |
Jonathan Nieder | 8c83968 | 2010-11-08 13:54:48 -0600 | [diff] [blame] | 228 | OPT__QUIET(&quiet, |
Nguyễn Thái Ngọc Duy | 0ed2171 | 2012-08-20 19:31:58 +0700 | [diff] [blame] | 229 | N_("no warning for existing files and files not in index")), |
Stefan Beller | 5d4d144 | 2013-08-03 13:51:25 +0200 | [diff] [blame] | 230 | OPT_BOOL('n', "no-create", ¬_new, |
Nguyễn Thái Ngọc Duy | 0ed2171 | 2012-08-20 19:31:58 +0700 | [diff] [blame] | 231 | N_("don't checkout new files")), |
Jeff King | 6a6df8a | 2016-01-31 06:29:36 -0500 | [diff] [blame] | 232 | OPT_BOOL('u', "index", &index_opt, |
| 233 | N_("update stat information in the index file")), |
Jeff King | 9096ee1 | 2016-01-31 06:25:43 -0500 | [diff] [blame] | 234 | OPT_BOOL('z', NULL, &nul_term_line, |
| 235 | N_("paths are separated with NUL character")), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 236 | OPT_BOOL(0, "stdin", &read_from_stdin, |
Nguyễn Thái Ngọc Duy | 0ed2171 | 2012-08-20 19:31:58 +0700 | [diff] [blame] | 237 | N_("read list of paths from the standard input")), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 238 | OPT_BOOL(0, "temp", &to_tempfile, |
Nguyễn Thái Ngọc Duy | 0ed2171 | 2012-08-20 19:31:58 +0700 | [diff] [blame] | 239 | N_("write the content to temporary files")), |
Jeff King | 5ed5f67 | 2016-01-31 06:26:16 -0500 | [diff] [blame] | 240 | OPT_STRING(0, "prefix", &state.base_dir, N_("string"), |
| 241 | N_("when creating files, prepend <string>")), |
Jeff King | 66e3309 | 2023-08-31 17:21:07 -0400 | [diff] [blame] | 242 | OPT_CALLBACK_F(0, "stage", &checkout_stage, "(1|2|3|all)", |
Nguyễn Thái Ngọc Duy | 0ed2171 | 2012-08-20 19:31:58 +0700 | [diff] [blame] | 243 | N_("copy out the files from named stage"), |
Denton Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 244 | PARSE_OPT_NONEG, option_parse_stage), |
Miklos Vajna | a6c7db1 | 2008-10-18 03:17:23 +0200 | [diff] [blame] | 245 | OPT_END() |
| 246 | }; |
Linus Torvalds | 33db5f4 | 2005-04-09 09:53:05 -0700 | [diff] [blame] | 247 | |
Nguyễn Thái Ngọc Duy | cf9d52e | 2010-10-22 01:44:01 -0500 | [diff] [blame] | 248 | if (argc == 2 && !strcmp(argv[1], "-h")) |
| 249 | usage_with_options(builtin_checkout_index_usage, |
| 250 | builtin_checkout_index_options); |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 251 | git_config(git_default_config, NULL); |
Junio C Hamano | c3e9a65 | 2005-11-26 00:22:48 -0800 | [diff] [blame] | 252 | prefix_length = prefix ? strlen(prefix) : 0; |
| 253 | |
Victoria Dye | 35682ad | 2022-01-11 18:05:03 +0000 | [diff] [blame] | 254 | prepare_repo_settings(the_repository); |
| 255 | the_repository->settings.command_requires_full_index = 0; |
| 256 | |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 257 | if (repo_read_index(the_repository) < 0) { |
Petr Baudis | 2de381f | 2005-04-13 02:28:48 -0700 | [diff] [blame] | 258 | die("invalid cache"); |
Linus Torvalds | 33db5f4 | 2005-04-09 09:53:05 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 261 | argc = parse_options(argc, argv, prefix, builtin_checkout_index_options, |
Miklos Vajna | a6c7db1 | 2008-10-18 03:17:23 +0200 | [diff] [blame] | 262 | builtin_checkout_index_usage, 0); |
Nguyễn Thái Ngọc Duy | 74cfc0e | 2018-08-13 18:14:32 +0200 | [diff] [blame] | 263 | state.istate = &the_index; |
Miklos Vajna | a6c7db1 | 2008-10-18 03:17:23 +0200 | [diff] [blame] | 264 | state.force = force; |
| 265 | state.quiet = quiet; |
| 266 | state.not_new = not_new; |
Linus Torvalds | a65a686 | 2005-10-17 17:32:12 -0700 | [diff] [blame] | 267 | |
Jeff King | 5ed5f67 | 2016-01-31 06:26:16 -0500 | [diff] [blame] | 268 | if (!state.base_dir) |
| 269 | state.base_dir = ""; |
| 270 | state.base_dir_len = strlen(state.base_dir); |
| 271 | |
Jeff King | 9b40386 | 2023-09-05 03:12:59 -0400 | [diff] [blame] | 272 | if (to_tempfile < 0) |
| 273 | to_tempfile = (checkout_stage == CHECKOUT_ALL); |
| 274 | if (!to_tempfile && checkout_stage == CHECKOUT_ALL) |
| 275 | die(_("options '%s' and '%s' cannot be used together"), |
| 276 | "--stage=all", "--no-temp"); |
| 277 | |
Jeff King | 6a6df8a | 2016-01-31 06:29:36 -0500 | [diff] [blame] | 278 | /* |
| 279 | * when --prefix is specified we do not want to update cache. |
| 280 | */ |
| 281 | if (index_opt && !state.base_dir_len && !to_tempfile) { |
| 282 | state.refresh_cache = 1; |
| 283 | state.istate = &the_index; |
Ævar Arnfjörð Bjarmason | 07047d6 | 2022-11-19 14:07:38 +0100 | [diff] [blame] | 284 | repo_hold_locked_index(the_repository, &lock_file, |
| 285 | LOCK_DIE_ON_ERROR); |
Linus Torvalds | a65a686 | 2005-10-17 17:32:12 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Matheus Tavares | 70b052b | 2021-05-04 13:27:30 -0300 | [diff] [blame] | 288 | get_parallel_checkout_configs(&pc_workers, &pc_threshold); |
| 289 | if (pc_workers > 1) |
| 290 | init_parallel_checkout(); |
| 291 | |
Linus Torvalds | a65a686 | 2005-10-17 17:32:12 -0700 | [diff] [blame] | 292 | /* Check out named files first */ |
Miklos Vajna | a6c7db1 | 2008-10-18 03:17:23 +0200 | [diff] [blame] | 293 | for (i = 0; i < argc; i++) { |
Linus Torvalds | a65a686 | 2005-10-17 17:32:12 -0700 | [diff] [blame] | 294 | const char *arg = argv[i]; |
Stefan Beller | d7a643b | 2015-05-04 12:11:54 -0700 | [diff] [blame] | 295 | char *p; |
Linus Torvalds | a65a686 | 2005-10-17 17:32:12 -0700 | [diff] [blame] | 296 | |
| 297 | if (all) |
Junio C Hamano | 7e44c93 | 2008-08-31 09:39:19 -0700 | [diff] [blame] | 298 | die("git checkout-index: don't mix '--all' and explicit filenames"); |
Shawn Pearce | 9debe63 | 2006-02-28 21:43:33 -0500 | [diff] [blame] | 299 | if (read_from_stdin) |
Junio C Hamano | 7e44c93 | 2008-08-31 09:39:19 -0700 | [diff] [blame] | 300 | die("git checkout-index: don't mix '--stdin' and explicit filenames"); |
Junio C Hamano | dc46da2 | 2006-05-05 22:38:06 -0700 | [diff] [blame] | 301 | p = prefix_path(prefix, prefix_length, arg); |
Jeff King | 7e41061 | 2020-10-27 03:37:14 -0400 | [diff] [blame] | 302 | err |= checkout_file(p, prefix); |
Stefan Beller | d7a643b | 2015-05-04 12:11:54 -0700 | [diff] [blame] | 303 | free(p); |
Linus Torvalds | 33db5f4 | 2005-04-09 09:53:05 -0700 | [diff] [blame] | 304 | } |
Junio C Hamano | 415e96c | 2005-05-15 14:23:12 -0700 | [diff] [blame] | 305 | |
Shawn Pearce | 9debe63 | 2006-02-28 21:43:33 -0500 | [diff] [blame] | 306 | if (read_from_stdin) { |
Jeff King | 0d4cc1b | 2016-01-31 06:25:26 -0500 | [diff] [blame] | 307 | struct strbuf buf = STRBUF_INIT; |
| 308 | struct strbuf unquoted = STRBUF_INIT; |
Junio C Hamano | a392f57 | 2016-01-13 16:14:48 -0800 | [diff] [blame] | 309 | strbuf_getline_fn getline_fn; |
Pierre Habouzit | 7fb1011 | 2007-09-20 00:42:14 +0200 | [diff] [blame] | 310 | |
Shawn Pearce | 9debe63 | 2006-02-28 21:43:33 -0500 | [diff] [blame] | 311 | if (all) |
Junio C Hamano | 7e44c93 | 2008-08-31 09:39:19 -0700 | [diff] [blame] | 312 | die("git checkout-index: don't mix '--all' and '--stdin'"); |
Pierre Habouzit | 7fb1011 | 2007-09-20 00:42:14 +0200 | [diff] [blame] | 313 | |
Junio C Hamano | a392f57 | 2016-01-13 16:14:48 -0800 | [diff] [blame] | 314 | getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf; |
| 315 | while (getline_fn(&buf, stdin) != EOF) { |
Stefan Beller | d7a643b | 2015-05-04 12:11:54 -0700 | [diff] [blame] | 316 | char *p; |
Junio C Hamano | a392f57 | 2016-01-13 16:14:48 -0800 | [diff] [blame] | 317 | if (!nul_term_line && buf.buf[0] == '"') { |
Jeff King | 0d4cc1b | 2016-01-31 06:25:26 -0500 | [diff] [blame] | 318 | strbuf_reset(&unquoted); |
| 319 | if (unquote_c_style(&unquoted, buf.buf, NULL)) |
Pierre Habouzit | 7fb1011 | 2007-09-20 00:42:14 +0200 | [diff] [blame] | 320 | die("line is badly quoted"); |
Jeff King | 0d4cc1b | 2016-01-31 06:25:26 -0500 | [diff] [blame] | 321 | strbuf_swap(&buf, &unquoted); |
Pierre Habouzit | 7fb1011 | 2007-09-20 00:42:14 +0200 | [diff] [blame] | 322 | } |
| 323 | p = prefix_path(prefix, prefix_length, buf.buf); |
Jeff King | 7e41061 | 2020-10-27 03:37:14 -0400 | [diff] [blame] | 324 | err |= checkout_file(p, prefix); |
Stefan Beller | d7a643b | 2015-05-04 12:11:54 -0700 | [diff] [blame] | 325 | free(p); |
Shawn Pearce | 9debe63 | 2006-02-28 21:43:33 -0500 | [diff] [blame] | 326 | } |
Jeff King | 0d4cc1b | 2016-01-31 06:25:26 -0500 | [diff] [blame] | 327 | strbuf_release(&unquoted); |
Pierre Habouzit | e6c019d | 2007-09-17 11:19:04 +0200 | [diff] [blame] | 328 | strbuf_release(&buf); |
Shawn Pearce | 9debe63 | 2006-02-28 21:43:33 -0500 | [diff] [blame] | 329 | } |
| 330 | |
Matheus Tavares | 70b052b | 2021-05-04 13:27:30 -0300 | [diff] [blame] | 331 | if (all) |
| 332 | err |= checkout_all(prefix, prefix_length); |
| 333 | |
| 334 | if (pc_workers > 1) |
| 335 | err |= run_parallel_checkout(&state, pc_workers, pc_threshold, |
| 336 | NULL, NULL); |
| 337 | |
Jeff King | 7e41061 | 2020-10-27 03:37:14 -0400 | [diff] [blame] | 338 | if (err) |
| 339 | return 1; |
| 340 | |
Martin Ågren | 02ae242 | 2017-10-05 22:32:07 +0200 | [diff] [blame] | 341 | if (is_lock_file_locked(&lock_file) && |
Nguyễn Thái Ngọc Duy | 03b8664 | 2014-06-13 19:19:23 +0700 | [diff] [blame] | 342 | write_locked_index(&the_index, &lock_file, COMMIT_LOCK)) |
Junio C Hamano | 021b6e4 | 2006-06-06 12:51:49 -0700 | [diff] [blame] | 343 | die("Unable to write new index file"); |
Linus Torvalds | 33db5f4 | 2005-04-09 09:53:05 -0700 | [diff] [blame] | 344 | return 0; |
| 345 | } |