git_diff_live 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '08d9bd13d91c8cf1d61b038b08ea6ad790ff488c36ddf910fc49d40a63d6754a'
4
+ data.tar.gz: 874c5e24e54c1bb02b2e231ea20808f71338b459606e6263b7a9fdf171655e5a
5
+ SHA512:
6
+ metadata.gz: 00f84995207f55c33944a235bef6887fcc3d17abab42d1d309ea89d88e0be139998b61c3a4182dc024209be1e08f114f37dc517e30c10b4eb1f24ce2f890a4e0
7
+ data.tar.gz: c8acef13ca42954686efe1dbadb2aa1d01b00d22a393d6f25305aabb7537ba6e282621ab3007c1cfbca9551c4ee68ed556edbec4cea37645e79a8f8782db5bad
data/CHANGELOG.md ADDED
@@ -0,0 +1,33 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/), and this project adheres to [Semantic Versioning](https://semver.org/).
6
+
7
+ ## [0.3.0] - 2026-11-20
8
+
9
+ ### Added
10
+
11
+ - Comments on a file as a whole (`git_diff_live comment path:file ...`), drawn under the file's heading and, while the file is collapsed, on one line in it. For a deleted, moved or binary file, which has no line of its own to comment on.
12
+ - `git_diff_live changes` lists a file with no changed lines (moved, binary) as a change to comment on, and names what a renamed file was renamed from.
13
+
14
+ ## [0.2.0] - 2026-11-20
15
+
16
+ ### Added
17
+
18
+ - Comments: a bubble beside a change saying why it was made. Written with the new `git_diff_live` command (`changes`, `comment`), kept in `.git_diff_live/comments.jsonl` and committed with the code; a comparison shows the comments written within it.
19
+ - `skills/explain-changes`: the Claude Code skill an agent writes the comments with.
20
+ - Compare two commits: `?base=HEAD~1&head=HEAD`, the "to" field beside "From".
21
+
22
+ ### Changed
23
+
24
+ - The "Compare with" field is now "From", with "to" beside it.
25
+
26
+ ## [0.1.0] - 2026-11-18
27
+
28
+ ### Added
29
+
30
+ - The changes page: every file that differs between the working tree and a commit, side by side, with the words that changed marked inside each line. Staged, unstaged and untracked work together; renames shown as `old → new`.
31
+ - Live reload: the page polls a fingerprint of the diff and redraws only the files that changed, came or went.
32
+ - "Whole file" per file, "Compare with" any revision, "Open" in VS Code at the first changed line (WSL aware, other editors through `config.editor_url`).
33
+ - `GitDiffLive.configure`: `root`, `enabled`, `poll_interval`, `context`, `stylesheets`, `editor_url`.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,128 @@
1
+ # git_diff_live
2
+
3
+ A live, side-by-side page of your working tree's git changes, for Rails apps in development. Made for reading a session's work before committing it.
4
+
5
+ - Every changed file on one page: before on the left, after on the right, removed in red, added in green, and the words that changed marked inside each line.
6
+ - Staged, unstaged and untracked work together. Renames shown as `old → new`.
7
+ - **Live**: the page redraws what changes as you (or an agent) edit, without losing your place.
8
+ - "Whole file" per file, compare from any revision (`main`, `HEAD~3`, a SHA) to the working tree or to another revision, and "Open" to jump to the file in your editor.
9
+ - **Comments**: a bubble beside a change saying why it was made, written by the agent that made it and committed with the code.
10
+
11
+ It is a mountable Rails engine with its own layout, stylesheet and JavaScript. Nothing is added to your app's pages, Tailwind build or import map.
12
+
13
+ ## Requirements
14
+
15
+ - Rails 7.1 or newer with Propshaft, and `git` on the `PATH`.
16
+ - [fontawesome_subsetter](https://github.com/optimuspwnius/fontawesome-subsetter) set up in the host app (Font Awesome in `vendor/fontawesome`). The page's icons come from the host's icon font; the engine registers its own views with the subsetter so they are in the build.
17
+
18
+ Turbo, Stimulus, importmap-rails and Slim come along as dependencies. The host does not have to use them itself.
19
+
20
+ ## Installation
21
+
22
+ ```ruby
23
+ # Gemfile
24
+ group :development do
25
+ gem "git_diff_live"
26
+ end
27
+ ```
28
+
29
+ ```ruby
30
+ # config/routes.rb
31
+ mount GitDiffLive::Engine => "/changes" if defined?(GitDiffLive)
32
+ ```
33
+
34
+ Start the server and open `/changes`. On its own subdomain instead:
35
+
36
+ ```ruby
37
+ if defined?(GitDiffLive)
38
+ constraints subdomain: "dev" do
39
+ mount GitDiffLive::Engine => "/"
40
+ end
41
+ end
42
+ ```
43
+
44
+ `defined?(GitDiffLive)` keeps the route out of every environment the gem is not bundled in.
45
+
46
+ ## Configuration
47
+
48
+ Every option has a default; an initializer is optional.
49
+
50
+ ```ruby
51
+ # config/initializers/git_diff_live.rb
52
+ GitDiffLive.configure do | config |
53
+ config.root = Rails.root # the repository to show
54
+ config.enabled = Rails.env.local? # anywhere else the engine answers 404
55
+ config.poll_interval = 2 # seconds between checks; nil turns live reload off
56
+ config.context = 3 # unchanged lines drawn around each change
57
+ config.stylesheets = [ "fontawesome" ] # the host's icon font stylesheet(s)
58
+ config.editor_url = nil # nil: VS Code. Or "cursor://file%{path}:%{line}"
59
+ end if defined?(GitDiffLive)
60
+ ```
61
+
62
+ ### The page
63
+
64
+ | URL | |
65
+ | --- | --- |
66
+ | `/changes` | the working tree against `HEAD` |
67
+ | `/changes?base=main` | against any revision git understands |
68
+ | `/changes?base=HEAD~1&head=HEAD` | one commit against another: here, the last commit on its own |
69
+
70
+ A deleted file starts collapsed. A file with more than 1,500 changed lines is drawn when you ask for it.
71
+
72
+ ## Comments
73
+
74
+ A change can carry a comment: a sentence or two on why it was made, drawn as a bubble in a column beside the code. They are meant to be written by the agent that made the change, while it still knows the reason.
75
+
76
+ ```sh
77
+ bundle exec git_diff_live changes # the changes that have no comment yet, each changed line with its reference
78
+ bundle exec git_diff_live comment <<'EOF' # one comment a line: path, reference, comment
79
+ app/models/order.rb:+41 Totals were computed before discounts applied, so a discounted order was invoiced at full price.
80
+ app/models/order.rb:-58 Nothing has called `legacy_total` since the invoice rewrite.
81
+ app/models/legacy_invoice.rb:file Deleted: invoices are built by `Invoice` since the rewrite.
82
+ EOF
83
+ ```
84
+
85
+ There are two kinds. A comment on a **line** is drawn beside it: `+41` is line 41 of the file as it is now; `-58` is line 58 of the file as it was. A comment on a **file** (`path:file`) is drawn under the file's heading, for changes that are the whole file: a delete, a move, a binary file. It also shows on one line in the heading while the file is collapsed, as a deleted file is to begin with. `--base <revision>` compares with something other than `HEAD`. `skills/explain-changes/SKILL.md` is the Claude Code skill that drives these two commands at the end of a session: copy it to `.claude/skills/explain-changes/` in the app.
86
+
87
+ ### Where they are kept
88
+
89
+ In `.git_diff_live/comments.jsonl`, one JSON object a line, in the repository. Commit it with the code it explains.
90
+
91
+ A comparison shows the comments that file **gained** between its two ends, read off the same `git diff` as the changed files. So a comment written for a commit shows with that commit: in the working tree before it is committed, in `?base=<its parent>&head=<it>` ever after, and in any wider comparison that spans it. Nothing earlier leaks in, and the file itself is never listed as a change.
92
+
93
+ A comment on a line is anchored to the line's text, not its position. It stays beside its code when lines above come and go, and it is left out once that code has been changed again. The command line removes such a comment the next time it writes, as long as it has not been committed. A comment on a file shows for as long as the file is one of the changes.
94
+
95
+ The file only grows, at roughly 250 bytes a comment. `.git_diff_live/.gitattributes` gives it git's `union` merge, so two branches that each added comments merge without a conflict.
96
+
97
+ ### Open in the editor
98
+
99
+ "Open" is a link to the editor's own URL scheme, at the file's first changed line. By default that is VS Code (`vscode://file/…`). When the server runs under WSL the link names the distro (`vscode://vscode-remote/wsl+Ubuntu/…`, from `WSL_DISTRO_NAME`), because a Windows browser cannot open a Linux path as a local file. The browser asks once whether it may open the editor.
100
+
101
+ ## How live reload works
102
+
103
+ There is no file watcher and no WebSocket. Every `poll_interval` seconds the page asks `GET /fingerprint` for a digest of the diff (one `git diff` and one `git ls-files`). When it is not the one the page was drawn from, the page posts the digest of every file it shows to `POST /refresh`, and the server answers with Turbo Streams for the difference only: the files that changed, came or went, and the counts. Two hundred files are not drawn again because one was saved. A hidden tab does not poll.
104
+
105
+ ## Who can see it
106
+
107
+ The engine does not ask anyone to sign in: it inherits from `ActionController::Base`, not your `ApplicationController`. What keeps it private is `config.enabled`, which is true only in the development and test environments unless you set it. It shows the source of your working tree to whoever can reach it, so do not enable it on a server other people can reach.
108
+
109
+ ## Claude Code skills
110
+
111
+ Copy them to `.claude/skills/` in the app.
112
+
113
+ - `skills/show-changes/SKILL.md` opens the page at the end of a session (`/show-changes`, or `/show-changes main`). Set its URL.
114
+ - `skills/explain-changes/SKILL.md` writes the comments (see Comments). The agent runs it by itself when it has finished changing code; a line in the app's `CLAUDE.md` makes that dependable.
115
+
116
+ ## Development
117
+
118
+ ```sh
119
+ bundle install
120
+ bundle exec rake test # the engine mounted in a small host app, against a scratch repository
121
+ bundle exec rake css # after changing a class in a view, a helper or the JavaScript
122
+ ```
123
+
124
+ The page's stylesheet is built with Tailwind from `tailwind/git_diff_live.css` into `app/assets/builds/git_diff_live.css`, and the built file is what ships. It is committed: run `rake css` and commit the result whenever the classes change.
125
+
126
+ ## License
127
+
128
+ MIT.
@@ -0,0 +1,2 @@
1
+ /*! tailwindcss v4.3.4 | MIT License | https://tailwindcss.com */
2
+ @layer properties{@supports (((-webkit-hyphens:none)) and (not (margin-trim:block))) or ((-moz-orient:inline) and (not (color:rgb(from red r g b)))){*,:before,:after,::backdrop{--tw-border-style:solid;--tw-leading:initial;--tw-font-weight:initial}}}@layer theme{:root,:host{--font-sans:-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", "Noto Sans", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";--font-mono:ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;--color-red-300:oklch(80.8% .114 19.571);--color-red-400:oklch(70.4% .191 22.216);--color-red-800:oklch(44.4% .177 26.899);--color-red-950:oklch(25.8% .092 26.042);--color-amber-400:oklch(82.8% .189 84.429);--color-emerald-300:oklch(84.5% .143 164.978);--color-emerald-400:oklch(76.5% .177 163.223);--color-emerald-800:oklch(43.2% .095 166.913);--color-emerald-950:oklch(26.2% .051 172.552);--color-sky-400:oklch(74.6% .16 232.661);--spacing:.25rem;--container-3xl:48rem;--text-xs:.75rem;--text-xs--line-height:calc(1 / .75);--text-sm:.875rem;--text-sm--line-height:calc(1.25 / .875);--text-lg:1.125rem;--text-lg--line-height:calc(1.75 / 1.125);--font-weight-semibold:600;--radius-xs:.125rem;--radius-lg:.5rem;--default-transition-duration:.15s;--default-transition-timing-function:cubic-bezier(.4, 0, .2, 1);--default-font-family:var(--font-sans);--default-mono-font-family:var(--font-mono);--color-canvas:oklch(13% .028 261.692);--color-surface:oklch(21% .034 264.665);--color-raised:oklch(27.8% .033 256.848);--color-edge:oklch(27.8% .033 256.848);--color-fg:oklch(92.8% .006 264.531);--color-fg-muted:oklch(70.7% .022 261.325);--color-accent:oklch(74.6% .16 232.661);--color-accent-hover:oklch(82.8% .111 230.318)}}@layer base{*,:after,:before,::backdrop{box-sizing:border-box;border:0 solid;margin:0;padding:0}::file-selector-button{box-sizing:border-box;border:0 solid;margin:0;padding:0}html,:host{-webkit-text-size-adjust:100%;tab-size:4;line-height:1.5;font-family:var(--default-font-family,-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", "Noto Sans", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji");font-feature-settings:var(--default-font-feature-settings,normal);font-variation-settings:var(--default-font-variation-settings,normal);-webkit-tap-highlight-color:transparent}hr{height:0;color:inherit;border-top-width:1px}abbr:where([title]){-webkit-text-decoration:underline dotted;text-decoration:underline dotted}h1,h2,h3,h4,h5,h6{font-size:inherit;font-weight:inherit}a{color:inherit;-webkit-text-decoration:inherit;-webkit-text-decoration:inherit;-webkit-text-decoration:inherit;text-decoration:inherit}b,strong{font-weight:bolder}code,kbd,samp,pre{font-family:var(--default-mono-font-family,ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace);font-feature-settings:var(--default-mono-font-feature-settings,normal);font-variation-settings:var(--default-mono-font-variation-settings,normal);font-size:1em}small{font-size:80%}sub,sup{vertical-align:baseline;font-size:75%;line-height:0;position:relative}sub{bottom:-.25em}sup{top:-.5em}table{text-indent:0;border-color:inherit;border-collapse:collapse}:-moz-focusring:where(:not(iframe)){outline:auto}progress{vertical-align:baseline}summary{display:list-item}ol,ul,menu{list-style:none}img,svg,video,canvas,audio,iframe,embed,object{vertical-align:middle;display:block}img,video{max-width:100%;height:auto}button,input,select,optgroup,textarea{font:inherit;font-feature-settings:inherit;font-variation-settings:inherit;letter-spacing:inherit;color:inherit;opacity:1;background-color:#0000;border-radius:0}::file-selector-button{font:inherit;font-feature-settings:inherit;font-variation-settings:inherit;letter-spacing:inherit;color:inherit;opacity:1;background-color:#0000;border-radius:0}:where(select:is([multiple],[size])) optgroup{font-weight:bolder}:where(select:is([multiple],[size])) optgroup option{padding-inline-start:20px}::file-selector-button{margin-inline-end:4px}::placeholder{opacity:1}@supports (not ((-webkit-appearance:-apple-pay-button))) or (contain-intrinsic-size:1px){::placeholder{color:currentColor}@supports (color:color-mix(in lab, red, red)){::placeholder{color:color-mix(in oklab, currentcolor 50%, transparent)}}}textarea{resize:vertical}::-webkit-search-decoration{-webkit-appearance:none}::-webkit-date-and-time-value{min-height:1lh;text-align:inherit}::-webkit-datetime-edit{display:inline-flex}::-webkit-datetime-edit-fields-wrapper{padding:0}::-webkit-datetime-edit{padding-block:0}::-webkit-datetime-edit-year-field{padding-block:0}::-webkit-datetime-edit-month-field{padding-block:0}::-webkit-datetime-edit-day-field{padding-block:0}::-webkit-datetime-edit-hour-field{padding-block:0}::-webkit-datetime-edit-minute-field{padding-block:0}::-webkit-datetime-edit-second-field{padding-block:0}::-webkit-datetime-edit-millisecond-field{padding-block:0}::-webkit-datetime-edit-meridiem-field{padding-block:0}::-webkit-calendar-picker-indicator{line-height:1}:-moz-ui-invalid{box-shadow:none}button,input:where([type=button],[type=reset],[type=submit]){appearance:button}::file-selector-button{appearance:button}::-webkit-inner-spin-button{height:auto}::-webkit-outer-spin-button{height:auto}[hidden]:where(:not([hidden=until-found])){display:none!important}}@layer components;@layer utilities{.visible{visibility:visible}.absolute{position:absolute}.fixed{position:fixed}.relative{position:relative}.static{position:static}.sticky{position:sticky}.-top-1{top:calc(var(--spacing) * -1)}.top-0{top:0}.top-2{top:calc(var(--spacing) * 2)}.top-11{top:calc(var(--spacing) * 11)}.-left-1{left:calc(var(--spacing) * -1)}.left-4{left:calc(var(--spacing) * 4)}.z-10{z-index:10}.col-span-4{grid-column:span 4/span 4}.col-span-full{grid-column:1/-1}.col-start-1{grid-column-start:1}.col-start-5{grid-column-start:5}.ml-auto{margin-left:auto}.block{display:block}.flex{display:flex}.grid{display:grid}.hidden{display:none}.inline{display:inline}.inline-flex{display:inline-flex}.table{display:table}.size-2{width:calc(var(--spacing) * 2);height:calc(var(--spacing) * 2)}.h-screen{height:100vh}.min-h-screen{min-height:100vh}.w-3{width:calc(var(--spacing) * 3)}.w-4{width:calc(var(--spacing) * 4)}.w-36{width:calc(var(--spacing) * 36)}.w-80{width:calc(var(--spacing) * 80)}.max-w-3xl{max-width:var(--container-3xl)}.min-w-0{min-width:0}.shrink-0{flex-shrink:0}.grow{flex-grow:1}.rotate-45{rotate:45deg}.cursor-pointer{cursor:pointer}.scroll-mt-2{scroll-margin-top:calc(var(--spacing) * 2)}.list-none{list-style-type:none}.grid-cols-\[3\.25rem_minmax\(0\,1fr\)_3\.25rem_minmax\(0\,1fr\)\]{grid-template-columns:3.25rem minmax(0,1fr) 3.25rem minmax(0,1fr)}.grid-cols-\[3\.25rem_minmax\(0\,1fr\)_3\.25rem_minmax\(0\,1fr\)_20rem\]{grid-template-columns:3.25rem minmax(0,1fr) 3.25rem minmax(0,1fr) 20rem}.grid-cols-subgrid{grid-template-columns:subgrid}.flex-col{flex-direction:column}.flex-wrap{flex-wrap:wrap}.content-start{align-content:flex-start}.items-center{align-items:center}.items-start{align-items:flex-start}.gap-2{gap:calc(var(--spacing) * 2)}.gap-3{gap:calc(var(--spacing) * 3)}.gap-4{gap:calc(var(--spacing) * 4)}.gap-x-6{column-gap:calc(var(--spacing) * 6)}.gap-y-2{row-gap:calc(var(--spacing) * 2)}.self-start{align-self:flex-start}.truncate{text-overflow:ellipsis;white-space:nowrap;overflow:hidden}.overflow-y-auto{overflow-y:auto}.rounded{border-radius:.25rem}.rounded-full{border-radius:2147483647px}.rounded-lg{border-radius:var(--radius-lg)}.rounded-xs{border-radius:var(--radius-xs)}.border{border-style:var(--tw-border-style);border-width:1px}.border-t{border-top-style:var(--tw-border-style);border-top-width:1px}.border-r{border-right-style:var(--tw-border-style);border-right-width:1px}.border-b{border-bottom-style:var(--tw-border-style);border-bottom-width:1px}.border-l{border-left-style:var(--tw-border-style);border-left-width:1px}.border-edge{border-color:var(--color-edge)}.bg-canvas{background-color:var(--color-canvas)}.bg-emerald-400{background-color:var(--color-emerald-400)}.bg-emerald-800{background-color:var(--color-emerald-800)}.bg-emerald-950{background-color:var(--color-emerald-950)}.bg-emerald-950\/50{background-color:#002c2280}@supports (color:color-mix(in lab, red, red)){.bg-emerald-950\/50{background-color:color-mix(in oklab, var(--color-emerald-950) 50%, transparent)}}.bg-raised{background-color:var(--color-raised)}.bg-red-800{background-color:var(--color-red-800)}.bg-red-950{background-color:var(--color-red-950)}.bg-red-950\/50{background-color:#46080980}@supports (color:color-mix(in lab, red, red)){.bg-red-950\/50{background-color:color-mix(in oklab, var(--color-red-950) 50%, transparent)}}.bg-surface{background-color:var(--color-surface)}.p-3{padding:calc(var(--spacing) * 3)}.p-12{padding:calc(var(--spacing) * 12)}.px-1{padding-inline:var(--spacing)}.px-2{padding-inline:calc(var(--spacing) * 2)}.px-3{padding-inline:calc(var(--spacing) * 3)}.px-4{padding-inline:calc(var(--spacing) * 4)}.py-0{padding-block:0}.py-0\.5{padding-block:calc(var(--spacing) * .5)}.py-1{padding-block:var(--spacing)}.py-2{padding-block:calc(var(--spacing) * 2)}.py-3{padding-block:calc(var(--spacing) * 3)}.pt-2{padding-top:calc(var(--spacing) * 2)}.pb-1{padding-bottom:var(--spacing)}.pb-3{padding-bottom:calc(var(--spacing) * 3)}.text-center{text-align:center}.text-right{text-align:right}.font-mono{font-family:var(--font-mono)}.font-sans{font-family:var(--font-sans)}.text-lg{font-size:var(--text-lg);line-height:var(--tw-leading,var(--text-lg--line-height))}.text-sm{font-size:var(--text-sm);line-height:var(--tw-leading,var(--text-sm--line-height))}.text-xs{font-size:var(--text-xs);line-height:var(--tw-leading,var(--text-xs--line-height))}.leading-5{--tw-leading:calc(var(--spacing) * 5);line-height:calc(var(--spacing) * 5)}.font-semibold{--tw-font-weight:var(--font-weight-semibold);font-weight:var(--font-weight-semibold)}.wrap-anywhere{overflow-wrap:anywhere}.break-all{word-break:break-all}.whitespace-nowrap{white-space:nowrap}.whitespace-pre-wrap{white-space:pre-wrap}.\[tab-size\:4\]{tab-size:4}.text-accent{color:var(--color-accent)}.text-amber-400{color:var(--color-amber-400)}.text-emerald-300{color:var(--color-emerald-300)}.text-emerald-400{color:var(--color-emerald-400)}.text-fg{color:var(--color-fg)}.text-fg-muted{color:var(--color-fg-muted)}.text-red-300{color:var(--color-red-300)}.text-red-400{color:var(--color-red-400)}.text-sky-400{color:var(--color-sky-400)}.line-through{text-decoration-line:line-through}.scheme-dark{color-scheme:dark}.opacity-30{opacity:.3}.transition-opacity{transition-property:opacity;transition-timing-function:var(--tw-ease,var(--default-transition-timing-function));transition-duration:var(--tw-duration,var(--default-transition-duration))}.transition-transform{transition-property:transform,translate,scale,rotate;transition-timing-function:var(--tw-ease,var(--default-transition-timing-function));transition-duration:var(--tw-duration,var(--default-transition-duration))}.select-none{-webkit-user-select:none;user-select:none}:is(:where(.group):is([open],:popover-open,:open) .group-open\:hidden){display:none}:is(:where(.group):is([open],:popover-open,:open) .group-open\:rotate-90){rotate:90deg}.placeholder\:text-fg-muted::placeholder{color:var(--color-fg-muted)}@media (hover:hover){.hover\:border-accent:hover{border-color:var(--color-accent)}.hover\:bg-surface:hover{background-color:var(--color-surface)}.hover\:text-accent-hover:hover{color:var(--color-accent-hover)}}.focus\:border-accent:focus{border-color:var(--color-accent)}.focus\:outline-none:focus{--tw-outline-style:none;outline-style:none}@media (min-width:64rem){.lg\:flex{display:flex}}}@property --tw-border-style{syntax:"*";inherits:false;initial-value:solid}@property --tw-leading{syntax:"*";inherits:false}@property --tw-font-weight{syntax:"*";inherits:false}
@@ -0,0 +1,27 @@
1
+ module GitDiffLive
2
+
3
+ # Base controller of the engine. It inherits from ActionController::Base, not
4
+ # the host's ApplicationController, so the host's sign-in and callbacks do
5
+ # not apply: nobody signs in to read a diff of their own working tree.
6
+ #
7
+ # What keeps the page private is that it only answers where it is enabled
8
+ # (Configuration#enabled?: the development and test environments unless the
9
+ # host says otherwise). A mount that reaches production by mistake serves
10
+ # nothing but 404s.
11
+ class ApplicationController < ActionController::Base
12
+
13
+ layout "git_diff_live/application"
14
+
15
+ protect_from_forgery with: :exception
16
+
17
+ before_action :refuse_unless_enabled
18
+
19
+ private
20
+
21
+ def refuse_unless_enabled
22
+ raise ActionController::RoutingError, "Not Found" unless GitDiffLive.configuration.enabled?
23
+ end
24
+
25
+ end
26
+
27
+ end
@@ -0,0 +1,73 @@
1
+ module GitDiffLive
2
+
3
+ # The changes page: every file that differs between the working tree and a
4
+ # commit, old on the left and new on the right (GitDiffLive::Diff).
5
+ #
6
+ # +base+ is the commit to compare with, HEAD unless given: any revision git
7
+ # understands (a SHA, a branch, HEAD~3). +head+ is the commit to compare
8
+ # with it instead of the working tree: base=HEAD~1&head=HEAD is the last
9
+ # commit on its own.
10
+ #
11
+ # Beside each change goes the comment written about it, if there is one
12
+ # (GitDiffLive::Comments).
13
+ #
14
+ # == Live reload
15
+ #
16
+ # The page is drawn once (index) and then kept up to date in pieces. Every
17
+ # few seconds it asks for the diff's fingerprint; when that is no longer the
18
+ # one it holds, it posts the digest of every file it shows to refresh, which
19
+ # answers with Turbo Streams for the difference only: the files that
20
+ # changed, came or went, and the counts. A page of two hundred files is not
21
+ # drawn again because one of them was saved.
22
+ class ChangesController < ApplicationController
23
+
24
+ rescue_from Diff::UnknownRevision, with: :unknown_revision
25
+
26
+ before_action :set_diff
27
+
28
+ # GET / — all changed files, each with the lines around its changes.
29
+ def index
30
+ @files = @diff.files
31
+ end
32
+
33
+ # GET /file?path= — one file, redrawn in place as a Turbo Stream: the
34
+ # "Whole file", "Changes only" and "Show them" links. +whole+ draws the
35
+ # complete file, +show+ draws it although it is large.
36
+ def file
37
+ @file = @diff.file(params[:path].to_s)
38
+
39
+ return head :not_found if @file.nil?
40
+
41
+ @file = @diff.whole(@file) if params[:whole].present?
42
+ @shown = params[:show].present?
43
+ end
44
+
45
+ # GET /fingerprint — the digest of the diff as it is now.
46
+ def fingerprint
47
+ render plain: @diff.fingerprint
48
+ end
49
+
50
+ # POST /refresh — what the page holds (paths and digests, in step; whole
51
+ # and shown name the files it draws whole, or draws although large), and
52
+ # back come the streams that bring it up to date.
53
+ def refresh
54
+ @files = @diff.files
55
+ @held = Array(params[:paths]).map(&:to_s).zip(Array(params[:digests]).map(&:to_s)).to_h
56
+ @whole = Array(params[:whole]).map(&:to_s)
57
+ @shown = Array(params[:shown]).map(&:to_s)
58
+ @gone = @held.keys - @files.map(&:path)
59
+ end
60
+
61
+ private
62
+
63
+ def set_diff
64
+ @diff = Diff.new(root: GitDiffLive.configuration.root, base: params[:base].presence || "HEAD", head: params[:head].presence, context: GitDiffLive.configuration.context)
65
+ end
66
+
67
+ def unknown_revision(error)
68
+ render plain: error.message, status: :not_found
69
+ end
70
+
71
+ end
72
+
73
+ end
@@ -0,0 +1,134 @@
1
+ module GitDiffLive
2
+
3
+ # The cells and links of the changes page. Its colours are fixed and dark
4
+ # (tailwind/git_diff_live.css); every class named here is compiled into the
5
+ # gem's stylesheet by `rake css`.
6
+ module ChangesHelper
7
+
8
+ BUTTON = "inline-flex items-center gap-2 px-3 py-1 rounded border border-edge bg-raised text-fg whitespace-nowrap cursor-pointer hover:border-accent"
9
+
10
+ LINK = "text-accent hover:text-accent-hover whitespace-nowrap"
11
+
12
+ FIELD = "w-36 px-2 py-1 rounded border border-edge bg-canvas font-mono text-fg placeholder:text-fg-muted focus:outline-none focus:border-accent"
13
+
14
+ # The line-number cell of one side of a row, by the line's kind.
15
+ NUMBER_CLASSES = {
16
+ context: "text-fg-muted",
17
+ removed: "bg-red-950 text-red-300",
18
+ added: "bg-emerald-950 text-emerald-300"
19
+ }.freeze
20
+
21
+ # The code cell.
22
+ CODE_CLASSES = {
23
+ context: "",
24
+ removed: "bg-red-950/50",
25
+ added: "bg-emerald-950/50"
26
+ }.freeze
27
+
28
+ # The part of a paired line that differs from its partner.
29
+ MARK_CLASSES = {
30
+ removed: "rounded-xs bg-red-800",
31
+ added: "rounded-xs bg-emerald-800"
32
+ }.freeze
33
+
34
+ # The comparison's columns: old number, old line, new number, new line,
35
+ # and the comments when the file has any.
36
+ GRID = "grid-cols-[3.25rem_minmax(0,1fr)_3.25rem_minmax(0,1fr)]"
37
+
38
+ COMMENTED_GRID = "grid-cols-[3.25rem_minmax(0,1fr)_3.25rem_minmax(0,1fr)_20rem]"
39
+
40
+ def change_button_classes
41
+ BUTTON
42
+ end
43
+
44
+ def change_link_classes
45
+ LINK
46
+ end
47
+
48
+ def change_field_classes
49
+ FIELD
50
+ end
51
+
52
+ def change_number_classes(line)
53
+ NUMBER_CLASSES.fetch(line.kind)
54
+ end
55
+
56
+ def change_code_classes(line)
57
+ CODE_CLASSES.fetch(line.kind)
58
+ end
59
+
60
+ # A line's text, the changed stretches marked. Built here and not in the
61
+ # template so no whitespace gets between the pieces: the cell is
62
+ # whitespace-pre-wrap and would show it.
63
+ def change_code(line)
64
+ safe_join(line.segments.map { | text, changed | changed ? tag.span(text, class: MARK_CLASSES.fetch(line.kind)) : text })
65
+ end
66
+
67
+ def change_grid_classes(file)
68
+ file.placed_comments.any? ? COMMENTED_GRID : GRID
69
+ end
70
+
71
+ # A hunk's rows in groups, each with the comments that go beside it:
72
+ # [ rows, comments ]. A group begins at every row that has a comment and
73
+ # runs to the next, so a comment has all the rows under it to be as tall
74
+ # as it needs.
75
+ def change_row_groups(file, hunk)
76
+ hunk.rows.slice_before { file.comments_beside(it).any? }.map { | rows | [ rows, file.comments_beside(rows.first) ] }
77
+ end
78
+
79
+ # A comment's text, what it puts between backticks set as code.
80
+ def change_comment(comment)
81
+ safe_join(comment.body.split(/(`[^`]+`)/).map { it.match?(/\A`.+`\z/) ? tag.code(it[1..-2], class: "px-1 rounded-xs bg-canvas font-mono") : it })
82
+ end
83
+
84
+ # The comments on a file as one line of plain text, for the heading of a
85
+ # closed file: there the backticks would only be noise.
86
+ def change_comment_line(file)
87
+ file.file_comments.map(&:body).join(" ").delete("`")
88
+ end
89
+
90
+ # The status glyph beside a file's name. Each icon is a literal icon(:fas, :name)
91
+ # call so fontawesome_subsetter finds it.
92
+ def change_status_icon(file)
93
+ case file.status
94
+ when :added then tag.span(icon(:fas, :plus), class: "text-emerald-400", title: file.untracked? ? "Added (untracked)" : "Added")
95
+ when :deleted then tag.span(icon(:fas, :minus), class: "text-red-400", title: "Deleted")
96
+ when :renamed then tag.span(icon(:fas, :right_left), class: "text-sky-400", title: "Renamed")
97
+ when :copied then tag.span(icon(:fas, :right_left), class: "text-sky-400", title: "Copied")
98
+ when :unmerged then tag.span(icon(:fas, :ban), class: "text-amber-400", title: "Unmerged")
99
+ else tag.span(icon(:fas, :pen), class: "text-amber-400", title: "Modified")
100
+ end
101
+ end
102
+
103
+ # "+12 −3", each number in its colour, zeros left out.
104
+ def change_counts(additions, deletions)
105
+ safe_join([
106
+ (tag.span("+#{ number_with_delimiter additions }", class: "text-emerald-400") if additions.positive?),
107
+ (tag.span("−#{ number_with_delimiter deletions }", class: "text-red-400") if deletions.positive?)
108
+ ].compact, " ")
109
+ end
110
+
111
+ # The query that keeps a link on the commits the page is comparing.
112
+ def change_range_params
113
+ { base: params[:base].presence, head: params[:head].presence }.compact
114
+ end
115
+
116
+ # A link the browser hands to an editor, which opens the file at that line.
117
+ # By default VS Code. A server running under WSL names its distro so the
118
+ # link reaches the Remote - WSL window: a Windows browser cannot open a
119
+ # Linux path as a local file.
120
+ def change_editor_url(path, line)
121
+ escaped = path.to_s.split("/").map { ERB::Util.url_encode(it) }.join("/")
122
+
123
+ if (template = GitDiffLive.configuration.editor_url)
124
+ format(template, path: escaped, line: line)
125
+ else
126
+ distro = ENV["WSL_DISTRO_NAME"]
127
+
128
+ "#{ distro.present? ? "vscode://vscode-remote/wsl+#{ distro }" : "vscode://file" }#{ escaped }:#{ line }"
129
+ end
130
+ end
131
+
132
+ end
133
+
134
+ end
@@ -0,0 +1,9 @@
1
+ // The changes page's JavaScript: Turbo (the page redraws itself by morphing)
2
+ // and the one Stimulus controller on it.
3
+ import "@hotwired/turbo-rails"
4
+ import { Application } from "@hotwired/stimulus"
5
+ import ChangesController from "controllers/changes_controller"
6
+
7
+ const application = Application.start()
8
+
9
+ application.register("changes", ChangesController)
@@ -0,0 +1,114 @@
1
+ /*
2
+
3
+ The changes page (git_diff_live/changes/index).
4
+
5
+ Live reload: every interval it asks the server for the diff's fingerprint.
6
+ When that is no longer the one the page was drawn from (the summary carries
7
+ it), it posts what the page holds, each file's path and digest and whether it
8
+ is drawn whole or shown although large, and renders the Turbo Streams that
9
+ come back: only the files that changed, came or went, and the counts. A
10
+ hidden tab does not poll; it catches up when it is shown again.
11
+
12
+ Each file is a <details>, so opening and closing one needs no JavaScript. This
13
+ controller opens or closes them all, and keeps a file the way the reader left
14
+ it when a refresh morphs it (the server always draws a file open, or closed if
15
+ it was deleted).
16
+
17
+ USAGE:
18
+
19
+ div data-controller="changes" data-changes-fingerprint-url-value="/changes/fingerprint" data-changes-refresh-url-value="/changes/refresh" data-changes-interval-value="2000" data-action="turbo:before-morph-attribute->changes#keepOpenState"
20
+ div data-changes-target="summary" data-fingerprint="3f2a…"
21
+ span data-changes-target="status"
22
+ button data-action="changes#expand"
23
+ button data-action="changes#collapse"
24
+ details data-changes-target="file" data-path="app/models/user.rb" data-digest="9c1e…" data-whole="true" data-shown="true"
25
+
26
+ */
27
+
28
+ import { Controller } from "@hotwired/stimulus"
29
+ import { Turbo } from "@hotwired/turbo-rails"
30
+
31
+ export default class extends Controller {
32
+
33
+ static targets = [ "file", "status", "summary" ]
34
+
35
+ static values = { fingerprintUrl: String, refreshUrl: String, interval: Number }
36
+
37
+ connect() {
38
+ if (this.intervalValue <= 0) return
39
+
40
+ this.poll = this.poll.bind(this)
41
+ this.timer = setInterval(this.poll, this.intervalValue)
42
+
43
+ document.addEventListener("visibilitychange", this.poll)
44
+ }
45
+
46
+ disconnect() {
47
+ clearInterval(this.timer)
48
+
49
+ document.removeEventListener("visibilitychange", this.poll)
50
+ }
51
+
52
+ async poll() {
53
+ if (document.hidden || this.polling) return
54
+
55
+ this.polling = true
56
+
57
+ try {
58
+ const response = await fetch(this.fingerprintUrlValue, { headers: { Accept: "text/plain" } })
59
+
60
+ this.connected = response.ok
61
+
62
+ if (response.ok && (await response.text()) !== this.summaryTarget.dataset.fingerprint)
63
+ await this.refresh()
64
+ } catch {
65
+ // The server is restarting or gone; the next tick tries again.
66
+ this.connected = false
67
+ } finally {
68
+ this.polling = false
69
+ }
70
+ }
71
+
72
+ // The summary in the answer carries the new fingerprint, so a refresh that
73
+ // fails leaves the old one in place and the next tick tries again.
74
+ async refresh() {
75
+ const held = new FormData()
76
+
77
+ this.fileTargets.forEach(file => {
78
+ held.append("paths[]", file.dataset.path)
79
+ held.append("digests[]", file.dataset.digest)
80
+
81
+ if (file.dataset.whole) held.append("whole[]", file.dataset.path)
82
+ if (file.dataset.shown) held.append("shown[]", file.dataset.path)
83
+ })
84
+
85
+ const response = await fetch(this.refreshUrlValue, {
86
+ method: "POST",
87
+ headers: { Accept: "text/vnd.turbo-stream.html", "X-CSRF-Token": document.querySelector("meta[name=csrf-token]")?.content },
88
+ body: held
89
+ })
90
+
91
+ if (response.ok)
92
+ Turbo.renderStreamMessage(await response.text())
93
+ }
94
+
95
+ // The live marker dims while the server does not answer.
96
+ set connected(connected) {
97
+ if (this.hasStatusTarget)
98
+ this.statusTarget.classList.toggle("opacity-30", !connected)
99
+ }
100
+
101
+ keepOpenState(event) {
102
+ if (event.detail.attributeName === "open")
103
+ event.preventDefault()
104
+ }
105
+
106
+ expand() {
107
+ this.fileTargets.forEach(file => file.open = true)
108
+ }
109
+
110
+ collapse() {
111
+ this.fileTargets.forEach(file => file.open = false)
112
+ }
113
+
114
+ }
@@ -0,0 +1,3 @@
1
+ / What the page says when there is nothing to show. Always there, hidden while there are files, so a refresh can bring it back.
2
+ #changes_empty class="p-12 text-center text-fg-muted #{ "hidden" if @files.any? }"
3
+ | Nothing has changed: #{ @diff.working_tree? ? "the working tree" : @diff.head } matches #{ @diff.base }.