git-tm 1.1.2 → 2.0.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.
package/README.md CHANGED
@@ -6,7 +6,10 @@
6
6
  <p>Explore any Git repository's history in your browser.<br>
7
7
  Commit graph, diffs, blame, insights and time travel between any two commits. One command, zero dependencies.</p>
8
8
 
9
+ <a href="https://github.com/nandhu-44/Git-Time-Machine/actions/workflows/ci.yml"><img src="https://github.com/nandhu-44/Git-Time-Machine/actions/workflows/ci.yml/badge.svg?branch=main" alt="CI status"></a>
9
10
  <a href="https://www.npmjs.com/package/git-tm"><img src="https://img.shields.io/npm/v/git-tm?color=f05133" alt="npm version"></a>
11
+ <a href="https://www.npmjs.com/package/git-tm"><img src="https://img.shields.io/npm/dm/git-tm?color=f05133" alt="npm downloads per month"></a>
12
+ <img src="https://img.shields.io/node/v/git-tm?color=339933" alt="node version">
10
13
  <img src="https://img.shields.io/badge/dependencies-0-2ea44f" alt="zero dependencies">
11
14
  <img src="https://img.shields.io/npm/l/git-tm" alt="license">
12
15
  <a href="https://git-time-machine.js.org"><img src="https://img.shields.io/badge/docs-git--time--machine.js.org-0969da" alt="documentation"></a>
@@ -50,12 +53,15 @@ git tm
50
53
 
51
54
  `git -C ../other-repo tm` works too. Use `git tm -h` for help: Git handles `git tm --help` itself and looks for a manual page instead.
52
55
 
56
+ While it runs, press `o` in the terminal to open the browser, `r` to restart the server (picks up repository changes), `h` to list these keys and `q` (or `Ctrl+C`) to stop.
57
+
53
58
  | Option | Description |
54
59
  | --- | --- |
55
60
  | `-p, --port <number>` | Port to listen on (default `7890`; the next free port is used if it is busy) |
56
61
  | `--host <address>` | Interface to bind (default `127.0.0.1`) |
57
62
  | `-o, --open` | Open the browser once the server is running |
58
63
  | `--no-avatars` | Do not load author avatars from GitHub or Gravatar |
