git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Phillip Wood <phillip.wood123@gmail.com>
To: Yoichi Nakayama via GitGitGadget <gitgitgadget@gmail.com>,
	git@vger.kernel.org
Cc: Jeff King <peff@peff.net>, Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
Subject: Re: [PATCH v5 3/3] git-jump: invoke emacs/emacsclient
Date: Wed, 23 Nov 2022 14:58:27 +0000	[thread overview]
Message-ID: <feb7db00-db00-6190-47cf-9101052b9be8@dunelm.org.uk> (raw)
In-Reply-To: <ad7c299cb0f78ae3f36d57b67fa91e5ccaab3181.1669187053.git.gitgitgadget@gmail.com>

Hi Yoichi

On 23/11/2022 07:04, Yoichi Nakayama via GitGitGadget wrote:
> From: Yoichi Nakayama <yoichi.nakayama@gmail.com>
> 
> It works with GIT_EDITOR="emacs", "emacsclient" or "emacsclient -t"
> 
> Signed-off-by: Yoichi Nakayama <yoichi.nakayama@gmail.com>
> ---
>   contrib/git-jump/git-jump | 17 ++++++++++++++++-
>   1 file changed, 16 insertions(+), 1 deletion(-)
> 
> diff --git a/contrib/git-jump/git-jump b/contrib/git-jump/git-jump
> index cc97b0dcf02..316e9628725 100755
> --- a/contrib/git-jump/git-jump
> +++ b/contrib/git-jump/git-jump
> @@ -23,7 +23,22 @@ EOF
>   
>   open_editor() {
>   	editor=`git var GIT_EDITOR`
> -	eval "$editor -q \$1"
> +	case "$editor" in
> +	*emacs*)
> +		# Supported editor values are:
> +		# - emacs
> +		# - emacsclient
> +		# - emacsclient -t
> +		#
> +		# Wait for completion of the asynchronously executed process
> +		# to avoid race conditions in case of "emacsclient".
> +		eval "$editor --eval \"(prog1 (pop-to-buffer (compilation-start \\\"cat $@\\\" 'grep-mode)) (while (get-buffer-process (current-buffer)) (sleep-for 0.1)) (select-frame-set-input-focus (selected-frame)))\""

I've just tried this out and it is much nicer than v4, thank you for 
tweaking it. It is a little sluggish to pop up the emacs window though - 
are you sure we need the while loop? I've commented it out and it seems 
to work just fine. The documentation for pop-to-buffer says it selects 
the frame displaying the buffer so I don't think we need to wait before 
calling select-frame-set-input-focus (I'm no emacs expert though). I do 
think it would be better to quote the filename or better still call 
git-jump from compilation-start as Peff suggested. It would also be nice 
to stop emacsclient from printing anything in the terminal.

It would be nice to be able to run git-jump from within emacs. I came up 
with the code below which prompts the user for the directory to run 
git-jump in (which only matters for grep and diff --relative I think) 
and then checks for modified buffers visiting files in that repository 
before running git-jump.

Best Wishes

Phillip

---- >8 ----
(require 'cl-lib)

(defun git-jump (dir)
   "Run 'git jump', prompts for the directory to run in. Also prompts to
    save modified buffers visiting files in the repository containing DIR"
   (interactive "DDirectory:")
   (let* ((dir (expand-file-name dir))
	 (worktree (git-jump--get-worktree dir)))
     (unless worktree
       (error "Not in a git repository"))
     (git-jump--save-worktree-buffers worktree)
     ;; Use "cd" rather than "git -C" so emacs can tell which directory
     ;; the command is being run in.
     (compilation-start (concat "cd " (shell-quote-argument dir)
			       " && git jump --stdout "
			       (read-string "Jump command: "))
		       'grep-mode
		       (lambda (mode) "*git-jump*"))))


(defun git-jump--save-worktree-buffers (worktree)
   "Prompt the user to save all the modified buffers in WORKTREE"
   (let ((ht (make-hash-table :test 'equal))
	(off (length worktree))
	(buffers nil))
     (dolist (b (buffer-list))
       (when (buffer-modified-p b)
	(let ((file (buffer-file-name b)))
	  (when file
	    (let ((path (file-truename file)))
	      (when (string-prefix-p worktree path)
		(puthash (substring path off) b ht)))))))
     (let ((modified (hash-table-keys ht)))
       (when modified
	(git-jump--ls-files worktree
			       modified
			       (lambda (path)
				 (push (gethash path ht) buffers)))
	(when buffers
	  (save-some-buffers nil (lambda ()
				   (memq (current-buffer) buffers))))))))


(defun git-jump--get-worktree (dir)
   "Get the git worktree containing DIR. Returns nil if DIR is not in a
    repository"
   (message (concat "dir: " dir))
   (let* ((toplevel "")
	    (filter (lambda (_proc text)
		      (setf toplevel (concat toplevel text))))
	    (proc (make-process :name "rev-parse--toplevel"
				:buffer nil
				:coding (or file-name-coding-system
					    default-file-name-coding-system)
				:command (list "git" "-C" dir "rev-parse"
					       "--show-toplevel")
				:connection-type 'pipe
				:filter filter)))
        (while (or (accept-process-output proc 120)
		  (not (memq (process-status proc) '(exit signal)))))
        (prog1
	   (if (and (eq (process-status proc) 'exit)
		    (zerop (process-exit-status proc)))
	       (concat (substring toplevel 0 -1) "/")
	     nil)
	 (delete-process proc))))


(defun git-jump--ls-files (worktree paths func)
   "Run FUNC on PATHS that are tracked by worktree. NB takes paths not 
pathspecs"
   (let* ((remainder "")
	 (filter (lambda (_proc text)
		   (let* ((text (concat remainder text))
			  (len (length text))
			  (pos 0))
		     (while (< pos len)
		       (cond
			((= pos (string-match "\\([^\0]+\\)\0" text pos))
			 (funcall func (match-string 1 text))
			 (setq pos (match-end 0)))
			(t
			 (setq remainder (substring text pos))
			 (setq pos len)))))))
        (proc (make-process :name "ls-files"
			   :buffer nil
			   :coding (or file-name-coding-system
				       default-file-name-coding-system)
			   :command (cl-list* "git" "-C" worktree
					      "--literal-pathspecs" "ls-files"
					      "-z" "--" paths)
			   :connection-type 'pipe
			   :filter filter)))
     (while (or (accept-process-output proc 120)
	       (not (memq (process-status proc) '(exit signal)))))
     (delete-process proc)))

(provide 'git-jump)




  reply	other threads:[~2022-11-23 14:59 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
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 [this message]
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=feb7db00-db00-6190-47cf-9101052b9be8@dunelm.org.uk \
    --to=phillip.wood123@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    --cc=peff@peff.net \
    --cc=phillip.wood@dunelm.org.uk \
    --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).