Skip to content

Commit 6fe783f

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: Thomas Gummerer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4d733c4 commit 6fe783f

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
@@ -537,6 +543,44 @@ static int drop_stash(int argc, const char **argv, const char *prefix)
537543
return ret;
538544
}
539545

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

567613
usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
568614
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)