From 1d1024005afc39da646320259f25dea5d3bbb579 Mon Sep 17 00:00:00 2001 From: Romain Cascino Date: Tue, 24 Nov 2026 08:59:35 +0100 Subject: [PATCH] Revert subject-prefix issue references in git reverts --- README.md | 2 +- src/extractors.test.ts | 10 +++++++++- src/extractors.ts | 14 +++++++++----- src/scan.test.ts | 18 ++++++++++++++++++ 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index c6fd708..b85c0f2 100644 --- a/README.md +++ b/README.md @@ -268,7 +268,7 @@ linear-release sync --issue-pattern='\b([A-Z]+-\d+)\b' # Also matches both IDs: fix(scope): color button red DEV-123 DEV-124 ``` -Identifiers that do not exist in the workspace are ignored server-side, so the broad recipe is safe but can be noisy (for example, it also matches `UTF-8`). Reverting a commit whose issues were linked only through `--issue-pattern` does not un-link them, just as with the built-in subject patterns. +Identifiers that do not exist in the workspace are ignored server-side, so the broad recipe is safe but can be noisy (for example, it also matches `UTF-8`). Reverting a commit whose issues were linked only through `--issue-pattern` does not un-link them. ### Disable branch ref detection diff --git a/src/extractors.test.ts b/src/extractors.test.ts index bf523a1..e78e5f0 100644 --- a/src/extractors.test.ts +++ b/src/extractors.test.ts @@ -689,11 +689,19 @@ describe("extractRevertedIssueIdentifiersForCommit", () => { expect(ids(result)).toEqual(["DRIVE-320"]); }); - it("ignores identifier in unwrapped message without magic word", () => { + it("extracts identifier from unwrapped subject prefix", () => { const result = extractRevertedIssueIdentifiersForCommit({ sha: "abc", message: 'Revert "DRIVE-320: Fix memory leak in background location service"', }); + expect(ids(result)).toEqual(["DRIVE-320"]); + }); + + it("ignores identifier in unwrapped message without magic word or subject prefix", () => { + const result = extractRevertedIssueIdentifiersForCommit({ + sha: "abc", + message: 'Revert "Fix memory leak, see DRIVE-320"', + }); expect(ids(result)).toEqual([]); }); diff --git a/src/extractors.ts b/src/extractors.ts index d4711ef..d73d5de 100644 --- a/src/extractors.ts +++ b/src/extractors.ts @@ -253,7 +253,7 @@ export function extractLinearIssueIdentifiersForCommit( // Odd depth = revert; even depth = non-revert or revert-of-revert (re-add). const { depth: branchDepth, inner: strippedBranch } = parseRevertBranch(commit.branchName ?? ""); - const { depth: messageDepth, afterTitle } = parseRevertMessage(commit.message ?? ""); + const { depth: messageDepth, inner: innerSubject, afterTitle } = parseRevertMessage(commit.message ?? ""); const found = new Map(); @@ -291,8 +291,11 @@ export function extractLinearIssueIdentifiersForCommit( // already-shipped references to this commit. const scanTarget = messageDepth % 2 === 1 ? afterTitle : (commit.message ?? ""); const message = stripSquashBlock(scanTarget); + // A revert of a revert re-adds the original subject's prefix (`[ENG-5] ...`), + // which only matches once the `Revert "` layers are unwrapped. + const subjectTarget = messageDepth > 0 && messageDepth % 2 === 0 ? innerSubject : message; if (message.length > 0) { - for (const match of [...matchCommonSubjectPatterns(message), ...matchMagicWordIdentifiers(message)]) { + for (const match of [...matchCommonSubjectPatterns(subjectTarget), ...matchMagicWordIdentifiers(message)]) { if (!found.has(match.identifier)) { found.set(match.identifier, { identifier: match.identifier, @@ -548,11 +551,12 @@ export function extractRevertedIssueIdentifiersForCommit(commit: CommitContext): } } - // Use magic-word gating on the inner message, same as the add path, to avoid - // false positives from generic word-number tokens (e.g. "Bump v1-2 to v1-3"). + // Match the inner subject the same way the add path matched the original + // commit: subject prefixes and magic words, not bare word-number tokens + // (e.g. "Bump v1-2 to v1-3"). if (messageDepth % 2 === 1) { const innerStripped = stripSquashBlock(innerMessage); - for (const match of matchMagicWordIdentifiers(innerStripped)) { + for (const match of [...matchCommonSubjectPatterns(innerStripped), ...matchMagicWordIdentifiers(innerStripped)]) { if (!found.has(match.identifier)) { found.set(match.identifier, { identifier: match.identifier, diff --git a/src/scan.test.ts b/src/scan.test.ts index 96c264d..b66dba7 100644 --- a/src/scan.test.ts +++ b/src/scan.test.ts @@ -131,6 +131,24 @@ describe("scanCommits", () => { }); }); + describe.each(["[ENG-5] Add thing", "(ENG-5) Add thing", "ENG-5: Add thing"])("revert of %s", (subject) => { + const add = { sha: "a1", message: subject }; + const revert = { sha: "r1", message: `Revert "${subject}"` }; + const reAdd = { sha: "rr1", message: `Revert "Revert "${subject}""` }; + + it("reverts the issue", () => { + const result = scanCommits([add, revert], {}); + expect(result.issueReferences).toEqual([]); + expect(result.revertedIssueReferences).toEqual([{ identifier: "ENG-5", commitSha: "r1" }]); + }); + + it("re-adds the issue on a revert of the revert", () => { + const result = scanCommits([add, revert, reAdd], {}); + expect(result.issueReferences).toEqual([{ identifier: "ENG-5", commitSha: "rr1" }]); + expect(result.revertedIssueReferences).toEqual([]); + }); + }); + describe("last-write-wins edge cases", () => { it("separates different issues into added and reverted lists", () => { const commits: CommitContext[] = [