Skip to content

Commit a4efac6

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 ee5e4c5 commit a4efac6

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
@@ -10,9 +10,12 @@
1010
#include "run-command.h"
1111
#include "dir.h"
1212
#include "rerere.h"
13+
#include "revision.h"
14+
#include "log-tree.h"
1315

1416
static const char * const git_stash_helper_usage[] = {
1517
N_("git stash--helper list [<options>]"),
18+
N_("git stash--helper show [<options>] [<stash>]"),
1619
N_("git stash--helper drop [-q|--quiet] [<stash>]"),
1720
N_("git stash--helper ( pop | apply ) [--index] [-q|--quiet] [<stash>]"),
1821
N_("git stash--helper branch <branchname> [<stash>]"),
@@ -25,6 +28,11 @@ static const char * const git_stash_helper_list_usage[] = {
2528
NULL
2629
};
2730

31+
static const char * const git_stash_helper_show_usage[] = {
32+
N_("git stash--helper show [<options>] [<stash>]"),
33+
NULL
34+
};
35+
2836
static const char * const git_stash_helper_drop_usage[] = {
2937
N_("git stash--helper drop [-q|--quiet] [<stash>]"),
3038
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
@@ -378,35 +378,6 @@ save_stash () {
378378
fi
379379
}
380380

381-
have_stash () {
382-
git rev-parse --verify --quiet $ref_stash >/dev/null
383-
}
384-
385-
show_stash () {
386-
ALLOW_UNKNOWN_FLAGS=t
387-
assert_stash_like "$@"
388-
389-
if test -z "$FLAGS"
390-
then
391-
if test "$(git config --bool stash.showStat || echo true)" = "true"
392-
then
393-
FLAGS=--stat
394-
fi
395-
396-
if test "$(git config --bool stash.showPatch || echo false)" = "true"
397-
then
398-
FLAGS=${FLAGS}${FLAGS:+ }-p
399-
fi
400-
401-
if test -z "$FLAGS"
402-
then
403-
return 0
404-
fi
405-
fi
406-
407-
git diff ${FLAGS} $b_commit $w_commit
408-
}
409-
410381
show_help () {
411382
exec git help stash
412383
exit 1
@@ -448,107 +419,6 @@ show_help () {
448419
# * unknown flags were set and ALLOW_UNKNOWN_FLAGS is not "t"
449420
#
450421

451-
parse_flags_and_rev()
452-
{
453-
test "$PARSE_CACHE" = "$*" && return 0 # optimisation
454-
PARSE_CACHE="$*"
455-
456-
IS_STASH_LIKE=
457-
IS_STASH_REF=
458-
INDEX_OPTION=
459-
s=
460-
w_commit=
461-
b_commit=
462-
i_commit=
463-
u_commit=
464-
w_tree=
465-
b_tree=
466-
i_tree=
467-
u_tree=
468-
469-
FLAGS=
470-
REV=
471-
for opt
472-
do
473-
case "$opt" in
474-
-q|--quiet)
475-
GIT_QUIET=-t
476-
;;
477-
--index)
478-
INDEX_OPTION=--index
479-
;;
480-
--help)
481-
show_help
482-
;;
483-
-*)
484-
test "$ALLOW_UNKNOWN_FLAGS" = t ||
485-
die "$(eval_gettext "unknown option: \$opt")"
486-
FLAGS="${FLAGS}${FLAGS:+ }$opt"
487-
;;
488-
*)
489-
REV="${REV}${REV:+ }'$opt'"
490-
;;
491-
esac
492-
done
493-
494-
eval set -- $REV
495-
496-
case $# in
497-
0)
498-
have_stash || die "$(gettext "No stash entries found.")"
499-
set -- ${ref_stash}@{0}
500-
;;
501-
1)
502-
:
503-
;;
504-
*)
505-
die "$(eval_gettext "Too many revisions specified: \$REV")"
506-
;;
507-
esac
508-
509-
case "$1" in
510-
*[!0-9]*)
511-
:
512-
;;
513-
*)
514-
set -- "${ref_stash}@{$1}"
515-
;;
516-
esac
517-
518-
REV=$(git rev-parse --symbolic --verify --quiet "$1") || {
519-
reference="$1"
520-
die "$(eval_gettext "\$reference is not a valid reference")"
521-
}
522-
523-
i_commit=$(git rev-parse --verify --quiet "$REV^2") &&
524-
set -- $(git rev-parse "$REV" "$REV^1" "$REV:" "$REV^1:" "$REV^2:" 2>/dev/null) &&
525-
s=$1 &&
526-
w_commit=$1 &&
527-
b_commit=$2 &&
528-
w_tree=$3 &&
529-
b_tree=$4 &&
530-
i_tree=$5 &&
531-
IS_STASH_LIKE=t &&
532-
test "$ref_stash" = "$(git rev-parse --symbolic-full-name "${REV%@*}")" &&
533-
IS_STASH_REF=t
534-
535-
u_commit=$(git rev-parse --verify --quiet "$REV^3") &&
536-
u_tree=$(git rev-parse "$REV^3:" 2>/dev/null)
537-
}
538-
539-
is_stash_like()
540-
{
541-
parse_flags_and_rev "$@"
542-
test -n "$IS_STASH_LIKE"
543-
}
544-
545-
assert_stash_like() {
546-
is_stash_like "$@" || {
547-
args="$*"
548-
die "$(eval_gettext "'\$args' is not a stash-like commit")"
549-
}
550-
}
551-
552422
test "$1" = "-p" && set "push" "$@"
553423

554424
PARSE_CACHE='--not-parsed'
@@ -573,7 +443,7 @@ list)
573443
;;
574444
show)
575445
shift
576-
show_stash "$@"
446+
git stash--helper show "$@"
577447
;;
578448
save)
579449
shift

0 commit comments

Comments
 (0)