# Architecture parterre is a Cargo workspace with six crates: the domain library, three adapters over external stacks, the handles they share, and the binary (#214). Forge depends on core; core and highlight on util; telemetry on none of ours; the binary on all five; nothing on the binary. ``` crates/parterre-util the cancellation handles: `Cancel` holds the one child process running under it and kills it when cancelled; `CancelTree` kills a git operation's whole process group or tree, hooks included crates/parterre-core GUI-free; everything testable lives here git.rs run `git log` / `git for-each-ref` / `git worktree list`, parse into a Repo; changed files of a commit (`git diff-tree`) repo.rs Repo snapshot: commits (with parent indices), refs, HEAD, worktrees, git's hash length, and git's listing of the refs and worktrees, which the branch catalogue is built from (#310) log.rs log query: tips and exclusions → commits in `git log --date-order` order, from the snapshot alone log_layout.rs the log window's four fixed layouts and their divider positions changed_files.rs changed-file types, `diff-tree -z` parser, files-before-folders order, the log window's file filter and column sort columns.rs tables' column widths: one column takes what the others leave, the others as the layout says until the user drags their border list_keys.rs where the arrow keys, Page Up/Down, Home and End move the selection of a list, tabs or segments that are one stop for Tab (#387) compare.rs comparing two commits, or a commit and the working tree: which goes left, and the files between them (`git diff-tree A B`, `git diff A`, or from `git merge-base`) blame.rs blaming a file (`git blame --line-porcelain`): each line's origin (commit, path, the version before), and where a line leads: its change, and the blame before it file_history.rs the blame window's history pane: `git log --no-follow` of the file plus the blame's own commits and the working tree, with which rows own lines and are in the snapshot, and their graph column text.rs URLs in commit messages, paths cut at the start, thousands separators text_size.rs the text size's steps, and Ctrl+wheel and pinch turned into steps revgraph.rs reduce the commit DAG to a revision graph (TortoiseGit's rules) upstream.rs local branches against their upstreams: ahead, behind, lost to a force push or replaced by a rebase (a counterpart by patch, or by author, author date and subject), and the edges that hold them merged.rs local branches whose upstream is gone, squash- or rebase-merged into the default branch, found by patch id in four git starts (#384), and the answers by branch and tip pattern.rs branch-name wildcards, for hiding and colouring branches recent.rs the recently opened repositories lenient.rs reading settings one at a time, so files of older and newer versions load all they can, and writing back what was left out watch.rs fingerprint of the files git keeps refs and worktrees in, for reloading by itself glyphs.rs toolbar and menu icons as SVG path data, and a path flattener layout/ layered (Sugiyama) layout rank.rs layer assignment (network simplex / longest path / chronological), plus splitting of over-wide layers trunk.rs the default branch's first-parent line, and which side of it every side branch goes layered.rs dummy items for long edges, optional edge bundling order.rs crossing minimisation (median sweeps, exact crossing count) position.rs coordinates within layers (L1 via isotonic regression) mod.rs pipeline, variable layer spacing, direction/rotation physics.rs rearranging by hand: springs, weak magnets, drag modes, undo route.rs routing edges afresh around rearranged nodes crates/parterre-forge open pull requests, on core's types lib.rs the model, where each is shown (head commit and base-branch refs), remotes and upstreams from git; which forge origin is on (`Origin`); the merged pull request of a gone branch, at its tip (#384) github.rs github.com remotes; one GraphQL request per 100 fetched branches of origin (and its parent), signed in with `gh auth token`, within a rate-limit budget; HTTPS through ureq behind the `github` feature azure.rs Azure DevOps remotes; one REST request per 500 active pull requests, signed in with what `git credential fill` gives for origin's HTTPS URL; each known organisation's repositories, to clone; HTTPS through ureq behind the `azure-devops` feature repositories.rs repositories to clone, from either forge, found again by URL crates/parterre-highlight syntax colour for the diff and blame windows (#209) lib.rs a file's language from its path; language-neutral spans per line through tree-sitter, 23 languages behind the `syntax` feature; the child process the app runs them in, with a budget and a Cancel proto_highlights.scm the Protocol Buffers query, which its crate doesn't export crates/parterre-telemetry what parterre asks and sends over the network (#175): the update check (#258), the usage statistics (#261, #262, #264) and the crash reports (#263) lib.rs the update check's thread, at start and then every 24 h; the channel, from the package's marker beside the binary, or Snap and Flatpak at run time; `Usage`, the usage statistics from start to close channel.rs how this copy was installed, and which channels take stable only ring.rs rings: which ring a version names, which builds each hears of, and how an installation keeps its ring update.rs versions and their order, which build a ring hears of, and where *Download* goes on each channel; no network download.rs *Update now*'s download (#431), checked against the release's `SHA256SUMS`, behind the `send` feature usage.rs whether anything may go to PostHog (the first-run prompt's answer, `DO_NOT_TRACK`, debug builds), the install ID, and which lifecycle events a launch sends; no network session.rs the `$session_id` (#262): a new UUIDv7 after 30 min idle, 24 h, or on opening another repository; the caller's clock, no network feature.rs feature events (#264): `record` a window, dialog, menu or action from fixed enums, and `register` the settings every event carries; a test sink (`recording`); no network crash.rs whether panics go to PostHog, and the home folder in them made `~`; no network github.rs GitHub's releases API through ureq, and the agent the download uses too, behind the `send` feature posthog.rs all of the PostHog code: posthog-rs's client on a thread of its own, the events, their standard properties and the registered ones, and the SDK's global client that sends panics, behind the `send` feature crates/parterre-stamp the pipeline's tool: a commit's published version, written into a built binary in place (not published) crates/parterre the binary (eframe/egui) build.rs asks git for the commit and sets the version string main.rs CLI (clap), window setup keys.rs one key per action and platform, and its label (⌘O, Ctrl+O) macos.rs folders opened from Finder and the Dock, the Services menu, the clipboard, Install Command Line Tool, the input method (macOS) version.rs the version counted from the tags, dev and per ring (runs in build.rs and parterre-stamp; see docs/releasing.md) slot.rs the version slot the pipeline writes the published version into self_update.rs *Update now* (#431): the download on a thread, then, on the next click, msiexec, the archive's binary swapped in, or the disk image's app, and the restart app.rs canvas interaction, search, status bar, windows, opening folders toolbar.rs the toolbar and its popovers menu_bar.rs the menu bar (#339): the menus as a tree of entries built from the app's state each frame, in each platform's case bar.rs drawn by egui above the toolbar (Windows, Linux) macos.rs the system's menu bar, built with AppKit (macOS) commands.rs what the menu bar's items do, on the window in front git_menu.rs the Git menu: fixed items on the selected node, from branches.rs's offers file_drop.rs a folder dropped on the window opens updates.rs the update check as the settings say, *Download*, and the dialog telling of each new release once, and the toolbar's button for it: download, then restart to update (#433) privacy.rs the first-run prompt, and the usage statistics and crash reports as the user's choices say, told of input and repositories opened; Settings › Privacy shows them settings_window.rs the settings: pages of rows, applied as you change them auto_reload.rs a worker thread that reloads when the refs change pull_requests.rs loads open pull requests on a worker thread while they are shown, cached per repository, with back-off merged.rs which gone branches were squash- or rebase-merged, asked of git and the forge on a worker thread, ten at a time, and kept between restarts log_window.rs the log window (Show log): an immediate viewport with three panes (commits, details, changed files) that one of four fixed layouts arranges, picked in its header; changed files come from git on a worker thread file_table.rs the changed-files table the log and compare windows share, and the worker thread that asks git for such lists compare_window.rs the compare window (Compare revisions, with HEAD, with the marked commit): two commits and the files they differ in commit_table.rs the commit table (graph column, hash, subject with refs, author, date) the log window and the blame window's history pane share column_borders.rs the borders between the column headings of both tables, dragged to resize a column and double-clicked to reset it blame_window.rs blame windows: a file's lines with a gutter naming their commits, shaded by age; a line's menu blames the version before (a new window), shows its change, or its log; the history pane below lists the file's commits (git lists them on a worker thread alongside the blame), and choosing a line or a row chooses the commit in both scene.rs node contents and sizes + layout + physics net, hit testing render.rs painting nodes, edges, arrows, overview upstreams.rs upstreams over the edges, the status bar's ahead|behind, ↑n ↓m counts merged_branches.rs a merged branch's stripes (graph, badges, SVG) and its dashed line to where it landed export.rs SVG export, and PNG and WebP export: render.rs painted in tiles by an offscreen egui context, sized to stay within 100 megapixels raster.rs software rasteriser for egui's meshes (for PNG and WebP, with no GPU or window) view.rs pan/zoom transform; the graph keeps its size on screen whatever the text size text_size.rs text size input (Ctrl+wheel off the graph, Ctrl+plus/minus/0) into the setting, which is egui's zoom factor for every window theme.rs TortoiseGit colours (light, and dark via lightness inversion) system_theme.rs light or dark desktop preference on Linux (XDG portal) menu.rs the look of menus and popovers, menu items, right-click menus that the keyboard opens too context_key.rs the context-menu key (Windows), which egui doesn't know, handed to the window in front as Shift+F10 browser.rs opens github.com and dev.azure.com pages with the platform's opener file_manager.rs opens a worktree's folder in the platform's file manager, or a terminal there widgets.rs icon buttons, segmented buttons, switches, text fields, tabs, and lists that are one stop for Tab focus.rs keyboard focus as desktop GUIs show it (#387): a ring once the keyboard is used, a click giving a dialog's button the keyboard (not on macOS), controls of several parts claiming their arrow keys settings.rs persisted settings, each repository's (the filters), and the Classic/Modern looks; apart from them, what is sent to PostHog (the first-run prompt's answer and the install ID), never exported or reset settings_file.rs settings as versioned JSON: stored, exported and imported usage.rs feature events as the app records them: a window, dialog or menu counted once per opening, and the settings as the properties every event carries automation.rs scripted runs (--script, --screenshot): steps fed to the window as input, frame by frame; see docs/automation.md script.rs workflow scripts (--script): click, type, screenshot, … one step a line record.rs --record: the window as a GIF, a video through ffmpeg, or PNG frames ``` ## Data flow 1. **Load** (`git.rs`): one `git log --all` (notes excluded) with a compact `\x1f`/`\x1e`-separated format, one `git for-each-ref`, and HEAD queries. About 100 ms for 15k commits. The snapshot holds *all* commits so view options never need git again. 2. **Reduce** (`revgraph.rs`): pick visible refs → reachable commits → decide which commits are nodes in one parents-first pass, recording for each hidden commit the node that represents it. Edges go from each node to the representatives of its parents. - Only refs that pass the filters start history. Branches matching the *Hide branches* wildcards don't, but they still label commits that other refs reach. So a hidden branch vanishes only if no shown branch contains it. - Open pull requests, while shown, label their head commits the same way: they never start history, and show only where one of their base branch's refs is shown. As labels they make their heads nodes, like tags. They are loaded from origin's forge on a worker thread (`forge`), separately from the snapshot, which stays what git has. - The commit a squash- or rebase-merged branch landed as stays a node where it is shown anyway, as a rebased branch's old commit does, so that the branch's dashed line has an end. Which branches those are is worked out on a worker thread too (`app/merged.rs`). - Worktrees, while shown: a branch another worktree has checked out is shown even where the local-branch switch or the hide list would leave it out, and starts history. A detached worktree's HEAD starts history too and labels its commit, above the hash. The snapshot always holds the commits of every worktree's HEAD, so the switch needs no git. - *Labelled commits* reproduces `git log --simplify-by-decoration`, including `simplify_merges` (redundant parents dropped) and empty-tree roots (TREESAME). The node sets are identical on the 15k-commit Apps repository and on 400 random repositories, and the edges match on 300 of them. - One deliberate exception: git hides an empty-tree root even when it carries a label; parterre shows it. - *Labelled forks* (not in TortoiseGit) runs the decorated reduction on the reversed graph to find where labelled histories fork apart, keeps those too, and leaves merges out: a hidden commit stands for all its nearest kept ancestors, so the nodes above a merge reach both sides. On the Apps repository it is 126 nodes against 89 labelled and 2467 for TortoiseGit's branchings and merges. - *Branchings and merges* reproduces TortoiseGit's chain collapse. 3. **Measure** (`scene.rs`): node boxes use TortoiseGit's geometry: one row per ref, or the short hash where there is none, then one row per pull request; 20 px side margins and 5 px top and bottom margins; monospace 12 px. 4. **Lay out** (`layout/`): rank → split wide layers → sides of the trunk (the default branch's first-parent line) → layered graph with dummies (optionally bundled per parent and side) → crossing minimisation, keeping each side on its side → L1 coordinates, the trunk pinned straight → variable layer gaps → rotate to the chosen direction. 5. **Simulate** (`physics.rs`): nodes and bend points become particles, each with a rest position (at first the layout). Springs along edges keep the offsets between rest positions; nodes near each other push apart like weak magnets, and neighbours in a row keep their order; weak anchors hold each particle to its rest position. - **Drag modes:** *Adapt* lets the rest of the graph give way; *Free* and *Subtree* move only the dragged nodes (Subtree adds their first-parent descendants) and stretch the edges to them. - **Shape:** each frame of an adaptive drag, the target shape is relaxed with Gauss-Seidel over displacements. Between sweeps, edge segments are put back in history order (children above parents, with a gap) by one pass along the flow and one against it, and overlapping boxes are pushed apart. Only the dragged nodes, and edges left reversed at rest, can break the order. - **Motion:** particles follow the target through damped springs. - **Drop:** whatever moved rests where it is from then on, so moved nodes keep giving way to later drags instead of being pinned. Drops, resets and returns to the layout are undoable. - **Relayout:** a reload (or the same options laid out again, as when pull requests arrive) puts the scene's nodes back by commit: a node resting away from the layout goes beside its first parent as it was, since a changed graph may lay the two out differently, and new commits go beside theirs. Remembered moves of an earlier session, and nodes without the same first parent, keep their offset from the layout. Then the graph settles as after a drop of the nodes moved by hand: the rest give way, overlapping boxes are pushed apart and children kept above their parents. - Only the dragged nodes' neighbourhood (up to 8000 particles) is simulated, and the simulation sleeps when still. - **Routing** (`route.rs`): edges whose layout route no longer fits are routed afresh. That means edges at nodes moved by hand, edges pulled far out of shape, and edges a moved node covers. The router groups the boxes in between into rows, picks a gap in each so that sideways moves happen where there is room, and pulls the route taut through them (funnel algorithm). Anything still in the way is walked around corner by corner. An edge whose parent has been moved before its child is routed round both nodes, from just below the child to just above the parent. 6. **Paint** (`render.rs`): edges then nodes, culled to the viewport; text is skipped below 4 px. Edges leave a node from the side facing its parents and enter from the side facing its children (bottom and top, newest on top), unlike TortoiseGit, which clips them to the box border wherever they hit it. An edge turned around therefore shows as a loop: it follows its route round the nodes, or else detours round the side of the boxes. ## Testing - Unit tests next to the code, plus integration tests (`crates/parterre-core/tests/`) that build throwaway repositories with the git CLI. - Property tests (`properties_*.rs`) run random DAGs, repositories and drags against the invariants: - every parent is below its children - coordinates are finite and inside the bounds - network simplex is optimal on tiny graphs (checked by brute force) - every edge ends on a node - the net comes to rest and stays there; it returns home after a reset - after an adaptive drag, every child is still above its parents - `cargo test --release -p parterre-core --test properties_layout -- --ignored --nocapture` prints timings for large, awkward inputs. ## Why these choices - **Rust + egui/eframe**: native speed, one codebase for Linux, Windows and macOS, and an immediate-mode canvas that makes custom drawing and dragging simple. No development packages of system libraries are needed to build on Linux (winit/glutin load Wayland/X11/GL at runtime); a C compiler is, for the TLS library (`ring`) and the tree-sitter grammars. - **git CLI instead of a git library**: always available where parterre is useful, honours every repo configuration, fast enough (see above), and needs no git library built or found. - **tree-sitter for syntax colour**: a grammar crate per language, each compiling its parser (C) in its build script, and the grammars' own highlight queries mapped onto a dozen kinds in `parterre-highlight`. Chosen over syntect in #205 and #208 for speed and precision, with binary size not a constraint. Each file is highlighted in a child process (`parterre --highlight LANG`), killed when the window closes or after ten seconds, so a grammar's abort or runaway parse on an odd file costs the colours, not the window. - **Own layout instead of a graph-layout crate**: git-specific needs (first-parent weighting, TortoiseGit parity, layer splitting, bundling, anytime network simplex) and full control over performance. 15k nodes lay out in about 200 ms.