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