git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
To: David Aguilar <davvid@gmail.com>
Cc: Junio C Hamano <gitster@pobox.com>, Git ML <git@vger.kernel.org>,
	Denton Liu <liu.denton@gmail.com>
Subject: Re: [PATCH] difftool: fix bug when printing usage
Date: Mon, 6 Feb 2017 17:58:49 +0100 (CET)	[thread overview]
Message-ID: <alpine.DEB.2.20.1702061716120.3496@virtualbox> (raw)
In-Reply-To: <20170205212338.17667-1-davvid@gmail.com>

Hi David,

On Sun, 5 Feb 2017, David Aguilar wrote:

> "git difftool -h" reports an error:
> 
> 	fatal: BUG: setup_git_env called without repository
> 
> Defer repository setup so that the help option processing happens before
> the repository is initialized.
> 
> Add tests to ensure that the basic usage works inside and outside of a
> repository.
> 
> Signed-off-by: David Aguilar <davvid@gmail.com>
> ---
> This bug exists in both "master" and "next".
> This patch has been tested on both branches.

Thanks for noticing and for patching!

> diff --git a/builtin/difftool.c b/builtin/difftool.c
> index b5e85ab079..d13350ce83 100644
> --- a/builtin/difftool.c
> +++ b/builtin/difftool.c
> @@ -647,10 +647,6 @@ int cmd_difftool(int argc, const char **argv, const char *prefix)
>  		OPT_END()
>  	};
>  
> -	/* NEEDSWORK: once we no longer spawn anything, remove this */
> -	setenv(GIT_DIR_ENVIRONMENT, absolute_path(get_git_dir()), 1);
> -	setenv(GIT_WORK_TREE_ENVIRONMENT, absolute_path(get_git_work_tree()), 1);
> -
>  	git_config(difftool_config, NULL);
>  	symlinks = has_symlinks;
>  
> @@ -661,6 +657,10 @@ int cmd_difftool(int argc, const char **argv, const char *prefix)
>  	if (tool_help)
>  		return print_tool_help();
>  
> +	/* NEEDSWORK: once we no longer spawn anything, remove this */
> +	setenv(GIT_DIR_ENVIRONMENT, absolute_path(get_git_dir()), 1);
> +	setenv(GIT_WORK_TREE_ENVIRONMENT, absolute_path(get_git_work_tree()), 1);
> +
>  	if (use_gui_tool && diff_gui_tool && *diff_gui_tool)
>  		setenv("GIT_DIFF_TOOL", diff_gui_tool, 1);
>  	else if (difftool_cmd) {

Aaargh. You know, when you are used to code review systems where you can
easily increase the context of the diff hunks, static patches are...
tedious to review.

For the record: the context between those two hunks is:

       argc = parse_options(argc, argv, prefix, builtin_difftool_options,
                             builtin_difftool_usage, PARSE_OPT_KEEP_UNKNOWN |
                             PARSE_OPT_KEEP_DASHDASH);

> diff --git a/t/t7800-difftool.sh b/t/t7800-difftool.sh
> index aa0ef02597..21e2ac4ad6 100755
> --- a/t/t7800-difftool.sh
> +++ b/t/t7800-difftool.sh
> @@ -23,6 +23,19 @@ prompt_given ()
>  	test "$prompt" = "Launch 'test-tool' [Y/n]? branch"
>  }
>  
> +test_expect_success 'basic usage requires no repo' '
> +	lines=$(git difftool -h | grep ^usage: | wc -l) &&
> +	test "$lines" -eq 1 &&

It may be easier to debug future breakages if you wrote

	git difftool -h | grep ^usage: >output &&
	test_line_count = 1 output &&

or even better (changing the semantics now):

	test_expect_code 129 git difftool -h >output &&
	grep ^usage: output &&

> +	# create a ceiling directory to prevent Git from finding a repo
> +	mkdir -p not/repo &&
> +	ceiling="$PWD/not" &&
> +	lines=$(cd not/repo &&
> +		GIT_CEILING_DIRECTORIES="$ceiling" git difftool -h |
> +		grep ^usage: | wc -l) &&
> +	test "$lines" -eq 1 &&

Likewise, this would become

	GIT_CEILING_DIRECTORIES="$PWD/not" \
	test_expect_code 129 git -C not/repo difftool -h >output &&
	grep ^usage: output

> +	rmdir -p not/repo

I am not sure how portable `rmdir -p` is. Why not use

	test_when_finished rm -r not

instead?

But those are all really minor bike-sheddings, and I promised myself that
I would avoid those (as I find them rather pointless) unless there is at
least one real benefit. In this case, I think the result becomes more
readable:

-- snip --
diff --git a/t/t7800-difftool.sh b/t/t7800-difftool.sh
index 21e2ac4ad6d..7d7cb63d61e 100755
--- a/t/t7800-difftool.sh
+++ b/t/t7800-difftool.sh
@@ -24,16 +24,14 @@ prompt_given ()
 }
 
 test_expect_success 'basic usage requires no repo' '
-	lines=$(git difftool -h | grep ^usage: | wc -l) &&
-	test "$lines" -eq 1 &&
-	# create a ceiling directory to prevent Git from finding a repo
-	mkdir -p not/repo &&
-	ceiling="$PWD/not" &&
-	lines=$(cd not/repo &&
-		GIT_CEILING_DIRECTORIES="$ceiling" git difftool -h |
-		grep ^usage: | wc -l) &&
-	test "$lines" -eq 1 &&
-	rmdir -p not/repo
+	test_expect_code 129 git difftool -h >output &&
+	grep ^usage: output &&
+	# create a ceiling directory to prevent Git from finding a repo
+	mkdir -p not/repo &&
+	test_when_finished rm -r not &&
+	GIT_CEILING_DIRECTORIES="$PWD/not" \
+	test_expect_code 129 git -C not/repo difftool -h >output &&
+	grep ^usage: output
 '
 
 # Create a file on master and change it on branch
-- snap --

More importantly: When I read $PWD all kinds of alarm bells go off in my
head, as I immediately think of all the issues we have on Windows due to
Git's regression test using POSIX paths all over the place.

In this particular instance, it *does* work, magically, because the POSIX
path $PWD is automatically converted (by the MSYS2 runtime) into a Windows
path when git.exe is called.

Read: I actually tested this, in line with my resolution to avoid to
review merely patches (which would likely miss serious bugs) and instead
try to do full code reviews, including testing. That means that I will
review less, but better.

Insofar as I am the author of the builtin difftool:

Acked-by: Johannes Schindelin <johannes.schindelin@gmx.de>

Thanks,
Dscho

  reply	other threads:[~2017-02-06 16:59 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-04  2:56 [PATCH] Remove --no-gui option from difftool usage string Denton Liu
2017-02-04  5:58 ` Jacob Keller
2017-02-04  6:23   ` Denton Liu
2017-02-05 10:22     ` David Aguilar
2017-02-05 20:17 ` [BUG] was: " David Aguilar
2017-02-05 21:23   ` [PATCH] difftool: fix bug when printing usage David Aguilar
2017-02-06 16:58     ` Johannes Schindelin [this message]
2017-02-06 18:13       ` Junio C Hamano
2017-02-07 11:21         ` Johannes Schindelin
2017-02-07 20:03           ` Junio C Hamano
2017-02-07 20:06             ` Junio C Hamano
2017-02-08 15:06               ` Johannes Schindelin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.DEB.2.20.1702061716120.3496@virtualbox \
    --to=johannes.schindelin@gmx.de \
    --cc=davvid@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=liu.denton@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).