Skip to content

Commit 30f5257

Browse files
committed
Merge branch 'rs/empty-reflog-check-fix'
The code to see if "git stash drop" can safely remove refs/stash has been made more carerful. * rs/empty-reflog-check-fix: stash: simplify reflog emptiness check
2 parents a643735 + 4f44c56 commit 30f5257

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)