]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/checkout-index.c
hex: introduce functions to print arbitrary hashes
[thirdparty/git.git] / builtin / checkout-index.c
CommitLineData
33db5f4d
LT
1/*
2 * Check-out files from the "current cache directory"
3 *
4 * Copyright (C) 2005 Linus Torvalds
5 *
33db5f4d 6 */
baffc0e7 7#include "builtin.h"
b2141fc1 8#include "config.h"
697cc8ef 9#include "lockfile.h"
9debe63d 10#include "quote.h"
bad68ec9 11#include "cache-tree.h"
a6c7db1b 12#include "parse-options.h"
33db5f4d 13
de84f99c 14#define CHECKOUT_ALL 4
a392f57d 15static int nul_term_line;
3bd348ae 16static int checkout_stage; /* default to checkout stage0 */
de84f99c 17static int to_tempfile;
af2a651d 18static char topath[4][TEMPORARY_FILENAME_LENGTH + 1];
c3e9a653 19
68e3d629 20static struct checkout state = CHECKOUT_INIT;
33db5f4d 21
74c4de58 22static void write_tempfile_record(const char *name, const char *prefix)
de84f99c
SP
23{
24 int i;
25
26 if (CHECKOUT_ALL == checkout_stage) {
27 for (i = 1; i < 4; i++) {
28 if (i > 1)
29 putchar(' ');
30 if (topath[i][0])
31 fputs(topath[i], stdout);
32 else
33 putchar('.');
34 }
35 } else
36 fputs(topath[checkout_stage], stdout);
37
38 putchar('\t');
a392f57d
JH
39 write_name_quoted_relative(name, prefix, stdout,
40 nul_term_line ? '\0' : '\n');
de84f99c
SP
41
42 for (i = 0; i < 4; i++) {
43 topath[i][0] = 0;
44 }
45}
46
74c4de58 47static int checkout_file(const char *name, const char *prefix)
33db5f4d 48{
3bd348ae
JH
49 int namelen = strlen(name);
50 int pos = cache_name_pos(name, namelen);
51 int has_same_name = 0;
de84f99c
SP
52 int did_checkout = 0;
53 int errs = 0;
3bd348ae
JH
54
55 if (pos < 0)
56 pos = -pos - 1;
57
58 while (pos < active_nr) {
59 struct cache_entry *ce = active_cache[pos];
f4f9adae 60 if (ce_namelen(ce) != namelen ||
3bd348ae
JH
61 memcmp(ce->name, name, namelen))
62 break;
63 has_same_name = 1;
3bd348ae 64 pos++;
de84f99c
SP
65 if (ce_stage(ce) != checkout_stage
66 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
67 continue;
68 did_checkout = 1;
69 if (checkout_entry(ce, &state,
70 to_tempfile ? topath[ce_stage(ce)] : NULL) < 0)
71 errs++;
72 }
73
74 if (did_checkout) {
75 if (to_tempfile)
74c4de58 76 write_tempfile_record(name, prefix);
de84f99c 77 return errs > 0 ? -1 : 0;
33db5f4d 78 }
3bd348ae
JH
79
80 if (!state.quiet) {
05207a28 81 fprintf(stderr, "git checkout-index: %s ", name);
3bd348ae
JH
82 if (!has_same_name)
83 fprintf(stderr, "is not in the cache");
84 else if (checkout_stage)
85 fprintf(stderr, "does not exist at stage %d",
86 checkout_stage);
87 else
88 fprintf(stderr, "is unmerged");
89 fputc('\n', stderr);
90 }
91 return -1;
33db5f4d
LT
92}
93
f7f0fbfc 94static void checkout_all(const char *prefix, int prefix_length)
33db5f4d 95{
4b12dae6 96 int i, errs = 0;
4b25d091 97 struct cache_entry *last_ce = NULL;
33db5f4d
LT
98
99 for (i = 0; i < active_nr ; i++) {
100 struct cache_entry *ce = active_cache[i];
de84f99c
SP
101 if (ce_stage(ce) != checkout_stage
102 && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
d9f98eeb 103 continue;
c3e9a653 104 if (prefix && *prefix &&
3bd348ae
JH
105 (ce_namelen(ce) <= prefix_length ||
106 memcmp(prefix, ce->name, prefix_length)))
c3e9a653 107 continue;
de84f99c
SP
108 if (last_ce && to_tempfile) {
109 if (ce_namelen(last_ce) != ce_namelen(ce)
110 || memcmp(last_ce->name, ce->name, ce_namelen(ce)))
74c4de58 111 write_tempfile_record(last_ce->name, prefix);
de84f99c
SP
112 }
113 if (checkout_entry(ce, &state,
114 to_tempfile ? topath[ce_stage(ce)] : NULL) < 0)
4b12dae6 115 errs++;
de84f99c 116 last_ce = ce;
33db5f4d 117 }
de84f99c 118 if (last_ce && to_tempfile)
74c4de58 119 write_tempfile_record(last_ce->name, prefix);
4b12dae6
JH
120 if (errs)
121 /* we have already done our error reporting.
122 * exit with the same code as die().
123 */
124 exit(128);
33db5f4d
LT
125}
126
a6c7db1b 127static const char * const builtin_checkout_index_usage[] = {
9c9b4f2f 128 N_("git checkout-index [<options>] [--] [<file>...]"),
a6c7db1b
MV
129 NULL
130};
d46ad9c9 131
a6c7db1b
MV
132static int option_parse_stage(const struct option *opt,
133 const char *arg, int unset)
134{
135 if (!strcmp(arg, "all")) {
136 to_tempfile = 1;
137 checkout_stage = CHECKOUT_ALL;
138 } else {
139 int ch = arg[0];
140 if ('1' <= ch && ch <= '3')
141 checkout_stage = arg[0] - '0';
142 else
22396175 143 die(_("stage should be between 1 and 3 or all"));
a6c7db1b
MV
144 }
145 return 0;
146}
147
e414156a 148int cmd_checkout_index(int argc, const char **argv, const char *prefix)
33db5f4d 149{
a65a686f 150 int i;
02ae242f 151 struct lock_file lock_file = LOCK_INIT;
a65a686f 152 int all = 0;
9debe63d 153 int read_from_stdin = 0;
e414156a 154 int prefix_length;
a6c7db1b 155 int force = 0, quiet = 0, not_new = 0;
6a6df8aa 156 int index_opt = 0;
a6c7db1b 157 struct option builtin_checkout_index_options[] = {
d5d09d47 158 OPT_BOOL('a', "all", &all,
f63cf8c9 159 N_("check out all files in the index")),
1224781d 160 OPT__FORCE(&force, N_("force overwrite of existing files"), 0),
8c839683 161 OPT__QUIET(&quiet,
0ed21718 162 N_("no warning for existing files and files not in index")),
5d4d1440 163 OPT_BOOL('n', "no-create", &not_new,
0ed21718 164 N_("don't checkout new files")),
6a6df8aa
JK
165 OPT_BOOL('u', "index", &index_opt,
166 N_("update stat information in the index file")),
9096ee16
JK
167 OPT_BOOL('z', NULL, &nul_term_line,
168 N_("paths are separated with NUL character")),
d5d09d47 169 OPT_BOOL(0, "stdin", &read_from_stdin,
0ed21718 170 N_("read list of paths from the standard input")),
d5d09d47 171 OPT_BOOL(0, "temp", &to_tempfile,
0ed21718 172 N_("write the content to temporary files")),
5ed5f671
JK
173 OPT_STRING(0, "prefix", &state.base_dir, N_("string"),
174 N_("when creating files, prepend <string>")),
168f32eb 175 { OPTION_CALLBACK, 0, "stage", NULL, "(1|2|3|all)",
0ed21718 176 N_("copy out the files from named stage"),
22396175 177 PARSE_OPT_NONEG, option_parse_stage },
a6c7db1b
MV
178 OPT_END()
179 };
33db5f4d 180
cf9d52e4
NTND
181 if (argc == 2 && !strcmp(argv[1], "-h"))
182 usage_with_options(builtin_checkout_index_usage,
183 builtin_checkout_index_options);
ef90d6d4 184 git_config(git_default_config, NULL);
c3e9a653
JH
185 prefix_length = prefix ? strlen(prefix) : 0;
186
33db5f4d 187 if (read_cache() < 0) {
2de381f9 188 die("invalid cache");
33db5f4d
LT
189 }
190
37782920 191 argc = parse_options(argc, argv, prefix, builtin_checkout_index_options,
a6c7db1b 192 builtin_checkout_index_usage, 0);
74cfc0ee 193 state.istate = &the_index;
a6c7db1b
MV
194 state.force = force;
195 state.quiet = quiet;
196 state.not_new = not_new;
a65a686f 197
5ed5f671
JK
198 if (!state.base_dir)
199 state.base_dir = "";
200 state.base_dir_len = strlen(state.base_dir);
201
6a6df8aa
JK
202 /*
203 * when --prefix is specified we do not want to update cache.
204 */
205 if (index_opt && !state.base_dir_len && !to_tempfile) {
206 state.refresh_cache = 1;
207 state.istate = &the_index;
02ae242f 208 hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
a65a686f
LT
209 }
210
211 /* Check out named files first */
a6c7db1b 212 for (i = 0; i < argc; i++) {
a65a686f 213 const char *arg = argv[i];
d7a643b7 214 char *p;
a65a686f
LT
215
216 if (all)
7e44c935 217 die("git checkout-index: don't mix '--all' and explicit filenames");
9debe63d 218 if (read_from_stdin)
7e44c935 219 die("git checkout-index: don't mix '--stdin' and explicit filenames");
dc46da22 220 p = prefix_path(prefix, prefix_length, arg);
74c4de58 221 checkout_file(p, prefix);
d7a643b7 222 free(p);
33db5f4d 223 }
415e96c8 224
9debe63d 225 if (read_from_stdin) {
0d4cc1b4
JK
226 struct strbuf buf = STRBUF_INIT;
227 struct strbuf unquoted = STRBUF_INIT;
a392f57d 228 strbuf_getline_fn getline_fn;
7fb1011e 229
9debe63d 230 if (all)
7e44c935 231 die("git checkout-index: don't mix '--all' and '--stdin'");
7fb1011e 232
a392f57d
JH
233 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
234 while (getline_fn(&buf, stdin) != EOF) {
d7a643b7 235 char *p;
a392f57d 236 if (!nul_term_line && buf.buf[0] == '"') {
0d4cc1b4
JK
237 strbuf_reset(&unquoted);
238 if (unquote_c_style(&unquoted, buf.buf, NULL))
7fb1011e 239 die("line is badly quoted");
0d4cc1b4 240 strbuf_swap(&buf, &unquoted);
7fb1011e
PH
241 }
242 p = prefix_path(prefix, prefix_length, buf.buf);
74c4de58 243 checkout_file(p, prefix);
d7a643b7 244 free(p);
9debe63d 245 }
0d4cc1b4 246 strbuf_release(&unquoted);
e6c019d0 247 strbuf_release(&buf);
9debe63d
SP
248 }
249
a65a686f 250 if (all)
e414156a 251 checkout_all(prefix, prefix_length);
a65a686f 252
02ae242f 253 if (is_lock_file_locked(&lock_file) &&
03b86647 254 write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
021b6e45 255 die("Unable to write new index file");
33db5f4d
LT
256 return 0;
257}