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