Skip to content

Commit 577c199

Browse files
klusarkgitster
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 4e2dd39 commit 577c199

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
@@ -14,6 +14,7 @@
1414
static const char * const git_stash_helper_usage[] = {
1515
N_("git stash--helper drop [-q|--quiet] [<stash>]"),
1616
N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
17+
N_("git stash--helper branch <branchname> [<stash>]"),
1718
N_("git stash--helper clear"),
1819
NULL
1920
};
@@ -28,6 +29,11 @@ static const char * const git_stash_helper_apply_usage[] = {
2829
NULL
2930
};
3031

32+
static const char * const git_stash_helper_branch_usage[] = {
33+
N_("git stash--helper branch <branchname> [<stash>]"),
34+
NULL
35+
};
36+
3137
static const char * const git_stash_helper_clear_usage[] = {
3238
N_("git stash--helper clear"),
3339
NULL
@@ -536,6 +542,44 @@ static int drop_stash(int argc, const char **argv, const char *prefix)
536542
return ret;
537543
}
538544

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

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