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