emacs fun: ghq-magit

ghq is a simple wrapper of git clone that save me about 7 seconds of time on each clone -- I don't have to decide where to place the workdir. Then, as a side-effect, Doing ghq list -p would give me a nice list of all the project paths.

From there, I wish to be able to quickly open any ghq-managed project root with magit from emacs, hence this interactive elisp function my/ghq-git:

(defun my/ghq-magit ()
  "Let you pick a directory from `ghq list -p` using only the folder name,
with fuzzy, case-insensitive search, and open it with Magit."
  (interactive)
  ;; Get the list of directories from `ghq list -p`
  (let* ((ghq-dirs (process-lines "ghq" "list" "-p"))
         ;; Make a list where the key is the folder name, and the value is the full path
         (dir-alist (mapcar (lambda (d)
                              (cons (file-name-nondirectory (directory-file-name d)) d))
                            ghq-dirs))
         ;; Let the user pick a folder name from the list, with fuzzy search
         (selected (completing-read
                    "Repository: "
                    (lambda (str pred action)
                      (if (eq action 'metadata)
                          '(metadata (category . project-file)
                                     (display-sort-function . identity)
                                     (cycle-sort-function . identity))
                        (complete-with-action action dir-alist str pred))))))
    ;; If the user picked a folder, open it in Magit
    (when-let ((dir (alist-get selected dir-alist nil nil #'equal)))
      (magit-status dir))))

Disclaimer: This function is mostly written by Perplexity first. I've briefly modify the expression that converts the output of "ghq list -p" to use process-lines instead.