checkout-index.c 6.6 KB
Newer Older
1 2 3 4 5 6
/*
 * Check-out files from the "current cache directory"
 *
 * Copyright (C) 2005 Linus Torvalds
 *
 */
7
#include "builtin.h"
8
#include "config.h"
9
#include "lockfile.h"
10
#include "quote.h"
11
#include "cache-tree.h"
12
#include "parse-options.h"
13

14
#define CHECKOUT_ALL 4
15
static int nul_term_line;
16
static int checkout_stage; /* default to checkout stage0 */
17
static int to_tempfile;
18
static char topath[4][TEMPORARY_FILENAME_LENGTH + 1];
19

R
René Scharfe 已提交
20
static struct checkout state = CHECKOUT_INIT;
21

22
static void write_tempfile_record(const char *name, const char *prefix)
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
{
	int i;

	if (CHECKOUT_ALL == checkout_stage) {
		for (i = 1; i < 4; i++) {
			if (i > 1)
				putchar(' ');
			if (topath[i][0])
				fputs(topath[i], stdout);
			else
				putchar('.');
		}
	} else
		fputs(topath[checkout_stage], stdout);

	putchar('\t');
39 40
	write_name_quoted_relative(name, prefix, stdout,
				   nul_term_line ? '\0' : '\n');
41 42 43 44 45 46

	for (i = 0; i < 4; i++) {
		topath[i][0] = 0;
	}
}

