@matheusbsbs / matheus-bagliano / commits / ee1bec3d10
Fork 0

update-index: --stdin and -z

The new option --stdin reads list of paths to be updated from the standard input. As usual, -z means the paths are terminated with NUL characters, as opposed to LF without that option. This is useful to use git-diff-files -z and git-ls-files -z when the platform xargs does not support -0 option, and obviously saves one process even when xargs can take -0. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Sep 26, 2005 at 18:13 UTC ee1bec3d1001ce3d84b72be4a72694c1aef37980
1 file changed +41 -13
update-index.c
+41 -13
@@ -4,6 +4,7 @@
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 +#include "strbuf.h"
8
9 /*
10 * Default to not allowing changes to the list of files. The
@@ -277,11 +278,30 @@ static int add_cacheinfo(const char *arg1, const char *arg2, const char *arg3)
278
279 static struct cache_file cache_file;
280
281 +
282 +static void update_one(const char *path, const char *prefix, int prefix_length)
283 +{
284 + const char *p = prefix_path(prefix, prefix_length, path);
285 + if (!verify_path(p)) {
286 + fprintf(stderr, "Ignoring path %s\n", path);
287 + return;
288 + }
289 + if (force_remove) {
290 + if (remove_file_from_cache(p))
291 + die("git-update-index: unable to remove %s", path);
292 + return;
293 + }
294 + if (add_file_to_cache(p))
295 + die("Unable to process file %s", path);
296 +}
297 +
298 int main(int argc, const char **argv)
299 {
282 - int i, newfd, entries, has_errors = 0;
300 + int i, newfd, entries, has_errors = 0, line_termination = '\n';
301 int allow_options = 1;
302 + int read_from_stdin = 0;
303 const char *prefix = setup_git_directory();
304 + int prefix_length = prefix ? strlen(prefix) : 0;
305
306 newfd = hold_index_file_for_update(&cache_file, get_index_file());
307 if (newfd < 0)
@@ -335,25 +355,33 @@ int main(int argc, const char **argv)
355 force_remove = 1;
356 continue;
357 }
338 -
358 + if (!strcmp(path, "-z")) {
359 + line_termination = 0;
360 + continue;
361 + }
362 + if (!strcmp(path, "--stdin")) {
363 + if (i != argc - 1)
364 + die("--stdin must be at the end");
365 + read_from_stdin = 1;
366 + break;
367 + }
368 if (!strcmp(path, "--ignore-missing")) {
369 not_new = 1;
370 continue;
371 }
372 die("unknown option %s", path);
373 }
345 - path = prefix_path(prefix, prefix ? strlen(prefix) : 0, path);
346 - if (!verify_path(path)) {
347 - fprintf(stderr, "Ignoring path %s\n", argv[i]);
348 - continue;
349 - }
350 - if (force_remove) {
351 - if (remove_file_from_cache(path))
352 - die("git-update-index: unable to remove %s", path);
353 - continue;
374 + update_one(path, prefix, prefix_length);
375 + }
376 + if (read_from_stdin) {
377 + struct strbuf buf;
378 + strbuf_init(&buf);
379 + while (1) {
380 + read_line(&buf, stdin, line_termination);
381 + if (buf.eof)
382 + break;
383 + update_one(buf.buf, prefix, prefix_length);
384 }
355 - if (add_file_to_cache(path))
356 - die("Unable to process file %s", path);
385 }
386 if (write_cache(newfd, active_cache, active_nr) ||
387 commit_index_file(&cache_file))