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 <joel@teichroeb.net> Signed-off-by: Paul-Sebastian Ungureanu <ungureanupaulsebastian@gmail.com> Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Joel Teichroeb committed Feb 25, 2019 at 23:16 UTC 577c1995e8c8cb414a6beb430f220e1ded216a64
2 files changed +48 -15
builtin/stash--helper.c
+46
@@ -14,6 +14,7 @@
14 static const char * const git_stash_helper_usage[] = {
15 N_("git stash--helper drop [-q|--quiet] [<stash>]"),
16 N_("git stash--helper apply [--index] [-q|--quiet] [<stash>]"),
17 + N_("git stash--helper branch <branchname> [<stash>]"),
18 N_("git stash--helper clear"),
19 NULL
20 };
@@ -28,6 +29,11 @@ static const char * const git_stash_helper_apply_usage[] = {
29 NULL
30 };
31
32 +static const char * const git_stash_helper_branch_usage[] = {
33 + N_("git stash--helper branch <branchname> [<stash>]"),
34 + NULL
35 +};
36 +
37 static const char * const git_stash_helper_clear_usage[] = {
38 N_("git stash--helper clear"),
39 NULL
@@ -536,6 +542,44 @@ static int drop_stash(int argc, const char **argv, const char *prefix)
542 return ret;
543 }
544
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 +
583 int cmd_stash__helper(int argc, const char **argv, const char *prefix)
584 {
585 pid_t pid = getpid();
@@ -562,6 +606,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
606 return !!clear_stash(argc, argv, prefix);
607 else if (!strcmp(argv[0], "drop"))
608 return !!drop_stash(argc, argv, prefix);
609 + else if (!strcmp(argv[0], "branch"))
610 + return !!branch_stash(argc, argv, prefix);
611
612 usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
613 git_stash_helper_usage, options);
git-stash.sh
+2 -15
@@ -615,20 +615,6 @@ drop_stash () {
615 clear_stash
616 }
617
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 -
618 test "$1" = "-p" && set "push" "$@"
619
620 PARSE_CACHE='--not-parsed'
@@ -690,7 +676,8 @@ pop)
676 ;;
677 branch)
678 shift
693 - apply_to_branch "$@"
679 + cd "$START_DIR"
680 + git stash--helper branch "$@"
681 ;;
682 *)
683 case $# in