git-tm 1.2.0 → 2.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.
@@ -0,0 +1,69 @@
1
+ import type { Repo } from "./git.js";
2
+
3
+ /** The kinds of edit edit mode supports. */
4
+ export type EditType = "reword" | "author" | "squash" | "drop" | "revert";
5
+
6
+ export interface EditOptions {
7
+ type: EditType;
8
+ /** The commit to edit (any revision git understands). */
9
+ commit: string;
10
+ /** Local branch whose history changes. Default: the current branch. */
11
+ branch?: string;
12
+ /** New message for reword; optional combined message for squash; optional message for revert. */
13
+ message?: string;
14
+ /** New author for the "author" edit. `date` is Unix seconds; the original time zone is kept. */
15
+ author?: { name: string; email: string; date?: number };
16
+ }
17
+
18
+ export interface EditPlan {
19
+ type: EditType;
20
+ branch: string;
21
+ ref: string;
22
+ oldTip: string;
23
+ newTip: string;
24
+ target: string;
25
+ targetSubject: string;
26
+ summary: string;
27
+ /** Old commits and what replaces them (`new` is null for a dropped commit). */
28
+ rewritten: { old: string; new: string | null; subject: string }[];
29
+ /** New commits that rewrite nothing (revert). */
30
+ added: { new: string; subject: string }[];
31
+ /** True when the final tree is unchanged, so the working tree is never touched. */
32
+ treePreserving: boolean;
33
+ /** Remote branches that already have the affected commits. Applying then needs `allowPushed`. */
34
+ pushed: string[];
35
+ /** Other refs that keep pointing at the old commits. */
36
+ otherRefs: { branches: string[]; tags: string[] };
37
+ /** Number of signed commits that lose their signature. */
38
+ signed: number;
39
+ warnings: string[];
40
+ /** Whether the branch is checked out in this worktree. */
41
+ checkedOut: boolean;
42
+ }
43
+
44
+ export interface EditResult {
45
+ id: string;
46
+ branch: string;
47
+ oldTip: string;
48
+ newTip: string;
49
+ }
50
+
51
+ export interface EditBackup extends EditResult {
52
+ op: EditType | "undo";
53
+ summary: string;
54
+ /** Unix seconds. */
55
+ time: number;
56
+ /** Where the branch is now. */
57
+ current: string | null;
58
+ /** True while the branch is still exactly where the edit left it. */
59
+ canUndo: boolean;
60
+ }
61
+
62
+ /** Work out an edit without changing anything. Throws GitError (409 with `conflicts` for conflicting drops and reverts). */
63
+ export function planEdit(repo: Repo, options: EditOptions): Promise<EditPlan>;
64
+ /** Apply a plan: backs up the old tip, then moves the branch only if it hasn't moved since planning. */
65
+ export function applyEdit(repo: Repo, plan: EditPlan, options?: { allowPushed?: boolean }): Promise<EditResult>;
66
+ /** Edits made by git-tm, newest first. */
67
+ export function listBackups(repo: Repo): Promise<EditBackup[]>;
68
+ /** Undo an edit, or with `newBranch` restore the old timeline as a new branch. */
69
+ export function undoEdit(repo: Repo, id: string, options?: { newBranch?: string }): Promise<{ id?: string; branch: string; newTip: string }>;
@@ -16,12 +16,19 @@ export interface GitOptions {
16
16
  maxBytes?: number;
17
17
  /** Kill git after this many milliseconds. Default 60 000. */
18
18
  timeout?: number;
19
+ /** Extra environment variables for this git process. */
20
+ env?: Record<string, string>;
19
21
  }
20
22
 
21
23
  /** Run git in `cwd` with a clean, non-interactive environment and resolve with its stdout. */
22
24
  export function git(cwd: string, args: string[], options: GitOptions & { raw: true }): Promise<Buffer & { truncated: boolean }>;
