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