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

Re: [PATCH] mergetool: Remove explicit references to /dev/tty

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Aug 20, 2010, 12:27 UTC
Message-ID
<20100820122724.GS10407@burratino>
In-Reply-To
<1282303049-11201-1-git-send-email-charles@hashpling.org>
Charles Bailey wrote:
Show 6 quoted lines
> mergetool used /dev/tty to switch back to receiving input from the user
> via inside a block with a redirected stdin.
> 
> This harms testability, so change mergetool to save its original stdin
> to an alternative fd in this block and restore it for those sub-commands
> that need the original stdin.
Sounds good.
Show 7 quoted lines
> +++ b/git-mergetool--lib.sh
> @@ -35,7 +35,7 @@ check_unchanged () {
>  		while true; do
>  			echo "$MERGED seems unchanged."
>  			printf "Was the merge successful? [y/n] "
> -			read answer < /dev/tty
> +			read answer

Part of the run_merge_tool codepath. The only place this is called with TOOL_MODE=merge is by merge_file which has stdin redirected, so this should be safe. Good.

Show 8 quoted lines
> +++ b/git-mergetool.sh
> @@ -292,14 +292,15 @@ if test $# -eq 0 ; then
>      printf "Merging:\n"
>      printf "$files\n"
>  
> -    files_to_merge |
> +    # Save original stdin to fd 3
> +    files_to_merge 3<&0 |

I would think this should work, but it doesn't feel idiomatic. Why not save stdin a little earlier, so the reader does not have to track down whether it has been redirected?

The test quietly passes for me with dash but fails with ksh:
 /home/jrn/src/git4/git-mergetool: line 303: 3: cannot open [Bad file descriptor]

With the patch below on top, it passes with dash and ksh. ---

diff --git a/git-mergetool.sh b/git-mergetool.sh
index 84edf7d..2e82522 100755
--- a/git-mergetool.sh
+++ b/git-mergetool.sh
@@ -275,10 +275,13 @@ files_to_merge() {
     fi
 }
 
 
 if test $# -eq 0 ; then
     cd_to_toplevel
 
+    # Save original stdin
+    exec 3<&0
+
     if test -e "$GIT_DIR/MERGE_RR"
     then
 	rerere=true
@@ -292,8 +294,7 @@ if test $# -eq 0 ; then
     printf "Merging:\n"
     printf "$files\n"
 
-    # Save original stdin to fd 3
-    files_to_merge 3<&0 |
+    files_to_merge |
     while IFS= read i
     do
 	if test $last_status -ne 0; then
Previous: Charles BaileyNext: Charles Bailey
Message 13 of 16 in “Status of conflicted files resolved with rerere”
  1. Magnus BäckAug 12, 2010
  2. Avery PennarunAug 12, 2010
  3. Jay SoffianAug 13, 2010
  4. David AguilarAug 15, 2010
  5. Junio C HamanoAug 15, 2010
  6. Magnus BäckAug 15, 2010
  7. mergetool: Skip autoresolved pathsDavid Aguilar, Aug 17, 2010
  8. Thomas RastAug 19, 2010
  9. David AguilarAug 20, 2010
  10. Charles BaileyAug 20, 2010
  11. Jonathan NiederAug 20, 2010
  12. mergetool: Remove explicit references to /dev/ttyCharles Bailey, Aug 20, 2010
  13. Jonathan NiederAug 20, 2010
  14. Charles BaileyAug 20, 2010
  15. Jonathan NiederAug 20, 2010
  16. mergetool: Remove explicit references to /dev/ttyCharles Bailey, Aug 20, 2010

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.