-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgit-pretty.js
executable file
·324 lines (285 loc) · 10.3 KB
/
git-pretty.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/env node
const inquirer = require('inquirer')
const chalk = require('chalk')
async function ask(question, choices) {
const { answer } = await inquirer.prompt([
{
type: 'list',
name: 'answer',
message: question,
choices: choices.map((name, index) => ({
name,
value: `${index + 1}`,
})),
},
])
return answer
}
function askYesNo(question) {
return ask(question, ['Yes', 'No'])
}
function printCode(code) {
console.log(chalk.bgGreenBright(chalk.black(` ${code} `)))
}
function dangerZone() {
console.log('\n' + chalk.bgRed('Welcome to the DANGER ZONE!') + '\n')
}
function interactiveRebase() {
dangerZone()
console.log(
`We're going to do an ${chalk.bold('** interactive rebase! **')}`,
)
printCode('git rebase -i {COMMITISH}')
console.log()
console.log("And when that's done, do this:")
console.log()
printCode('git push --force origin {branch}')
console.log()
}
async function main() {
let answer = await ask(
'So you have a mess on your hands. What sort of mess?',
[
'An uncommitted mess',
'I accidentally committed something',
'My Git history is ugly',
'I have a bunch of old branches I want gone',
'I want to sync my fork with the original repo',
'I want my branch to be exactly like on GitHub!',
],
)
if (answer === '1') {
answer = await askYesNo(
'Do you care enough about your mess to keep it?',
)
if (answer === '1') {
console.log('Looks like we caught this just in time.')
console.log(
'Split off a logical chunk from your mess, stage it and commit it with a good message.',
)
console.log('Still have a mess? Do it again.')
return '11'
}
if (answer === '2') {
console.log('Looks like this is what you are looking for:')
console.log()
printCode('git reset --hard')
console.log()
return '12'
}
}
if (answer === '2') {
answer = await askYesNo('Has anyone else seen it?')
if (answer === '1') {
console.log('Looks like this is what you are looking for:')
console.log()
printCode('git revert {COMMITISH}')
console.log()
return '21'
}
if (answer === '2') {
answer = await ask('How long ago?', [
'Last commit',
'It seems like forever ago',
])
if (answer === '1') {
answer = await ask('What would make this better?', [
'I forgot to add a file',
'A better message',
'Remove the last commit, but keep the changes',
'Throw the last commit away, and delete its changes',
])
if (answer === '1') {
console.log('Looks like this is what you are looking for:')
console.log()
printCode('git add {my_awesome_file}')
printCode('git commit --amend')
console.log()
return '2211'
}
if (answer === '2') {
console.log('Looks like this is what you are looking for:')
console.log()
printCode('git commit --amend')
console.log()
return '2212'
}
if (answer === '3') {
console.log('Looks like this is what you are looking for:')
console.log()
printCode('git reset HEAD~')
console.log()
return '2213'
}
if (answer === '4') {
console.log('Looks like this is what you are looking for:')
console.log()
printCode('git reset --hard HEAD^')
console.log()
return '2214'
}
}
if (answer === '2') {
answer = await askYesNo('Take a mulligan?')
if (answer === '1') {
console.log("We'll reset and commit from scratch:")
console.log()
printCode('git reset {COMMITISH}')
console.log()
console.log(
'Then split off a logical chunk from your mess, stage it and commit it with a good message.',
)
console.log('Still have a mess? Do it again.')
return '2221'
}
if (answer === '2') {
interactiveRebase()
return '2222'
}
}
}
}
if (answer === '3') {
answer = await askYesNo('Is it already on GitHub?')
if (answer === '1') {
answer = await askYesNo('Is anyone down stream?')
if (answer === '1') {
answer = await askYesNo('Enough to form a lynch mob?')
if (answer === '1') {
console.log("It's safest to let it stay ugly then")
return '3111'
}
if (answer === '2') {
answer = await askYesNo('Do you hate them?')
if (answer === '1') {
interactiveRebase()
return '31121'
}
if (answer === '2') {
console.log(
"Send them a note, let 'em know you're changing history.",
)
console.log()
interactiveRebase()
return '31122'
}
}
}
if (answer === '2') {
interactiveRebase()
return '312'
}
}
if (answer === '2') {
answer = await ask('Should we remove merge conflicts?', [
'That would do the trick',
'No, I need to change history!',
])
if (answer === '1') {
console.log('Looks like this is what you are looking for:')
console.log()
printCode('git rebase origin/{branch}')
console.log()
return '321'
}
if (answer === '2') {
interactiveRebase()
return '322'
}
}
}
if (answer === '4') {
dangerZone()
console.log(
'To delete all local branches that are already merged into the currently checked out branch:',
)
printCode(
"git branch --merged | egrep -v '(^\\*|master|main|dev)' | xargs git branch -d",
)
console.log()
console.log(
'You can see that master and dev are excluded in case they are an ancestor.',
)
console.log(
'Check out https://stackoverflow.com/questions/6127328/how-can-i-delete-all-git-branches-which-have-been-merged',
)
return '4'
}
if (answer === '5') {
console.log(
"Alright! You have forked a repo a while ago, and now it's time to update it with the changes in the original repo.",
)
console.log()
const answer = await ask(
'Have you already configured the original repo as a remote?',
['No', 'Yes', 'Not sure'],
)
if (answer === '1') {
console.log("Let's set up a remote.")
printCode(
'git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git',
)
console.log()
console.log('Verify that it was added by listing all remotes:')
printCode('git remote -v')
console.log()
console.log('Fetch the changes from the remote:')
printCode('git fetch upstream')
console.log()
console.log('Now checkout your local master branch')
printCode('git checkout master')
console.log()
console.log(
'Merge the changes from the remote into your local branch',
)
printCode('git merge upstream/master')
console.log()
console.log(
"Read GitHub's guide for this if you don't trust git-pretty: https://help.github.com/en/articles/syncing-a-fork",
)
return '52'
}
if (answer === '2') {
console.log(
'Ok then. I\'ll assume the name of your remote for the original repo is "upstream"',
)
console.log()
console.log('Fetch the changes from the remote:')
printCode('git fetch upstream')
console.log()
console.log('Now checkout your local master branch')
printCode('git checkout master')
console.log()
console.log(
'Merge the changes from the remote into your local branch',
)
printCode('git merge upstream/master')
console.log()
console.log(
"Read GitHub's guide for this if you don't trust git-pretty: https://help.github.com/en/articles/syncing-a-fork",
)
return '51'
}
if (answer === '3') {
console.log('Not sure? Do this:')
printCode('git remote -v')
console.log(
'If the original repo is in that list, that means the remote is configured.',
)
return '53'
}
}
if (answer === '6') {
console.log(
'Here is what you need. if master is not your desired branch, replace it in the code below with the desired branch name.',
)
console.log(
"Make sure you don't have any local changes you want to keep!",
)
dangerZone()
printCode('git fetch')
printCode('git reset --hard origin/master')
return '6'
}
}
main()