|
22 | 22 | * |
23 | 23 | * find . -name '*.h' -print0 | xargs -0 git-checkout-index -f -- |
24 | 24 | * |
| 25 | + * or: |
| 26 | + * |
| 27 | + * find . -name '*.h' -print0 | git-checkout-index -f -z --stdin |
| 28 | + * |
25 | 29 | * which will force all existing *.h files to be replaced with |
26 | 30 | * their cached copies. If an empty command line implied "all", |
27 | 31 | * then this would force-refresh everything in the cache, which |
|
33 | 37 | * but get used to it in scripting!). |
34 | 38 | */ |
35 | 39 | #include "cache.h" |
| 40 | +#include "strbuf.h" |
| 41 | +#include "quote.h" |
36 | 42 |
|
37 | 43 | static const char *prefix; |
38 | 44 | static int prefix_length; |
@@ -114,6 +120,8 @@ int main(int argc, char **argv) |
114 | 120 | int i; |
115 | 121 | int newfd = -1; |
116 | 122 | int all = 0; |
| 123 | + int read_from_stdin = 0; |
| 124 | + int line_termination = '\n'; |
117 | 125 |
|
118 | 126 | prefix = setup_git_directory(); |
119 | 127 | git_config(git_default_config); |
@@ -156,6 +164,17 @@ int main(int argc, char **argv) |
156 | 164 | die("cannot open index.lock file."); |
157 | 165 | continue; |
158 | 166 | } |
| 167 | + if (!strcmp(arg, "-z")) { |
| 168 | + line_termination = 0; |
| 169 | + continue; |
| 170 | + } |
| 171 | + if (!strcmp(arg, "--stdin")) { |
| 172 | + if (i != argc - 1) |
| 173 | + die("--stdin must be at the end"); |
| 174 | + read_from_stdin = 1; |
| 175 | + i++; /* do not consider arg as a file name */ |
| 176 | + break; |
| 177 | + } |
159 | 178 | if (!strncmp(arg, "--prefix=", 9)) { |
160 | 179 | state.base_dir = arg+9; |
161 | 180 | state.base_dir_len = strlen(state.base_dir); |
@@ -191,9 +210,31 @@ int main(int argc, char **argv) |
191 | 210 |
|
192 | 211 | if (all) |
193 | 212 | die("git-checkout-index: don't mix '--all' and explicit filenames"); |
| 213 | + if (read_from_stdin) |
| 214 | + die("git-checkout-index: don't mix '--stdin' and explicit filenames"); |
194 | 215 | checkout_file(prefix_path(prefix, prefix_length, arg)); |
195 | 216 | } |
196 | 217 |
|
| 218 | + if (read_from_stdin) { |
| 219 | + struct strbuf buf; |
| 220 | + if (all) |
| 221 | + die("git-checkout-index: don't mix '--all' and '--stdin'"); |
| 222 | + strbuf_init(&buf); |
| 223 | + while (1) { |
| 224 | + char *path_name; |
| 225 | + read_line(&buf, stdin, line_termination); |
| 226 | + if (buf.eof) |
| 227 | + break; |
| 228 | + if (line_termination && buf.buf[0] == '"') |
| 229 | + path_name = unquote_c_style(buf.buf, NULL); |
| 230 | + else |
| 231 | + path_name = buf.buf; |
| 232 | + checkout_file(prefix_path(prefix, prefix_length, path_name)); |
| 233 | + if (path_name != buf.buf) |
| 234 | + free(path_name); |
| 235 | + } |
| 236 | + } |
| 237 | + |
197 | 238 | if (all) |
198 | 239 | checkout_all(); |
199 | 240 |
|
|
0 commit comments