25
+ /** With `allowExit`, the listed non-zero exit codes (and 0) resolve with stdout and the code instead of throwing. */
26
+ export function git(cwd: string, args: string[], options: GitOptions & { allowExit: number[] }): Promise<{ stdout: string; code: number }>;
23
27
  export function git(cwd: string, args: string[], options?: GitOptions): Promise<string>;
24
28
 
29
+ /** Validate a user-supplied revision so it can never be read as a git option. Returns it unchanged or throws GitError. */
30
+ export function checkRev(rev: string): string;
31
+
25
32
  /** Open the repository containing `dir` (any folder inside it works). Throws GitError if there is none. */
26
33
  export function openRepo(dir: string): Promise<Repo>;
27
34
 
@@ -1,5 +1,7 @@
1
1
  export * from "./git.js";
2
- export { createServer, listen } from "./server.js";
3
- export type { ServerOptions } from "./server.js";
2
+ export { createServer, createApiHandler, listen } from "./server.js";
3
+ export type { ServerOptions, ApiHandler, ApiHandlerOptions, ApiRequest, ApiResponse } from "./server.js";
4
+ export { planEdit, applyEdit, listBackups, undoEdit } from "./edit.js";
5
+ export type { EditType, EditOptions, EditPlan, EditResult, EditBackup } from "./edit.js";
4
6
  export { createLayout, layout } from "./graph.js";
5
7
  export type { GraphRow, LayoutState } from "./graph.js";
@@ -6,8 +6,50 @@ export interface ServerOptions {
6
6
  avatars?: boolean;
7
7
  /** The host you will listen on. For loopback hosts, requests for other host names are rejected. Default "127.0.0.1". */
8
8
  host?: string;
9
+ /** Enable edit mode: the /api/edit/* routes that change history. Only allowed with a loopback host. Default false. */
10
+ edit?: boolean;
11
+ /** Called after each applied edit, undo or restore (for logging). */
12
+ onEdit?: (event: { id?: string; branch: string; newTip: string; op: string; summary?: string; count?: number }) => void;
13
+ /** Called after each handled API request (excluding /api/state polling). */
14
+ onLog?: (event: { method: string; path: string; query: URLSearchParams; status: number; ms: number; error?: string }) => void;
9
15
  }
10
16
 
17
+ export interface ApiHandlerOptions {
18
+ /** Include avatar URLs in responses. Default true. */
19
+ avatars?: boolean;
20
+ /** Enable the /api/edit/* routes. The caller decides who can reach the handler. Default false. */
21
+ edit?: boolean;
22
+ /** Error message for edit routes while edit mode is off. */
23
+ editOffMessage?: string;
24
+ onEdit?: ServerOptions["onEdit"];
25
+ onLog?: ServerOptions["onLog"];
26
+ }
27
+
28
+ export interface ApiRequest {
29
+ method?: "GET" | "POST";
30
+ /** For example "/api/history". */
31
+ path: string;
32
+ query?: URLSearchParams | string | Record<string, string>;
33
+ /** A parsed JSON object, or a function that resolves to one (read only by routes that need a body). */
34
+ body?: object | (() => Promise<object>);
35
+ }
36
+
37
+ export interface ApiResponse {
38
+ status: number;
39
+ /** The route's result, or { error, conflicts? } when status is not 200. */
40
+ data: any;
41
+ }
42
+
43
+ export interface ApiHandler {
44
+ /** True when `method path` is a known endpoint. */
45
+ has(method: string, path: string): boolean;
46
+ /** Run one API request. Never rejects: errors come back with a 4xx/5xx status. */
47
+ handle(request: ApiRequest): Promise<ApiResponse>;
48
+ }
49
+
50
+ /** The git-tm JSON API without a transport, shared by createServer() and the VS Code extension. */
51
+ export function createApiHandler(repo: Repo, options?: ApiHandlerOptions): ApiHandler;
52
+
11
53
  /** An HTTP server for the git-tm web interface and its JSON API. Call listen() to start it. */
12
54
  export function createServer(repo: Repo, options?: ServerOptions): Server;
13
55