Commit a8a3b3823cba

Vincent Demeester <vincent@sbr.pm>
2026-07-30 13:07:42
refactor: simplify pi-from-github sessions
Removed worktree/checkout creation in favor of starting pi sessions directly in the repo root. Session names now use org/repo#N format inferred from GitHub metadata.
1 parent 461a1a0
Changed files (1)
dots
config
emacs
dots/config/emacs/site-lisp/pi-from-github.el
@@ -1,7 +1,7 @@
 ;;; pi-from-github.el --- Start pi sessions from GitHub PRs/issues -*- lexical-binding: t -*-
 ;;; Commentary:
 ;; Start a pi-coding-agent session from a GitHub PR or issue.
-;; Uses `gh` CLI for listing and `lazyworktree` for worktree creation.
+;; Uses `gh` CLI for listing PRs/issues.
 ;;; Code:
 
 (require 'json)
@@ -57,57 +57,23 @@
                     (if (string-empty-p label-str) "" (format " [%s]" label-str)))
             .number))))
 
-(defun pi-from-github--create-worktree-for-pr (number)
-  "Create a worktree for PR NUMBER using lazyworktree.
-Uses `gh pr checkout' to fetch the PR (handles forks transparently),
-then creates a worktree from the resulting local branch.  Returns the
-worktree path."
+(defun pi-from-github--org-repo ()
+  "Return the org/repo slug (e.g. \"openshift/pipelines\") for the current GitHub repo."
   (let* ((default-directory (pi-from-github--repo-root))
-         ;; First try lazyworktree directly
-         (cmd (format "lazyworktree create --from-pr %d --json --silent 2>&1" number))
-         (output (shell-command-to-string cmd))
-         (json-data (condition-case nil
-                        (json-read-from-string output)
-                      (error nil))))
-    (if (and json-data (alist-get 'path json-data))
-        (alist-get 'path json-data)
-      ;; Fallback: gh pr checkout handles forks transparently
-      (message "Fetching PR #%d via gh..." number)
-      (let* ((branch (format "pr-%d" number))
-             (checkout-cmd (format "gh pr checkout %d --branch %s 2>&1"
-                                   number (shell-quote-argument branch)))
-             (checkout-out (shell-command-to-string checkout-cmd)))
-        ;; gh pr checkout switches the current worktree — switch back
-        (shell-command-to-string "git checkout - 2>/dev/null")
-        ;; Now create worktree from the local branch
-        (let* ((cmd2 (format "lazyworktree create --from-branch %s --json --silent 2>&1"
-                             (shell-quote-argument branch)))
-               (out2 (shell-command-to-string cmd2))
-               (json2 (condition-case nil
-                          (json-read-from-string out2)
-                        (error nil))))
-          (if (and json2 (alist-get 'path json2))
-              (alist-get 'path json2)
-            (error "Failed to create worktree for PR #%d: %s" number out2)))))))
-
-(defun pi-from-github--create-worktree-for-issue (number)
-  "Create a worktree for issue NUMBER using lazyworktree.
-Returns the worktree path."
-  (let* ((default-directory (pi-from-github--repo-root))
-         (cmd (format "lazyworktree create --from-issue %d --json --silent 2>&1" number))
-         (output (shell-command-to-string cmd))
-         (json-data (condition-case nil
-                        (json-read-from-string output)
-                      (error nil))))
-    (if (and json-data (alist-get 'path json-data))
-        (alist-get 'path json-data)
-      (error "Failed to create worktree for issue #%d: %s" number output))))
+         (url (string-trim (shell-command-to-string "gh repo view --json nameWithOwner -q .nameWithOwner 2>/dev/null"))))
+    (if (string-empty-p url)
+        ;; Fallback: parse git remote
+        (let ((remote (shell-command-to-string "git remote get-url origin 2>/dev/null")))
+          (if (string-match "github\\.com[:/]\\([^/]+/[^/.]+\\)" remote)
+              (match-string 1 remote)
+            "unknown/repo"))
+      url)))
 
 ;;;###autoload
 (defun pi-from-github-pr ()
   "Start a pi-coding-agent session from a GitHub PR.
-Lists open PRs using `gh`, creates a worktree with `lazyworktree`,
-and starts a pi session in that worktree."
+Lists open PRs using `gh` and starts a pi session in the repo root.
+Session is named org/repo#N."
   (interactive)
   (unless (pi-from-github--in-github-repo-p)
     (user-error "Not in a GitHub repository"))
@@ -116,11 +82,10 @@ and starts a pi session in that worktree."
          (selected (completing-read "PR: " candidates nil t))
          (number (cdr (assoc selected candidates))))
     (unless number (user-error "No PR selected"))
-    (message "Creating worktree for PR #%d..." number)
-    (let ((wt-path (pi-from-github--create-worktree-for-pr number)))
-      (message "Starting pi session in %s" wt-path)
-      (let* ((chat-buf (pi-coding-agent--setup-session wt-path
-                                                        (format "pr-%d" number)))
+    (let* ((repo-root (pi-from-github--repo-root))
+           (session-name (format "%s#%d" (pi-from-github--org-repo) number)))
+      (message "Starting pi session %s in %s" session-name repo-root)
+      (let* ((chat-buf (pi-coding-agent--setup-session repo-root session-name))
              (input-buf (buffer-local-value 'pi-coding-agent--input-buffer
                                             chat-buf)))
         (pi-coding-agent--show-session-buffers chat-buf input-buf)))))
@@ -128,8 +93,8 @@ and starts a pi session in that worktree."
 ;;;###autoload
 (defun pi-from-github-issue ()
   "Start a pi-coding-agent session from a GitHub issue.
-Lists open issues using `gh`, creates a worktree with `lazyworktree`,
-and starts a pi session in that worktree."
+Lists open issues using `gh` and starts a pi session in the repo root.
+Session is named org/repo#N."
   (interactive)
   (unless (pi-from-github--in-github-repo-p)
     (user-error "Not in a GitHub repository"))
@@ -138,11 +103,10 @@ and starts a pi session in that worktree."
          (selected (completing-read "Issue: " candidates nil t))
          (number (cdr (assoc selected candidates))))
     (unless number (user-error "No issue selected"))
-    (message "Creating worktree for issue #%d..." number)
-    (let ((wt-path (pi-from-github--create-worktree-for-issue number)))
-      (message "Starting pi session in %s" wt-path)
-      (let* ((chat-buf (pi-coding-agent--setup-session wt-path
-                                                        (format "issue-%d" number)))
+    (let* ((repo-root (pi-from-github--repo-root))
+           (session-name (format "%s#%d" (pi-from-github--org-repo) number)))
+      (message "Starting pi session %s in %s" session-name repo-root)
+      (let* ((chat-buf (pi-coding-agent--setup-session repo-root session-name))
              (input-buf (buffer-local-value 'pi-coding-agent--input-buffer
                                             chat-buf)))
         (pi-coding-agent--show-session-buffers chat-buf input-buf)))))