git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jeff King <peff@peff.net>
To: Yoichi Nakayama via GitGitGadget <gitgitgadget@gmail.com>
Cc: git@vger.kernel.org, Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
Subject: Re: [PATCH v4 2/2] git-jump: invoke emacs/emacsclient
Date: Tue, 22 Nov 2022 13:54:10 -0500	[thread overview]
Message-ID: <Y30a0ulfxyE7dnYi@coredump.intra.peff.net> (raw)
In-Reply-To: <2f0bffb484beccf58f2440ed5e2c04a1ba26e6c3.1669126703.git.gitgitgadget@gmail.com>

On Tue, Nov 22, 2022 at 02:18:23PM +0000, Yoichi Nakayama via GitGitGadget wrote:

> diff --git a/contrib/git-jump/git-jump b/contrib/git-jump/git-jump
> index babb3b5c68d..bfd759aa4b2 100755
> --- a/contrib/git-jump/git-jump
> +++ b/contrib/git-jump/git-jump
> @@ -26,6 +26,17 @@ open_editor() {
>  	eval "$editor -q \$1"
>  }
>  
> +open_emacs() {
> +	# Supported editor values are:
> +	# - emacs
> +	# - emacsclient
> +	# - emacsclient -t
> +	editor=`git var GIT_EDITOR`
> +	# Wait for completion of the asynchronously executed process
> +	# to avoid race conditions in case of "emacsclient".
> +	eval "$editor --eval \"(prog1 (switch-to-buffer-other-frame (compilation-start \\\"cat $@\\\" 'grep-mode)) (delete-other-windows) (while (get-buffer-process (current-buffer)) (sleep-for 0.1)) (select-frame-set-input-focus (selected-frame)))\""
> +}

Hmm, I know I suggested using a temporary file since "cat $tmpfile"
should be pretty safe. But it does still have problems if your tmp
directory has spaces. Or even other metacharacters, which I think will
be interpreted by the eval, since $@ is expanded in the outermost level
of the shell.

Those are fairly unlikely, but we could handle it. I think you'd need
something like:

	open_emacs() {
		quoted_args=
		for i in "$@"; do
			quoted_args="$quoted_args '$(printf %s "$i" | sed "s/'/'\\\\''/g")'"
		done
		eval "$editor --eval \"...\\\"cat \$quoted_args\\\"...\""
	}

which you can test with:

	cat >fake-emacs <<-\EOF
	#!/bin/sh
	echo "fake-emacs got args: "
	for i in "$@"; do
		echo "arg: $i"
	done
	EOF
	chmod +x fake-emacs

	editor=./fake-emacs
	open_emacs 'multiple args' 'with spaces'
	open_emacs '$dollar is ok because we use single-quotes'
	open_emacs "but 'single quotes' themselves need quoted"

Though it's possible you also need to be adding an extra layer of
quoting due to emacs parsing the string. So you'd probably need to
additionally escape double-quotes and backslashes, perhaps by changing
the sed invocation to:

  sed -e 's/\\/\\\\/g' \
      -e "s/'/'\\\\''/g" \
      -e 's/"/\\"/g'

Which is kind of horrific, but I think is bullet-proof.

Like I said, it's not that likely that somebody's tempfile path would
need all that (though spaces aren't totally out of the question,
especially on Windows). But...

If we have bullet-proof quoting, then you could go back to skipping the
tempfile for emacs, which avoids the race and sleep that you have here.

> @@ -98,4 +109,8 @@ tmp=`mktemp -t git-jump.XXXXXX` || exit 1
>  type "mode_$mode" >/dev/null 2>&1 || { usage >&2; exit 1; }
>  "mode_$mode" "$@" >"$tmp"
>  test -s "$tmp" || exit 0
> +if git var GIT_EDITOR | grep emacs >/dev/null; then
> +	open_emacs "$tmp"
> +	exit 0
> +fi
>  open_editor "$tmp"

If we are going to use a tempfile, this logic should probably get
stuffed into open_editor itself, like:

  open_editor() {
          editor=`git var GIT_EDITOR`
          case "$editor" in
          *emacs*)
                  ...do-the-emacs-thing...
          *)
                  # assume anything else is vi-compatible
                  eval "$editor -q \$1"
          esac
  }

but if you take the quoting suggestion above, then open_emacs() would
continue to be a top-level thing, before we even create the tempfile.

-Peff

  parent reply	other threads:[~2022-11-22 18:54 UTC|newest]

