Skip to content

Commit a096466

Browse files
committed
Merge branch 'am/pathspec-from-file' into pu
A few commands learned to take the pathspec from the standard input or a named file, instead of taking it as the command line arguments. * am/pathspec-from-file: commit: support the --pathspec-from-file option doc: commit: unify <pathspec> description reset: support the `--pathspec-from-file` option doc: reset: unify <pathspec> description pathspec: add new function to parse file parse-options.h: add new options `--pathspec-from-file`, `--pathspec-file-nul`
2 parents 6e9ab2d + e6f4d1d commit a096466

File tree

9 files changed

+425
-26
lines changed

9 files changed

+425
-26
lines changed

Documentation/git-commit.txt

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ SYNOPSIS
1313
[-F <file> | -m <msg>] [--reset-author] [--allow-empty]
1414
[--allow-empty-message] [--no-verify] [-e] [--author=<author>]
1515
[--date=<date>] [--cleanup=<mode>] [--[no-]status]
16-
[-i | -o] [-S[<keyid>]] [--] [<file>...]
16+
[-i | -o] [--pathspec-from-file=<file> [--pathspec-file-nul]]
17+
[-S[<keyid>]] [--] [<pathspec>...]
1718

1819
DESCRIPTION
1920
-----------
@@ -278,6 +279,18 @@ FROM UPSTREAM REBASE" section in linkgit:git-rebase[1].)
278279
already been staged. If used together with `--allow-empty`
279280
paths are also not required, and an empty commit will be created.
280281

282+
--pathspec-from-file=<file>::
283+
Pathspec is passed in `<file>` instead of commandline args. If
284+
`<file>` is exactly `-` then standard input is used. Pathspec
285+
elements are separated by LF or CR/LF. Pathspec elements can be
286+
quoted as explained for the configuration variable `core.quotePath`
287+
(see linkgit:git-config[1]). See also `--pathspec-file-nul` and
288+
global `--literal-pathspecs`.
289+
290+
--pathspec-file-nul::
291+
Only meaningful with `--pathspec-from-file`. Pathspec elements are
292+
separated with NUL character and are not expected to be quoted.
293+
281294
-u[<mode>]::
282295
--untracked-files[=<mode>]::
283296
Show untracked files.
@@ -345,12 +358,14 @@ changes to tracked files.
345358
\--::
346359
Do not interpret any more arguments as options.
347360

348-
<file>...::
349-
When files are given on the command line, the command
350-
commits the contents of the named files, without
351-
recording the changes already staged. The contents of
352-
these files are also staged for the next commit on top
353-
of what have been staged before.
361+
<pathspec>...::
362+
When pathspec is given on the command line, commit the contents of
363+
the files that match the pathspec without recording the changes
364+
already added to the index. The contents of these files are also
365+
staged for the next commit on top of what have been staged before.
366+
+
367+
For more details about the <pathspec> syntax, see the 'pathspec' entry
368+
in linkgit:gitglossary[7].
354369

355370
:git-commit: 1
356371
include::date-formats.txt[]

Documentation/git-reset.txt

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,38 @@ git-reset - Reset current HEAD to the specified state
88
SYNOPSIS
99
--------
1010
[verse]
11-
'git reset' [-q] [<tree-ish>] [--] <paths>...
12-
'git reset' (--patch | -p) [<tree-ish>] [--] [<paths>...]
11+
'git reset' [-q] [<tree-ish>] [--] <pathspec>...
12+
'git reset' [-q] [--pathspec-from-file=<file> [--pathspec-file-nul]] [<tree-ish>]
13+
'git reset' (--patch | -p) [<tree-ish>] [--] [<pathspec>...]
1314
'git reset' [--soft | --mixed [-N] | --hard | --merge | --keep] [-q] [<commit>]
1415

1516
DESCRIPTION
1617
-----------
17-
In the first and second form, copy entries from `<tree-ish>` to the index.
18-
In the third form, set the current branch head (`HEAD`) to `<commit>`,
18+
In the first three forms, copy entries from `<tree-ish>` to the index.
19+
In the last form, set the current branch head (`HEAD`) to `<commit>`,
1920
optionally modifying index and working tree to match.
2021
The `<tree-ish>`/`<commit>` defaults to `HEAD` in all forms.
22+
The <pathspec> is used to limit the paths affected by the operation
23+
(see the entry for 'pathspec' in linkgit:gitglossary[7] for more details).
2124

