Skip to content

Commit 6d3bc21

Browse files
authored
Merge pull request #71 from jonny64/issue_70
#70 FIX git 2.11.0
2 parents 2188643 + 77a3c11 commit 6d3bc21

9 files changed

Lines changed: 89 additions & 38 deletions

File tree

lib/CreateCommand.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ module.exports = class {
1010
src = src || await defaultBranch.print()
1111

1212
return {
13+
confirmLabel: `Create new branch '${dst}' from '${src}' [Y/n]? `,
1314
todo: [
1415
`git fetch`,
15-
{
16-
todo: `git switch --guess --merge --create ${dst} ${src}`,
17-
confirm: `Create new branch '${dst}' from '${src}' [Y/n]? `,
18-
},
16+
`git switch --guess --merge --create ${dst} ${src}`,
1917
`git config branch.${dst}.mr-target ${src}`,
2018
]
2119
}

lib/GitBranch.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ module.exports = class GitBranch {
4343
return ''
4444
}
4545

46-
// @todo #62:1h move to constructor
47-
let mrSet = await this.run (`git config --default '' --get branch.${this.name}.description`)
46+
let mrSet = await this.gitRepo.config (`branch.${this.name}.description`)
47+
4848
if (mrSet) {
4949
return ''
5050
}
@@ -63,15 +63,15 @@ module.exports = class GitBranch {
6363
}
6464

6565
async parentBranch () {
66-
let parent = await this.run (`git config --default '' --get branch.${this.name}.mr-target`)
66+
let parent = await this.gitRepo.config (`branch.${this.name}.mr-target`)
6767
if (parent) {
6868
return GitBranch.withName (parent, this.gitRepo)
6969
}
7070
return (await this.gitRepo.defaultBranch ())
7171
}
7272

7373
async isOriginGitlab () {
74-
let originUrl = await this.run (`git config remote.origin.url`)
74+
let originUrl = await this.gitRepo.config (`remote.origin.url`)
7575
return originUrl.includes ('gitlab')
7676
}
7777

lib/GitRepo.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ module.exports = class {
3333
}
3434

3535
async toMergeAfter (branch) {
36-
let listComma = await this.run (`git config --default '' --get branch.${branch.name}.mr-merge-after`)
36+
let listComma = await this.config (`branch.${branch.name}.mr-merge-after`)
3737
if (!listComma) {
3838
// @todo #40:1h move all console.log to injected LogCommand
3939
console.log (`to set up premerge for branch '${branch.name}': `.padEnd (40) + `git config branch.${branch.name}.mr-merge-after test,release`)
@@ -47,7 +47,7 @@ module.exports = class {
4747
return this.commandTest
4848
}
4949

50-
let listComma = await this.run (`git config --default '' --get mr.test`)
50+
let listComma = await this.config (`mr.test`)
5151
if (!listComma) {
5252
console.log (`to set up prepush command: `.padEnd (40) + `git config mr.test 'npm test'`)
5353
this.commandTest = []
@@ -57,6 +57,17 @@ module.exports = class {
5757
return this.commandTest
5858
}
5959

60+
async config (key) {
61+
let v
62+
try {
63+
v = await this.run (`git config ${key}`)
64+
} catch (x) {
65+
// @todo #70:1h replace catch by --default '' when git 2.11 EOL
66+
return ''
67+
}
68+
return v
69+
}
70+
6071
async run (cmd) {
6172
return ShellCommand.withText (cmd).runSilent ()
6273
}

lib/MrCommand.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const GitRepo = require('./GitRepo')
2+
const OldGit = require('./OldGit')
23
// @todo #0:30m move to lib/commands/Push, lib/commands/Switch, etc.
34
const Push = require('./PushCommand')
45
const Create = require('./CreateCommand')
@@ -19,22 +20,26 @@ module.exports = class MrCommand {
1920
for (let [name, clazz] of Object.entries (name2command)) {
2021
commands [name] = new clazz ({parsedArgs, gitRepo, createCommand})
2122
}
22-
return new MrCommand({parsedArgs, gitRepo, commands})
23+
const oldGit = new OldGit ()
24+
return new MrCommand({parsedArgs, gitRepo, commands, oldGit})
2325
}
2426

2527
constructor(o) {
2628
this.parsedArgs = o.parsedArgs
2729
this.gitRepo = o.gitRepo
2830
this.commands = o.commands
31+
this.oldGit = o.oldGit
2932
}
3033

3134
async todo () {
32-
let {gitRepo, parsedArgs, commands} = this
35+
let {oldGit, parsedArgs, commands} = this
3336
let args = await parsedArgs.value ()
3437
let c = commands [args.action]
3538
if (!c) {
3639
throw new Error (`unknown action: ${args.action}!`)
3740
}
38-
return c.todo()
41+
let todo = await c.todo ()
42+
return oldGit.translate (todo)
3943
}
44+
4045
}

lib/OldGit.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const ShellCommand = require('./ShellCommand')
2+
3+
module.exports = class OldGit {
4+
5+
async translate (o) {
6+
let v = await ShellCommand.withText (`git --version`).runSilent ()
7+
let version = v.split ('git version')[1]
8+
9+
if (version > '2.23') {
10+
return o
11+
}
12+
13+
let translated = []
14+
15+
for (let i of o.todo) {
16+
translated = translated.concat (await this.translateCmd (i))
17+
}
18+
o.todo = translated
19+
20+
return o
21+
}
22+
23+
async translateCmd (cmd) {
24+
if (/^git switch --guess --merge --create/.test (cmd)) {
25+
return [
26+
cmd.split ('git switch --guess --merge --create').join ('git checkout -b'),
27+
]
28+
}
29+
if (/^git switch --guess --merge/.test (cmd)) {
30+
return [
31+
'git stash',
32+
cmd.split ('git switch --guess --merge').join ('git checkout'),
33+
'git stash pop -q'
34+
]
35+
}
36+
return [cmd]
37+
}
38+
}

lib/RunCommand.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ const UserInput = require ('./UserInput')
44

55
module.exports = class {
66
constructor(o) {
7-
// @todo #40:2h/ARC remove typeof
8-
this.todo = o.todo.map (i => (typeof i == "string"? {todo: i} : i))
7+
this.todo = o.todo
8+
this.confirmLabel = o.confirmLabel
99
}
1010

1111
async confirm () {
12-
if (this.todo.length <= 3) {
12+
if (this.todo.length <= 4 && !this.confirmLabel) {
1313
return true
1414
}
1515

1616
console.log (`\nOk, here is the plan:\n${await this.print ()}\n`)
1717

18-
return new UserInput (`Proceed [Y/n]?`).confirm ()
18+
return new UserInput (this.confirmLabel || `Proceed [Y/n]?`).confirm ()
1919
}
2020

2121
async run () {
@@ -27,14 +27,7 @@ module.exports = class {
2727
for (let idx = 0; idx < this.todo.length; idx++) {
2828
let i = this.todo [idx]
2929

30-
if (i.confirm) {
31-
let ok = await new UserInput (i.confirm).confirm ()
32-
if (!ok) {
33-
throw new Error ('Aborted')
34-
}
35-
}
36-
37-
let cmd = ShellCommand.withText (i.todo)
30+
let cmd = ShellCommand.withText (i)
3831

3932
try {
4033
await cmd.run ()
@@ -55,6 +48,6 @@ module.exports = class {
5548

5649
async print (idx) {
5750
let tail = !idx? this.todo: this.todo.slice (idx + 1)
58-
return tail.map (i => ` ${i.todo}`).join ('\n')
51+
return tail.map (i => ` ${i}`).join ('\n')
5952
}
6053
}

lib/SwitchCommand.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module.exports = class {
2020
return {
2121
todo: [
2222
`git fetch`,
23-
`git switch --merge --guess ${dst}`,
23+
`git switch --guess --merge ${dst}`,
2424
]
2525
}
2626
}

tests/MergeCommand.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ const ParsedArgs = require ('../lib/ParsedArgs')
99
describe('MergeCommand', () => {
1010
let f = function () {
1111
switch (this.cmd) {
12-
case "git config mr.master.mergeAfter --default ''":
12+
case "git config mr.master.mergeAfter":
1313
return [GitBranch.withName ('test')]
1414
case "git push --set-upstream origin TASK-42:TASK-42":
1515
case "git push --set-upstream origin TASK-0:TASK-0":
1616
case "git push --set-upstream origin TASK-9:TASK-9":
1717
case "git push --set-upstream origin dummy:dummy":
1818
return "pushed"
19-
case "git config --default '' --get mr.test":
19+
case "git config mr.test":
2020
return "npm test"
2121
case "npm test":
2222
return "OK"

tests/MrCommand.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const assert = require ('assert')
33
const ShellCommand = require ('../lib/ShellCommand')
44
const GitRepo = require ('../lib/GitRepo')
55
const GitBranch = require ('../lib/GitBranch')
6+
const OldGit = require ('../lib/OldGit')
67
const MrCommand = require ('../lib/MrCommand')
78
const ParsedArgs = require ('../lib/ParsedArgs')
89
const Push = require ('../lib/PushCommand')
@@ -19,7 +20,7 @@ describe('random input', () => {
1920
return `e46431c45f3c46d87c22cf63d70db2f4435bd89b refs/heads/TASK-42`
2021
case 'git branch --list TASK-43':
2122
return ``
22-
case "git config --default '' --get mr.test":
23+
case "git config mr.test":
2324
return ``
2425
default:
2526
throw new Error (`Unknow command: ${this.cmd}`)
@@ -41,11 +42,16 @@ describe('random input', () => {
4142
return new GitBranch ({ name: 'main', origin: 'gitlab' })
4243
})
4344

45+
mock.method(OldGit.prototype, 'translate', function (o) {
46+
return o
47+
})
48+
4449
it ('push on empty arguments', async (t) => {
4550
const parsedArgs = new ParsedArgs ([])
4651
const gitRepo = new GitRepo ()
4752
const commands = {Push: new Push ({gitRepo, parsedArgs})}
48-
const todo = await new MrCommand ({parsedArgs, gitRepo, commands}).todo()
53+
const oldGit = new OldGit ()
54+
const todo = await new MrCommand ({parsedArgs, gitRepo, commands, oldGit}).todo()
4955
assert.deepStrictEqual(todo, {
5056
todo: [
5157
'git push --set-upstream gitlab TASK-42:TASK-42'
@@ -57,11 +63,12 @@ describe('random input', () => {
5763
const parsedArgs = new ParsedArgs (['TASK-42'])
5864
const gitRepo = new GitRepo ()
5965
const commands = {Switch: new Switch ({gitRepo, parsedArgs})}
60-
const todo = await new MrCommand ({parsedArgs, gitRepo, commands}).todo()
66+
const oldGit = new OldGit ()
67+
const todo = await new MrCommand ({parsedArgs, gitRepo, commands, oldGit}).todo()
6168
assert.deepStrictEqual(todo, {
6269
todo: [
6370
'git fetch',
64-
'git switch --merge --guess TASK-42',
71+
'git switch --guess --merge TASK-42',
6572
]
6673
})
6774
})
@@ -70,15 +77,14 @@ describe('random input', () => {
7077
const parsedArgs = new ParsedArgs (['TASK-43'])
7178
const gitRepo = new GitRepo ()
7279
const createCommand = new Create ({gitRepo, parsedArgs})
80+
const oldGit = new OldGit ()
7381
const commands = {Switch: new Switch ({gitRepo, parsedArgs, createCommand})}
74-
const todo = await new MrCommand ({parsedArgs, gitRepo, commands}).todo()
82+
const todo = await new MrCommand ({parsedArgs, gitRepo, commands, oldGit}).todo()
7583
assert.deepStrictEqual(todo, {
84+
confirmLabel: `Create new branch 'TASK-43' from 'gitlab/main' [Y/n]? `,
7685
todo: [
7786
'git fetch',
78-
{
79-
todo: 'git switch --guess --merge --create TASK-43 gitlab/main',
80-
confirm: "Create new branch 'TASK-43' from 'gitlab/main' [Y/n]? ",
81-
},
87+
'git switch --guess --merge --create TASK-43 gitlab/main',
8288
`git config branch.TASK-43.mr-target gitlab/main`
8389
]
8490
})

0 commit comments

Comments
 (0)