Skip to content

Commit 348e437

Browse files
dschoGit for Windows Build Agent
authored andcommitted
reset: support the experimental --stdin option
Just like with other Git commands, this option makes it read the paths from the standard input. It comes in handy when resetting many, many paths at once and wildcards are not an option (e.g. when the paths are generated by a tool). Note: we first parse the entire list and perform the actual reset action only in a second phase. Not only does this make things simpler, it also helps performance, as do_diff_cache() traverses the index and the (sorted) pathspecs in simultaneously to avoid unnecessary lookups. This feature is marked experimental because it is still under review in the upstream Git project. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent b712994 commit 348e437

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

Documentation/git-reset.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ SYNOPSIS
1010
[verse]
1111
'git reset' [-q] [<tree-ish>] [--] <paths>...
1212
'git reset' (--patch | -p) [<tree-ish>] [--] [<paths>...]
13+
EXPERIMENTAL: 'git reset' [-q] [--stdin [-z]] [<tree-ish>]
1314
'git reset' [--soft | --mixed [-N] | --hard | --merge | --keep] [-q] [<commit>]
1415

1516
DESCRIPTION
@@ -97,6 +98,15 @@ OPTIONS
9798
--quiet::
9899
Be quiet, only report errors.
99100

101+
--stdin::
102+
EXPERIMENTAL: Instead of taking list of paths from the
103+
command line, read list of paths from the standard input.
104+
Paths are separated by LF (i.e. one path per line) by
105+
default.
106+
107+
-z::
108+
EXPERIMENTAL: Only meaningful with `--stdin`; paths are
109+
separated with NUL character instead of LF.
100110

101111
EXAMPLES
102112
--------

builtin/reset.c

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@
2424
#include "cache-tree.h"
2525
#include "submodule.h"
2626
#include "submodule-config.h"
27+
#include "strbuf.h"
28+
#include "quote.h"
2729

2830
static const char * const git_reset_usage[] = {
2931
N_("git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]"),
3032
N_("git reset [-q] [<tree-ish>] [--] <paths>..."),
33+
N_("EXPERIMENTAL: git reset [-q] [--stdin [-z]] [<tree-ish>]"),
3134
N_("git reset --patch [<tree-ish>] [--] [<paths>...]"),
3235
NULL
3336
};
@@ -288,7 +291,9 @@ static int git_reset_config(const char *var, const char *value, void *cb)
288291
int cmd_reset(int argc, const char **argv, const char *prefix)
289292
{
290293
int reset_type = NONE, update_ref_status = 0, quiet = 0;
291-
int patch_mode = 0, unborn;
294+
int patch_mode = 0, nul_term_line = 0, read_from_stdin = 0, unborn;
295+
char **stdin_paths = NULL;
296+
int stdin_nr = 0, stdin_alloc = 0;
292297
const char *rev;
293298
struct object_id oid;
294299
struct pathspec pathspec;
@@ -310,6 +315,10 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
310315
OPT_BOOL('p', "patch", &patch_mode, N_("select hunks interactively")),
311316
OPT_BOOL('N', "intent-to-add", &intent_to_add,
312317
N_("record only the fact that removed paths will be added later")),
318+
OPT_BOOL('z', NULL, &nul_term_line,
319+
N_("EXPERIMENTAL: paths are separated with NUL character")),
320+
OPT_BOOL(0, "stdin", &read_from_stdin,
321+
N_("EXPERIMENTAL: read paths from <stdin>")),
313322
OPT_END()
314323
};
315324

@@ -319,6 +328,42 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
319328
PARSE_OPT_KEEP_DASHDASH);
320329
parse_args(&pathspec, argv, prefix, patch_mode, &rev);
321330

331+
if (read_from_stdin) {
332+
strbuf_getline_fn getline_fn = nul_term_line ?
333+
strbuf_getline_nul : strbuf_getline_lf;
334+
int flags = PATHSPEC_PREFER_FULL;
335+
struct strbuf buf = STRBUF_INIT;
336+
struct strbuf unquoted = STRBUF_INIT;
337+
338+
if (patch_mode)
339+
die(_("--stdin is incompatible with --patch"));
340+
341+
if (pathspec.nr)
342+
die(_("--stdin is incompatible with path arguments"));
343+
344+
while (getline_fn(&buf, stdin) != EOF) {
345+
if (!nul_term_line && buf.buf[0] == '"') {
346+
strbuf_reset(&unquoted);
347+
if (unquote_c_style(&unquoted, buf.buf, NULL))
348+
die(_("line is badly quoted"));
349+
strbuf_swap(&buf, &unquoted);
350+
}
351+
ALLOC_GROW(stdin_paths, stdin_nr + 1, stdin_alloc);
352+
stdin_paths[stdin_nr++] = xstrdup(buf.buf);
353+
strbuf_reset(&buf);
354+
}
355+
strbuf_release(&unquoted);
356+
strbuf_release(&buf);
357+
358+
ALLOC_GROW(stdin_paths, stdin_nr + 1, stdin_alloc);
359+
stdin_paths[stdin_nr++] = NULL;
360+
flags |= PATHSPEC_LITERAL_PATH;
361+
parse_pathspec(&pathspec, 0, flags, prefix,
362+
(const char **)stdin_paths);
363+
364+
} else if (nul_term_line)
365+
die(_("-z requires --stdin"));
366+
322367
unborn = !strcmp(rev, "HEAD") && get_oid("HEAD", &oid);
323368
if (unborn) {
324369
/* reset on unborn branch: treat as reset to empty tree */
@@ -409,5 +454,11 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
409454
if (!pathspec.nr)
410455
remove_branch_state();
411456

457+
if (stdin_paths) {
458+
while (stdin_nr)
459+
free(stdin_paths[--stdin_nr]);
460+
free(stdin_paths);
461+
}
462+
412463
return update_ref_status;
413464
}

t/t7108-reset-stdin.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/sh
2+
3+
test_description='reset --stdin'
4+
5+
. ./test-lib.sh
6+
7+
test_expect_success 'reset --stdin' '
8+
test_commit hello &&
9+
git rm hello.t &&
10+
test -z "$(git ls-files hello.t)" &&
11+
echo hello.t | git reset --stdin &&
12+
test hello.t = "$(git ls-files hello.t)"
13+
'
14+
15+
test_expect_success 'reset --stdin -z' '
16+
test_commit world &&
17+
git rm hello.t world.t &&
18+
test -z "$(git ls-files hello.t world.t)" &&
19+
printf world.tQworld.tQhello.tQ | q_to_nul | git reset --stdin -z &&
20+
printf "hello.t\nworld.t\n" >expect &&
21+
git ls-files >actual &&
22+
test_cmp expect actual
23+
'
24+
25+
test_expect_success '--stdin requires --mixed' '
26+
echo hello.t >list &&
27+
test_must_fail git reset --soft --stdin <list &&
28+
test_must_fail git reset --hard --stdin <list &&
29+
git reset --mixed --stdin <list
30+
'
31+
32+
test_done

0 commit comments

Comments
 (0)