|
| 1 | +import {shell} from './shell'; |
| 2 | + |
| 3 | +type Dir = { |
| 4 | + dir?: string; |
| 5 | +}; |
| 6 | + |
| 7 | +type Repo = Dir & { |
| 8 | + repo: string; |
| 9 | + onProgress?: (phase: string, percentage: number) => void; |
| 10 | +}; |
| 11 | + |
| 12 | +type Ref = Dir & { |
| 13 | + ref: string; |
| 14 | +}; |
| 15 | + |
| 16 | +type Pull = Dir & { |
| 17 | + ref?: string; |
| 18 | + remote?: string; |
| 19 | +}; |
| 20 | + |
| 21 | +type Log = { |
| 22 | + oid: string; |
| 23 | + commit: { |
| 24 | + author: { |
| 25 | + name: string; |
| 26 | + email: string; |
| 27 | + timestamp: number; |
| 28 | + }; |
| 29 | + message: string; |
| 30 | + }; |
| 31 | +}; |
| 32 | + |
| 33 | +const ProgressRegExp = new RegExp(/^(remote: )?([^:]+?):[ ]+(\d+)%/); |
| 34 | + |
| 35 | +const clone = ({dir, repo, onProgress}: Repo) => { |
| 36 | + const stderr = onProgress |
| 37 | + ? (chunk?: string) => { |
| 38 | + if (!chunk) return; |
| 39 | + const progress = ProgressRegExp.exec(chunk); |
| 40 | + if (progress) { |
| 41 | + onProgress(progress[2].toLowerCase(), parseInt(progress[3], 10)); |
| 42 | + } |
| 43 | + } |
| 44 | + : undefined; |
| 45 | + |
| 46 | + return shell.exec(`git clone --single-branch --progress [email protected]:${repo}.git ${dir}`, { |
| 47 | + cwd: process.cwd(), |
| 48 | + stderr, |
| 49 | + }); |
| 50 | +}; |
| 51 | + |
| 52 | +const remove = ({dir, repo}: Repo) => |
| 53 | + shell.exec(`rm -rf ${dir || repo.split('/').pop()}`, { |
| 54 | + cwd: process.cwd(), |
| 55 | + }); |
| 56 | + |
| 57 | +const pull = ({dir, remote, ref}: Pull = {}) => |
| 58 | + shell.exec(`git pull ${remote} ${ref}`, { |
| 59 | + cwd: dir || process.cwd(), |
| 60 | + }); |
| 61 | + |
| 62 | +const main = async ({dir}: Dir = {}): Promise<string> => { |
| 63 | + const r = await shell.exec( |
| 64 | + `git branch --remote --list 'origin/*' --format '{"branch":"%(refname:short)"}'`, |
| 65 | + { |
| 66 | + cwd: dir || process.cwd(), |
| 67 | + }, |
| 68 | + ); |
| 69 | + if (r.stdout) { |
| 70 | + return r.stdout |
| 71 | + .split('\n') |
| 72 | + .map((line) => JSON.parse(line)) |
| 73 | + .filter((line) => line.branch.startsWith('origin/')) |
| 74 | + .pop() |
| 75 | + .branch.replace(/^origin\//, ''); |
| 76 | + } |
| 77 | + return ''; |
| 78 | +}; |
| 79 | + |
| 80 | +const checkout = ({ref, dir}: Ref) => |
| 81 | + shell.exec(`git checkout --force ${ref}`, { |
| 82 | + cwd: dir || process.cwd(), |
| 83 | + }); |
| 84 | + |
| 85 | +const log = async ({dir}: Dir): Promise<Array<Log>> => { |
| 86 | + const r = await shell.exec( |
| 87 | + `git log --reflog --pretty=format:'{"oid":"%H","commit":{"author":{"name":"%aN","email":"%aE","timestamp":%ad},"message": "%f"}}' --date=unix`, |
| 88 | + { |
| 89 | + cwd: dir || process.cwd(), |
| 90 | + }, |
| 91 | + ); |
| 92 | + if (r.stdout) { |
| 93 | + return r.stdout.split('\n').map((line) => JSON.parse(line.replace(/""(.+)""/g, '"$1"'))); |
| 94 | + } |
| 95 | + return []; |
| 96 | +}; |
| 97 | + |
| 98 | +const clean = ({dir}: Dir = {}) => |
| 99 | + shell.exec(`git clean -fdx`, { |
| 100 | + cwd: dir || process.cwd(), |
| 101 | + }); |
| 102 | + |
| 103 | +const reset = ({dir}: Dir = {}) => |
| 104 | + shell.exec(`git reset HEAD --hard`, { |
| 105 | + cwd: dir || process.cwd(), |
| 106 | + }); |
| 107 | + |
| 108 | +export const git = { |
| 109 | + clone, |
| 110 | + pull, |
| 111 | + checkout, |
| 112 | + log, |
| 113 | + reset, |
| 114 | + remove, |
| 115 | + clean, |
| 116 | + main, |
| 117 | +}; |
0 commit comments