Skip to content

Commit bef7e56

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: Thomas Gummerer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1e3b9c2 commit bef7e56

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
@@ -645,6 +653,83 @@ static int list_stash(int argc, const char **argv, const char *prefix)
645653
return run_command(&cp);
646654
}
647655

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

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