21
21
#include "parse-options.h"
22
22
#include "unpack-trees.h"
23
23
#include "cache-tree.h"
24
+ #include "strbuf.h"
25
+ #include "quote.h"
24
26
25
27
static const char * const git_reset_usage [] = {
26
28
N_ ("git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]" ),
27
- N_ ("git reset [-q] <tree-ish> [--] <paths>..." ),
29
+ N_ ("git reset [-q] [<tree-ish>] [--] <paths>..." ),
30
+ N_ ("EXPERIMENTAL: git reset [-q] [--stdin [-z]] [<tree-ish>]" ),
28
31
N_ ("git reset --patch [<tree-ish>] [--] [<paths>...]" ),
29
32
NULL
30
33
};
@@ -267,7 +270,9 @@ static int reset_refs(const char *rev, const unsigned char *sha1)
267
270
int cmd_reset (int argc , const char * * argv , const char * prefix )
268
271
{
269
272
int reset_type = NONE , update_ref_status = 0 , quiet = 0 ;
270
- int patch_mode = 0 , unborn ;
273
+ int patch_mode = 0 , nul_term_line = 0 , read_from_stdin = 0 , unborn ;
274
+ char * * stdin_paths = NULL ;
275
+ int stdin_nr = 0 , stdin_alloc = 0 ;
271
276
const char * rev ;
272
277
struct object_id oid ;
273
278
struct pathspec pathspec ;
@@ -286,6 +291,10 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
286
291
OPT_BOOL ('p' , "patch" , & patch_mode , N_ ("select hunks interactively" )),
287
292
OPT_BOOL ('N' , "intent-to-add" , & intent_to_add ,
288
293
N_ ("record only the fact that removed paths will be added later" )),
294
+ OPT_BOOL ('z' , NULL , & nul_term_line ,
295
+ N_ ("EXPERIMENTAL: paths are separated with NUL character" )),
296
+ OPT_BOOL (0 , "stdin" , & read_from_stdin ,
297
+ N_ ("EXPERIMENTAL: read paths from <stdin>" )),
289
298
OPT_END ()
290
299
};
291
300
@@ -295,6 +304,43 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
295
304
PARSE_OPT_KEEP_DASHDASH );
296
305
parse_args (& pathspec , argv , prefix , patch_mode , & rev );
297
306
307
+ if (read_from_stdin ) {
308
+ strbuf_getline_fn getline_fn = nul_term_line ?
309
+ strbuf_getline_nul : strbuf_getline_lf ;
310
+ int flags = PATHSPEC_PREFER_FULL |
311
+ PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP ;
312
+ struct strbuf buf = STRBUF_INIT ;
313
+ struct strbuf unquoted = STRBUF_INIT ;
314
+
315
+ if (patch_mode )
316
+ die (_ ("--stdin is incompatible with --patch" ));
317
+
318
+ if (pathspec .nr )
319
+ die (_ ("--stdin is incompatible with path arguments" ));
320
+
321
+ while (getline_fn (& buf , stdin ) != EOF ) {
322
+ if (!nul_term_line && buf .buf [0 ] == '"' ) {
323
+ strbuf_reset (& unquoted );
324
+ if (unquote_c_style (& unquoted , buf .buf , NULL ))
325
+ die (_ ("line is badly quoted" ));
326
+ strbuf_swap (& buf , & unquoted );
327
+ }
328
+ ALLOC_GROW (stdin_paths , stdin_nr + 1 , stdin_alloc );
329
+ stdin_paths [stdin_nr ++ ] = xstrdup (buf .buf );
330
+ strbuf_reset (& buf );
331
+ }
332
+ strbuf_release (& unquoted );
333
+ strbuf_release (& buf );
334
+
335
+ ALLOC_GROW (stdin_paths , stdin_nr + 1 , stdin_alloc );
336
+ stdin_paths [stdin_nr ++ ] = NULL ;
337
+ flags |= PATHSPEC_LITERAL_PATH ;
338
+ parse_pathspec (& pathspec , 0 , flags , prefix ,
339
+ (const char * * )stdin_paths );
340
+
341
+ } else if (nul_term_line )
342
+ die (_ ("-z requires --stdin" ));
343
+
298
344
unborn = !strcmp (rev , "HEAD" ) && get_sha1 ("HEAD" , oid .hash );
299
345
if (unborn ) {
300
346
/* reset on unborn branch: treat as reset to empty tree */
@@ -385,5 +431,11 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
385
431
if (!pathspec .nr )
386
432
remove_branch_state ();
387
433
434
+ if (stdin_paths ) {
435
+ while (stdin_nr )
436
+ free (stdin_paths [-- stdin_nr ]);
437
+ free (stdin_paths );
438
+ }
439
+
388
440
return update_ref_status ;
389
441
}
0 commit comments