stash: convert show to builtin

Add stash show to the helper and delete the show_stash, have_stash, assert_stash_like, is_stash_like and parse_flags_and_rev functions from the shell script now that they are no longer needed. In shell version, although `git stash show` accepts `--index` and `--quiet` options, it ignores them. In C, both options are passed further to `git diff`. 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>

Paul-Sebastian Ungureanu committed Feb 25, 2019 at 23:16 UTC dc7bd382b1063303f4f45d243bff371899285acb
2 files changed +88 -131
builtin/stash--helper.c
+87
@@ -10,9 +10,12 @@
10 #include "run-command.h"
11 #include "dir.h"
12 #include "rerere.h"
13 +#include "revision.h"
14 +#include "log-tree.h"
15
16 static const char * const git_stash_helper_usage[] = {
17 N_("git stash--helper list [<options>]"),
18 + N_("git stash--helper show [<options>] [<stash>]"),
19 N_("git stash--helper drop [-q|--quiet] [<stash>]"),
20 N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
21 N_("git stash--helper branch <branchname> [<stash>]"),
@@ -25,6 +28,11 @@ static const char * const git_stash_helper_list_usage[] = {
28 NULL
29 };
30
31 +static const char * const git_stash_helper_show_usage[] = {
32 + N_("git stash--helper show [<options>] [<stash>]"),
33 + NULL
34 +};
35 +
36 static const char * const git_stash_helper_drop_usage[] = {
37 N_("git stash--helper drop [-q|--quiet] [<stash>]"),
38 NULL
@@ -644,6 +652,83 @@ static int list_stash(int argc, const char **argv, const char *prefix)
652 return run_command(&cp);
653 }
654
655 +static int show_stat = 1;
656 +static int show_patch;
657 +
658 +static int git_stash_config(const char *var, const char *value, void *cb)
659 +{
660 + if (!strcmp(var, "stash.showstat")) {
661 + show_stat = git_config_bool(var, value);
662 + return 0;
663 + }
664 + if (!strcmp(var, "stash.showpatch")) {
665 + show_patch = git_config_bool(var, value);
666 + return 0;
667 + }
668 + return git_default_config(var, value, cb);
669 +}
670 +
671 +static int show_stash(int argc, const char **argv, const char *prefix)
672 +{
673 + int i;
674 + int opts = 0;
675 + int ret = 0;
676 + struct stash_info info;
677 + struct rev_info rev;
678 + struct argv_array stash_args = ARGV_ARRAY_INIT;
679 + struct option options[] = {
680 + OPT_END()
681 + };
682 +
683 + init_diff_ui_defaults();
684 + git_config(git_diff_ui_config, NULL);
685 + init_revisions(&rev, prefix);
686 +
687 + for (i = 1; i < argc; i++) {
688 + if (argv[i][0] != '-')
689 + argv_array_push(&stash_args, argv[i]);
690 + else
691 + opts++;
692 + }
693 +
694 + ret = get_stash_info(&info, stash_args.argc, stash_args.argv);
695 + argv_array_clear(&stash_args);
696 + if (ret)
697 + return -1;
698 +
699 + /*
700 + * The config settings are applied only if there are not passed
701 + * any options.
702 + */
703 + if (!opts) {
704 + git_config(git_stash_config, NULL);
705 + if (show_stat)
706 + rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT;
707 +
708 + if (show_patch)
709 + rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
710 +
711 + if (!show_stat && !show_patch) {
712 + free_stash_info(&info);
713 + return 0;
714 + }
715 + }
716 +
717 + argc = setup_revisions(argc, argv, &rev, NULL);
718 + if (argc > 1) {
719 + free_stash_info(&info);
720 + usage_with_options(git_stash_helper_show_usage, options);
721 + }
722 +
723 + rev.diffopt.flags.recursive = 1;
724 + setup_diff_pager(&rev.diffopt);
725 + diff_tree_oid(&info.b_commit, &info.w_commit, "", &rev.diffopt);
726 + log_tree_diff_flush(&rev);
727 +
728 + free_stash_info(&info);
729 + return diff_result_code(&rev.diffopt, 0);
730 +}
731 +
732 int cmd_stash__helper(int argc, const char **argv, const char *prefix)
733 {
734 pid_t pid = getpid();
@@ -676,6 +761,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
761 return !!branch_stash(argc, argv, prefix);
762 else if (!strcmp(argv[0], "list"))
763 return !!list_stash(argc, argv, prefix);
764 + else if (!strcmp(argv[0], "show"))
765 + return !!show_stash(argc, argv, prefix);
766
767 usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
768 git_stash_helper_usage, options);
git-stash.sh
+1 -131
@@ -395,35 +395,6 @@ save_stash () {
395 fi
396 }
397
398 -have_stash () {
399 - git rev-parse --verify --quiet $ref_stash >/dev/null
400 -}
401 -
402 -show_stash () {
403 - ALLOW_UNKNOWN_FLAGS=t
404 - assert_stash_like "$@"
405 -
406 - if test -z "$FLAGS"
407 - then
408 - if test "$(git config --bool stash.showStat || echo true)" = "true"
409 - then
410 - FLAGS=--stat
411 - fi
412 -
413 - if test "$(git config --bool stash.showPatch || echo false)" = "true"
414 - then
415 - FLAGS=${FLAGS}${FLAGS:+ }-p
416 - fi
417 -
418 - if test -z "$FLAGS"
419 - then
420 - return 0
421 - fi
422 - fi
423 -
424 - git diff ${FLAGS} $b_commit $w_commit
425 -}
426 -
398 show_help () {
399 exec git help stash
400 exit 1
@@ -465,107 +436,6 @@ show_help () {
436 # * unknown flags were set and ALLOW_UNKNOWN_FLAGS is not "t"
437 #
438
468 -parse_flags_and_rev()
469 -{
470 - test "$PARSE_CACHE" = "$*" && return 0 # optimisation
471 - PARSE_CACHE="$*"
472 -
473 - IS_STASH_LIKE=
474 - IS_STASH_REF=
475 - INDEX_OPTION=
476 - s=
477 - w_commit=
478 - b_commit=
479 - i_commit=
480 - u_commit=
481 - w_tree=
482 - b_tree=
483 - i_tree=
484 - u_tree=
485 -
486 - FLAGS=
487 - REV=
488 - for opt
489 - do
490 - case "$opt" in
491 - -q|--quiet)
492 - GIT_QUIET=-t
493 - ;;
494 - --index)
495 - INDEX_OPTION=--index
496 - ;;
497 - --help)
498 - show_help
499 - ;;
500 - -*)
501 - test "$ALLOW_UNKNOWN_FLAGS" = t ||
502 - die "$(eval_gettext "unknown option: \$opt")"
503 - FLAGS="${FLAGS}${FLAGS:+ }$opt"
504 - ;;
505 - *)
506 - REV="${REV}${REV:+ }'$opt'"
507 - ;;
508 - esac
509 - done
510 -
511 - eval set -- $REV
512 -
513 - case $# in
514 - 0)
515 - have_stash || die "$(gettext "No stash entries found.")"
516 - set -- ${ref_stash}@{0}
517 - ;;
518 - 1)
519 - :
520 - ;;
521 - *)
522 - die "$(eval_gettext "Too many revisions specified: \$REV")"
523 - ;;
524 - esac
525 -
526 - case "$1" in
527 - *[!0-9]*)
528 - :
529 - ;;
530 - *)
531 - set -- "${ref_stash}@{$1}"
532 - ;;
533 - esac
534 -
535 - REV=$(git rev-parse --symbolic --verify --quiet "$1") || {
536 - reference="$1"
537 - die "$(eval_gettext "\$reference is not a valid reference")"
538 - }
539 -
540 - i_commit=$(git rev-parse --verify --quiet "$REV^2") &&
541 - set -- $(git rev-parse "$REV" "$REV^1" "$REV:" "$REV^1:" "$REV^2:" 2>/dev/null) &&
542 - s=$1 &&
543 - w_commit=$1 &&
544 - b_commit=$2 &&
545 - w_tree=$3 &&
546 - b_tree=$4 &&
547 - i_tree=$5 &&
548 - IS_STASH_LIKE=t &&
549 - test "$ref_stash" = "$(git rev-parse --symbolic-full-name "${REV%@*}")" &&
550 - IS_STASH_REF=t
551 -
552 - u_commit=$(git rev-parse --verify --quiet "$REV^3") &&
553 - u_tree=$(git rev-parse "$REV^3:" 2>/dev/null)
554 -}
555 -
556 -is_stash_like()
557 -{
558 - parse_flags_and_rev "$@"
559 - test -n "$IS_STASH_LIKE"
560 -}
561 -
562 -assert_stash_like() {
563 - is_stash_like "$@" || {
564 - args="$*"
565 - die "$(eval_gettext "'\$args' is not a stash-like commit")"
566 - }
567 -}
568 -
439 test "$1" = "-p" && set "push" "$@"
440
441 PARSE_CACHE='--not-parsed'
@@ -590,7 +460,7 @@ list)
460 ;;
461 show)
462 shift
593 - show_stash "$@"
463 + git stash--helper show "$@"
464 ;;
465 save)
466 shift