Skip to content

Commit 4f44c56

Browse files
rscharfegitster
authored andcommitted
stash: simplify reflog emptiness check
Calling rev-parse to check if the drop subcommand removed the last stash and treating its failure as confirmation is fragile, as the command can fail for other reasons, e.g. because the system is out of memory. Directly check if the reflog is empty instead, which is more robust. Reported-by: Marek Mrva <[email protected]> Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 898f807 commit 4f44c56

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

builtin/stash.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -534,11 +534,22 @@ static int apply_stash(int argc, const char **argv, const char *prefix)
534534
return ret;
535535
}
536536

537+
static int reject_reflog_ent(struct object_id *ooid, struct object_id *noid,
538+
const char *email, timestamp_t timestamp, int tz,
539+
const char *message, void *cb_data)
540+
{
541+
return 1;
542+
}
543+
544+
static int reflog_is_empty(const char *refname)
545+
{
546+
return !for_each_reflog_ent(refname, reject_reflog_ent, NULL);
547+
}
548+
537549
static int do_drop_stash(struct stash_info *info, int quiet)
538550
{
539551
int ret;
540552
struct child_process cp_reflog = CHILD_PROCESS_INIT;
541-
struct child_process cp = CHILD_PROCESS_INIT;
542553

543554
/*
544555
* reflog does not provide a simple function for deleting refs. One will
@@ -559,19 +570,7 @@ static int do_drop_stash(struct stash_info *info, int quiet)
559570
info->revision.buf);
560571
}
561572

562-
/*
563-
* This could easily be replaced by get_oid, but currently it will throw
564-
* a fatal error when a reflog is empty, which we can not recover from.
565-
*/
566-
cp.git_cmd = 1;
567-
/* Even though --quiet is specified, rev-parse still outputs the hash */
568-
cp.no_stdout = 1;
569-
strvec_pushl(&cp.args, "rev-parse", "--verify", "--quiet", NULL);
570-
strvec_pushf(&cp.args, "%s@{0}", ref_stash);
571-
ret = run_command(&cp);
572-
573-
/* do_clear_stash if we just dropped the last stash entry */
574-
if (ret)
573+
if (reflog_is_empty(ref_stash))
575574
do_clear_stash();
576575

577576
return 0;

0 commit comments

Comments
 (0)