Skip to content

Commit 4641fdb

Browse files
committed
add reset script
Signed-off-by: shmck <shawn.j.mckay@gmail.com>
1 parent b56993a commit 4641fdb

File tree

2 files changed

+63
-37
lines changed

2 files changed

+63
-37
lines changed

Diff for: src/channel/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { openWorkspace, checkWorkspaceEmpty } from '../services/workspace'
1818
import { showOutput } from '../services/testRunner/output'
1919
import { exec } from '../services/node'
2020
import { WORKSPACE_ROOT, TUTORIAL_URL } from '../environment'
21+
import reset from '../services/git/reset'
2122
import getLastCommitHash from '../services/git/lastHash'
2223

2324
const readFileAsync = promisify(readFile)
@@ -330,7 +331,7 @@ class Channel implements Channel {
330331
const hash = getLastCommitHash(position, tutorial?.levels || [])
331332

332333
// load timeline until last pass commit
333-
// TODO: run reset script
334+
reset({ branch: tutorial?.config.repo.branch, hash })
334335

335336
// if tutorial.config.reset.command, run it
336337
if (tutorial?.config?.reset?.command) {

Diff for: src/services/git/reset.ts

+61-36
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,74 @@
11
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)
36

47
interface Input {
58
hash: string
69
branch: string
710
}
811

12+
const ignoreError = () => {}
13+
914
// note: attempted to do this as a bash script
1015
// but requires the bash script has git permissions
1116
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+
}
4772
}
4873

4974
export default reset

0 commit comments

Comments
 (0)