Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/GitBranch.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ const ShellCommand = require ('./ShellCommand')

module.exports = class GitBranch {

static withName (id, gitRepo) {
static async withName (id, gitRepo) {
let [origin, name] = id.split ('/')
if (!name) {
name = origin
origin = 'origin'
}
if (name == '__CURRENT_BRANCH__') {
return await gitRepo.currentBranch ()
}
return new GitBranch ({ name, origin, gitRepo })
}

Expand Down Expand Up @@ -79,7 +82,7 @@ module.exports = class GitBranch {
async parentBranch () {
let parent = await this.gitRepo.config (`branch.${this.name}.mr-target`)
if (parent) {
return GitBranch.withName (parent, this.gitRepo)
return await GitBranch.withName (parent, this.gitRepo)
}
return (await this.gitRepo.defaultBranch ())
}
Expand Down
4 changes: 2 additions & 2 deletions lib/GitRepo.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = class {

async defaultBranch () {
let [origin, name] = (await this.run (`git symbolic-ref refs/remotes/origin/HEAD --short`)).split ('/')
return GitBranch.withName (`${origin}/${name}`, this)
return await GitBranch.withName (`${origin}/${name}`, this)
}

async countDiffCommits (src, dst) {
Expand Down Expand Up @@ -41,7 +41,7 @@ module.exports = class {
this.log (`to set up premerge for branch '${branch.name}': `.padEnd (40) + `git config branch.${branch.name}.mr-merge-after test,release`)
return []
}
return listComma.split (',').map (i => GitBranch.withName (i, this))
return Promise.all (listComma.split (',').map (i => GitBranch.withName (i, this)))
}

async toTest () {
Expand Down
6 changes: 3 additions & 3 deletions lib/MergeCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ module.exports = class {
async todo () {
let {gitRepo, parsedArgs} = this
let {src, dst} = await parsedArgs.value ()
let srcBranch = GitBranch.withName (src, this.gitRepo)
let dstBranch = GitBranch.withName (dst, this.gitRepo)
let srcBranch = await GitBranch.withName (src, this.gitRepo)
let dstBranch = await GitBranch.withName (dst, this.gitRepo)

if (await srcBranch.current()) {
await srcBranch.push ()
Expand All @@ -22,7 +22,7 @@ module.exports = class {
todo = todo.concat (await this.mergeTodo (srcBranch, branch))
}
if (todo.length) {
todo = todo.concat (`git checkout ${src}`)
todo = todo.concat (`git checkout ${srcBranch.name}`)
}

return { todo }
Expand Down
29 changes: 14 additions & 15 deletions lib/ParsedArgs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,36 @@ module.exports = class {
}

value() {
const [a1, verb, a2] = this.argv
// @todo #0:30m remove need of 'to' to mimic old syntax
// less key presses, e.g.:
// mr TASK-42 from origin/release
// git commit -m "TASK-42: commit msg"
// mr TASK-42 release
const [branch, verb, src_or_dst] = this.argv
const usage = `usage: mr TASK-42 [from|to main]`

if (verb && !['from', 'to'].includes(verb)) {
if (verb && !['from', 'to'].includes(verb) && !['to'].includes(branch)) {
throw new Error (usage)
}

if (['from', 'to'].includes(verb) && !a2) {
if (['from', 'to'].includes(verb) && !src_or_dst) {
throw new Error (usage)
}

if (!a1 && !a2) {
return {src: '', dst: '', action: 'Push'}
if (!branch && !src_or_dst) {
return {src: '__CURRENT_BRANCH__', dst: '', action: 'Push'}
}

let [src, dst, action] = ['', a1, 'Switch']
if (['to'].includes(branch) && verb && !src_or_dst) {
return {src: '__CURRENT_BRANCH__', dst: verb, action: 'Merge'}
}

let [src, dst, action] = ['', branch, 'Switch']

switch (verb) {
case 'from':
src = a2
dst = a1
src = src_or_dst
dst = branch
action = 'Create'
break;
case 'to':
src = a1
dst = a2
src = branch
dst = src_or_dst
action = 'Merge'
break;
}
Expand Down
8 changes: 6 additions & 2 deletions lib/PushCommand.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
const GitBranch = require ('./GitBranch')

module.exports = class {
constructor(o) {
this.gitRepo = o.gitRepo
this.parsedArgs = o.parsedArgs
}

async todo () {
let currentBranch = await this.gitRepo.currentBranch ()
let {gitRepo, parsedArgs} = this
let {src} = await parsedArgs.value ()
let srcBranch = await GitBranch.withName (src, this.gitRepo)

return {
todo: await currentBranch.pushTodo ()
todo: await srcBranch.pushTodo ()
}
}
}
2 changes: 1 addition & 1 deletion lib/SwitchCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = class {
async todo () {
let {parsedArgs, gitRepo} = this
let {dst} = await parsedArgs.value ()
let dstBranch = GitBranch.withName (dst, gitRepo)
let dstBranch = await GitBranch.withName (dst, gitRepo)

if (!await gitRepo.existBranch (dstBranch)) {
return this.createCommand.todo ()
Expand Down
6 changes: 3 additions & 3 deletions tests/GitBranch.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const GitRepo = require ('../lib/GitRepo')
const ShellCommand= require ('../lib/ShellCommand')
const ORIGIN_MASTER = 'origin/master'

describe('git branch print', () => {
let task42 = GitBranch.withName ('TASK-42')
let task43 = GitBranch.withName ('gh/TASK-42')
describe('git branch print', async () => {
let task42 = await GitBranch.withName ('TASK-42')
let task43 = await GitBranch.withName ('gh/TASK-42')

it ('should print', async (t) => {
assert.strictEqual(await task42.print(), 'origin/TASK-42')
Expand Down
16 changes: 8 additions & 8 deletions tests/MergeCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ const GitBranch = require ('../lib/GitBranch')
const MergeCommand = require ('../lib/MergeCommand')
const ParsedArgs = require ('../lib/ParsedArgs')

describe('MergeCommand', () => {
let f = function () {
describe('MergeCommand', async () => {
let f = async function () {
switch (this.cmd) {
case "git config mr.master.mergeAfter":
return [GitBranch.withName ('test')]
return [await GitBranch.withName ('test')]
case "git push --set-upstream origin TASK-42:TASK-42":
case "git push --set-upstream origin TASK-0:TASK-0":
case "git push --set-upstream origin TASK-9:TASK-9":
Expand Down Expand Up @@ -48,22 +48,22 @@ describe('MergeCommand', () => {
return true
})
mock.method(GitRepo.prototype, 'countDiffCommits', async function (src, dst) {
if (await src.equals (GitBranch.withName ('TASK-0'))) {
if (await src.equals (await GitBranch.withName ('TASK-0'))) {
return 0
}
if (await src.equals (GitBranch.withName ('TASK-9'))) {
if (await src.equals (await GitBranch.withName ('TASK-9'))) {
return 101
}
return 1
})

mock.method(GitRepo.prototype, 'existInRemote', async function (dst) {
return !await dst.equals (GitBranch.withName ('dummy'))
return !await dst.equals (await GitBranch.withName ('dummy'))
})

mock.method(GitRepo.prototype, 'toMergeAfter', async function (dst) {
if (await dst.equals (GitBranch.withName ('master'))) {
return [GitBranch.withName ('test', this)]
if (await dst.equals (await GitBranch.withName ('master'))) {
return [await GitBranch.withName ('test', this)]
}
return []
})
Expand Down
5 changes: 4 additions & 1 deletion tests/ParsedArgs.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('ParsedArgs', () => {
assert.throws(() => new ParsedArgs (toArgv (`TASK-42 origin/release`)).value ())
})
it ('push branch', async (t) => {
assert.deepStrictEqual(await new ParsedArgs ([]).value (), {dst: '', action: 'Push', src: ''})
assert.deepStrictEqual(await new ParsedArgs ([]).value (), {dst: '', action: 'Push', src: '__CURRENT_BRANCH__'})
})
it ('switch to branch', async (t) => {
assert.deepStrictEqual(await new ParsedArgs (toArgv (`TASK-42`)).value (), {dst: 'TASK-42', action: 'Switch', src: ''})
Expand All @@ -23,4 +23,7 @@ describe('ParsedArgs', () => {
it ('deploy to release', async (t) => {
assert.deepStrictEqual(await new ParsedArgs (toArgv (`TASK-42 to release`)).value (), {src: 'TASK-42', action: 'Merge', dst: 'release'})
})
it ('deploy current to release', async (t) => {
assert.deepStrictEqual(await new ParsedArgs (toArgv (`to release`)).value (), {src: '__CURRENT_BRANCH__', action: 'Merge', dst: 'release'})
})
})
Loading