22-
'git reset' [-q] [<tree-ish>] [--] <paths>...::
23-
This form resets the index entries for all `<paths>` to their
25+
'git reset' [-q] [<tree-ish>] [--] <pathspec>...::
26+
'git reset' [-q] [--pathspec-from-file=<file> [--pathspec-file-nul]] [<tree-ish>]::
27+
These forms reset the index entries matching pathspec to their
2428
state at `<tree-ish>`. (It does not affect the working tree or
2529
the current branch.)
2630
+
27-
This means that `git reset <paths>` is the opposite of `git add
28-
<paths>`. This command is equivalent to
29-
`git restore [--source=<tree-ish>] --staged <paths>...`.
31+
This means that `git reset <pathspec>` is the opposite of `git add
32+
<pathspec>`. This command is equivalent to
33+
`git restore [--source=<tree-ish>] --staged <pathspec>...`.
3034
+
31-
After running `git reset <paths>` to update the index entry, you can
35+
After running `git reset <pathspec>` to update the index entry, you can
3236
use linkgit:git-restore[1] to check the contents out of the index to
3337
the working tree. Alternatively, using linkgit:git-restore[1]
3438
and specifying a commit with `--source`, you
3539
can copy the contents of a path out of a commit to the index and to the
3640
working tree in one go.
3741

38-
'git reset' (--patch | -p) [<tree-ish>] [--] [<paths>...]::
42+
'git reset' (--patch | -p) [<tree-ish>] [--] [<pathspec>...]::
3943
Interactively select hunks in the difference between the index
4044
and `<tree-ish>` (defaults to `HEAD`). The chosen hunks are applied
4145
in reverse to the index.
@@ -101,6 +105,17 @@ OPTIONS
101105
`reset.quiet` config option. `--quiet` and `--no-quiet` will
102106
override the default behavior.
103107

108+
--pathspec-from-file=<file>::
109+
Pathspec is passed in `<file>` instead of commandline args. If
110+
`<file>` is exactly `-` then standard input is used. Pathspec
111+
elements are separated by LF or CR/LF. Pathspec elements can be
112+
quoted as explained for the configuration variable `core.quotePath`
113+
(see linkgit:git-config[1]). See also `--pathspec-file-nul` and
114+
global `--literal-pathspecs`.
115+
116+
--pathspec-file-nul::
117+
Only meaningful with `--pathspec-from-file`. Pathspec elements are
118+
separated with NUL character and are not expected to be quoted.
104119

105120
EXAMPLES
106121
--------

builtin/commit.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ static int all, also, interactive, patch_interactive, only, amend, signoff;
110110
static int edit_flag = -1; /* unspecified */
111111
static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
112112
static int config_commit_verbose = -1; /* unspecified */
113-
static int no_post_rewrite, allow_empty_message;
113+
static int no_post_rewrite, allow_empty_message, pathspec_file_nul;
114114
static char *untracked_files_arg, *force_date, *ignore_submodule_arg, *ignored_arg;
115-
static char *sign_commit;
115+
static char *sign_commit, *pathspec_from_file;
116116

117117
/*
118118
* The default commit message cleanup mode will remove the lines
@@ -348,6 +348,23 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
348348
PATHSPEC_PREFER_FULL,
349349
prefix, argv);
350350

351+
if (pathspec_from_file) {
352+
if (interactive)
353+
die(_("--pathspec-from-file is incompatible with --interactive/--patch"));
354+
355+
if (pathspec.nr)
356+
die(_("--pathspec-from-file is incompatible with pathspec arguments"));
357+
358+
parse_pathspec_file(&pathspec, 0,
359+
PATHSPEC_PREFER_FULL,
360+
prefix, pathspec_from_file, pathspec_file_nul);
361+
} else if (pathspec_file_nul) {
362+
die(_("--pathspec-file-nul requires --pathspec-from-file"));
363+
}
364+
365+
if (!pathspec.nr && (also || (only && !amend && !allow_empty)))
366+
die(_("No paths with --include/--only does not make sense."));
367+
351368
if (read_cache_preload(&pathspec) < 0)
352369
die(_("index file corrupt"));
353370

@@ -1211,8 +1228,6 @@ static int parse_and_validate_options(int argc, const char *argv[],
12111228

12121229
if (also + only + all + interactive > 1)
12131230
die(_("Only one of --include/--only/--all/--interactive/--patch can be used."));
1214-
if (argc == 0 && (also || (only && !amend && !allow_empty)))
1215-
die(_("No paths with --include/--only does not make sense."));
12161231
cleanup_mode = get_cleanup_mode(cleanup_arg, use_editor);
12171232

12181233
handle_untracked_files_arg(s);
@@ -1526,6 +1541,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
15261541
OPT_BOOL(0, "amend", &amend, N_("amend previous commit")),
15271542
OPT_BOOL(0, "no-post-rewrite", &no_post_rewrite, N_("bypass post-rewrite hook")),
15281543
{ OPTION_STRING, 'u', "untracked-files", &untracked_files_arg, N_("mode"), N_("show untracked files, optional modes: all, normal, no. (Default: all)"), PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
1544+
OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
1545+
OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
15291546
/* end commit contents options */
15301547

