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