Something went wrong. Try again.
Juju is a git/jj interface for the Helix editor
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360;; Copyright (C) 2026 Tom Waddington;;;; This program is free software: you can redistribute it and/or modify;; it under the terms of the GNU Affero General Public License as published by;; the Free Software Foundation, either version 3 of the License, or;; (at your option) any later version.
;;; diff-view.scm - the full-width diff component;;;;;; Every standalone diff juju shows lands here: the working copy (`:juju-diff`,;;; or `d` in the status view) and a single commit (`Enter` on a commit row,;;; `:juju-show`). It is the same row machinery as the status view, so a diff;;; keeps its per-file structure, folds, search and the selection-first;;; stage/unstage/discard grammar. `t` toggles unified against side by side.;;;;;; Content comes from one of two places, and the difference matters:;;;;;; worktree the backend status, narrowed to its file sections, with each;;; file's diff fetched the same per-file way the status view;;; fetches it. That per-section fetch (`--cached` for the index,;;; `--no-index` for an untracked file) is what makes staging from;;; this view correct, so it is worth one call per changed file.;;; commit one `--git` diff, split per file by `parse-diff-files`. History;;; cannot be staged, so the mutation keys report and do nothing;;; (see `read-only-source?` in view-common.scm).;;;;;; Files open unfolded, since showing the diff is the point; a refresh expands;;; only files it has not seen, leaving folds the user made alone.
(require-builtin helix/components)(require "helix/misc.scm")
(require "backend-interface.scm")(require "config.scm")(require "model.scm")(require "view-common.scm")(require "view-rows.scm")(require "render.scm") ; juju-tag->style(require "text-view.scm")(require "ui-utils.hx/keys.scm")(require "ui-utils.hx/strings.scm")(require "ui-utils.hx/overlay-view.scm")
(provide open-worktree-diff open-commit-diff)
(define COMPONENT-NAME "juju-diff")
;; The section kind a commit's files sit under. It is not a working-copy;; section, so nothing tries to stage it.(define COMMIT-SECTION-KIND 'commit-files)
;; The status sections that hold changed files. The rest of a status (recent;; commits, bookmarks, the operation log) is history, not a diff.(define FILE-SECTION-KINDS '(conflicts untracked unstaged staged working-copy))
;;; Content ;;;
(define (file-sections st) (filter (lambda (sec) (and (member (section-kind sec) FILE-SECTION-KINDS) #t)) (status-sections st)))
;; The working copy's changed files, with every diff fetched up front.(define (fill-worktree! state) (let* ([backend (view-state-backend state)] [raw (backend-status backend)] [st (make-status (status-header raw) (file-sections raw))] [fold (view-state-fold state)]) (expand-sections! fold (status-sections st)) (set-view-state-status! state (apply-fold-state fold st)) (prefetch-diffs! state)))
;; One commit's diff. `header` is carried over on a refresh, since the commit;; record that produced it is not re-read.(define (fill-commit! state rev header) (let* ([backend (view-state-backend state)] [entries (backend-diff backend (hash 'type 'commit-files 'rev rev))] [st (make-status header (commit-sections entries))] [fold (view-state-fold state)]) (expand-sections! fold (status-sections st)) (set-view-state-diff-cache! state (entry-cache entries (hash))) (set-view-state-status! state (apply-fold-state fold st))))
(define (commit-sections entries) (maybe-section "commit-files" "Changed files" COMMIT-SECTION-KIND (entry-items entries '())))
(define (entry-items entries acc) (if (null? entries) (reverse acc) (entry-items (cdr entries) (cons (entry-item (car entries)) acc))))
(define (entry-item e) (make-file-item (or (hash-ref e 'path) "(unknown)") (hash-ref e 'status-code) (hash-ref e 'hunks) #t (entry-extra e)))
(define (entry-extra e) (hash 'orig-path (hash-ref e 'orig-path) 'binary? (hash-ref (hash-ref e 'flags) 'binary?)))
;; A commit's hunks are already parsed, so seed the cache the rows read from;; rather than fetching per file.(define (entry-cache entries acc) (if (null? entries) acc (entry-cache (cdr entries) (hash-insert acc (diff-cache-key COMMIT-SECTION-KIND (or (hash-ref (car entries) 'path) "(unknown)")) (hash-ref (car entries) 'hunks)))))
(define (expand-sections! fold secs) (if (null? secs) #t (begin (expand-new-files! fold (section-kind (car secs)) (file-paths (section-items (car secs)) '())) (expand-sections! fold (cdr secs)))))
(define (file-paths items acc) (if (null? items) (reverse acc) (file-paths (cdr items) (if (file-item? (car items)) (cons (file-item-path (car items)) acc) acc))))
(define (prefetch-diffs! state) (prefetch-sections! state (status-sections (view-state-status state))))
(define (prefetch-sections! state secs) (if (null? secs) #t (begin (prefetch-files! state (section-kind (car secs)) (file-paths (section-items (car secs)) '())) (prefetch-sections! state (cdr secs)))))
(define (prefetch-files! state kind paths) (if (null? paths) #t (begin (ensure-diff! state kind (car paths)) (prefetch-files! state kind (cdr paths)))))
;;; Refresh ;;;
;; The refresh-fn every mutation runs through: re-read whichever content this;; view is showing, then settle the cursor and clear the transient state.(define (refresh-diff-view! state-box) (let* ([state (unbox state-box)] [source (view-state-source state)]) (if (string? source) (fill-commit! state source (status-header (view-state-status state))) (begin (set-view-state-diff-cache! state (hash)) (fill-worktree! state))) (set-view-state-selection! state '()) (clear-search! state) (clamp-cursor! state) (set-msg! state "Refreshed" 'info)))
;;; Display mode ;;;
;; `t` flips the mode and remembers it in config, so the next diff opens the way;; this one was left.(define (toggle-mode! state) (let ([next (if (eq? (view-state-mode state) 'split) 'unified 'split)]) (set-view-state-mode! state next) (set-juju-diff-mode! next) (set-msg! state (if (eq? next 'split) "Side by side" "Unified") 'info)))
;;; Rendering ;;;
(define (view-title state) (if (read-only-source? state) (string-append " juju diff " (commit-label state) " ") (string-append " juju diff " (backend-root (view-state-backend state)) " ")))
;; Named from the header the commit fill put there, so the title survives a;; refresh without re-reading the commit record.(define (commit-label state) (let ([header (status-header (view-state-status state))]) (string-append (header-value header "Commit" (view-state-source state)) " " (header-value header "Subject" ""))))
(define (header-value header label fallback) (cond [(null? header) fallback] [(string=? (car (car header)) label) (cdr (car header))] [else (header-value (cdr header) label fallback)]))
(define (view-status state) (cons (status-line-text state) (view-state-message-tag state)))
(define (status-line-text state) (let ([msg (view-state-message state)] [n (length (view-state-selection state))]) (cond [(not (string=? msg "")) msg] [(> n 0) (string-append (number->string n) " marked " "s stage u unstage x discard t " (other-mode-label state) " v unmark q quit")] [(read-only-source? state) (string-append "t " (other-mode-label state) " Tab fold / search Enter visit q quit")] [else (string-append "s stage u unstage x discard c commit t " (other-mode-label state) " / search ? keys")])))
(define (other-mode-label state) (if (eq? (view-state-mode state) 'split) "unified" "side by side"))
(define (help-lines) (list "juju diff - keys" "" "Display: t unified / side by side" "Movement: j/k up/down C-u/C-d page Home/End ends" "Sections: } next { previous ^ enclosing section" "Search: / search n next match N previous match" "Folding: Tab fold or unfold the file" "Visit: Enter open the file at this line" "Select: v mark/unmark row (acts on marks, else cursor)" "" "Working copy only (a commit's diff is read-only):" " s stage u unstage x discard" " S stage all U unstage all" " c commit a amend" "" "Side by side stages a whole change: selecting the row that holds a" "deletion and its replacement stages both." "" "Other: g refresh q / Esc close"))
;;; Event handling ;;;
;; Close from within a handler (visiting a file), matching the shell's on-close.(define (close-view) (pop-last-component-by-name! COMPONENT-NAME))
(define (view-keys state-box event) (let ([state (unbox state-box)]) (cond [(char-is? event #\t) (toggle-mode! state) event-result/consume]
;; Section navigation. [(char-is? event #\}) (cursor-to-section! state 'next) event-result/consume] [(char-is? event #\{) (cursor-to-section! state 'prev) event-result/consume] [(char-is? event #\^) (cursor-to-section! state 'parent) event-result/consume]
;; In-buffer search. [(char-is? event #\/) (start-search! state-box) event-result/consume] [(char-is? event #\n) (search-step! state 1) event-result/consume] [(char-is? event #\N) (search-step! state -1) event-result/consume]
[(key-event-tab? event) (clear-message! state) (toggle-fold-at-cursor! state) event-result/consume] [(key-event-enter? event) (clear-message! state) (visit-file-at-cursor! state close-view) event-result/consume] [(char-is? event #\g) (refresh-diff-view! state-box) event-result/consume]
;; Selection. [(char-is? event #\v) (toggle-mark! state) event-result/consume]
;; Stage / unstage / discard, inert on a commit's diff. [(char-is? event #\s) (do-selection-op! state-box 'stage 'stage) event-result/consume] [(char-is? event #\u) (do-selection-op! state-box 'unstage 'unstage) event-result/consume] [(char-is? event #\x) (do-discard! state-box) event-result/consume] [(char-is? event #\S) (do-bulk! state-box 'stage-all 'stage-all) event-result/consume] [(char-is? event #\U) (do-bulk! state-box 'unstage-all 'unstage-all) event-result/consume] [(char-is? event #\c) (do-commit! state-box #f) event-result/consume] [(char-is? event #\a) (do-commit! state-box #t) event-result/consume]
[(char-is? event #\?) (show-text-view "juju diff keys" (help-lines)) event-result/consume]
[else #f])))
;;; Lifecycle ;;;
(define diff-view-spec (make-overlay-view #:name COMPONENT-NAME #:title view-title #:rows current-rows #:cursor view-state-cursor #:set-cursor! set-view-state-cursor! #:top view-state-top #:set-top! set-view-state-top! #:status view-status #:marked view-state-selection #:on-key view-keys #:move! move-cursor! #:page! page-cursor! #:edge! cursor-to-edge! #:page-size 8 #:tag->style juju-tag->style #:overlay-scale (lambda () (juju-overlay-scale))))
(define (empty-status) (make-status '() '()))
;;@doc;; Open the diff view over `backend`'s working copy: every changed file, with;; its diff, foldable and stageable. `on-change` is a thunk run after every;; mutation, so a status view underneath can re-read. Echoes instead when;; nothing has changed.(define (open-worktree-diff backend on-change) (let ([state (make-view-state backend 'worktree (juju-diff-mode) (empty-status) (make-fold-state) refresh-diff-view! on-change)]) (fill-worktree! state) (if (= 0 (status-file-count (view-state-status state))) (set-status! "juju: no changes to diff") (open-overlay-view! diff-view-spec state))))
;;@doc;; Open the diff view over the commit `c` (a commit-record) in `backend`. The;; content is read-only: history has nothing to stage into.(define (open-commit-diff backend c) (let ([state (make-view-state backend (commit-record-id c) (juju-diff-mode) (empty-status) (make-fold-state) refresh-diff-view! (lambda () #t))]) (fill-commit! state (commit-record-id c) (commit-header c)) (if (= 0 (status-file-count (view-state-status state))) (set-status! "juju: that change touches no files") (open-overlay-view! diff-view-spec state))))
(define (commit-header c) (list (cons "Commit" (commit-record-short-id c)) (cons "Author" (commit-record-author c)) (cons "Date" (commit-record-date c)) (cons "Subject" (commit-record-subject c))))