Skip to content

Commit 684ee99

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]>
1 parent 75ad6d7 commit 684ee99

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
@@ -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
@@ -598,20 +598,6 @@ drop_stash () {
598598
clear_stash
599599
}
600600

601-
apply_to_branch () {
602-
test -n "$1" || die "$(gettext "No branch name specified")"
603-
branch=$1
604-
shift 1
605-
606-
set -- --index "$@"
607-
assert_stash_like "$@"
608-
609-
git checkout -b $branch $REV^ &&
610-
apply_stash "$@" && {
611-
test -z "$IS_STASH_REF" || drop_stash "$@"
612-
}
613-
}
614-
615601
test "$1" = "-p" && set "push" "$@"
616602

617603
PARSE_CACHE='--not-parsed'
@@ -673,7 +659,8 @@ pop)
673659
;;
674660
branch)
675661
shift
676-
apply_to_branch "$@"
662+
cd "$START_DIR"
663+
git stash--helper branch "$@"
677664
;;
678665
*)
679666
case $# in

0 commit comments

Comments
 (0)