From b9d741bb795cf6dce5e501344700f582b2accb24 Mon Sep 17 00:00:00 2001 From: Christopher Carman Date: Mon, 31 Aug 2020 16:58:39 -0700 Subject: [PATCH] feat: Support patterns for speculative branch execution --- .../src/speculativeBranchSelection.test.ts | 23 +++++++++++++++++++ .../client/src/speculativeBranchSelection.ts | 10 ++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 packages/client/src/speculativeBranchSelection.test.ts diff --git a/packages/client/src/speculativeBranchSelection.test.ts b/packages/client/src/speculativeBranchSelection.test.ts new file mode 100644 index 0000000..bf189b2 --- /dev/null +++ b/packages/client/src/speculativeBranchSelection.test.ts @@ -0,0 +1,23 @@ +import {findSpeculativeBaseBranch} from "./speculativeBranchSelection" + +describe("speculativeBranchSelection", () => { + describe("findSpeculativeBaseBranch", () => { + it("should return first matching branch", () => { + const branches = ["master", "develop", "foobar"]; + + expect(findSpeculativeBaseBranch("develop", branches)).toBe("develop") + }); + + it("should match branch pattern", () => { + const branches = ["master", "/v\d+-maintenance/"]; + + expect(findSpeculativeBaseBranch("v1-maintenance", branches)).toBe("v1-maintenance") + }); + + it("should return first configured branch by default", () => { + const branches = ["master", "develop"]; + + expect(findSpeculativeBaseBranch("someBranch", branches)).toBe("master") + }); + }) +}) \ No newline at end of file diff --git a/packages/client/src/speculativeBranchSelection.ts b/packages/client/src/speculativeBranchSelection.ts index 431df58..6cd061a 100644 --- a/packages/client/src/speculativeBranchSelection.ts +++ b/packages/client/src/speculativeBranchSelection.ts @@ -66,11 +66,17 @@ async function getBaseCommit(repoPath: string, speculativeBranchesInOrder: strin /** * Finds speculative branch based on the current branch name. If current branch is not on the list at all, select first. If it is, take the next one. If it's the last one just return undefined. */ -function findSpeculativeBaseBranch( +export function findSpeculativeBaseBranch( currentBranchName: string, speculativeBranchesInOrder: string[], ): string | undefined { - const currentBranchInOrder = speculativeBranchesInOrder.indexOf(currentBranchName); + const currentBranchInOrder = speculativeBranchesInOrder.findIndex((branch) => { + if (branch.startsWith("/")) { + return new RegExp(branch).test(currentBranchName); + } + + return branch === currentBranchName; + }); return speculativeBranchesInOrder[currentBranchInOrder + 1]; }