git-tm 1.0.2 → 1.1.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
@@ -9,14 +9,15 @@
9
9
  <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>
10
10
  <img src="https://img.shields.io/badge/dependencies-0-2ea44f" alt="zero dependencies">
11
11
  <img src="https://img.shields.io/npm/l/git-tm" alt="license">
12
-
13
- <p><a href="https://git-time-machine.js.org"><b>Documentation</b></a> · <a href="https://www.npmjs.com/package/git-tm">npm</a></p>
12
+ <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>
14
13
  </div>
15
14
 
16
15
  <br>
17
16
 
18
17
  ![Git Time Machine showing a commit graph and a merge commit's changes](docs/screenshot.png)
19
18
 
19
+ > **Full documentation:** [git-time-machine.js.org](https://git-time-machine.js.org) covers every feature, time travel, keyboard shortcuts, troubleshooting and the JavaScript API.
20
+
20
21
  ## Install
21
22
 
22
23
  ```bash
@@ -104,6 +105,21 @@ git tm
104
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
105
106
  - Works on phones and tablets
106
107
 
108
+ ## Use it from code
109
+
110
+ git-tm is also a library, with TypeScript types included:
111
+
112
+ ```js
113
+ import { openRepo } from "git-tm";
114
+
115
+ const repo = await openRepo(".");
116
+ const { commits } = await repo.log({ ref: "main", limit: 20 });
117
+ const trip = await repo.range("v1.0.0", "main");
118
+ console.log(`${trip.addedCount} commits applied, ${trip.removedCount} undone`);
119
+ ```
120
+
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.
122
+
107
123
  ## Privacy and safety
108
124
 
109
125
  - 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.
@@ -117,17 +133,19 @@ git tm
117
133
  ```
118
134
  src/
119
135
  cli.js command-line entry point
136
+ index.js JavaScript API
120
137
  server.js HTTP server and JSON API
121
138
  git.js git commands and output parsing
122
139
  public/ the web interface
123
140
  js/graph.js commit graph layout
124
141
  js/diff.js diff rendering
142
+ types/ TypeScript declarations
125
143
  test/ node:test suites
126
144
  ```
127
145
 
128
- ## Development
146
+ ## Contributing
129
147
 
130
- There is nothing to install or build. Clone and run:
148
+ Contributions are welcome! There is nothing to install or build:
131
149
 
132
150
  ```bash
133
151
  git clone https://github.com/nandhu-44/Git-Time-Machine.git
@@ -135,47 +153,7 @@ cd Git-Time-Machine
135
153
  node src/cli.js .
136
154
  ```
137
155
 
138
- Run the tests:
139
-
140
- ```bash
141
- npm test
142
- ```
143
-
144
- Use your working copy as the global `git-tm` command (undo with `npm unlink -g git-tm`):
145
-
146
- ```bash
147
- npm link
148
- ```
149
-
150
- Check exactly what would be published:
151
-
152
- ```bash
153
- npm pack --dry-run
154
- ```
155
-
156
- ### Releasing
157
-
158
- Pushing a version tag publishes to npm from GitHub Actions ([`publish.yml`](.github/workflows/publish.yml)), using npm trusted publishing, so no npm token is stored anywhere.
159
-
160
- ```bash
161
- npm version patch
162
- ```
163
-
164
- ```bash
165
- git push origin main --follow-tags
166
- ```
167
-
168
- `npm version` bumps `package.json`, commits and creates the `vX.Y.Z` tag. Use `minor` or `major` as needed. The workflow checks that the tag matches `package.json`, runs the tests and publishes.
169
-
170
- GitLab CI ([`.gitlab-ci.yml`](.gitlab-ci.yml)) runs the same tests. It publishes only if an `NPM_TOKEN` CI/CD variable is set, so a mirrored tag is not published twice.
171
-
172
- ### Website
173
-
174
- The documentation site at [git-time-machine.js.org](https://git-time-machine.js.org) is plain HTML in [`docs/`](docs), served by GitHub Pages from the `main` branch's `/docs` folder. There is no build step; preview it with any static server:
175
-
176
- ```bash
177
- python3 -m http.server -d docs 8000
178
- ```
156
+ Run the tests with `npm test`. See [CONTRIBUTING.md](CONTRIBUTING.md) for how the project is organised, its principles and how to send a pull request.
179
157
 
180
158
  ## Background
181
159
 
package/package.json CHANGED
@@ -1,8 +1,29 @@
1
1
  {
2
2
  "name": "git-tm",
3
- "version": "1.0.2",
3
+ "version": "1.1.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
+ "main": "./src/index.js",
7
+ "types": "./src/types/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./src/types/index.d.ts",
11
+ "default": "./src/index.js"
12
+ },
13
+ "./git": {
14
+ "types": "./src/types/git.d.ts",
15
+ "default": "./src/git.js"
16
+ },
17
+ "./server": {
18
+ "types": "./src/types/server.d.ts",
19
+ "default": "./src/server.js"
20
+ },
21
+ "./graph": {
22
+ "types": "./src/types/graph.d.ts",
23
+ "default": "./src/public/js/graph.js"
24
+ },
25
+ "./package.json": "./package.json"
26
+ },
6
27
  "bin": {
7
28
  "git-tm": "src/cli.js"
8
29
  },
@@ -37,7 +58,8 @@
37
58
  "compare",
38
59
  "cli",
39
60
  "developer-tools",
40
- "visualization"
61
+ "visualization",
62
+ "api"
41
63
  ],
42
64
  "author": "nandhu-44",
43
65
  "license": "MIT",
package/src/index.js ADDED
@@ -0,0 +1,4 @@
1
+ // Public API. The CLI is src/cli.js; everything here can be imported as `git-tm`.
2
+ export { openRepo, git, GitError, WORKTREE, webUrl } from "./git.js";
3
+ export { createServer, listen } from "./server.js";
4
+ export { createLayout, layout } from "./public/js/graph.js";
@@ -0,0 +1,214 @@
1
+ /** Stands for the working tree (uncommitted changes) wherever a revision is accepted as a target. */
2
+ export const WORKTREE: "WORKTREE";
3
+
4
+ /** Error thrown for git failures and invalid input. `status` is an HTTP-style code (400, 404, 500, 504). */
5
+ export class GitError extends Error {
6
+ constructor(message: string, status?: number);
7
+ status: number;
8
+ }
9
+
10
+ export interface GitOptions {
11
+ /** Resolve with a Buffer instead of a string. */
12
+ raw?: boolean;
13
+ /** Written to git's stdin. */
14
+ input?: string | Buffer;
15
+ /** Stop reading after this many bytes of output; the result gets `truncated: true`. Default 64 MB. */
16
+ maxBytes?: number;
17
+ /** Kill git after this many milliseconds. Default 60 000. */
18
+ timeout?: number;
19
+ }
20
+
21
+ /** Run git in `cwd` with a clean, non-interactive environment and resolve with its stdout. */
22
+ export function git(cwd: string, args: string[], options: GitOptions & { raw: true }): Promise<Buffer & { truncated: boolean }>;
23
+ export function git(cwd: string, args: string[], options?: GitOptions): Promise<string>;
24
+
25
+ /** Open the repository containing `dir` (any folder inside it works). Throws GitError if there is none. */
26
+ export function openRepo(dir: string): Promise<Repo>;
27
+
28
+ /** Turn a remote URL (https or ssh) into a browsable https URL, or null. */
29
+ export function webUrl(url: string | null | undefined): string | null;
30
+
31
+ /** A branch, tag or HEAD decoration on a commit. */
32
+ export interface Ref {
33
+ type: "head" | "branch" | "remote" | "tag" | "detached";
34
+ name: string;
35
+ }
36
+
37
+ export interface CommitSummary {
38
+ hash: string;
39
+ parents: string[];
40
+ author: string;
41
+ email: string;
42
+ /** Author date, Unix seconds. */
43
+ date: number;
44
+ committer: string;
45
+ committerEmail: string;
46
+ /** Commit date, Unix seconds. */
47
+ commitDate: number;
48
+ refs: Ref[];
49
+ subject: string;
50
+ }
51
+
52
+ export interface Commit extends CommitSummary {
53
+ body: string;
54
+ }
55
+
56
+ export interface RepoInfo {
57
+ name: string;
58
+ root: string;
59
+ bare: boolean;
60
+ /** Current branch, or null when HEAD is detached. */
61
+ branch: string | null;
62
+ head: string | null;
63
+ branches: { name: string; target: string; upstream: string | null; ahead: number; behind: number; gone: boolean; date: number }[];
64
+ remotes: { name: string; remote: string; short: string; target: string; date: number }[];
65
+ tags: { name: string; target: string; annotated: boolean; message: string; date: number }[];
66
+ /** Number of changed or untracked paths in the working tree. */
67
+ changes: number;
68
+ emptyTree: string;
69
+ remoteUrl: string | null;
70
+ gitVersion: string | null;
71
+ }
72
+
73
+ export interface LogOptions {
74
+ /** Revision to start from. Default: HEAD plus every branch, remote and tag. */
75
+ ref?: string;
76
+ skip?: number;
77
+ /** Page size, 1 to 2000. Default 300. */
78
+ limit?: number;
79
+ /** Commit message contains this text (case-insensitive). */
80
+ message?: string;
81
+ /** Author name or email contains this text. */
82
+ author?: string;
83
+ /** Commits that add or remove this string (`git log -S`). */
84
+ code?: string;
85
+ /** Only commits touching this path. */
86
+ path?: string;
87
+ /** Follow renames of `path`. */
88
+ follow?: boolean;
89
+ }
90
+
91
+ export interface LogPage {
92
+ commits: CommitSummary[];
93
+ /** False when a filter makes the graph meaningless. */
94
+ graph: boolean;
95
+ /** True when there are no more pages. */
96
+ done: boolean;
97
+ }
98
+
99
+ export interface FileChange {
100
+ /** A, M, D, R (renamed), C (copied), T (type change) or ? (untracked). */
101
+ status: string;
102
+ path: string;
103
+ oldPath?: string;
104
+ similarity?: number;
105
+ oldMode?: string;
106
+ newMode?: string;
107
+ additions: number;
108
+ deletions: number;
109
+ binary: boolean;
110
+ }
111
+
112
+ export interface FileList {
113
+ from: string;
114
+ to: string;
115
+ files: FileChange[];
116
+ additions: number;
117
+ deletions: number;
118
+ }
119
+
120
+ export interface DiffOptions {
121
+ ignoreWhitespace?: boolean;
122
+ }
123
+
124
+ export interface Patch {
125
+ /** Unified diff text. */
126
+ patch: string;
127
+ truncated: boolean;
128
+ }
129
+
130
+ export interface Range {
131
+ /** Commits only in `to`: applied by the move, newest first. */
132
+ added: CommitSummary[];
133
+ /** Commits only in `from`: undone by the move, newest first. */
134
+ removed: CommitSummary[];
135
+ /** Snapshots to step through, in order. The last entry is `to`. */
136
+ path: CommitSummary[];
137
+ addedCount: number;
138
+ removedCount: number;
139
+ mergeBase: string | null;
140
+ }
141
+
142
+ export interface TreeEntry {
143
+ name: string;
144
+ path: string;
145
+ type: "tree" | "blob" | "commit";
146
+ mode: string;
147
+ oid: string;
148
+ size: number | null;
149
+ }
150
+
151
+ export interface Blob {
152
+ path: string;
153
+ size: number;
154
+ binary: boolean;
155
+ truncated?: boolean;
156
+ content: string;
157
+ }
158
+
159
+ export interface Blame {
160
+ rev: string;
161
+ path: string;
162
+ /** Commit id for each line of the file. */
163
+ lines: string[];
164
+ commits: Record<string, { author?: string; email?: string; date?: number; subject?: string }>;
165
+ }
166
+
167
+ export interface Insights {
168
+ total: number;
169
+ first: number | null;
170
+ last: number | null;
171
+ /** Commits per day, keyed YYYY-MM-DD (local time). */
172
+ days: Record<string, number>;
173
+ /** punch[day 0-6, Sunday first][hour 0-23] = commits. */
174
+ punch: number[][];
175
+ contributorCount: number;
176
+ /** Top 100 by commit count. */
177
+ contributors: { name: string; email: string; commits: number; first: number; last: number }[];
178
+ }
179
+
180
+ /**
181
+ * A repository opened with openRepo(). Methods accept any revision git understands
182
+ * (branch, tag, commit id, HEAD~3, ...); results for immutable objects are cached.
183
+ */
184
+ export interface Repo {
185
+ readonly root: string;
186
+ readonly gitDir: string;
187
+ readonly bare: boolean;
188
+ readonly name: string;
189
+ readonly emptyTree: string;
190
+
191
+ /** Run git in this repository. */
192
+ run(args: string[], options?: GitOptions): Promise<string>;
193
+ /** Full commit id for a revision, or WORKTREE. */
194
+ resolve(rev: string): Promise<string>;
195
+ info(): Promise<RepoInfo>;
196
+ /** Changes whenever refs, HEAD or the index change. */
197
+ fingerprint(): Promise<string>;
198
+ log(options?: LogOptions): Promise<LogPage>;
199
+ commit(rev: string): Promise<Commit>;
200
+ /** Branches, remote branches and tags whose history contains `rev`. */
201
+ containedIn(rev: string): Promise<{ branches: string[]; remotes: string[]; tags: string[] }>;
202
+ /** Changed files from `from` to `to`. `from` may be "" (empty tree); `to` may be WORKTREE. */
203
+ files(from: string, to: string, options?: DiffOptions): Promise<FileList>;
204
+ patch(from: string, to: string, file: string, oldFile?: string, options?: DiffOptions & { context?: number }): Promise<Patch>;
205
+ /** What moving from `from` to `to` applies and undoes, and the snapshots on the way. */
206
+ range(from: string, to: string): Promise<Range>;
207
+ tree(rev: string, dir?: string): Promise<{ rev: string; path: string; entries: TreeEntry[] }>;
208
+ blob(rev: string, file: string): Promise<Blob>;
209
+ blame(rev: string, file: string): Promise<Blame>;
210
+ /** Activity for one revision, or for all branches when omitted. */
211
+ insights(ref?: string): Promise<Insights>;
212
+ /** `git fetch --all --prune`. */
213
+ fetch(): Promise<string>;
214
+ }
@@ -0,0 +1,33 @@
1
+ export interface GraphRow {
2
+ /** Column of this commit's node. */
3
+ col: number;
4
+ /** Columns whose line comes from the row above into the node. */
5
+ up: number[];
6
+ /** Columns the node's line continues into, one per parent. */
7
+ down: number[];
8
+ /** Columns that run straight through this row. */
9
+ pass: number[];
10
+ /** [from, to] lanes that move to another column in this row. */
11
+ shift: [number, number][];
12
+ /** Number of columns this row needs. */
13
+ width: number;
14
+ }
15
+
16
+ export interface LayoutState {
17
+ lanes: (string | null)[];
18
+ seen: Set<string>;
19
+ }
20
+
21
+ /** Fresh layout state. */
22
+ export function createLayout(): LayoutState;
23
+
24
+ /**
25
+ * Lay out commits (newest first, children before parents), continuing from `state`
26
+ * so pages can be appended. Sets `commit.g` on each commit.
27
+ */
28
+ export function layout<T extends { hash: string; parents: string[] }>(state: LayoutState, commits: T[]): LayoutState;
29
+
30
+ export const LANE_W: number;
31
+
32
+ /** SVG markup for one row of the graph. */
33
+ export function rowSvg(g: GraphRow, options: { height: number; maxLanes: number; merge?: boolean; head?: boolean }): string;
@@ -0,0 +1,5 @@
1
+ export * from "./git.js";
2
+ export { createServer, listen } from "./server.js";
3
+ export type { ServerOptions } from "./server.js";
4
+ export { createLayout, layout } from "./graph.js";
5
+ export type { GraphRow, LayoutState } from "./graph.js";
@@ -0,0 +1,15 @@
1
+ import type { Server } from "node:http";
2
+ import type { Repo } from "./git.js";
3
+
4
+ export interface ServerOptions {
5
+ /** Load author avatars from GitHub and Gravatar. Default true. */
6
+ avatars?: boolean;
7
+ /** The host you will listen on. For loopback hosts, requests for other host names are rejected. Default "127.0.0.1". */
8
+ host?: string;
9
+ }
10
+
11
+ /** An HTTP server for the git-tm web interface and its JSON API. Call listen() to start it. */
12
+ export function createServer(repo: Repo, options?: ServerOptions): Server;
13
+
14
+ /** Listen on `port`, or the next free one (up to `attempts` tries). Resolves with the port used. */
15
+ export function listen(server: Server, port: number, host: string, attempts?: number): Promise<number>;