Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

git.ts

109 lines2.7 KB

1/**
2 * Git commit and push helpers. All git I/O goes through an injected runner
3 * so tests never spawn git.
4 */
5​
6export type GitRunner = (
7 args: Array<string>,
8 cwd: string,
9) => Promise<{ stdout: string; stderr: string; code: number }>
10​
11function gitFailed(
12 args: Array<string>,
13 result: { stdout: string; stderr: string; code: number },
14) {
15 const detail = result.stderr.trim() || result.stdout.trim()
16 const suffix = detail.length > 0 ? `: ${detail}` : ''
17 throw new Error(`git ${args.join(' ')} exited ${result.code}${suffix}`)
19​
20/**
21 * Stage every change in `cwd` and commit with `identity`.
22 *
23 * Runs `git add -A`, then `git -c user.name= -c user.email= commit -m`.
24 * Returns `{ committed: false }` when commit exits 1 because there is
25 * nothing to commit. Other non-zero exits throw.
26 *
27 * @param cwd git working tree
28 * @param message commit message
29 * @param runner injected git runner
30 * @param identity author and committer name and email
31 */
32export async function commitAll(
33 cwd: string,
34 message: string,
35 runner: GitRunner,
36 identity: { name: string; email: string },
37) {
38 const addArgs = ['add', '-A']
39 const add = await runner(addArgs, cwd)
40 if (add.code !== 0) {
41 gitFailed(addArgs, add)
42 }
43​
44 const commitArgs = [
45 '-c',
46 `user.name=${identity.name}`,
47 '-c',
48 `user.email=${identity.email}`,
49 'commit',
50 '-m',
51 message,
52 ]
53 const commit = await runner(commitArgs, cwd)
54 if (commit.code === 0) {
55 return { committed: true }
56 }
57​
58 const output = `${commit.stdout}\n${commit.stderr}`
59 const isEmptyCommit =
60 commit.code === 1 && output.includes('nothing to commit')
61 if (isEmptyCommit) {
62 return { committed: false }
63 }
64​
65 gitFailed(commitArgs, commit)
67​
68/**
69 * Push `HEAD` to `dest.ref` on `dest.remoteUrl` with `--force-with-lease`.
70 *
71 * Never uses a bare `--force`. Throws when git exits non-zero.
72 *
73 * @param cwd git working tree
74 * @param runner injected git runner
75 * @param dest remote URL and destination ref
76 */
77export async function pushHead(
78 cwd: string,
79 runner: GitRunner,
80 dest: { remoteUrl: string; ref: string },
81) {
82 const args = [
83 'push',
84 '--force-with-lease',
85 dest.remoteUrl,
86 `HEAD:${dest.ref}`,
87 ]
88 const result = await runner(args, cwd)
89 if (result.code !== 0) {
90 gitFailed(args, result)
91 }
93​
94/**
95 * HTTPS remote URL for pushing the PR head, with an access token.
96 *
97 * Same-repo PRs use `originRepo`. Forks use `headRepo`.
98 *
99 * @param opts fork flag, origin/head repos, and token
100 */
101export function headRemoteUrl(opts: {
102 isFork: boolean
103 originRepo: string
104 headRepo: string
105 token: string
106}) {
107 const repo = opts.isFork ? opts.headRepo : opts.originRepo
108 return `https://x-access-token:${opts.token}@github.com/${repo}.git`