git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] git-difftool: allow skipping file by typing 'n' at prompt

From
Junio C Hamano <gitster@pobox.com>
Date
Oct 10, 2011, 20:56 UTC
Message-ID
<7v8voslg4l.fsf@alter.siamese.dyndns.org>
In-Reply-To
<20111008131015.GA28213@sita-lt.atc.tcs.com>
Sitaram Chamarty <sitaramc@gmail.com> writes:
Show 5 quoted lines
> However, I'm not sure the file names that 'git difftool'
> comes up with are in a predictable order.  That would mess
> up the test, but I can neither make it fail not find
> definitive information on the order in which the changed
> files are processed.
Hmm, that may be an issue, I would think.
Show 38 quoted lines
>  git-difftool--helper.sh |    9 +++++----
>  t/t7800-difftool.sh     |   44 +++++++++++++++++++++++++++++++++++++++++++-
>  2 files changed, 48 insertions(+), 5 deletions(-)
>
> diff --git a/git-difftool--helper.sh b/git-difftool--helper.sh
> index 8452890..0468446 100755
> --- a/git-difftool--helper.sh
> +++ b/git-difftool--helper.sh
> @@ -38,15 +38,16 @@ launch_merge_tool () {
>  
>  	# $LOCAL and $REMOTE are temporary files so prompt
>  	# the user with the real $MERGED name before launching $merge_tool.
> +	ans=y
>  	if should_prompt
>  	then
>  		printf "\nViewing: '$MERGED'\n"
>  		if use_ext_cmd
>  		then
> -			printf "Hit return to launch '%s': " \
> +			printf "Launch '%s' [Y/n]: " \
>  				"$GIT_DIFFTOOL_EXTCMD"
>  		else
> -			printf "Hit return to launch '%s': " "$merge_tool"
> +			printf "Launch '%s' [Y/n]: " "$merge_tool"
>  		fi
>  		read ans
>  	fi
> @@ -54,9 +55,9 @@ launch_merge_tool () {
>  	if use_ext_cmd
>  	then
>  		export BASE
> -		eval $GIT_DIFFTOOL_EXTCMD '"$LOCAL"' '"$REMOTE"'
> +		test "$ans" != "n" && eval $GIT_DIFFTOOL_EXTCMD '"$LOCAL"' '"$REMOTE"'
>  	else
> -		run_merge_tool "$merge_tool"
> +		test "$ans" != "n" && run_merge_tool "$merge_tool"
>  	fi
>  }

I also found suggestion by Charles Bailey to return from the launch function when the user says "no" easier to follow.

Show 22 quoted lines
> diff --git a/t/t7800-difftool.sh b/t/t7800-difftool.sh
> index 395adfc..f547e0b 100755
> --- a/t/t7800-difftool.sh
> +++ b/t/t7800-difftool.sh
> @@ -38,7 +38,18 @@ restore_test_defaults()
>  prompt_given()
>  {
>  	prompt="$1"
> -	test "$prompt" = "Hit return to launch 'test-tool': branch"
> +	test "$prompt" = "Launch 'test-tool' [Y/n]: branch"
> +}
> +
> +stdin_contains()
> +{
> +	grep >/dev/null "$1"
> +}
> +
> +stdin_doesnot_contain()
> +{
> +	grep >/dev/null "$1" && return 1
> +	return 0
>  }
Doesn't
	! grep >/dev/null "$1"
work in this case?        
I also wondered if this is easier to read:
	pipe | stdin_contains m2 &&
	! pipe | stdin_contains master
but I do not think it is (we cannot say "pipe | ! stdin_contains master").
In any case, here is what I ended up queuing.  Thanks.
-- >8 --
From: Sitaram Chamarty <sitaramc@gmail.com>
Date: Sat, 8 Oct 2011 18:40:15 +0530
Subject: [PATCH] git-difftool: allow skipping file by typing 'n' at prompt

This is useful if you forgot to restrict the diff to the paths you want to see, or selecting precisely the ones you want is too much typing.

[jc: with a change to return from the function upon 'n' by Charles Bailey and a small tweak in stdin_doesnot_contain() in the test]

Signed-off-by: Sitaram Chamarty <sitaram@atc.tcs.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 git-difftool--helper.sh |    9 ++++++---
 t/t7800-difftool.sh     |   43 ++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 48 insertions(+), 4 deletions(-)
diff --git a/git-difftool--helper.sh b/git-difftool--helper.sh
index 8452890..e6558d1 100755
--- a/git-difftool--helper.sh
+++ b/git-difftool--helper.sh
@@ -43,12 +43,15 @@ launch_merge_tool () {
 		printf "\nViewing: '$MERGED'\n"
 		if use_ext_cmd
 		then
-			printf "Hit return to launch '%s': " \
+			printf "Launch '%s' [Y/n]: " \
 				"$GIT_DIFFTOOL_EXTCMD"
 		else
-			printf "Hit return to launch '%s': " "$merge_tool"
+			printf "Launch '%s' [Y/n]: " "$merge_tool"
+		fi
+		if read ans && test "$ans" = n
+		then
+			return
 		fi
-		read ans
 	fi
 
 	if use_ext_cmd
diff --git a/t/t7800-difftool.sh b/t/t7800-difftool.sh
index 395adfc..7fc2b3a 100755
--- a/t/t7800-difftool.sh
+++ b/t/t7800-difftool.sh
@@ -38,7 +38,17 @@ restore_test_defaults()
 prompt_given()
 {
 	prompt="$1"
-	test "$prompt" = "Hit return to launch 'test-tool': branch"
+	test "$prompt" = "Launch 'test-tool' [Y/n]: branch"
+}
+
+stdin_contains()
+{
+	grep >/dev/null "$1"
+}
+
+stdin_doesnot_contain()
+{
+	! stdin_contains "$1"
 }
 
 # Create a file on master and change it on branch
@@ -265,4 +275,35 @@ test_expect_success PERL 'difftool --extcmd cat arg2' '
 	test "$diff" = branch
 '
 
+# Create a second file on master and a different version on branch
+test_expect_success PERL 'setup with 2 files different' '
+	echo m2 >file2 &&
+	git add file2 &&
+	git commit -m "added file2" &&
+
+	git checkout branch &&
+	echo br2 >file2 &&
+	git add file2 &&
+	git commit -a -m "branch changed file2" &&
+	git checkout master
+'
+
+test_expect_success PERL 'say no to the first file' '
+	diff=$((echo n; echo) | git difftool -x cat branch) &&
+
+	echo "$diff" | stdin_contains m2 &&
+	echo "$diff" | stdin_contains br2 &&
+	echo "$diff" | stdin_doesnot_contain master &&
+	echo "$diff" | stdin_doesnot_contain branch
+'
+
+test_expect_success PERL 'say no to the second file' '
+	diff=$((echo; echo n) | git difftool -x cat branch) &&
+
+	echo "$diff" | stdin_contains master &&
+	echo "$diff" | stdin_contains branch &&
+	echo "$diff" | stdin_doesnot_contain m2 &&
+	echo "$diff" | stdin_doesnot_contain br2
+'
+
 test_done
-- 
1.7.7.138.g7f41b6
Previous: Charles BaileyNext: Sitaram Chamarty
Message 13 of 14 in “git-difftool: allow skipping file by typing 'n' at prompt”
  1. git-difftool: allow skipping file by typing 'n' at promptSitaram Chamarty, Oct 4, 2011
  2. Junio C HamanoOct 4, 2011
  3. Jeff KingOct 4, 2011
  4. Phil HordOct 4, 2011
  5. Junio C HamanoOct 4, 2011
  6. Sitaram ChamartyOct 4, 2011
  7. git-difftool: allow skipping file by typing 'n' at promptSitaram Chamarty, Oct 6, 2011
  8. Junio C HamanoOct 6, 2011
  9. git-difftool: allow skipping file by typing 'n' at promptSitaram Chamarty, Oct 6, 2011
  10. Junio C HamanoOct 7, 2011
  11. git-difftool: allow skipping file by typing 'n' at promptSitaram Chamarty, Oct 8, 2011
  12. Charles BaileyOct 9, 2011
  13. Junio C HamanoOct 10, 2011
  14. Sitaram ChamartyOct 10, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.