47
static int checkout_file(const char *name, const char *prefix)
48
{
49 50 51
	int namelen = strlen(name);
	int pos = cache_name_pos(name, namelen);
	int has_same_name = 0;
52 53
	int did_checkout = 0;
	int errs = 0;
54 55 56 57 58 59

	if (pos < 0)
		pos = -pos - 1;

	while (pos < active_nr) {
		struct cache_entry *ce = active_cache[pos];
60
		if (ce_namelen(ce) != namelen ||
61 62 63 64
		    memcmp(ce->name, name, namelen))
			break;
		has_same_name = 1;
		pos++;
65 66 67 68 69 70 71 72 73 74 75
		if (ce_stage(ce) != checkout_stage
		    && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
			continue;
		did_checkout = 1;
		if (checkout_entry(ce, &state,
		    to_tempfile ? topath[ce_stage(ce)] : NULL) < 0)
			errs++;
	}

	if (did_checkout) {
		if (to_tempfile)
76
			write_tempfile_record(name, prefix);
77
		return errs > 0 ? -1 : 0;
78
	}
79 80

	if (!state.quiet) {
81
		fprintf(stderr, "git checkout-index: %s ", name);
82 83 84 85 86 87 88 89 90 91
		if (!has_same_name)
			fprintf(stderr, "is not in the cache");
		else if (checkout_stage)
			fprintf(stderr, "does not exist at stage %d",
				checkout_stage);
		else
			fprintf(stderr, "is unmerged");
		fputc('\n', stderr);
	}
	return -1;
92 93
}

D
David Rientjes 已提交
94
static void checkout_all(const char *prefix, int prefix_length)
95
{
96
	int i, errs = 0;
97
	struct cache_entry *last_ce = NULL;
98 99 100

	for (i = 0; i < active_nr ; i++) {
		struct cache_entry *ce = active_cache[i];
101 102
		if (ce_stage(ce) != checkout_stage
		    && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
103
			continue;
104
		if (prefix && *prefix &&
105 106
		    (ce_namelen(ce) <= prefix_length ||
		     memcmp(prefix, ce->name, prefix_length)))
107
			continue;
108 109 110
		if (last_ce && to_tempfile) {
			if (ce_namelen(last_ce) != ce_namelen(ce)
			    || memcmp(last_ce->name, ce->name, ce_namelen(ce)))
111
				write_tempfile_record(last_ce->name, prefix);
112 113 114
		}
		if (checkout_entry(ce, &state,
		    to_tempfile ? topath[ce_stage(ce)] : NULL) < 0)
115
			errs++;
116
		last_ce = ce;
117
	}
118
	if (last_ce && to_tempfile)
119
		write_tempfile_record(last_ce->name, prefix);
120 121 122 123 124
	if (errs)
		/* we have already done our error reporting.
		 * exit with the same code as die().
		 */
		exit(128);
125 126
}

127
static const char * const builtin_checkout_index_usage[] = {
128
	N_("git checkout-index [<options>] [--] [<file>...]"),
129 130
	NULL
};
131

132 133 134
static int option_parse_stage(const struct option *opt,
			      const char *arg, int unset)
{
135 136
	BUG_ON_OPT_NEG(unset);

137 138 139 140 141 142 143 144
	if (!strcmp(arg, "all")) {
		to_tempfile = 1;
		checkout_stage = CHECKOUT_ALL;
	} else {
		int ch = arg[0];
		if ('1' <= ch && ch <= '3')
			checkout_stage = arg[0] - '0';
		else
145
			die(_("stage should be between 1 and 3 or all"));
146 147 148 149
	}
	return 0;
}

150
int cmd_checkout_index(int argc, const char **argv, const char *prefix)
151
{
152
	int i;
153
	struct lock_file lock_file = LOCK_INIT;
154
	int all = 0;
155
	int read_from_stdin = 0;
156
	int prefix_length;
157
	int force = 0, quiet = 0, not_new = 0;
158
	int index_opt = 0;
159
	struct option builtin_checkout_index_options[] = {
160
		OPT_BOOL('a', "all", &all,
161
			N_("check out all files in the index")),
162
		OPT__FORCE(&force, N_("force overwrite of existing files"), 0),
163
		OPT__QUIET(&quiet,
164
			N_("no warning for existing files and files not in index")),
165
		OPT_BOOL('n', "no-create", &not_new,
166
			N_("don't checkout new files")),
167 168
		OPT_BOOL('u', "index", &index_opt,
			 N_("update stat information in the index file")),
169 170
		OPT_BOOL('z', NULL, &nul_term_line,
			N_("paths are separated with NUL character")),
171
		OPT_BOOL(0, "stdin", &read_from_stdin,
172
			N_("read list of paths from the standard input")),
173
		OPT_BOOL(0, "temp", &to_tempfile,
174
			N_("write the content to temporary files")),
175 176
		OPT_STRING(0, "prefix", &state.base_dir, N_("string"),
			N_("when creating files, prepend <string>")),
177
		{ OPTION_CALLBACK, 0, "stage", NULL, "(1|2|3|all)",
178
			N_("copy out the files from named stage"),
179
			PARSE_OPT_NONEG, option_parse_stage },
180 181
		OPT_END()
	};
182

183 184 185
	if (argc == 2 && !strcmp(argv[1], "-h"))
		usage_with_options(builtin_checkout_index_usage,
				   builtin_checkout_index_options);
186
	git_config(git_default_config, NULL);
187 188
	prefix_length = prefix ? strlen(prefix) : 0;

189
	if (read_cache() < 0) {
190
		die("invalid cache");
191 192
	}

193
	argc = parse_options(argc, argv, prefix, builtin_checkout_index_options,
194
			builtin_checkout_index_usage, 0);
195
	state.istate = &the_index;
196 197 198
	state.force = force;
	state.quiet = quiet;
	state.not_new = not_new;
199

200 201 202 203
	if (!state.base_dir)
		state.base_dir = "";
	state.base_dir_len = strlen(state.base_dir);

204 205 206 207 208 209
	/*
	 * when --prefix is specified we do not want to update cache.
	 */
	if (index_opt && !state.base_dir_len && !to_tempfile) {
		state.refresh_cache = 1;
		state.istate = &the_index;
210
		hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
211 212 213
	}

	/* Check out named files first */
214
	for (i = 0; i < argc; i++) {
215
		const char *arg = argv[i];
216
		char *p;
217 218

		if (all)
219
			die("git checkout-index: don't mix '--all' and explicit filenames");
220
		if (read_from_stdin)
221
			die("git checkout-index: don't mix '--stdin' and explicit filenames");
222
		p = prefix_path(prefix, prefix_length, arg);
223
		checkout_file(p, prefix);
224
		free(p);
225
	}
226

227
	if (read_from_stdin) {
228 229
		struct strbuf buf = STRBUF_INIT;
		struct strbuf unquoted = STRBUF_INIT;
230
		strbuf_getline_fn getline_fn;
231

232
		if (all)
233
			die("git checkout-index: don't mix '--all' and '--stdin'");
234

235 236
		getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
		while (getline_fn(&buf, stdin) != EOF) {
237
			char *p;
238
			if (!nul_term_line && buf.buf[0] == '"') {
239 240
				strbuf_reset(&unquoted);
				if (unquote_c_style(&unquoted, buf.buf, NULL))
241
					die("line is badly quoted");
242
				strbuf_swap(&buf, &unquoted);
243 244
			}
			p = prefix_path(prefix, prefix_length, buf.buf);
245
			checkout_file(p, prefix);
246
			free(p);
247
		}
248
		strbuf_release(&unquoted);
249
		strbuf_release(&buf);
250 251
	}

252
	if (all)
253
		checkout_all(prefix, prefix_length);
254

255
	if (is_lock_file_locked(&lock_file) &&
256
	    write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
257
		die("Unable to write new index file");
258 259
	return 0;
}