@matheusbsbs / matheus-bagliano / commits / 24e4750c96
Fork 0

pathspec: add new function to parse file

This will be used to support the new option '--pathspec-from-file' in `git add`, `git-commit`, `git reset` etc. Note also that we specifically handle CR/LF line endings to support Windows better. To simplify code, file is first parsed into `argv_array`. This allows to avoid refactoring `parse_pathspec()`. I considered adding `nul_term_line` to `flags` instead, but decided that it doesn't fit there. The new code is mostly taken from `cmd_update_index()` and `split_mail_conv()`. Co-authored-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Alexandr Miloslavskiy <alexandr.miloslavskiy@syntevo.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Alexandr Miloslavskiy committed Nov 19, 2019 at 16:48 UTC 24e4750c969b844536af1d286e7d977c74c078a2
2 files changed +48
pathspec.c
+38
@@ -3,6 +3,8 @@
3 #include "dir.h"
4 #include "pathspec.h"
5 #include "attr.h"
6 +#include "argv-array.h"
7 +#include "quote.h"
8
9 /*
10 * Finds which of the given pathspecs match items in the index.
@@ -613,6 +615,42 @@ void parse_pathspec(struct pathspec *pathspec,
615 }
616 }
617
618 +void parse_pathspec_file(struct pathspec *pathspec, unsigned magic_mask,
619 + unsigned flags, const char *prefix,
620 + const char *file, int nul_term_line)
621 +{
622 + struct argv_array parsed_file = ARGV_ARRAY_INIT;
623 + strbuf_getline_fn getline_fn = nul_term_line ? strbuf_getline_nul :
624 + strbuf_getline;
625 + struct strbuf buf = STRBUF_INIT;
626 + struct strbuf unquoted = STRBUF_INIT;
627 + FILE *in;
628 +
629 + if (!strcmp(file, "-"))
630 + in = stdin;
631 + else
632 + in = xfopen(file, "r");
633 +
634 + while (getline_fn(&buf, in) != EOF) {
635 + if (!nul_term_line && buf.buf[0] == '"') {
636 + strbuf_reset(&unquoted);
637 + if (unquote_c_style(&unquoted, buf.buf, NULL))
638 + die(_("line is badly quoted: %s"), buf.buf);
639 + strbuf_swap(&buf, &unquoted);
640 + }
641 + argv_array_push(&parsed_file, buf.buf);
642 + strbuf_reset(&buf);
643 + }
644 +
645 + strbuf_release(&unquoted);
646 + strbuf_release(&buf);
647 + if (in != stdin)
648 + fclose(in);
649 +
650 + parse_pathspec(pathspec, magic_mask, flags, prefix, parsed_file.argv);
651 + argv_array_clear(&parsed_file);
652 +}
653 +
654 void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
655 {
656 int i, j;
pathspec.h
+10
@@ -85,6 +85,16 @@ void parse_pathspec(struct pathspec *pathspec,
85 unsigned flags,
86 const char *prefix,
87 const char **args);
88 +/*
89 + * Same as parse_pathspec() but uses file as input.
90 + * When 'file' is exactly "-" it uses 'stdin' instead.
91 + */
92 +void parse_pathspec_file(struct pathspec *pathspec,
93 + unsigned magic_mask,
94 + unsigned flags,
95 + const char *prefix,
96 + const char *file,
97 + int nul_term_line);
98 void copy_pathspec(struct pathspec *dst, const struct pathspec *src);
99 void clear_pathspec(struct pathspec *);
100