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

Re: [PATCH v5 3/3] git-jump: invoke emacs/emacsclient

From
PWPhillip Wood <phillip.wood123@gmail.com>
Date
Nov 23, 2022, 14:58 UTC
Message-ID
<feb7db00-db00-6190-47cf-9101052b9be8@dunelm.org.uk>
In-Reply-To
<ad7c299cb0f78ae3f36d57b67fa91e5ccaab3181.1669187053.git.gitgitgadget@gmail.com>
Hi Yoichi
On 23/11/2022 07:04, Yoichi Nakayama via GitGitGadget wrote:
Show 28 quoted lines
> 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)
Previous: Yoichi Nakayama via GitGitGadgetNext: Yoichi Nakayama
Message 42 of 68 in “git-jump: support Emacs”
  1. 0/2 git-jump: support EmacsYoichi NAKAYAMA via GitGitGadget, Nov 19, 2022
  2. 1/2 git-jump: add an optional argument 'stdout'Yoichi Nakayama via GitGitGadget, Nov 19, 2022
  3. 2/2 git-jump: invoke emacsclientYoichi Nakayama via GitGitGadget, Nov 19, 2022
  4. Eric SunshineNov 19, 2022
  5. Yoichi NakayamaNov 19, 2022
  6. Eric SunshineNov 19, 2022
  7. Yoichi NakayamaNov 21, 2022
  8. 0/2 git-jump: support EmacsYoichi NAKAYAMA via GitGitGadget, Nov 20, 2022
  9. 1/2 git-jump: add an optional argument 'stdout'Yoichi Nakayama via GitGitGadget, Nov 20, 2022
  10. Junio C HamanoNov 21, 2022
  11. Yoichi NakayamaNov 21, 2022
  12. Jeff KingNov 21, 2022
  13. 2/2 git-jump: invoke emacsclientYoichi Nakayama via GitGitGadget, Nov 20, 2022
  14. 0/2 git-jump: support EmacsYoichi NAKAYAMA via GitGitGadget, Nov 21, 2022
  15. 1/2 git-jump: add an optional argument '--stdout'Yoichi Nakayama via GitGitGadget, Nov 21, 2022
  16. Jeff KingNov 21, 2022
  17. Junio C HamanoNov 21, 2022
  18. Yoichi NakayamaNov 22, 2022
  19. Jeff KingNov 22, 2022
  20. Yoichi NakayamaNov 22, 2022
  21. 2/2 git-jump: invoke emacs/emacsclientYoichi Nakayama via GitGitGadget, Nov 21, 2022
  22. Jeff KingNov 21, 2022
  23. Yoichi NakayamaNov 22, 2022
  24. 0/2 git-jump: support EmacsYoichi NAKAYAMA via GitGitGadget, Nov 22, 2022
  25. 1/2 git-jump: add an optional argument '--stdout'Yoichi Nakayama via GitGitGadget, Nov 22, 2022
  26. Jeff KingNov 22, 2022
  27. 2/2 git-jump: invoke emacs/emacsclientYoichi Nakayama via GitGitGadget, Nov 22, 2022
  28. Phillip WoodNov 22, 2022
  29. Yoichi NakayamaNov 23, 2022
  30. Jeff KingNov 22, 2022
  31. Yoichi NakayamaNov 23, 2022
  32. Jeff KingNov 24, 2022
  33. Yoichi NakayamaNov 24, 2022
  34. Yoichi NakayamaNov 24, 2022
  35. Jeff KingNov 24, 2022
  36. Jeff KingNov 24, 2022
  37. Yoichi NakayamaNov 25, 2022
  38. 0/3 git-jump: support EmacsYoichi NAKAYAMA via GitGitGadget, Nov 23, 2022
  39. 1/3 git-jump: add an optional argument '--stdout'Yoichi Nakayama via GitGitGadget, Nov 23, 2022
  40. 2/3 git-jump: move valid-mode check earlierJeff King via GitGitGadget, Nov 23, 2022
  41. 3/3 git-jump: invoke emacs/emacsclientYoichi Nakayama via GitGitGadget, Nov 23, 2022
  42. Phillip WoodNov 23, 2022
  43. Yoichi NakayamaNov 23, 2022
  44. Jeff KingNov 24, 2022
  45. 0/3 git-jump: support EmacsYoichi NAKAYAMA via GitGitGadget, Nov 24, 2022
  46. 1/3 git-jump: add an optional argument '--stdout'Yoichi Nakayama via GitGitGadget, Nov 24, 2022
  47. 2/3 git-jump: move valid-mode check earlierJeff King via GitGitGadget, Nov 24, 2022
  48. 3/3 git-jump: invoke emacs/emacsclientYoichi Nakayama via GitGitGadget, Nov 24, 2022
  49. 0/3 git-jump: support EmacsYoichi NAKAYAMA via GitGitGadget, Nov 25, 2022
  50. 1/3 git-jump: add an optional argument '--stdout'Yoichi Nakayama via GitGitGadget, Nov 25, 2022
  51. Ævar Arnfjörð BjarmasonNov 25, 2022
  52. Yoichi NakayamaNov 27, 2022
  53. 2/3 git-jump: move valid-mode check earlierJeff King via GitGitGadget, Nov 25, 2022
  54. 3/3 git-jump: invoke emacs/emacsclientYoichi Nakayama via GitGitGadget, Nov 25, 2022
  55. Ævar Arnfjörð BjarmasonNov 25, 2022
  56. Yoichi NakayamaNov 25, 2022
  57. Ævar Arnfjörð BjarmasonNov 25, 2022
  58. Jeff KingNov 28, 2022
  59. Ævar Arnfjörð BjarmasonNov 28, 2022
  60. Yoichi NakayamaNov 28, 2022
  61. Yoichi NakayamaNov 27, 2022
  62. 0/3 git-jump: support EmacsYoichi NAKAYAMA via GitGitGadget, Nov 27, 2022
  63. 1/3 git-jump: add an optional argument '--stdout'Yoichi Nakayama via GitGitGadget, Nov 27, 2022
  64. 2/3 git-jump: move valid-mode check earlierJeff King via GitGitGadget, Nov 27, 2022
  65. 3/3 git-jump: invoke emacs/emacsclientYoichi Nakayama via GitGitGadget, Nov 27, 2022
  66. Phillip WoodNov 28, 2022
  67. Junio C HamanoNov 28, 2022
  68. Yoichi NakayamaNov 29, 2022

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.