Thread overview: 68+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-19 14:02 [PATCH 0/2] git-jump: support Emacs Yoichi NAKAYAMA via GitGitGadget
2022-11-19 14:02 ` [PATCH 1/2] git-jump: add an optional argument 'stdout' Yoichi Nakayama via GitGitGadget
2022-11-19 14:02 ` [PATCH 2/2] git-jump: invoke emacsclient Yoichi Nakayama via GitGitGadget
2022-11-19 15:53   ` Eric Sunshine
2022-11-19 23:44     ` Yoichi Nakayama
2022-11-19 23:59       ` Eric Sunshine
2022-11-21  4:05         ` Yoichi Nakayama
2022-11-20  1:27 ` [PATCH v2 0/2] git-jump: support Emacs Yoichi NAKAYAMA via GitGitGadget
2022-11-20  1:27   ` [PATCH v2 1/2] git-jump: add an optional argument 'stdout' Yoichi Nakayama via GitGitGadget
2022-11-21  5:43     ` Junio C Hamano
2022-11-21 11:25       ` Yoichi Nakayama
2022-11-21 18:18       ` Jeff King
2022-11-20  1:27   ` [PATCH v2 2/2] git-jump: invoke emacsclient Yoichi Nakayama via GitGitGadget
2022-11-21 12:26   ` [PATCH v3 0/2] git-jump: support Emacs Yoichi NAKAYAMA via GitGitGadget
2022-11-21 12:26     ` [PATCH v3 1/2] git-jump: add an optional argument '--stdout' Yoichi Nakayama via GitGitGadget
2022-11-21 18:38       ` Jeff King
2022-11-21 23:38         ` Junio C Hamano
2022-11-22 13:00           ` Yoichi Nakayama
2022-11-22 18:23             ` Jeff King
2022-11-22 13:29         ` Yoichi Nakayama
2022-11-21 12:27     ` [PATCH v3 2/2] git-jump: invoke emacs/emacsclient Yoichi Nakayama via GitGitGadget
2022-11-21 18:50       ` Jeff King
2022-11-22 12:06         ` Yoichi Nakayama
2022-11-22 14:18     ` [PATCH v4 0/2] git-jump: support Emacs Yoichi NAKAYAMA via GitGitGadget
2022-11-22 14:18       ` [PATCH v4 1/2] git-jump: add an optional argument '--stdout' Yoichi Nakayama via GitGitGadget
2022-11-22 18:30         ` Jeff King
2022-11-22 14:18       ` [PATCH v4 2/2] git-jump: invoke emacs/emacsclient Yoichi Nakayama via GitGitGadget
2022-11-22 16:40         ` Phillip Wood
2022-11-23  5:01           ` Yoichi Nakayama
2022-11-22 18:54         ` Jeff King [this message]
2022-11-23  5:33           ` Yoichi Nakayama
2022-11-24  1:09             ` Jeff King
2022-11-24 12:32               ` Yoichi Nakayama
2022-11-24 12:58                 ` Yoichi Nakayama
2022-11-24 16:31                   ` Jeff King
2022-11-24 16:29                 ` Jeff King
2022-11-25  3:46                   ` Yoichi Nakayama
2022-11-23  7:04       ` [PATCH v5 0/3] git-jump: support Emacs Yoichi NAKAYAMA via GitGitGadget
2022-11-23  7:04         ` [PATCH v5 1/3] git-jump: add an optional argument '--stdout' Yoichi Nakayama via GitGitGadget
2022-11-23  7:04         ` [PATCH v5 2/3] git-jump: move valid-mode check earlier Jeff King via GitGitGadget
2022-11-23  7:04         ` [PATCH v5 3/3] git-jump: invoke emacs/emacsclient Yoichi Nakayama via GitGitGadget
2022-11-23 14:58           ` Phillip Wood
2022-11-23 21:54             ` Yoichi Nakayama
2022-11-24  1:11         ` [PATCH v5 0/3] git-jump: support Emacs Jeff King
2022-11-24  3:47         ` [PATCH v6 " Yoichi NAKAYAMA via GitGitGadget
2022-11-24  3:47           ` [PATCH v6 1/3] git-jump: add an optional argument '--stdout' Yoichi Nakayama via GitGitGadget
2022-11-24  3:47           ` [PATCH v6 2/3] git-jump: move valid-mode check earlier Jeff King via GitGitGadget
2022-11-24  3:47           ` [PATCH v6 3/3] git-jump: invoke emacs/emacsclient Yoichi Nakayama via GitGitGadget
2022-11-25  3:36           ` [PATCH v7 0/3] git-jump: support Emacs Yoichi NAKAYAMA via GitGitGadget
2022-11-25  3:36             ` [PATCH v7 1/3] git-jump: add an optional argument '--stdout' Yoichi Nakayama via GitGitGadget
2022-11-25  9:06               ` Ævar Arnfjörð Bjarmason
2022-11-27  0:31                 ` Yoichi Nakayama
2022-11-25  3:37             ` [PATCH v7 2/3] git-jump: move valid-mode check earlier Jeff King via GitGitGadget
2022-11-25  3:37             ` [PATCH v7 3/3] git-jump: invoke emacs/emacsclient Yoichi Nakayama via GitGitGadget
2022-11-25  8:55               ` Ævar Arnfjörð Bjarmason
2022-11-25 16:01                 ` Yoichi Nakayama
2022-11-25 23:52                   ` Ævar Arnfjörð Bjarmason
2022-11-28  5:10                     ` Jeff King
2022-11-28 10:54                       ` Ævar Arnfjörð Bjarmason
2022-11-28 15:38                         ` Yoichi Nakayama
2022-11-27  0:37                   ` Yoichi Nakayama
2022-11-27  1:18             ` [PATCH v8 0/3] git-jump: support Emacs Yoichi NAKAYAMA via GitGitGadget
2022-11-27  1:18               ` [PATCH v8 1/3] git-jump: add an optional argument '--stdout' Yoichi Nakayama via GitGitGadget
2022-11-27  1:18               ` [PATCH v8 2/3] git-jump: move valid-mode check earlier Jeff King via GitGitGadget
2022-11-27  1:18               ` [PATCH v8 3/3] git-jump: invoke emacs/emacsclient Yoichi Nakayama via GitGitGadget
2022-11-28 10:13               ` [PATCH v8 0/3] git-jump: support Emacs Phillip Wood
2022-11-28 23:35                 ` Junio C Hamano
2022-11-29 21:05                 ` Yoichi Nakayama

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=Y30a0ulfxyE7dnYi@coredump.intra.peff.net \
    --to=peff@peff.net \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    --cc=yoichi.nakayama@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).