From baa7345bbaa8864f177df0e5a9c44aa760885d45 Mon Sep 17 00:00:00 2001 From: Maksim Gruzdev Date: Sun, 26 Jan 2025 13:48:11 +0300 Subject: [PATCH 1/2] #126 + syntax 'mr to test' --- lib/GitBranch.js | 7 +++++-- lib/GitRepo.js | 4 ++-- lib/MergeCommand.js | 4 ++-- lib/ParsedArgs.js | 29 ++++++++++++++--------------- lib/PushCommand.js | 8 ++++++-- lib/SwitchCommand.js | 2 +- tests/GitBranch.js | 6 +++--- tests/MergeCommand.js | 16 ++++++++-------- tests/ParsedArgs.js | 5 ++++- 9 files changed, 45 insertions(+), 36 deletions(-) diff --git a/lib/GitBranch.js b/lib/GitBranch.js index ee4a0ed..21430ac 100644 --- a/lib/GitBranch.js +++ b/lib/GitBranch.js @@ -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 }) } @@ -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 ()) } diff --git a/lib/GitRepo.js b/lib/GitRepo.js index d59ff66..1df69b0 100644 --- a/lib/GitRepo.js +++ b/lib/GitRepo.js @@ -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) { @@ -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 () { diff --git a/lib/MergeCommand.js b/lib/MergeCommand.js index 079e7d4..e093259 100644 --- a/lib/MergeCommand.js +++ b/lib/MergeCommand.js @@ -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 () diff --git a/lib/ParsedArgs.js b/lib/ParsedArgs.js index 5332c63..416aa1c 100644 --- a/lib/ParsedArgs.js +++ b/lib/ParsedArgs.js @@ -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; } diff --git a/lib/PushCommand.js b/lib/PushCommand.js index df6c347..7507c82 100644 --- a/lib/PushCommand.js +++ b/lib/PushCommand.js @@ -1,3 +1,5 @@ +const GitBranch = require ('./GitBranch') + module.exports = class { constructor(o) { this.gitRepo = o.gitRepo @@ -5,10 +7,12 @@ module.exports = class { } 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 () } } } diff --git a/lib/SwitchCommand.js b/lib/SwitchCommand.js index 252ed86..a5246d8 100644 --- a/lib/SwitchCommand.js +++ b/lib/SwitchCommand.js @@ -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 () diff --git a/tests/GitBranch.js b/tests/GitBranch.js index 43a6f7b..2259922 100644 --- a/tests/GitBranch.js +++ b/tests/GitBranch.js @@ -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') diff --git a/tests/MergeCommand.js b/tests/MergeCommand.js index 223ab1b..79a5af7 100644 --- a/tests/MergeCommand.js +++ b/tests/MergeCommand.js @@ -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": @@ -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 [] }) diff --git a/tests/ParsedArgs.js b/tests/ParsedArgs.js index c93987a..2cee25b 100644 --- a/tests/ParsedArgs.js +++ b/tests/ParsedArgs.js @@ -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: ''}) @@ -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'}) + }) }) From 14729fffda22a11d91ecdd7e2b9faf7555723cd1 Mon Sep 17 00:00:00 2001 From: Maksim Gruzdev Date: Sun, 26 Jan 2025 13:55:24 +0300 Subject: [PATCH 2/2] #126 FIX checkout back to src branch --- lib/MergeCommand.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/MergeCommand.js b/lib/MergeCommand.js index e093259..f8822e1 100644 --- a/lib/MergeCommand.js +++ b/lib/MergeCommand.js @@ -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 }