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
17 changes: 17 additions & 0 deletions src/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,23 @@ describe("extractBranchNameFromMergeMessage", () => {
});
});

describe("GitLab format", () => {
it("should extract branch name from GitLab merge message with target", () => {
const message = "Merge branch 'ax/ENG-123-add-button' into 'develop'";
expect(extractBranchNameFromMergeMessage(message)).toBe("ax/ENG-123-add-button");
});

it("should extract branch name from GitLab merge message without target", () => {
const message = "Merge branch 'feature/ENG-456-fix-auth'";
expect(extractBranchNameFromMergeMessage(message)).toBe("feature/ENG-456-fix-auth");
});

it("should handle case insensitivity for GitLab format", () => {
const message = "MERGE BRANCH 'feature/LIN-100'";
expect(extractBranchNameFromMergeMessage(message)).toBe("feature/LIN-100");
});
});

describe("edge cases", () => {
it("should return null for non-merge messages", () => {
expect(extractBranchNameFromMergeMessage("Some regular commit")).toBeNull();
Expand Down
17 changes: 13 additions & 4 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,24 @@ export function isMergeCommit(sha: string, cwd: string = process.cwd()): boolean
}

/**
* Extracts the branch name from a GitHub merge commit message.
* Matches: "Merge pull request #X from owner/branch-name"
* Extracts the branch name from a merge commit message.
* Supports:
* - GitHub: "Merge pull request #X from owner/branch-name"
* - GitLab: "Merge branch 'branch-name' into 'target'"
* - GitLab (no target): "Merge branch 'branch-name'"
*/
export function extractBranchNameFromMergeMessage(message: string | null | undefined): string | null {
if (!message) {
return null;
}
const match = message.match(/Merge pull request #\d+ from [^/]+\/(\S+)/i);
return match?.[1] ?? null;
// GitHub: "Merge pull request #123 from owner/branch-name"
const githubMatch = message.match(/Merge pull request #\d+ from [^/]+\/(\S+)/i);
if (githubMatch?.[1]) {
return githubMatch[1];
}
// GitLab: "Merge branch 'branch-name' into 'target'" or "Merge branch 'branch-name'"
const gitlabMatch = message.match(/Merge branch '([^']+)'/i);
return gitlabMatch?.[1] ?? null;
}

/**
Expand Down
Loading