| 1 | import { runGit } from '../exec/run-git.js'; |
| 2 | |
| 3 | const RECORD = ' |
| 4 | const FIELD = ' |
| 5 | const SUMMARY_FORMAT = ` |
| 6 | const FORMAT = ` |
| 7 | |
| 8 | export interface Commit |
| 9 | sha: string; |
| 10 | authorName: string; |
| 11 | authorEmail: string; |
| 12 | /** Committer timestamp, ISO 8601. */ |
| 13 | committedAt: string; |
| 14 | subject: string; |
| 15 | body: string; |
| 16 | } |
| 17 | |
| 18 | export interface CommitSummary |
| 19 | sha: string; |
| 20 | subject: string; |
| 21 | /** Committer timestamp, ISO 8601. */ |
| 22 | committedAt: string; |
| 23 | } |
| 24 | |
| 25 | export interface CommitFile |
| 26 | /** A for added, M for modified, D for deleted. */ |
| 27 | status: string; |
| 28 | path: string; |
| 29 | } |
| 30 | |
| 31 | /** A commit-ish a caller supplied, safe to hand git as a revision. */ |
| 32 | export function isSha(value |
| 33 | return /^[0-9a-f] |
| 34 | } |
| 35 | |
| 36 | function parse(record: |
| 37 | const [sha, |
| 38 | record.split(FIELD |
| 39 | if (!sha) |
| 40 | |
| 41 | return { |
| 42 | sha, |
| 43 | authorName, |
| 44 | authorEmail, |
| 45 | committedAt: new Date |
| 46 | subject, |
| 47 | body: (body ?? |
| 48 | }; |
| 49 | } |
| 50 | |
| 51 | /** Newest-first history of a ref, one page at a time. `cursor` is the sha to resume from, which stays correct as new commits land on top. */ |
| 52 | export async function listCommits |
| 53 | gitDir, |
| 54 | env, |
| 55 | ref, |
| 56 | path, |
| 57 | limit, |
| 58 | cursor, |
| 59 | }: { |
| 60 | gitDir: string |
| 61 | env?: Record |
| 62 | ref: string |
| 63 | path?: string |
| 64 | limit: number |
| 65 | cursor?: string |
| 66 | }): Promise<{ |
| 67 | const args = |
| 68 | 'log', |
| 69 | FORMAT, |
| 70 | // one extra row answers "is there another page" without a second walk |
| 71 | '-n', |
| 72 | String(limit + |
| 73 | '--end-of-options', |
| 74 | cursor ?? ref, |
| 75 | ]; |
| 76 | if (path) args |
| 77 | |
| 78 | const raw = |
| 79 | const commits = |
| 80 | |
| 81 | return { |
| 82 | commits: commits.slice |
| 83 | nextCursor: commits.length |
| 84 | }; |
| 85 | } |
| 86 | |
| 87 | /** One commit with the paths it changed, or null when the sha is unknown. */ |
| 88 | export async function readCommit |
| 89 | gitDir, |
| 90 | sha, |
| 91 | }: { |
| 92 | gitDir: string |
| 93 | sha: string |
| 94 | }): Promise<( |
| 95 | const raw = |
| 96 | args: ['show', |
| 97 | gitDir, |
| 98 | }).catch(() |
| 99 | |
| 100 | const commit = |
| 101 | if (!commit) |
| 102 | |
| 103 | const changes = |
| 104 | args: [ |
| 105 | 'show', |
| 106 | '--name-status', |
| 107 | '--format=', |
| 108 | '-z', |
| 109 | '--no-renames', |
| 110 | // a merge shows nothing without this, and its first-parent diff is what a reader expects to see |
| 111 | '--first-parent', |
| 112 | '--end-of-options', |
| 113 | commit.sha, |
| 114 | ], |
| 115 | gitDir, |
| 116 | }); |
| 117 | |
| 118 | const parts = |
| 119 | const files: |
| 120 | for (let i |
| 121 | files.push({ |
| 122 | } |
| 123 | |
| 124 | return { ...commit |
| 125 | } |
| 126 | |
| 127 | /** The newest commit touching `path`, or the tip of `ref` when no path is given. Null when the ref has no history. */ |
| 128 | export async function readCommitSummary |
| 129 | gitDir, |
| 130 | ref, |
| 131 | path, |
| 132 | }: { |
| 133 | gitDir: string |
| 134 | ref: string |
| 135 | path?: string |
| 136 | }): Promise<CommitSummary |
| 137 | const args = |
| 138 | if (path) args |
| 139 | |
| 140 | const raw = |
| 141 | const [sha, |
| 142 | if (!sha) |
| 143 | |
| 144 | return { |
| 145 | sha, |
| 146 | subject, |
| 147 | committedAt: new Date |
| 148 | }; |
| 149 | } |