If we are outside a repo and have any arguments left after
option-parsing, `setup_revisions()` will try to do its job and
something like this will happen:

$ git shortlog v2.16.0..
BUG: environment.c:183: git environment hasn't been setup
Aborted (core dumped)

The usage is wrong, but we could obviously handle this better. Note that
commit abe549e179 (shortlog: do not require to run from inside a git
repository, 2008-03-14) explicitly enabled `git shortlog` to run from
outside a repo, since we do not need a repo for parsing data from stdin.

Disallow left-over arguments when run from outside a repo. Another
approach would be to disallow them when reading from stdin. However, our
logic is currently the other way round: we check the number of revisions
in order to decide whether we should read from stdin. (So yes, after
this patch, we will still silently ignore stdin for confused usage such
as `git log v2.15.0.. | git shortlog v2.16.0..`. But at least that does
not crash.)

Signed-off-by: Martin Ågren <martin.ag...@gmail.com>
---
 t/t4201-shortlog.sh | 5 +++++
 builtin/shortlog.c  | 6 ++++++
 2 files changed, 11 insertions(+)

diff --git a/t/t4201-shortlog.sh b/t/t4201-shortlog.sh
index da10478f5..78c5645a9 100755
--- a/t/t4201-shortlog.sh
+++ b/t/t4201-shortlog.sh
@@ -127,6 +127,11 @@ test_expect_success !MINGW 'shortlog can read --format=raw 
output' '
        test_cmp expect out
 '
 
+test_expect_success 'shortlog from non-git directory refuses revisions' '
+       test_must_fail env GIT_DIR=non-existing git shortlog HEAD 2>out &&
+       test_i18ngrep "no revisions can be given" out
+'
+
 test_expect_success 'shortlog should add newline when input line matches 
wraplen' '
        cat >expect <<\EOF &&
 A U Thor (2):
diff --git a/builtin/shortlog.c b/builtin/shortlog.c
index dc4af03fc..35e8c1ead 100644
--- a/builtin/shortlog.c
+++ b/builtin/shortlog.c
@@ -293,6 +293,12 @@ int cmd_shortlog(int argc, const char **argv, const char 
*prefix)
 parse_done:
        argc = parse_options_end(&ctx);
 
+       if (nongit && argc != 1) {
+               error(_("no revisions can be given when running "
+                       "from outside a repository"));
+               usage_with_options(shortlog_usage, options);
+       }
+
        if (setup_revisions(argc, argv, &rev, NULL) != 1) {
                error(_("unrecognized argument: %s"), argv[1]);
                usage_with_options(shortlog_usage, options);
-- 
2.16.2.246.ga4ee44448f

Reply via email to