stripspace: allow -s/-c outside git repository
authorJonathan Nieder <jrnieder@gmail.com>
Mon, 17 Dec 2018 16:59:57 +0000 (08:59 -0800)
committerJunio C Hamano <gitster@pobox.com>
Wed, 26 Dec 2018 23:41:47 +0000 (15:41 -0800)
v2.11.0-rc3~3^2~1 (stripspace: respect repository config, 2016-11-21)
improved stripspace --strip-comments / --comentlines by teaching them
to read repository config, but it went a little too far: when running
stripspace outside any repository, the result is

$ git stripspace --strip-comments <test-input
fatal: not a git repository (or any parent up to mount point /tmp)

That makes experimenting with the stripspace command unnecessarily
fussy. Fix it by discovering the git directory gently, as intended
all along.

Reported-by: Han-Wen Nienhuys <hanwen@google.com>
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/stripspace.c
t/t0030-stripspace.sh
index bdf032886912bb768cf3528efbffab4fe3029a58..be33eb83c1b72664dffc17ddffd09f79b57a2887 100644 (file)
@@ -30,6 +30,7 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix)
 {
        struct strbuf buf = STRBUF_INIT;
        enum stripspace_mode mode = STRIP_DEFAULT;
+       int nongit;
 
        const struct option options[] = {
                OPT_CMDMODE('s', "strip-comments", &mode,
@@ -46,7 +47,7 @@ int cmd_stripspace(int argc, const char **argv, const char *prefix)
                usage_with_options(stripspace_usage, options);
 
        if (mode == STRIP_COMMENTS || mode == COMMENT_LINES) {
-               setup_git_directory_gently(NULL);
+               setup_git_directory_gently(&nongit);
                git_config(git_default_config, NULL);
        }
 
index 5ce47e8af51d63c7bd294e6f7582f10e4891566d..0c24a0f9a377103f49fa7cb749a5657058da3490 100755 (executable)
@@ -430,9 +430,15 @@ test_expect_success '-c with changed comment char' '
 test_expect_success '-c with comment char defined in .git/config' '
        test_config core.commentchar = &&
        printf "= foo\n" >expect &&
-       printf "foo" | (
-               mkdir sub && cd sub && git stripspace -c
-       ) >actual &&
+       rm -fr sub &&
+       mkdir sub &&
+       printf "foo" | git -C sub stripspace -c >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success '-c outside git repository' '
+       printf "# foo\n" >expect &&
+       printf "foo" | nongit git stripspace -c >actual &&
        test_cmp expect actual
 '