Teach git-checkout-index to read filenames from stdin.
Since git-checkout-index is often used from scripts which may have a stream of filenames they wish to checkout it is more convenient to use --stdin than xargs. On platforms where fork performance is currently sub-optimal and the length of a command line is limited (*cough* Cygwin *cough*) running a single git-checkout-index process for a large number of files beats spawning it multiple times from xargs. File names are still accepted on the command line if --stdin is not supplied. Nothing is performed if no files are supplied on the command line or by stdin. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Shawn Pearce committed
Feb 28, 2006 at 21:43 UTC
9debe63d10e476b96ff550d550df344a68b240e8
2 files changed
+59
-2
Documentation/git-checkout-index.txt
+18
-2
@@ -10,7 +10,9 @@ SYNOPSIS
10
--------
11
[verse]
12
'git-checkout-index' [-u] [-q] [-a] [-f] [-n] [--prefix=<string>]
13
- [--stage=<number>] [--] <file>...
13
+ [--stage=<number>]
14
+ [-z] [--stdin]
15
+ [--] [<file>]\*
16
17
DESCRIPTION
18
-----------
@@ -45,6 +47,15 @@ OPTIONS
47
Instead of checking out unmerged entries, copy out the
48
files from named stage. <number> must be between 1 and 3.
49
50
+--stdin::
51
+ Instead of taking list of paths from the command line,
52
+ read list of paths from the standard input. Paths are
53
+ separated by LF (i.e. one path per line) by default.
54
+
55
+-z::
56
+ Only meaningful with `--stdin`; paths are separated with
57
+ NUL character instead of LF.
58
+
59
--::
60
Do not interpret any more arguments as options.
61
@@ -64,7 +75,12 @@ $ find . -name '*.h' -print0 | xargs -0 git-checkout-index -f --
75
76
which will force all existing `*.h` files to be replaced with their
77
cached copies. If an empty command line implied "all", then this would
67
-force-refresh everything in the index, which was not the point.
78
+force-refresh everything in the index, which was not the point. But
79
+since git-checkout-index accepts --stdin it would be faster to use:
80
+
81
+----------------
82
+$ find . -name '*.h' -print0 | git-checkout-index -f -z --stdin
83
+----------------
84
85
The `--` is just a good idea when you know the rest will be filenames;
86
it will prevent problems with a filename of, for example, `-a`.
checkout-index.c
+41
@@ -22,6 +22,10 @@
22
*
23
* find . -name '*.h' -print0 | xargs -0 git-checkout-index -f --
24
*
25
+ * or:
26
+ *
27
+ * find . -name '*.h' -print0 | git-checkout-index -f -z --stdin
28
+ *
29
* which will force all existing *.h files to be replaced with
30
* their cached copies. If an empty command line implied "all",
31
* then this would force-refresh everything in the cache, which
@@ -33,6 +37,8 @@
37
* but get used to it in scripting!).
38
*/
39
#include "cache.h"
40
+#include "strbuf.h"
41
+#include "quote.h"
42
43
static const char *prefix;
44
static int prefix_length;
@@ -114,6 +120,8 @@ int main(int argc, char **argv)
120
int i;
121
int newfd = -1;
122
int all = 0;
123
+ int read_from_stdin = 0;
124
+ int line_termination = '\n';
125
126
prefix = setup_git_directory();
127
git_config(git_default_config);
@@ -156,6 +164,17 @@ int main(int argc, char **argv)
164
die("cannot open index.lock file.");
165
continue;
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
+ }
178
if (!strncmp(arg, "--prefix=", 9)) {
179
state.base_dir = arg+9;
180
state.base_dir_len = strlen(state.base_dir);
@@ -191,9 +210,31 @@ int main(int argc, char **argv)
210
211
if (all)
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");
215
checkout_file(prefix_path(prefix, prefix_length, arg));
216
}
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
+
238
if (all)
239
checkout_all();
240