24
24
#include "cache-tree.h"
25
25
#include "submodule.h"
26
26
#include "submodule-config.h"
27
+ #include "strbuf.h"
28
+ #include "quote.h"
27
29
28
30
static const char * const git_reset_usage [] = {
29
31
N_ ("git reset [--mixed | --soft | --hard | --merge | --keep] [-q] [<commit>]" ),
30
32
N_ ("git reset [-q] [<tree-ish>] [--] <paths>..." ),
33
+ N_ ("EXPERIMENTAL: git reset [-q] [--stdin [-z]] [<tree-ish>]" ),
31
34
N_ ("git reset --patch [<tree-ish>] [--] [<paths>...]" ),
32
35
NULL
33
36
};
@@ -277,7 +280,9 @@ static int git_reset_config(const char *var, const char *value, void *cb)
277
280
int cmd_reset (int argc , const char * * argv , const char * prefix )
278
281
{
279
282
int reset_type = NONE , update_ref_status = 0 , quiet = 0 ;
280
- int patch_mode = 0 , unborn ;
283
+ int patch_mode = 0 , nul_term_line = 0 , read_from_stdin = 0 , unborn ;
284
+ char * * stdin_paths = NULL ;
285
+ int stdin_nr = 0 , stdin_alloc = 0 ;
281
286
const char * rev ;
282
287
struct object_id oid ;
283
288
struct pathspec pathspec ;
@@ -299,6 +304,10 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
299
304
OPT_BOOL ('p' , "patch" , & patch_mode , N_ ("select hunks interactively" )),
300
305
OPT_BOOL ('N' , "intent-to-add" , & intent_to_add ,
301
306
N_ ("record only the fact that removed paths will be added later" )),
307
+ OPT_BOOL ('z' , NULL , & nul_term_line ,
308
+ N_ ("EXPERIMENTAL: paths are separated with NUL character" )),
309
+ OPT_BOOL (0 , "stdin" , & read_from_stdin ,
310
+ N_ ("EXPERIMENTAL: read paths from <stdin>" )),
302
311
OPT_END ()
303
312
};
304
313
@@ -310,6 +319,42 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
310
319
311
320
load_submodule_cache ();
312
321
322
+ if (read_from_stdin ) {
323
+ strbuf_getline_fn getline_fn = nul_term_line ?
324
+ strbuf_getline_nul : strbuf_getline_lf ;
325
+ int flags = PATHSPEC_PREFER_FULL ;
326
+ struct strbuf buf = STRBUF_INIT ;
327
+ struct strbuf unquoted = STRBUF_INIT ;
328
+
329
+ if (patch_mode )
330
+ die (_ ("--stdin is incompatible with --patch" ));
331
+
332
+ if (pathspec .nr )
333
+ die (_ ("--stdin is incompatible with path arguments" ));
334
+
335
+ while (getline_fn (& buf , stdin ) != EOF ) {
336
+ if (!nul_term_line && buf .buf [0 ] == '"' ) {
337
+ strbuf_reset (& unquoted );
338
+ if (unquote_c_style (& unquoted , buf .buf , NULL ))
339
+ die (_ ("line is badly quoted" ));
340
+ strbuf_swap (& buf , & unquoted );
341
+ }
342
+ ALLOC_GROW (stdin_paths , stdin_nr + 1 , stdin_alloc );
343
+ stdin_paths [stdin_nr ++ ] = xstrdup (buf .buf );
344
+ strbuf_reset (& buf );
345
+ }
346
+ strbuf_release (& unquoted );
347
+ strbuf_release (& buf );
348
+
349
+ ALLOC_GROW (stdin_paths , stdin_nr + 1 , stdin_alloc );
350
+ stdin_paths [stdin_nr ++ ] = NULL ;
351
+ flags |= PATHSPEC_LITERAL_PATH ;
352
+ parse_pathspec (& pathspec , 0 , flags , prefix ,
353
+ (const char * * )stdin_paths );
354
+
355
+ } else if (nul_term_line )
356
+ die (_ ("-z requires --stdin" ));
357
+
313
358
unborn = !strcmp (rev , "HEAD" ) && get_sha1 ("HEAD" , oid .hash );
314
359
if (unborn ) {
315
360
/* reset on unborn branch: treat as reset to empty tree */
@@ -400,5 +445,11 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
400
445
if (!pathspec .nr )
401
446
remove_branch_state ();
402
447
448
+ if (stdin_paths ) {
449
+ while (stdin_nr )
450
+ free (stdin_paths [-- stdin_nr ]);
451
+ free (stdin_paths );
452
+ }
453
+
403
454
return update_ref_status ;
404
455
}
0 commit comments