Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

g1t.sh
578 linesCodeBlameRaw
1import type { Contributors, Languages, License, NewRelease, Release, ReleaseChange, RepoAbout, Stargazer, StarredRepo, Stars } from "./about";
2import type { User, Viewer } from "./identity";
3import type { Result } from "./result";
4
5export type Repo = {
6 id: string;
7 /** The slug of the workspace that owns it: the first URL segment. */
8 namespace: string;
9 name: string;
10 isPrivate: boolean;
11 ownerId: string;
12 defaultBranch: string;
13 /**
14 * Set when this repo is a pull request's working copy of another repo
15 * (namespace `pulls`, hidden from listings). A fork someone made into a
16 * workspace is an ordinary repository with `parentId` instead.
17 */
18 forkOf: string | null;
19 /**
20 * The repository this one was forked from, when someone forked it into
21 * their workspace. Null once it is detached, or when its parent changed
22 * who can see it.
23 */
24 parentId?: string | null;
25 /**
26 * What it was made a fork of, kept when it is detached: Actions and
27 * issues stay off by default, and its pull requests into the parent
28 * still merge.
29 */
30 forkedFrom?: string | null;
31 /** Where `parentId` is, for a viewer who may read it (from `get`, `getById` and `forks`). */
32 parent?: RepoPath | null;
33 /** A private repository's admins let others fork it. Public ones can always be forked. */
34 allowForking?: boolean;
35 /** The most branches and tags one push may change; pushes over it are refused whole. Null for no limit. */
36 maxRefUpdates?: number | null;
37 /**
38 * Whether the default branch is protected: it changes only by merging a
39 * pull request, and pushes to it are refused.
40 */
41 protected: boolean;
42 /** RFC 3339. */
43 createdAt: string;
44 /**
45 * Words that say what it is about, for search and Explore: lowercase
46 * letters, digits and hyphens, at most 20.
47 */
48 topics: string[];
49 /** Its home page, an http(s) address. */
50 website?: string | null;
51 /**
52 * RFC 3339: when it was archived. While archived it is read-only:
53 * pushes, merges, agents and workflows are refused, and issues and pull
54 * requests are locked. Null when it is not archived.
55 */
56 archivedAt?: string | null;
57};
58
59export type ForkRepoInput = {
60 /** The workspace to fork it into. */
61 namespace: string;
62 /** The fork's name; the source's when absent. */
63 name?: string;
64 /** Copy only the default branch (the default), or every branch and tag. */
65 defaultBranchOnly?: boolean;
66};
67
68/** How a pull request's branch is brought up to date: a merge commit, or its commits made again on the base. */
69export type UpdateMethod = "merge" | "rebase";
70
71/** What `syncFork` did. */
72export type SyncStatus = "up_to_date" | "fast_forwarded" | "merged" | "conflicted";
73
74export type SyncedFork = {
75 status: SyncStatus;
76 branch: string;
77 /** The branch's head after syncing. */
78 head: string;
79 /** Commits the parent's branch had that the fork's did not. */
80 behind: number;
81 /** Commits the fork's branch had that the parent's did not. */
82 ahead: number;
83 /** For `conflicted`: the files both sides changed (at most 20). */
84 conflicts: string[];
85};
86
87/** How long a deleted repository can be restored before it is purged. */
88export const RESTORE_DAYS = 30;
89
90/** A deleted repository, as its workspace's Recently deleted list shows it. */
91export type DeletedRepo = {
92 id: string;
93 namespace: string;
94 name: string;
95 isPrivate: boolean;
96 /** RFC 3339. */
97 deletedAt: string;
98 /** The username of who deleted it. */
99 deletedBy: string;
100 /** RFC 3339: when it is purged, unless restored first. */
101 purgeAfter: string;
102};
103
104/** Whether a repository is archived or deleted, for services deciding whether to act on it. */
105export type RepoStatus = { archived: boolean; deleted: boolean };
106
107/** The most topics a repository has, and the longest topic. */
108export const MAX_TOPICS = 20;
109export const MAX_TOPIC_CHARS = 35;
110
111export type RepoPath = { namespace: string; name: string };
112
113export type Commit = {
114 hash: string;
115 treeHash: string;
116 message: string;
117 author: { name: string; email: string };
118 parents: string[];
119 /** RFC 3339. */
120 authoredAt: string;
121};
122
123export type TreeEntry = {
124 name: string;
125 hash: string;
126 kind: "tree" | "blob" | "symlink" | "gitlink" | "exec";
127};
128
129export type TreeView = {
130 repo: Repo;
131 ref: string;
132 path: string;
133 /** Null when the repo has no commits yet. */
134 head: Commit | null;
135 entries: TreeEntry[];
136 readme: { name: string; text: string | null } | null;
137};
138
139export type BlobView = {
140 repo: Repo;
141 ref: string;
142 path: string;
143 size: number;
144 /** Null when the file is binary or too large to show. */
145 text: string | null;
146};
147
148/** A git remote and a short-lived credential for it. */
149export type GitAccess = { remote: string; token: string };
150
151export type GitService = "git-upload-pack" | "git-receive-pack";
152
153export type CreateRepoInput = {
154 /** The workspace to create it in; the creator must be a member. */
155 namespace: string;
156 name: string;
157 isPrivate?: boolean;
158 /**
159 * The https address of a public git repository to copy the default branch
160 * of, such as `https://github.com/owner/repo`.
161 */
162 importUrl?: string;
163};
164
165/**
166 * Where repositories may be kept. `euAvailable`: an EU namespace takes new
167 * repositories, so a workspace may keep its data in the EU.
168 */
169export type StorageOptions = { euAvailable: boolean };
170
171/** Repositories: metadata, contents and git access. */
172export interface ReposApi {
173 get(path: RepoPath, viewer: Viewer): Promise<Result<Repo>>;
174 getById(id: string, viewer: Viewer): Promise<Result<Repo>>;
175 /**
176 * Of these ids (at most 500), the repositories the viewer can read, in
177 * one call. Forks and deleted repositories are left out.
178 */
179 readable(ids: string[], viewer: Viewer): Promise<Repo[]>;
180 /**
181 * The workspaces in which this account made a public repository, and so
182 * a public project anyone can see. By account id.
183 */
184 publicNamespaces(ownerId: string): Promise<string[]>;
185 /** Repos the viewer may see, newest first, optionally matching `query`. */
186 list(
187 viewer: Viewer,
188 options?: {
189 query?: string;
190 /** Only repos in this workspace. */
191 namespace?: string;
192 /** Only repos in workspaces the viewer belongs to. */
193 memberOnly?: boolean;
194 },
195 ): Promise<Repo[]>;
196 create(owner: User, input: CreateRepoInput): Promise<Result<Repo>>;
197 /**
198 * Changes whichever details are given. Members of the repository's
199 * workspace only. A repository has no description: its project does.
200 */
201 update(
202 actor: User,
203 path: RepoPath,
204 changes: {
205 isPrivate?: boolean;
206 protected?: boolean;
207 topics?: string[];
208 /** An empty string clears it. */
209 website?: string;
210 /** Whether a private repository may be forked (Admin). */
211 allowForking?: boolean;
212 /** The most branches and tags one push may change (Admin); 0 for no limit. */
213 maxRefUpdates?: number;
214 },
215 ): Promise<Result<Repo>>;
216 /**
217 * Deletes the repository. Owners only, who type its full name as
218 * `confirm`. It is hidden at once and can be restored for
219 * `RESTORE_DAYS` days, then purged. Publishes `repo.deleted`.
220 */
221 delete(actor: User, path: RepoPath, confirm: string): Promise<Result<DeletedRepo>>;
222 /** A workspace's recently deleted repositories, newest first. Owners only; empty otherwise. */
223 deleted(viewer: Viewer, namespace: string): Promise<DeletedRepo[]>;
224 /** Brings a deleted repository back as it was. Owners only. Publishes `repo.restored`. */
225 restore(actor: User, path: RepoPath): Promise<Result<Repo>>;
226 /**
227 * Removes a deleted repository for good now, its git data with it, and
228 * frees its name. Owners only, who type its full name. Publishes `repo.purged`.
229 */
230 purge(actor: User, path: RepoPath, confirm: string): Promise<Result<boolean>>;
231 /**
232 * Renames the repository in its workspace. Owners only. The old path
233 * redirects until a repository is made there. Publishes `repo.renamed`.
234 */
235 rename(actor: User, path: RepoPath, name: string): Promise<Result<Repo>>;
236 /**
237 * Archives the repository (read-only) or unarchives it. Owners only.
238 * Publishes `repo.archived` or `repo.unarchived`.
239 */
240 archive(actor: User, path: RepoPath, archived: boolean): Promise<Result<Repo>>;
241 /**
242 * Makes the repository public or private. Owners only, who type its full
243 * name as `confirm`. Publishes `repo.updated` and `repo.visibility_changed`.
244 */
245 setVisibility(actor: User, path: RepoPath, isPrivate: boolean, confirm: string): Promise<Result<Repo>>;
246 /** Makes another existing branch the default. Members. */
247 setDefaultBranch(actor: User, path: RepoPath, branch: string): Promise<Result<Repo>>;
248 /**
249 * Renames a branch; pull requests from it follow, and addresses naming the
250 * old one redirect. Members; only owners rename the default branch.
251 */
252 renameBranch(actor: User, path: RepoPath, from: string, to: string): Promise<Result<Repo>>;
253 /**
254 * Makes a branch named `name` at `from`: a branch, a tag or a commit (the
255 * default branch when empty). Needs push; the rules for creating it hold.
256 * Restoring a deleted branch is making it again at the commit
257 * `removeBranch` answered with.
258 */
259 createBranch(actor: User, path: RepoPath, name: string, from?: string): Promise<Result<Branch>>;
260 /**
261 * Deletes a branch, never the default. Needs push; the rules for deleting
262 * it hold. Given `head`, a branch that has moved from it is left alone.
263 * Answers with the branch as it was, to restore it with `createBranch`.
264 */
265 removeBranch(actor: User, path: RepoPath, branch: string, head?: string): Promise<Result<Branch>>;
266 /** What a branch renamed away from `branch` is called now, or null. */
267 resolveBranch(repoId: string, branch: string): Promise<string | null>;
268 /** Whether a repository is archived or deleted; an unknown id answers as deleted. */
269 statusById(id: string): Promise<RepoStatus>;
270 /** What a workspace may choose about where its repositories are kept. */
271 storageOptions(): Promise<StorageOptions>;
272 /**
273 * Moves the repository to the workspace `to`, keeping its name, id and
274 * everything under it. The actor must own both workspaces. The old path
275 * redirects until a repository is made there. Publishes `repo.transferred`.
276 */
277 transfer(actor: User, path: RepoPath, to: string): Promise<Result<Repo>>;
278 /**
279 * Where a repository transferred away from `path` is now; null when
280 * `path` is a repository or never was one that moved. Callers check the
281 * viewer may see it there.
282 */
283 resolvePath(path: RepoPath): Promise<RepoPath | null>;
284
285 tree(path: RepoPath, viewer: Viewer, ref: string | null, treePath: string): Promise<Result<TreeView>>;
286 blob(path: RepoPath, viewer: Viewer, ref: string, filePath: string): Promise<Result<BlobView>>;
287 log(path: RepoPath, viewer: Viewer, ref: string | null, limit: number): Promise<Result<Commit[]>>;
288 /**
289 * Who last changed each line of a file as of `ref` (the default branch if
290 * null). Not found when the file is missing or is not text.
291 */
292 blame(path: RepoPath, viewer: Viewer, ref: string | null, filePath: string): Promise<Result<Blame>>;
293
294 /**
295 * A copy-on-write copy of `source`, hidden from listings, for one pull request
296 * to work in.
297 */
298 forkForPull(sourceId: string, pullId: string, actor: User): Promise<Result<Repo>>;
299 /**
300 * Forks `path` into the workspace `input.namespace`: a new repository
301 * with the source's code (its default branch unless
302 * `defaultBranchOnly` is false), nothing else; its project starts
303 * with the source project's description. Public
304 * repositories can always be forked, private ones when their admins
305 * allow it; a fork is as visible as its source. Publishes `repo.created`
306 * and `repo.forked`.
307 */
308 fork(actor: User, path: RepoPath, input: ForkRepoInput): Promise<Result<Repo>>;
309 /** The forks of `path` the viewer may read, newest first. */
310 forks(path: RepoPath, viewer: Viewer, limit?: number): Promise<Result<Repo[]>>;
311 /**
312 * Brings a fork's branch (its default branch when absent) up to date
313 * with its parent's: fast-forwarded when only behind, merged when the
314 * two changed different files, left alone (`conflicted`) otherwise.
315 */
316 syncFork(actor: User, path: RepoPath, branch?: string): Promise<Result<SyncedFork>>;
317 /** Makes a fork an ordinary repository with no parent (Admin). */
318 detachFork(actor: User, path: RepoPath): Promise<Result<Repo>>;
319
320 /**
321 * Authorizes a git operation and returns where to send it. Pushing to a
322 * repo that does not exist creates it in the pusher's own namespace.
323 */
324 gitAccess(path: RepoPath, viewer: Viewer, service: GitService): Promise<Result<GitAccess>>;
325
326 /** The repository's branches, default branch first. */
327 branches(path: RepoPath, viewer: Viewer): Promise<Result<Branch[]>>;
328
329 /** Where a repository is now, by id; null when it is gone. For services: it checks nobody's access. */
330 pathById(id: string): Promise<RepoPath | null>;
331
332 /** Which commit last changed each entry of a directory at `ref` (the default branch when null). */
333 lastCommits(path: RepoPath, viewer: Viewer, ref: string | null, treePath: string): Promise<Result<LastCommits>>;
334
335 /**
336 * How far each branch head has moved from `base` (the default branch's
337 * head commit), with each head's commit and `base`'s own, in one call.
338 * Kept by the pair of hashes in the repos service.
339 */
340 branchDrift(path: RepoPath, viewer: Viewer, base: string, heads: string[]): Promise<Result<BranchDrifts>>;
341
342 /** The repository's tags, newest commit first, at most 100. */
343 tags(path: RepoPath, viewer: Viewer): Promise<Result<Tag[]>>;
344
345 /**
346 * The Files page's About in one answer: license, security policy,
347 * languages, contributors, stars and releases. What comes from files and
348 * history is kept by commit and worked out in the background when it is
349 * behind the head (see `about.ts`).
350 */
351 about(path: RepoPath, viewer: Viewer): Promise<Result<RepoAbout>>;
352 languages(path: RepoPath, viewer: Viewer): Promise<Result<Languages>>;
353 /** Everyone whose commits are on the default branch, with their weeks. */
354 contributors(path: RepoPath, viewer: Viewer): Promise<Result<Contributors>>;
355 license(path: RepoPath, viewer: Viewer): Promise<Result<License | null>>;
356 /** How many starred it, and whether the viewer did. */
357 stars(path: RepoPath, viewer: Viewer): Promise<Result<Stars>>;
358 /** Stars it for the actor, or takes the star back. */
359 star(actor: User, path: RepoPath, starred: boolean): Promise<Result<Stars>>;
360 /** Who starred it, newest first, 100 a page from 1. */
361 stargazers(path: RepoPath, viewer: Viewer, page?: number): Promise<Result<Stargazer[]>>;
362 /** What a person starred that the viewer can see, newest first, at most 100. */
363 starred(username: string, viewer: Viewer): Promise<StarredRepo[]>;
364 /** Releases, newest first, at most 100: drafts only for those who can push. */
365 releases(path: RepoPath, viewer: Viewer): Promise<Result<Release[]>>;
366 /** One release by id or tag, or the latest. */
367 release(path: RepoPath, viewer: Viewer, which: { id?: string; tag?: string; latest?: boolean }): Promise<Result<Release>>;
368 /** Needs Write. A tag that does not exist yet is made at `target`. */
369 createRelease(actor: User, path: RepoPath, release: NewRelease): Promise<Result<Release>>;
370 updateRelease(actor: User, path: RepoPath, id: string, change: ReleaseChange): Promise<Result<Release>>;
371 /** The tag stays. */
372 deleteRelease(actor: User, path: RepoPath, id: string): Promise<Result<boolean>>;
373
374 /**
375 * Every file at `ref` (the default branch when null), at most `limit`
376 * (10,000 at most); symlinks too with `links`, for an archive. No viewer:
377 * check access first.
378 */
379 listFiles(repoId: string, ref: string | null, limit: number, options?: { links?: boolean }): Promise<FileList>;
380
381 /**
382 * What an archive link's `token` (made by the API's tarball and zipball
383 * routes) lets anyone holding it download from the repository at
384 * `path`: one commit, for ten minutes. Null when it is not for this
385 * repository, has ended or does not open. No viewer: the token is the
386 * permission.
387 */
388 openArchiveToken(path: RepoPath, token: string): Promise<ArchiveGrant | null>;
389
390 /** Blobs' bytes as standard base64, at most 100; `data` is null for one missing or over `maxBytes`. No viewer. */
391 rawBlobs(repoId: string, hashes: string[], maxBytes: number): Promise<RawBlob[]>;
392
393 /** One file's bytes at a branch, tag or commit; null when missing or over `maxBytes`. No viewer: check access first. */
394 rawFile(repoId: string, ref: string, path: string, maxBytes: number): Promise<RawFile | null>;
395
396 /**
397 * Writes one file on a new branch made from the default branch's head, as
398 * one commit by `actor`, for a change g1t proposes on their behalf (a
399 * starter workflow). Refused unless they may push, when the branch exists,
400 * or when the file is already there.
401 */
402 commitFile(
403 repo: RepoPath,
404 actor: User,
405 file: { branch: string; path: string; content: string; message: string },
406 ): Promise<Result<{ branch: string; commit: string }>>;
407
408 /**
409 * Moves a repository's default branch to the head of a pull request's
410 * source: a fork (`sourceId` is the fork) or one of the repository's own
411 * branches (`sourceId` is the repository, and `branch` is required).
412 * Refused with "conflict" when the source is behind, since that would
413 * discard commits.
414 */
415 land(sourceId: string, actor: User, branch?: string | null): Promise<Result<{ commit: string; previous: string | null }>>;
416
417 /**
418 * What `head` changes relative to `base`. `head` is a branch or a commit
419 * and defaults to the default branch. With no `base`, a fork is compared
420 * with the last commit it shares with the repository it came from, and a
421 * branch with the point where it left the default branch.
422 */
423 /**
424 * With no `base`, a branch is compared from where it left `baseBranch`
425 * (the default branch when absent). With `paths`, only those files (at
426 * most 50), with their lines: how a file a large comparison lists as
427 * `omitted` is read. With `targetId` (no `base`), `repoId` is a fork of
428 * that repository, directly or through other forks, and the head is
429 * compared from where it left `baseBranch` there: a pull request from a
430 * fork. Both must be readable.
431 */
432 compare(
433 repoId: string,
434 viewer: Viewer,
435 base?: string | null,
436 head?: string | null,
437 baseBranch?: string | null,
438 paths?: string[] | null,
439 targetId?: string | null,
440 ): Promise<Result<Comparison>>;
441
442 /**
443 * Services only, for the runner's sweep: up to `limit` queued nightly
444 * backups, each now running with a token of its own, so long as no more
445 * than `maxRunning` are then running. Empty when backups are off.
446 */
447 claimBackups(limit: number, maxRunning: number): Promise<BackupClaim[]>;
448
449 /**
450 * Services only: a backup's sandbox stopped before it reported, so the
451 * job is tried again later. Refused harmlessly once it has reported.
452 */
453 failBackup(jobId: string, token: string, error: string): Promise<Result<boolean>>;
454}
455
456/**
457 * A nightly backup to start (`g1t_contracts::backups`): the sandbox is
458 * given the job's id and token, and nothing else.
459 */
460export type BackupClaim = {
461 jobId: string;
462 token: string;
463 repoId: string;
464 path: RepoPath;
465};
466
467/** Lines `start` to `end` (inclusive, from 1) last changed by `commit`. */
468export type BlameRange = { start: number; end: number; commit: string };
469
470/** Who last changed each line of a file. */
471export type Blame = {
472 /** The commit the file was read at. */
473 head: string;
474 /** Every line, in order, in runs that share a commit. */
475 ranges: BlameRange[];
476 /** The commits the ranges name, each once. */
477 commits: Commit[];
478 /** True when the history was too long to read in full (too many commits, or too long to read). */
479 partial: boolean;
480};
481
482/** A branch and the commit it points to. */
483export type Branch = { name: string; hash: string };
484
485/** Each entry's last commit; `complete` is false when some were not reached. */
486export type LastCommits = { entries: { name: string; commit: Commit }[]; complete: boolean };
487
488/** Commits a branch has that the default branch does not, and the other way round. */
489export type BranchDriftCount = { ahead: number; behind: number };
490
491/**
492 * `base`'s commit, and each head's commit and drift in the order asked;
493 * `drift` is null when the two histories do not meet within what is read.
494 */
495export type BranchDrifts = {
496 base: Commit | null;
497 branches: { head: string; commit: Commit | null; drift: BranchDriftCount | null }[];
498};
499
500/** A tag, and its commit when it could be read. */
501export type Tag = { name: string; commit: Commit | null };
502
503/** Files at a commit, and whether there were more than were listed. */
504export type FileList = {
505 commit: string | null;
506 /** When the commit was made (RFC 3339). */
507 date?: string;
508 /** `kind`: `exec` for an executable file, `symlink` for a link (its blob is where it points). */
509 files: { path: string; hash: string | null; kind?: "blob" | "exec" | "symlink" }[];
510 truncated: boolean;
511};
512
513/** One commit of a repository an archive link lets its holder download. */
514export type ArchiveGrant = { repoId: string; namespace: string; name: string; commit: string };
515
516/** One blob's bytes, standard base64; null when missing or too large. */
517export type RawBlob = { hash: string; size: number; data: string | null };
518
519/** One file's bytes, standard base64. */
520export type RawFile = { size: number; data: string };
521
522/**
523 * What came of bringing a pull request up to date with the default branch
524 * without a sandbox. `needs_agent` pushed nothing: the runner's `update`
525 * merges it in a sandbox, with an agent if it conflicts.
526 */
527export type PullBranchUpdate =
528 | { outcome: "updated"; commit: string; previous: string }
529 | { outcome: "up_to_date"; commit: string }
530 | {
531 outcome: "needs_agent";
532 /**
533 * `overlap`: both sides changed some of the same files. `conflicting`:
534 * merging is known to conflict. `unsupported`: it could not be worked
535 * out without git, such as for a very large change.
536 */
537 reason: "overlap" | "conflicting" | "unsupported";
538 detail: string;
539 /** The files both changed, or that conflict, when known. */
540 paths: string[];
541 };
542
543export type DiffLine = {
544 kind: "context" | "add" | "delete";
545 /** Line number in the old file; null for added lines. */
546 old: number | null;
547 /** Line number in the new file; null for deleted lines. */
548 new: number | null;
549 text: string;
550};
551
552/** A run of changed lines with their surrounding context. */
553export type Hunk = { lines: DiffLine[] };
554
555export type FileDiff = {
556 path: string;
557 status: "added" | "modified" | "deleted";
558 additions: number;
559 deletions: number;
560 /** True when the file is binary or too large, so no lines are shown. */
561 binary: boolean;
562 hunks: Hunk[];
563 /**
564 * True when the comparison reached its limit on lines before this file,
565 * so it is listed without its lines or counts: `compare` with its path
566 * in `paths` has them.
567 */
568 omitted?: boolean;
569};
570
571/** What changed between two commits. */
572export type Comparison = {
573 base: string | null;
574 head: string;
575 files: FileDiff[];
576 /** True when more files changed than are listed. */
577 truncated: boolean;
578};