Commit784d6e16Recorded18 Mar 2026Repositorysigil-git
Fetch commit hash before worktree creation in bare repos
Message
When git-worktree-add receives a ref that looks like a commit hash, fetch it explicitly from origin first. Bare repos after a branch-only fetch may not have arbitrary commits available, causing worktree creation to fail with "invalid reference" for short or full hashes.
Changed
src/sigil/git.sgl | 14 ++++++++++++++
1 file changed, 14 insertions(+)Diff
src/sigil/git.sglmodified
@@ -194,10 +194,24 @@
194
(define (git-worktree-add repo-dir worktree-dir ref) 195
(let ((old-dir (current-directory))) 196
(set-current-directory! repo-dir)+197
;; If the ref looks like a commit hash (hex), fetch it explicitly+198
;; since bare repos may not have it after a shallow/branch-only fetch+199
(when (looks-like-hash? ref)+200
(process-run "git" "fetch" "origin" ref "--quiet")) 201
(let ((result (process-run "git" "worktree" "add" "--detach" "--quiet" worktree-dir ref))) 202
(set-current-directory! old-dir) 203
(= result 0)))) 204
+205
(define (looks-like-hash? s)+206
(and (>= (string-length s) 7)+207
(let loop ((i 0))+208
(if (>= i (string-length s))+209
#t+210
(let ((c (string-ref s i)))+211
(and (or (and (char>=? c #\0) (char<=? c #\9))+212
(and (char>=? c #\a) (char<=? c #\f)))+213
(loop (+ i 1))))))))+214
215
;; Remove a worktree 216
;; repo-dir: path to the bare repository 217
;; worktree-dir: path to the worktree to remove