64
+ | `--edit` | Allow changing history: reword, change author or date, squash, drop and revert (off by default; see [Edit mode](#edit-mode)) |
59
65
  | `-v, --version` | Print the version |
60
66
  | `-h, --help` | Print help |
61
67
 
@@ -70,6 +76,7 @@ git tm
70
76
  **Commits**
71
77
  - Full message, author and committer, parents, and every branch and tag that contains the commit
72
78
  - Unified or split diffs with word-level highlights, a hide-whitespace toggle and full-file context
79
+ - Syntax highlighting in diffs and the file viewer for common languages (JavaScript and TypeScript, Python, Go, Rust, C and C++, Java, Ruby, shell, HTML, CSS, JSON, YAML, SQL, Markdown, Dockerfiles), with no dependencies
73
80
  - Merge commits can be diffed against any parent
74
81
  - Open the commit on GitHub, GitLab or Bitbucket when the repository has a remote
75
82
 
@@ -85,6 +92,11 @@ git tm
85
92
  - Pick commits with a searchable picker, with *Time travel* mode in History (click From, then To), from a commit's detail pane, or by shift-clicking a second commit
86
93
  - *Direct* (`a..b`) or *since branch point* (`a...b`, like a pull request)
87
94
 
95
+ **Reflog**
96
+ - Recent `HEAD` movements: commits, checkouts, resets, rebases, merges and pulls
97
+ - Commits that are no longer on any branch or tag are marked *lost*, with copyable commands to recover them
98
+ - Read-only like everything else: git-tm shows the commands, you decide whether to run them
99
+
88
100
  **Search**
89
101
 
90
102
  | Query | Finds |
@@ -102,9 +114,36 @@ git tm
102
114
  - Light and dark themes (follows your system by default)
103
115
  - Notices when the repository changes on disk and offers to refresh; if the git-tm process stops, the page says so and reconnects when it is back
104
116
  - Fetch all remotes with one click
105
- - Keyboard shortcuts: `j`/`k` to move, `Enter` to open, `t` for time travel mode, `←`/`→` to step through a journey, `/` to search, `Esc` to close, `g h`/`g c`/`g i` to switch views, `?` for help
117
+ - Keyboard shortcuts: `j`/`k` to move, `Enter` to open, `t` for time travel mode, `←`/`→` to step through a journey, `/` to search, `Esc` to close, `g h`/`g c`/`g r`/`g i` to switch views, `?` for help. Right-click a commit or a branch for quick actions such as copying the SHA or comparing
106
118
  - Works on phones and tablets
107
119
 
120
+ ## Edit mode
121
+
122
+ Start git-tm with `--edit` and the time machine can change the past:
123
+
124
+ ```bash
125
+ git tm --edit
126
+ ```
127
+
128
+ Every commit gets an **Edit** button (and an *Edit commit* item when you right-click it in History):
129
+
130
+ - **Reword** the message, **change the author**, or just **change the date**
131
+ - **Squash** a commit into its parent
132
+ - **Drop** a commit and its changes
133
+ - **Revert** a commit: adds a new commit that undoes it, without rewriting anything
134
+
135
+ ![Edit mode previewing a reword: later commits get new IDs, with a warning that they are already pushed](docs/img/edit.jpg)
136
+
137
+ Edit mode is built to be hard to regret:
138
+
139
+ - **Off by default.** Without `--edit`, git-tm stays read-only. It only works on a local address, never with `--host 0.0.0.0`.
140
+ - **Preview first.** You see the new timeline before anything changes: which commits get new IDs, the file changes, and who still has the old commits.
141
+ - **Everything can be undone.** Before each edit, git-tm saves a backup of the branch under `refs/git-tm/backups/`. Undo it from the toast or from the Reflog view, or restore it as a new branch if you've moved on since.
142
+ - **Your work stays safe.** Message, author and squash edits never touch your working tree. Drop and revert refuse if you have staged changes, and stop if anything conflicts, leaving everything as it was.
143
+ - **Pushed commits** can be rewritten only after a clear warning: it means a force-push and a different history for everyone else. git-tm never pushes for you.
144
+
145
+ Each edit is printed in the terminal with its backup id. Drop and revert need Git 2.40 or newer. Backups stay until you delete them (`git for-each-ref refs/git-tm/backups/` lists them, `git update-ref -d <ref>` removes one).
146
+
108
147
  ## Use it from code
109
148
 
110
149
  git-tm is also a library, with TypeScript types included:
@@ -118,12 +157,12 @@ const trip = await repo.range("v1.0.0", "main");
118
157
  console.log(`${trip.addedCount} commits applied, ${trip.removedCount} undone`);
119
158
  ```
120
159
 
121
- Entry points: `git-tm`, `git-tm/git`, `git-tm/server` and `git-tm/graph`. See the [API documentation](https://git-time-machine.js.org/guide.html#api) for every method.
160
+ Entry points: `git-tm`, `git-tm/git`, `git-tm/server`, `git-tm/edit` and `git-tm/graph`. See the [API documentation](https://git-time-machine.js.org/guide.html#api) for every method.
122
161
 
123
162
  ## Privacy and safety
124
163
 
125
164
  - Everything runs locally. The server binds to `127.0.0.1` and rejects requests for other host names, so other websites cannot read your repository.
126
- - Read-only. Git Time Machine never checks out, commits or rewrites anything. The only command that touches the network is **Fetch**, and only when you press it.
165
+ - Read-only by default. Git Time Machine never checks out, commits or rewrites anything unless you start it with `--edit`, and even then only after a preview, with a backup you can undo. The only command that touches the network is **Fetch**, and only when you press it.
127
166
  - Author avatars are loaded from GitHub (for `users.noreply.github.com` addresses) or Gravatar. Use `--no-avatars` to keep everything offline; initials are shown instead.
128
167
 
129
168
  ## How it works
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "git-tm",
3
- "version": "1.1.2",
3
+ "version": "2.0.0",
4
4
  "description": "Explore any Git repository in your browser: commit graph, diffs, blame, insights and time travel between any two commits. Zero dependencies.",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
@@ -18,6 +18,10 @@
18
18
  "types": "./src/types/server.d.ts",
19
19
  "default": "./src/server.js"
20
20
  },
21
+ "./edit": {
22
+ "types": "./src/types/edit.d.ts",
23
+ "default": "./src/edit.js"
24
+ },
21
25
  "./graph": {
22
26
  "types": "./src/types/graph.d.ts",
23
27
  "default": "./src/public/js/graph.js"
package/src/cli.js CHANGED
@@ -6,6 +6,7 @@ import path from "node:path";
6
6
  import { parseArgs } from "node:util";
7
7
  import { openRepo } from "./git.js";
8
8
  import { createServer, listen } from "./server.js";
9
+ import { listenAgain } from "./listen-again.js";
9
10
 
10
11
  const pkg = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
11
12
 
@@ -17,6 +18,7 @@ const c = {
17
18
  red: paint("31"),
18
19
  green: paint("32"),
19
20
  cyan: paint("36"),
21
+ yellow: paint("33"),
20
22
  accent: (s) => (useColor ? `\x1b[38;2;240;81;51m${s}\x1b[0m` : String(s)),
21
23
  };
22
24
 
@@ -32,6 +34,7 @@ ${c.bold("Options")}
32
34
  --host <address> Interface to bind ${c.dim("(default 127.0.0.1)")}
33
35
  -o, --open Open the browser once the server is running
34
36
  --no-avatars Do not load author avatars from GitHub / Gravatar
37
+ --edit Allow changing history: reword, squash, drop, revert ${c.dim("(off by default)")}
35
38
  -v, --version Print the version
36
39
  -h, --help Print this help
37
40
 
@@ -59,6 +62,7 @@ function parse() {
59
62
  open: { type: "boolean", short: "o", default: false },
60
63
  "no-open": { type: "boolean", default: false }, // accepted for compatibility; opening is now opt-in
61
64
  "no-avatars": { type: "boolean", default: false },
65
+ edit: { type: "boolean", default: false },
62
66
  version: { type: "boolean", short: "v", default: false },
63
67
  help: { type: "boolean", short: "h", default: false },
64
68
  },
@@ -98,6 +102,10 @@ async function main() {
98
102
  const port = Number(values.port);
99
103
  if (!Number.isInteger(port) || port < 0 || port > 65535) fail(`Invalid port: ${values.port}`);
100
104
 
105
+ if (values.edit && !["127.0.0.1", "localhost", "::1", "[::1]"].includes(values.host)) {
106
+ fail("--edit only works on 127.0.0.1, localhost or ::1, so nothing else on your network can change your history");
107
+ }
108
+
101
109
  const target = path.resolve(positionals[0] ?? ".");
102
110
  let repo;
103
111
  try {
@@ -106,7 +114,57 @@ async function main() {
106
114
  fail(err.message);
107
115
  }
108
116
 
109
- const server = createServer(repo, { avatars: !values["no-avatars"], host: values.host });
117
+ let shortcutsShown = false;
118
+ const clock = () => {
119
+ const d = new Date();
120
+ const pad = (n) => String(n).padStart(2, "0");
121
+ return `${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`;
122
+ };
123
+ const log = (tag, text, extra = "") => {
124
+ shortcutsShown = false;
125
+ console.log(` ${c.dim(clock())} ${tag} ${text}${extra}`);
126
+ };
127
+
128
+ // In edit mode, log every change to the terminal as an audit trail.
129
+ const onEdit = (e) => {
130
+ const what =
131
+ e.op === "undo" ? `undid an edit on ${e.branch}.`
132
+ : e.op === "restore" ? `restored the old timeline as ${e.branch}.`
133
+ : e.summary || `${e.op} on ${e.branch}.`;
134
+ const backup = e.id ? c.dim(` (backup ${e.id})`) : "";
135
+ log(c.yellow("edit "), what, backup);
136
+ };
137
+
138
+ const shortRev = (r) => (r && /^[0-9a-f]{40,64}$/i.test(r) ? r.slice(0, 7) : r || "");
139
+ const onLog = ({ path: p, query: q, status, ms, error }) => {
140
+ const timing = c.dim(` (${ms} ms)`);
141
+ if (status >= 400) {
142
+ log(c.red(`${status} `), `${p}${error ? ` — ${error}` : ""}`, timing);
143
+ return;
144
+ }
145
+ if (p === "/api/history") {
146
+ if (Number(q.get("skip")) > 0) return;
147
+ const parts = [q.get("rev"), q.get("message"), q.get("author") && `author:${q.get("author")}`, q.get("path") && `path:${q.get("path")}`, q.get("code") && `code:${q.get("code")}`].filter(Boolean);
148
+ log(c.dim("history"), parts.length ? parts.join(" ") : "all branches", timing);
149
+ } else if (p === "/api/commit") {
150
+ log(c.dim("commit "), shortRev(q.get("rev")), timing);
151
+ } else if (p === "/api/range") {
152
+ log(c.dim("compare"), `${shortRev(q.get("from"))}..${shortRev(q.get("to"))}`, timing);
153
+ } else if (p === "/api/reflog") {
154
+ if (Number(q.get("skip")) > 0) return;
155
+ log(c.dim("reflog "), q.get("rev") || "HEAD", timing);
156
+ } else if (p === "/api/insights") {
157
+ log(c.dim("insight"), q.get("rev") || "all branches", timing);
158
+ } else if (p === "/api/blame") {
159
+ log(c.dim("blame "), `${q.get("path")} @ ${shortRev(q.get("rev"))}`, timing);
160
+ } else if (p === "/api/fetch") {
161
+ log(c.green("fetch "), "fetched all remotes", timing);
162
+ } else if (p === "/api/edit/preview") {
163
+ log(c.dim("preview"), "edit plan ready", timing);
164
+ }
165
+ };
166
+
167
+ let server = createServer(repo, { avatars: !values["no-avatars"], host: values.host, edit: values.edit, onEdit, onLog });
110
168
  let actual;
111
169
  try {
112
170
  actual = await listen(server, port, values.host);
@@ -114,9 +172,34 @@ async function main() {
114
172
  fail(`Could not start server: ${err.message}`);
115
173
  }
116
174
 
175
+ let rawModeEnabled = false;
176
+ let stopping = false;
177
+ let reloading = false;
178
+ let onKeypress;
179
+ const restoreTerminal = () => {
180
+ if (onKeypress) {
181
+ process.stdin.off("data", onKeypress);
182
+ onKeypress = undefined;
183
+ }
184
+ if (!rawModeEnabled) return;
185
+ try {
186
+ process.stdin.setRawMode(false);
187
+ } catch {
188
+ // The process may be exiting after the terminal has already disconnected.
189
+ }
190
+ rawModeEnabled = false;
191
+ process.stdin.pause();
192
+ };
193
+
194
+ // Restore terminal input even if another code path exits the process.
195
+ process.once("exit", restoreTerminal);
196
+
117
197
  // Handle Ctrl+C and SIGTERM before printing the address, so a signal that
118
198
  // arrives right after startup still shuts down cleanly with exit code 0.
119
199
  const stop = () => {
200
+ if (stopping) return;
201
+ stopping = true;
202
+ restoreTerminal();
120
203
  server.close();
121
204
  process.exit(0);
122
205
  };
@@ -128,20 +211,93 @@ async function main() {
128
211
  const info = await repo.info().catch(() => ({}));
129
212
  const where = info.branch ?? (info.head ? info.head.slice(0, 7) : "no commits yet");
130
213
 
214
+ const reload = async () => {
215
+ if (reloading || stopping) return;
216
+ reloading = true;
217
+ const t0 = performance.now();
218
+ try {
219
+ // Open the repository first: if that fails, the running server keeps serving.
220
+ const nextRepo = await openRepo(target);
221
+ const old = server;
222
+ await new Promise((resolve) => {
223
+ old.close(() => resolve()); // also resolves when a previous reload left it not listening
224
+ old.closeAllConnections?.();
225
+ });
226
+ repo = nextRepo;
227
+ server = createServer(repo, { avatars: !values["no-avatars"], host: values.host, edit: values.edit, onEdit, onLog });
228
+ await listenAgain(server, actual, values.host);
229
+ log(c.green("reload "), `restarted server on ${c.cyan(url)}`, c.dim(` (${Math.round(performance.now() - t0)} ms)`));
230
+ } catch (err) {
231
+ const why = err.code === "EADDRINUSE" ? `port ${actual} is in use by another program` : err.message;
232
+ log(c.red("reload "), `failed: ${why}. Press r to try again, or q to quit.`);
233
+ } finally {
234
+ reloading = false;
235
+ }
236
+ };
237
+
131
238
  console.log();
132
239
  console.log(` ${c.accent(c.bold("git-tm"))} ${c.dim(`v${pkg.version}`)}`);
133
240
  console.log();
134
241
  console.log(` ${c.dim("repo")} ${tildify(repo.root)} ${c.dim(`(${where})`)}`);
135
242
  console.log(` ${c.dim("url ")} ${c.cyan(url)}`);
136
243
  if (actual !== port && port !== 0) console.log(` ${c.dim(`port ${port} was busy, using ${actual}`)}`);
137
- console.log();
138
- console.log(c.dim(" Press Ctrl+C to stop"));
244
+ if (values.edit) console.log(` ${c.yellow("edit")} on: history can be changed. Every edit is backed up and can be undone. Mind the timeline.`);
139
245
  console.log();
140
246
 
141
247
  if (values.open) openBrowser(url);
142
248
 
249
+ let interactiveInput = false;
250
+ if (process.stdin.isTTY && typeof process.stdin.setRawMode === "function") {
251
+ try {
252
+ process.stdin.setRawMode(true);
253
+ rawModeEnabled = true;
254
+ onKeypress = (chunk) => {
255
+ for (const key of chunk.toString()) {
256
+ const normalizedKey = key.toLowerCase();
257
+ if (key === "\u0003" || normalizedKey === "q") {
258
+ stop();
259
+ return;
260
+ }
261
+ if (normalizedKey === "o") {
262
+ openBrowser(url);
263
+ log(c.dim("open "), c.cyan(url));
264
+ } else if (normalizedKey === "r") {
265
+ reload();
266
+ } else if (normalizedKey === "h" && !shortcutsShown) {
267
+ shortcutsShown = true;
268
+ console.log();
269
+ console.log(c.dim(" Keyboard shortcuts:"));
270
+ console.log(c.dim(" o Open browser"));
271
+ console.log(c.dim(" r Reload server"));
272
+ console.log(c.dim(" h Show shortcuts"));
273
+ console.log(c.dim(" q Quit server"));
274
+ console.log(c.dim(" Ctrl+C Quit server"));
275
+ console.log();
276
+ }
277
+ }
278
+ };
279
+ process.stdin.on("data", onKeypress);
280
+ process.stdin.resume();
281
+ interactiveInput = true;
282
+ } catch {
283
+ restoreTerminal();
284
+ }
285
+ }
286
+
287
+ console.log(
288
+ c.dim(
289
+ interactiveInput
290
+ ? " Press o to open, r to reload, h for help, q to quit (Ctrl+C to quit)"
291
+ : " Press Ctrl+C to stop",
292
+ ),
293
+ );
294
+ console.log();
295
+
143
296
  // Keep serving if something unexpected goes wrong; say what happened instead of dying silently.
144
- const report = (err) => console.error(`${c.red("error")} ${err?.stack || err}`);
297
+ const report = (err) => {
298
+ shortcutsShown = false;
299
+ console.error(`${c.red("error")} ${err?.stack || err}`);
300
+ };
145
301
  process.on("uncaughtException", report);
146
302
  process.on("unhandledRejection", report);
147
303
  }