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