15311548
OPT_HIDDEN_BOOL(0, "allow-empty", &allow_empty,

builtin/reset.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@
3030

3131
static const char * const git_reset_usage[] = {
3232
N_("git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]"),
33-
N_("git reset [-q] [<tree-ish>] [--] <paths>..."),
34-
N_("git reset --patch [<tree-ish>] [--] [<paths>...]"),
33+
N_("git reset [-q] [<tree-ish>] [--] <pathspec>..."),
34+
N_("git reset [-q] [--pathspec-from-file [--pathspec-file-nul]] [<tree-ish>]"),
35+
N_("git reset --patch [<tree-ish>] [--] [<pathspec>...]"),
3536
NULL
3637
};
3738

@@ -285,8 +286,8 @@ static int git_reset_config(const char *var, const char *value, void *cb)
285286
int cmd_reset(int argc, const char **argv, const char *prefix)
286287
{
287288
int reset_type = NONE, update_ref_status = 0, quiet = 0;
288-
int patch_mode = 0, unborn;
289-
const char *rev;
289+
int patch_mode = 0, pathspec_file_nul = 0, unborn;
290+
const char *rev, *pathspec_from_file = NULL;
290291
struct object_id oid;
291292
struct pathspec pathspec;
292293
int intent_to_add = 0;
@@ -307,6 +308,8 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
307308
OPT_BOOL('p', "patch", &patch_mode, N_("select hunks interactively")),
308309
OPT_BOOL('N', "intent-to-add", &intent_to_add,
309310
N_("record only the fact that removed paths will be added later")),
311+
OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
312+
OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
310313
OPT_END()
311314
};
312315

@@ -317,6 +320,20 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
317320
PARSE_OPT_KEEP_DASHDASH);
318321
parse_args(&pathspec, argv, prefix, patch_mode, &rev);
319322

323+
if (pathspec_from_file) {
324+
if (patch_mode)
325+
die(_("--pathspec-from-file is incompatible with --patch"));
326+
327+
if (pathspec.nr)
328+
die(_("--pathspec-from-file is incompatible with pathspec arguments"));
329+
330+
parse_pathspec_file(&pathspec, 0,
331+
PATHSPEC_PREFER_FULL,
332+
prefix, pathspec_from_file, pathspec_file_nul);
333+
} else if (pathspec_file_nul) {
334+
die(_("--pathspec-file-nul requires --pathspec-from-file"));
335+
}
336+
320337
unborn = !strcmp(rev, "HEAD") && get_oid("HEAD", &oid);
321338
if (unborn) {
322339
/* reset on unborn branch: treat as reset to empty tree */

parse-options.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,5 +330,7 @@ int parse_opt_passthru_argv(const struct option *, const char *, int);
330330
#define OPT_WITH(v, h) _OPT_CONTAINS_OR_WITH("with", v, h, PARSE_OPT_HIDDEN | PARSE_OPT_NONEG)
331331
#define OPT_WITHOUT(v, h) _OPT_CONTAINS_OR_WITH("without", v, h, PARSE_OPT_HIDDEN | PARSE_OPT_NONEG)
332332
#define OPT_CLEANUP(v) OPT_STRING(0, "cleanup", v, N_("mode"), N_("how to strip spaces and #comments from message"))
333+
#define OPT_PATHSPEC_FROM_FILE(v) OPT_FILENAME(0, "pathspec-from-file", v, N_("read pathspec from file"))
334+
#define OPT_PATHSPEC_FILE_NUL(v) OPT_BOOL(0, "pathspec-file-nul", v, N_("with --pathspec-from-file, pathspec elements are separated with NUL character"))
333335

334336
#endif

pathspec.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "dir.h"
44
#include "pathspec.h"
55
#include "attr.h"
6+
#include "argv-array.h"
7+
#include "quote.h"
68

79
/*
810
* Finds which of the given pathspecs match items in the index.
@@ -613,6 +615,42 @@ void parse_pathspec(struct pathspec *pathspec,
613615
}
614616
}
615617

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 = NULL;
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+
616654
void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
617655
{
618656
int i, j;

pathspec.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ void parse_pathspec(struct pathspec *pathspec,
8585
unsigned flags,
8686
const char *prefix,
8787
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);
8898
void copy_pathspec(struct pathspec *dst, const struct pathspec *src);
8999
void clear_pathspec(struct pathspec *);
90100

0 commit comments

Comments
 (0)