]> git.ipfire.org Git - thirdparty/git.git/blame - checkout-cache.c
[PATCH] cat-file: be consistent in usage string and documentation.
[thirdparty/git.git] / checkout-cache.c
CommitLineData
33db5f4d
LT
1/*
2 * Check-out files from the "current cache directory"
3 *
4 * Copyright (C) 2005 Linus Torvalds
5 *
6 * Careful: order of argument flags does matter. For example,
7 *
667bb59b 8 * git-checkout-cache -a -f file.c
33db5f4d
LT
9 *
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).
14 *
667bb59b
AN
15 * Also, just doing "git-checkout-cache" does nothing. You probably
16 * meant "git-checkout-cache -a". And if you want to force it, you
17 * want "git-checkout-cache -f -a".
33db5f4d
LT
18 *
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
22 *
667bb59b 23 * find . -name '*.h' -print0 | xargs -0 git-checkout-cache -f --
33db5f4d
LT
24 *
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.
29 *
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!).
34 */
35#include "cache.h"
36
12dccc16
LT
37static 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,
44};
33db5f4d 45
d7f6ea3d 46static int checkout_file(const char *name)
33db5f4d
LT
47{
48 int pos = cache_name_pos(name, strlen(name));
49 if (pos < 0) {
12dccc16 50 if (!state.quiet) {
a38800fd
JH
51 pos = -pos - 1;
52 fprintf(stderr,
667bb59b 53 "git-checkout-cache: %s is %s.\n",
a38800fd
JH
54 name,
55 (pos < active_nr &&
56 !strcmp(active_cache[pos]->name, name)) ?
57 "unmerged" : "not in the cache");
58 }
33db5f4d
LT
59 return -1;
60 }
12dccc16 61 return checkout_entry(active_cache[pos], &state);
33db5f4d
LT
62}
63
d7f6ea3d 64static int checkout_all(void)
33db5f4d
LT
65{
66 int i;
67
68 for (i = 0; i < active_nr ; i++) {
69 struct cache_entry *ce = active_cache[i];
d9f98eeb
LT
70 if (ce_stage(ce))
71 continue;
12dccc16 72 if (checkout_entry(ce, &state) < 0)
33db5f4d
LT
73 return -1;
74 }
75 return 0;
76}
77
78int main(int argc, char **argv)
79{
80 int i, force_filename = 0;
415e96c8
JH
81 struct cache_file cache_file;
82 int newfd = -1;
33db5f4d
LT
83
84 if (read_cache() < 0) {
2de381f9 85 die("invalid cache");
33db5f4d
LT
86 }
87
88 for (i = 1; i < argc; i++) {
89 const char *arg = argv[i];
90 if (!force_filename) {
91 if (!strcmp(arg, "-a")) {
d7f6ea3d 92 checkout_all();
33db5f4d
LT
93 continue;
94 }
95 if (!strcmp(arg, "--")) {
96 force_filename = 1;
97 continue;
98 }
99 if (!strcmp(arg, "-f")) {
12dccc16 100 state.force = 1;
33db5f4d
LT
101 continue;
102 }
103 if (!strcmp(arg, "-q")) {
12dccc16 104 state.quiet = 1;
33db5f4d
LT
105 continue;
106 }
32718b6c 107 if (!strcmp(arg, "-n")) {
12dccc16 108 state.not_new = 1;
32718b6c
JB
109 continue;
110 }
415e96c8 111 if (!strcmp(arg, "-u")) {
12dccc16 112 state.refresh_cache = 1;
415e96c8
JH
113 if (newfd < 0)
114 newfd = hold_index_file_for_update
115 (&cache_file,
116 get_index_file());
117 if (newfd < 0)
118 die("cannot open index.lock file.");
119 continue;
120 }
65bb4914 121 if (!memcmp(arg, "--prefix=", 9)) {
12dccc16
LT
122 state.base_dir = arg+9;
123 state.base_dir_len = strlen(state.base_dir);
65bb4914
LT
124 continue;
125 }
33db5f4d 126 }
12dccc16 127 if (state.base_dir_len) {
415e96c8
JH
128 /* when --prefix is specified we do not
129 * want to update cache.
130 */
12dccc16 131 if (state.refresh_cache) {
415e96c8
JH
132 close(newfd); newfd = -1;
133 rollback_index_file(&cache_file);
134 }
12dccc16 135 state.refresh_cache = 0;
415e96c8 136 }
d7f6ea3d 137 checkout_file(arg);
33db5f4d 138 }
415e96c8
JH
139
140 if (0 <= newfd &&
141 (write_cache(newfd, active_cache, active_nr) ||
142 commit_index_file(&cache_file)))
143 die("Unable to write new cachefile");
33db5f4d
LT
144 return 0;
145}