|
1 | 1 | import * as fs from 'fs'
|
2 |
| -import { exec, exists } from '../node' |
| 2 | +import { promisify } from 'util' |
| 3 | +import { exec } from '../node' |
| 4 | + |
| 5 | +const removeFile = promisify(fs.unlink) |
3 | 6 |
|
4 | 7 | interface Input {
|
5 | 8 | hash: string
|
6 | 9 | branch: string
|
7 | 10 | }
|
8 | 11 |
|
| 12 | +const ignoreError = () => {} |
| 13 | + |
9 | 14 | // note: attempted to do this as a bash script
|
10 | 15 | // but requires the bash script has git permissions
|
11 | 16 | const reset = async ({ branch, hash }: Input): Promise<void> => {
|
12 |
| - // TODO: capture branch |
13 |
| - const localBranch = 'master' |
14 |
| - |
15 |
| - // switch to an empty branch |
16 |
| - await exec({ |
17 |
| - command: 'git checkout --orphan reset-orphan-branch', |
18 |
| - }) |
19 |
| - // stash any current work |
20 |
| - await exec({ |
21 |
| - command: 'git stash', |
22 |
| - }) |
23 |
| - // remove any other files |
24 |
| - await exec({ |
25 |
| - command: 'git rm -rf .', |
26 |
| - }) |
27 |
| - // TODO: delete .gitignore |
28 |
| - |
29 |
| - await exec({ |
30 |
| - command: `git branch -D ${localBranch}`, |
31 |
| - }) |
32 |
| - await exec({ |
33 |
| - command: `git checkout -b ${localBranch}`, |
34 |
| - }) |
35 |
| - |
36 |
| - // load git timeline |
37 |
| - await exec({ |
38 |
| - command: `git fetch coderoad ${branch}`, |
39 |
| - }) |
40 |
| - await exec({ |
41 |
| - command: `git merge coderoad/${localBranch}`, |
42 |
| - }) |
43 |
| - // reset to target commit hash |
44 |
| - await exec({ |
45 |
| - command: `git reset --hard ${hash}`, |
46 |
| - }) |
| 17 | + const remote = 'coderoad' |
| 18 | + |
| 19 | + try { |
| 20 | + // if no git init, will initialize |
| 21 | + // otherwise re-initializes git |
| 22 | + await exec({ command: 'git init' }).catch(console.log) |
| 23 | + |
| 24 | + // capture current branch |
| 25 | + const hasBranch = await exec({ command: 'git branch --show-current' }) |
| 26 | + const localBranch = hasBranch.stdout |
| 27 | + // check if coderoad remote exists |
| 28 | + const hasRemote = await exec({ command: 'git remote -v' }).catch(console.warn) |
| 29 | + if (!hasRemote || !hasRemote.stdout || !hasRemote.stdout.length) { |
| 30 | + throw new Error('No remote found') |
| 31 | + } else if (!hasRemote.stdout.match(new RegExp(remote))) { |
| 32 | + throw new Error(`No "${remote}" remote found`) |
| 33 | + } |
| 34 | + |
| 35 | + // switch to an empty branch |
| 36 | + await exec({ |
| 37 | + command: 'git checkout --orphan reset-orphan-branch', |
| 38 | + }) |
| 39 | + // stash any current work |
| 40 | + await exec({ |
| 41 | + command: 'git stash', |
| 42 | + }).catch(ignoreError) |
| 43 | + |
| 44 | + // remove any other files |
| 45 | + await exec({ |
| 46 | + command: 'git rm -rf .', |
| 47 | + }).catch(ignoreError) |
| 48 | + await removeFile('.gitignore').catch(ignoreError) |
| 49 | + |
| 50 | + await exec({ |
| 51 | + command: `git branch -D ${localBranch}`, |
| 52 | + }) |
| 53 | + await exec({ |
| 54 | + command: `git checkout -b ${localBranch}`, |
| 55 | + }) |
| 56 | + |
| 57 | + // load git timeline |
| 58 | + await exec({ |
| 59 | + command: `git fetch coderoad ${branch}`, |
| 60 | + }) |
| 61 | + await exec({ |
| 62 | + command: `git merge coderoad/${branch}`, |
| 63 | + }) |
| 64 | + // reset to target commit hash |
| 65 | + await exec({ |
| 66 | + command: `git reset --hard ${hash}`, |
| 67 | + }) |
| 68 | + } catch (error) { |
| 69 | + console.error('Error resetting') |
| 70 | + console.error(error.message) |
| 71 | + } |
47 | 72 | }
|
48 | 73 |
|
49 | 74 | export default reset
|
0 commit comments