Skip to content

Commit 7d20ff6

Browse files
ungpsdscho
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fd997ed commit 7d20ff6

File tree

2 files changed

+88
-131
lines changed

2 files changed

+88
-131
lines changed

builtin/stash--helper.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
#include "run-command.h"
1212
#include "dir.h"
1313
#include "rerere.h"
14+
#include "revision.h"
15+
#include "log-tree.h"
1416

1517
static const char * const git_stash_helper_usage[] = {
1618
N_("git stash--helper list [<options>]"),
19+
N_("git stash--helper show [<options>] [<stash>]"),
1720
N_("git stash--helper drop [-q|--quiet] [<stash>]"),
1821
N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
1922
N_("git stash--helper branch <branchname> [<stash>]"),
@@ -26,6 +29,11 @@ static const char * const git_stash_helper_list_usage[] = {
2629
NULL
2730
};
2831

32+
static const char * const git_stash_helper_show_usage[] = {
33+
N_("git stash--helper show [<options>] [<stash>]"),
34+
NULL
35+
};
36+
2937
static const char * const git_stash_helper_drop_usage[] = {
3038
N_("git stash--helper drop [-q|--quiet] [<stash>]"),
3139
NULL
@@ -646,6 +654,83 @@ static int list_stash(int argc, const char **argv, const char *prefix)
646654
return run_command(&cp);
647655
}
648656

657+
static int show_stat = 1;
658+
static int show_patch;
659+
660+
static int git_stash_config(const char *var, const char *value, void *cb)
661+
{
662+
if (!strcmp(var, "stash.showstat")) {
663+
show_stat = git_config_bool(var, value);
664+
return 0;
665+
}
666+
if (!strcmp(var, "stash.showpatch")) {
667+
show_patch = git_config_bool(var, value);
668+
return 0;
669+
}
670+
return git_default_config(var, value, cb);
671+
}
672+
673+
static int show_stash(int argc, const char **argv, const char *prefix)
674+
{
675+
int i;
676+
int opts = 0;
677+
int ret = 0;
678+
struct stash_info info;
679+
struct rev_info rev;
680+
struct argv_array stash_args = ARGV_ARRAY_INIT;
681+
struct option options[] = {
682+
OPT_END()
683+
};
684+
685+
init_diff_ui_defaults();
686+
git_config(git_diff_ui_config, NULL);
687+
init_revisions(&rev, prefix);
688+
689+
for (i = 1; i < argc; i++) {
690+
if (argv[i][0] != '-')
691+
argv_array_push(&stash_args, argv[i]);
692+
else
693+
opts++;
694+
}
695+
696+
ret = get_stash_info(&info, stash_args.argc, stash_args.argv);
697+
argv_array_clear(&stash_args);
698+
if (ret)
699+
return -1;
700+
701+
/*
702+
* The config settings are applied only if there are not passed
703+
* any options.
704+
*/
705+
if (!opts) {
706+
git_config(git_stash_config, NULL);
707+
if (show_stat)
708+
rev.diffopt.output_format = DIFF_FORMAT_DIFFSTAT;
709+
710+
if (show_patch)
711+
rev.diffopt.output_format |= DIFF_FORMAT_PATCH;
712+
713+
if (!show_stat && !show_patch) {
714+
free_stash_info(&info);
715+
return 0;
716+
}
717+
}
718+
719+
argc = setup_revisions(argc, argv, &rev, NULL);
720+
if (argc > 1) {
721+
free_stash_info(&info);
722+
usage_with_options(git_stash_helper_show_usage, options);
723+
}
724+
725+
rev.diffopt.flags.recursive = 1;
726+
setup_diff_pager(&rev.diffopt);
727+
diff_tree_oid(&info.b_commit, &info.w_commit, "", &rev.diffopt);
728+
log_tree_diff_flush(&rev);
729+
730+
free_stash_info(&info);
731+
return diff_result_code(&rev.diffopt, 0);
732+
}
733+
649734
int cmd_stash__helper(int argc, const char **argv, const char *prefix)
650735
{
651736
pid_t pid = getpid();
@@ -678,6 +763,8 @@ int cmd_stash__helper(int argc, const char **argv, const char *prefix)
678763
return !!branch_stash(argc, argv, prefix);
679764
else if (!strcmp(argv[0], "list"))
680765
return !!list_stash(argc, argv, prefix);
766+
else if (!strcmp(argv[0], "show"))
767+
return !!show_stash(argc, argv, prefix);
681768

682769
usage_msg_opt(xstrfmt(_("unknown subcommand: %s"), argv[0]),
683770
git_stash_helper_usage, options);

git-stash.sh

Lines changed: 1 addition & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -395,35 +395,6 @@ save_stash () {
395395
fi
396396
}
397397

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-
427398
show_help () {
428399
exec git help stash
429400
exit 1
@@ -465,107 +436,6 @@ show_help () {
465436
# * unknown flags were set and ALLOW_UNKNOWN_FLAGS is not "t"
466437
#
467438

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-
569439
test "$1" = "-p" && set "push" "$@"
570440

571441
PARSE_CACHE='--not-parsed'
@@ -590,7 +460,7 @@ list)
590460
;;
591461
show)
592462
shift
593-
show_stash "$@"
463+
git stash--helper show "$@"
594464
;;
595465
save)
596466
shift

0 commit comments

Comments
 (0)