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
};
@@ -288,7 +291,9 @@ static int git_reset_config(const char *var, const char *value, void *cb)
288
291
int cmd_reset (int argc , const char * * argv , const char * prefix )
289
292
{
290
293
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 ;
292
297
const char * rev ;
293
298
struct object_id oid ;
294
299
struct pathspec pathspec ;
@@ -310,6 +315,10 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
310
315
OPT_BOOL ('p' , "patch" , & patch_mode , N_ ("select hunks interactively" )),
311
316
OPT_BOOL ('N' , "intent-to-add" , & intent_to_add ,
312
317
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>" )),
313
322
OPT_END ()
314
323
};
315
324
@@ -319,6 +328,42 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
319
328
PARSE_OPT_KEEP_DASHDASH );
320
329
parse_args (& pathspec , argv , prefix , patch_mode , & rev );
321
330
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
+
322
367
unborn = !strcmp (rev , "HEAD" ) && get_oid ("HEAD" , & oid );
323
368
if (unborn ) {
324
369
/* 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)
409
454
if (!pathspec .nr )
410
455
remove_branch_state ();
411
456
457
+ if (stdin_paths ) {
458
+ while (stdin_nr )
459
+ free (stdin_paths [-- stdin_nr ]);
460
+ free (stdin_paths );
461
+ }
462
+
412
463
return update_ref_status ;
413
464
}
0 commit comments