Skip to content

Commit 9384306

Browse files
klusarkdscho
authored andcommitted
stash: convert branch to builtin
Add stash branch to the helper and delete the apply_to_branch function from the shell script. Checkout does not currently provide a function for checking out a branch as cmd_checkout does a large amount of sanity checks first that we require here. Signed-off-by: Joel Teichroeb <[email protected]> Signed-off-by: Paul-Sebastian Ungureanu <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3b1c037 commit 9384306

File tree

2 files changed

+48
-15
lines changed

2 files changed

+48
-15
lines changed

builtin/stash--helper.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
static const char * const git_stash_helper_usage[] = {
1616
N_("git stash--helper drop [-q|--quiet] [<stash>]"),
1717
N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
18+
N_("git stash--helper branch <branchname> [<stash>]"),
1819
N_("git stash--helper clear"),
1920
NULL
2021
};
@@ -29,6 +30,11 @@ static const char * const git_stash_helper_apply_usage[] = {
2930
NULL
3031
};
3132

33+
static const char * const git_stash_helper_branch_usage[] = {
34+
N_("git stash--helper branch <branchname> [<stash>]"),
35+
NULL
36+
};
37+
3238
static const char * const git_stash_helper_clear_usage[] = {
3339
N_("git stash--helper clear"),
3440
NULL
@@ -538,6 +544,44 @@ static int drop_stash(int argc, const char **argv, const char *prefix)
538544
return ret;
539545
}
540546

547+
static int branch_stash(int argc, const char **argv, const char *prefix)
548+
{
549+
int ret;
550+
const char *branch = NULL;
551+
struct stash_info info;
552+
struct child_process cp = CHILD_PROCESS_INIT;
553+
struct option options[] = {
554+
OPT_END()
555+
};
556+
557+
argc = parse_options(argc, argv, prefix, options,
558+
git_stash_helper_branch_usage, 0);
559+
560+
if (!argc) {
561+
fprintf_ln(stderr, _("No branch name specified"));
562+
return -1;
563+
}
564+
565+
branch = argv[0];
566+
567+
if (get_stash_info(&info, argc - 1, argv + 1))
568+
return -1;
569+
570+
cp.git_cmd = 1;
571+
argv_array_pushl(&cp.args, "checkout", "-b", NULL);
572+
argv_array_push(&cp.args, branch);
573+
argv_array_push(&cp.args, oid_to_hex(&info.b_commit));
574+
ret = run_command(&cp);
575+
if (!ret)
576+
ret = do_apply_stash(prefix, &info, 1, 0);
577+
if (!ret && info.is_stash_ref)
578+
ret = do_drop_stash(prefix, &info, 0);
579+
580+
free_stash_info(&info);
581+
582+
return ret;
583+
}
584+
541585
int cmd_stash__helper(int argc, const char **argv, const char *prefix)
542586
{
543587
pid_t pid = getpid();
@@ -564,6 +608,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
564608
return !!clear_stash(argc, argv, prefix);
565609
else if (!strcmp(argv[0], "drop"))
566610
return !!drop_stash(argc, argv, prefix);
611+
else if (!strcmp(argv[0], "branch"))
612+
return !!branch_stash(argc, argv, prefix);
567613

568614
usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
569615
git_stash_helper_usage, options);

git-stash.sh

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -615,20 +615,6 @@ drop_stash () {
615615
clear_stash
616616
}
617617

618-
apply_to_branch () {
619-
test -n "$1" || die "$(gettext "No branch name specified")"
620-
branch=$1
621-
shift 1
622-
623-
set -- --index "$@"
624-
assert_stash_like "$@"
625-
626-
git checkout -b $branch $REV^ &&
627-
apply_stash "$@" && {
628-
test -z "$IS_STASH_REF" || drop_stash "$@"
629-
}
630-
}
631-
632618
test "$1" = "-p" && set "push" "$@"
633619

634620
PARSE_CACHE='--not-parsed'
@@ -690,7 +676,8 @@ pop)
690676
;;
691677
branch)
692678
shift
693-
apply_to_branch "$@"
679+
cd "$START_DIR"
680+
git stash--helper branch "$@"
694681
;;
695682
*)
696683
case $# in

0 commit comments

Comments
 (0)