Merge branch 'js/ci-github-set-env'
[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_COMPATIBILITY_MACROS
8 #include "builtin.h"
9 #include "config.h"
10 #include "lockfile.h"
11 #include "quote.h"
12 #include "cache-tree.h"
13 #include "parse-options.h"
14
15 #define CHECKOUT_ALL 4
16 static int nul_term_line;
17 static int checkout_stage; /* default to checkout stage0 */
18 static int to_tempfile;
19 static char topath[4][TEMPORARY_FILENAME_LENGTH + 1];
20
21 static struct checkout state = CHECKOUT_INIT;
22
23 static void write_tempfile_record(const char *name, const char *prefix)
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');
40         write_name_quoted_relative(name, prefix, stdout,
41                                    nul_term_line ? '\0' : '\n');
42
43         for (i = 0; i < 4; i++) {
44                 topath[i][0] = 0;
45         }
46 }
47
48 static int checkout_file(const char *name, const char *prefix)
49 {
50         int namelen = strlen(name);
51         int pos = cache_name_pos(name, namelen);
52         int has_same_name = 0;
53         int did_checkout = 0;
54         int errs = 0;
55
56         if (pos < 0)
57                 pos = -pos - 1;
58
59         while (pos < active_nr) {
60                 struct cache_entry *ce = active_cache[pos];
61                 if (ce_namelen(ce) != namelen ||
62                     memcmp(ce->name, name, namelen))
63                         break;
64                 has_same_name = 1;
65                 pos++;
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,
71                                    to_tempfile ? topath[ce_stage(ce)] : NULL,
72                                    NULL) < 0)
73                         errs++;
74         }
75
76         if (did_checkout) {
77                 if (to_tempfile)
78                         write_tempfile_record(name, prefix);
79                 return errs > 0 ? -1 : 0;
80         }
81
82         /*
83          * At this point we know we didn't try to check anything out. If it was
84          * because we did find an entry but it was stage 0, that's not an
85          * error.
86          */
87         if (has_same_name && checkout_stage == CHECKOUT_ALL)
88                 return 0;
89
90         if (!state.quiet) {
91                 fprintf(stderr, "git checkout-index: %s ", name);
92                 if (!has_same_name)
93                         fprintf(stderr, "is not in the cache");
94                 else if (checkout_stage)
95                         fprintf(stderr, "does not exist at stage %d",
96                                 checkout_stage);
97                 else
98                         fprintf(stderr, "is unmerged");
99                 fputc('\n', stderr);
100         }
101         return -1;
102 }
103
104 static void checkout_all(const char *prefix, int prefix_length)
105 {
106         int i, errs = 0;
107         struct cache_entry *last_ce = NULL;
108
109         for (i = 0; i < active_nr ; i++) {
110                 struct cache_entry *ce = active_cache[i];
111                 if (ce_stage(ce) != checkout_stage
112                     && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
113                         continue;
114                 if (prefix && *prefix &&
115                     (ce_namelen(ce) <= prefix_length ||
116                      memcmp(prefix, ce->name, prefix_length)))
117                         continue;
118                 if (last_ce && to_tempfile) {
119                         if (ce_namelen(last_ce) != ce_namelen(ce)
120                             || memcmp(last_ce->name, ce->name, ce_namelen(ce)))
121                                 write_tempfile_record(last_ce->name, prefix);
122                 }
123                 if (checkout_entry(ce, &state,
124                                    to_tempfile ? topath[ce_stage(ce)] : NULL,
125                                    NULL) < 0)
126                         errs++;
127                 last_ce = ce;
128         }
129         if (last_ce && to_tempfile)
130                 write_tempfile_record(last_ce->name, prefix);
131         if (errs)
132                 /* we have already done our error reporting.
133                  * exit with the same code as die().
134                  */
135                 exit(128);
136 }
137
138 static const char * const builtin_checkout_index_usage[] = {
139         N_("git checkout-index [<options>] [--] [<file>...]"),
140         NULL
141 };
142
143 static int option_parse_stage(const struct option *opt,
144                               const char *arg, int unset)
145 {
146         BUG_ON_OPT_NEG(unset);
147
148         if (!strcmp(arg, "all")) {
149                 to_tempfile = 1;
150                 checkout_stage = CHECKOUT_ALL;
151         } else {
152                 int ch = arg[0];
153                 if ('1' <= ch && ch <= '3')
154                         checkout_stage = arg[0] - '0';
155                 else
156                         die(_("stage should be between 1 and 3 or all"));
157         }
158         return 0;
159 }
160
161 int cmd_checkout_index(int argc, const char **argv, const char *prefix)
162 {
163         int i;
164         struct lock_file lock_file = LOCK_INIT;
165         int all = 0;
166         int read_from_stdin = 0;
167         int prefix_length;
168         int force = 0, quiet = 0, not_new = 0;
169         int index_opt = 0;
170         int err = 0;
171         struct option builtin_checkout_index_options[] = {
172                 OPT_BOOL('a', "all", &all,
173                         N_("check out all files in the index")),
174                 OPT__FORCE(&force, N_("force overwrite of existing files"), 0),
175                 OPT__QUIET(&quiet,
176                         N_("no warning for existing files and files not in index")),
177                 OPT_BOOL('n', "no-create", &not_new,
178                         N_("don't checkout new files")),
179                 OPT_BOOL('u', "index", &index_opt,
180                          N_("update stat information in the index file")),
181                 OPT_BOOL('z', NULL, &nul_term_line,
182                         N_("paths are separated with NUL character")),
183                 OPT_BOOL(0, "stdin", &read_from_stdin,
184                         N_("read list of paths from the standard input")),
185                 OPT_BOOL(0, "temp", &to_tempfile,
186                         N_("write the content to temporary files")),
187                 OPT_STRING(0, "prefix", &state.base_dir, N_("string"),
188                         N_("when creating files, prepend <string>")),
189                 OPT_CALLBACK_F(0, "stage", NULL, "(1|2|3|all)",
190                         N_("copy out the files from named stage"),
191                         PARSE_OPT_NONEG, option_parse_stage),
192                 OPT_END()
193         };
194
195         if (argc == 2 && !strcmp(argv[1], "-h"))
196                 usage_with_options(builtin_checkout_index_usage,
197                                    builtin_checkout_index_options);
198         git_config(git_default_config, NULL);
199         prefix_length = prefix ? strlen(prefix) : 0;
200
201         if (read_cache() < 0) {
202                 die("invalid cache");
203         }
204
205         argc = parse_options(argc, argv, prefix, builtin_checkout_index_options,
206                         builtin_checkout_index_usage, 0);
207         state.istate = &the_index;
208         state.force = force;
209         state.quiet = quiet;
210         state.not_new = not_new;
211
212         if (!state.base_dir)
213                 state.base_dir = "";
214         state.base_dir_len = strlen(state.base_dir);
215
216         /*
217          * when --prefix is specified we do not want to update cache.
218          */
219         if (index_opt && !state.base_dir_len && !to_tempfile) {
220                 state.refresh_cache = 1;
221                 state.istate = &the_index;
222                 hold_locked_index(&lock_file, LOCK_DIE_ON_ERROR);
223         }
224
225         /* Check out named files first */
226         for (i = 0; i < argc; i++) {
227                 const char *arg = argv[i];
228                 char *p;
229
230                 if (all)
231                         die("git checkout-index: don't mix '--all' and explicit filenames");
232                 if (read_from_stdin)
233                         die("git checkout-index: don't mix '--stdin' and explicit filenames");
234                 p = prefix_path(prefix, prefix_length, arg);
235                 err |= checkout_file(p, prefix);
236                 free(p);
237         }
238
239         if (read_from_stdin) {
240                 struct strbuf buf = STRBUF_INIT;
241                 struct strbuf unquoted = STRBUF_INIT;
242                 strbuf_getline_fn getline_fn;
243
244                 if (all)
245                         die("git checkout-index: don't mix '--all' and '--stdin'");
246
247                 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
248                 while (getline_fn(&buf, stdin) != EOF) {
249                         char *p;
250                         if (!nul_term_line && buf.buf[0] == '"') {
251                                 strbuf_reset(&unquoted);
252                                 if (unquote_c_style(&unquoted, buf.buf, NULL))
253                                         die("line is badly quoted");
254                                 strbuf_swap(&buf, &unquoted);
255                         }
256                         p = prefix_path(prefix, prefix_length, buf.buf);
257                         err |= checkout_file(p, prefix);
258                         free(p);
259                 }
260                 strbuf_release(&unquoted);
261                 strbuf_release(&buf);
262         }
263
264         if (err)
265                 return 1;
266
267         if (all)
268                 checkout_all(prefix, prefix_length);
269
270         if (is_lock_file_locked(&lock_file) &&
271             write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
272                 die("Unable to write new index file");
273         return 0;
274 }