daemon.c: remove trailing whitespace.
[git/jrn.git] / checkout-index.c
blob596d320fb67983a7f899f184024dde26caec956e
1 /*
2 * Check-out files from the "current cache directory"
4 * Copyright (C) 2005 Linus Torvalds
6 * Careful: order of argument flags does matter. For example,
8 * git-checkout-index -a -f file.c
10 * Will first check out all files listed in the cache (but not
11 * overwrite any old ones), and then force-checkout "file.c" a
12 * second time (ie that one _will_ overwrite any old contents
13 * with the same filename).
15 * Also, just doing "git-checkout-index" does nothing. You probably
16 * meant "git-checkout-index -a". And if you want to force it, you
17 * want "git-checkout-index -f -a".
19 * Intuitiveness is not the goal here. Repeatability is. The
20 * reason for the "no arguments means no work" thing is that
21 * from scripts you are supposed to be able to do things like
23 * find . -name '*.h' -print0 | xargs -0 git-checkout-index -f --
25 * which will force all existing *.h files to be replaced with
26 * their cached copies. If an empty command line implied "all",
27 * then this would force-refresh everything in the cache, which
28 * was not the point.
30 * Oh, and the "--" is just a good idea when you know the rest
31 * will be filenames. Just so that you wouldn't have a filename
32 * of "-a" causing problems (not possible in the above example,
33 * but get used to it in scripting!).
35 #include "cache.h"
37 static struct checkout state = {
38 .base_dir = "",
39 .base_dir_len = 0,
40 .force = 0,
41 .quiet = 0,
42 .not_new = 0,
43 .refresh_cache = 0,
46 static int checkout_file(const char *name)
48 int pos = cache_name_pos(name, strlen(name));
49 if (pos < 0) {
50 if (!state.quiet) {
51 pos = -pos - 1;
52 fprintf(stderr,
53 "git-checkout-index: %s is %s.\n",
54 name,
55 (pos < active_nr &&
56 !strcmp(active_cache[pos]->name, name)) ?
57 "unmerged" : "not in the cache");
59 return -1;
61 return checkout_entry(active_cache[pos], &state);
64 static int checkout_all(void)
66 int i;
68 for (i = 0; i < active_nr ; i++) {
69 struct cache_entry *ce = active_cache[i];
70 if (ce_stage(ce))
71 continue;
72 if (checkout_entry(ce, &state) < 0)
73 return -1;
75 return 0;
78 static const char checkout_cache_usage[] =
79 "git-checkout-index [-u] [-q] [-a] [-f] [-n] [--prefix=<string>] [--] <file>...";
81 static struct cache_file cache_file;
83 int main(int argc, char **argv)
85 int i;
86 int newfd = -1;
87 int all = 0;
89 if (read_cache() < 0) {
90 die("invalid cache");
93 for (i = 1; i < argc; i++) {
94 const char *arg = argv[i];
96 if (!strcmp(arg, "--")) {
97 i++;
98 break;
100 if (!strcmp(arg, "-a") || !strcmp(arg, "--all")) {
101 all = 1;
102 continue;
104 if (!strcmp(arg, "-f") || !strcmp(arg, "--force")) {
105 state.force = 1;
106 continue;
108 if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
109 state.quiet = 1;
110 continue;
112 if (!strcmp(arg, "-n") || !strcmp(arg, "--no-create")) {
113 state.not_new = 1;
114 continue;
116 if (!strcmp(arg, "-u") || !strcmp(arg, "--index")) {
117 state.refresh_cache = 1;
118 if (newfd < 0)
119 newfd = hold_index_file_for_update
120 (&cache_file,
121 get_index_file());
122 if (newfd < 0)
123 die("cannot open index.lock file.");
124 continue;
126 if (!memcmp(arg, "--prefix=", 9)) {
127 state.base_dir = arg+9;
128 state.base_dir_len = strlen(state.base_dir);
129 continue;
131 if (arg[0] == '-')
132 usage(checkout_cache_usage);
133 break;
136 if (state.base_dir_len) {
137 /* when --prefix is specified we do not
138 * want to update cache.
140 if (state.refresh_cache) {
141 close(newfd); newfd = -1;
142 rollback_index_file(&cache_file);
144 state.refresh_cache = 0;
147 /* Check out named files first */
148 for ( ; i < argc; i++) {
149 const char *arg = argv[i];
151 if (all)
152 die("git-checkout-index: don't mix '--all' and explicit filenames");
153 checkout_file(arg);
156 if (all)
157 checkout_all();
159 if (0 <= newfd &&
160 (write_cache(newfd, active_cache, active_nr) ||
161 commit_index_file(&cache_file)))
162 die("Unable to write new cachefile");
163 return 0;