diff --git a/packages/source-control-core/src/server/mergeMessage.test.ts b/packages/source-control-core/src/server/mergeMessage.test.ts index 0151f09a5c097..a857f2d6dc912 100644 --- a/packages/source-control-core/src/server/mergeMessage.test.ts +++ b/packages/source-control-core/src/server/mergeMessage.test.ts @@ -14,6 +14,7 @@ describe("removeAgentCredits", () => { "* Co-authored-by: Codex ", "co-authored-by: Cursor ", "Co-authored-by: Copilot ", + "Co-Authored-By:\tGitHub Copilot ", "", "🤖 Generated with [Claude Code](https://claude.ai/code)", ].join("\n"), diff --git a/packages/source-control-core/src/server/mergeMessage.ts b/packages/source-control-core/src/server/mergeMessage.ts index 159c074260cdd..ebbfffb1c79ed 100644 --- a/packages/source-control-core/src/server/mergeMessage.ts +++ b/packages/source-control-core/src/server/mergeMessage.ts @@ -11,6 +11,22 @@ const AGENT_EMAILS = new Set([ const GENERATED_BY_AGENT = /^(?:🤖\s*)?generated (?:with|by)\s+(?:claude(?: code)?|codex|cursor|github copilot|copilot|opencode|grok|antigravity)[.!]?$/i; +const CO_AUTHOR_PREFIX = /^co-authored-by:/i; + +function isAgentAuthor(text: string): boolean { + const prefix = CO_AUTHOR_PREFIX.exec(text); + if (prefix === null || !text.endsWith(">")) return false; + const emailStart = text.lastIndexOf("<"); + if (emailStart < prefix[0].length) return false; + const email = text.slice(emailStart + 1, -1).toLowerCase(); + if (!AGENT_EMAILS.has(email)) return false; + // GitHub's generic noreply address is also used by people; require Copilot's name. + return ( + email !== "noreply@github.com" || + /^(?:github )?copilot$/i.test(text.slice(prefix[0].length, emailStart).trim()) + ); +} + export function removeAgentCredits(message: string): string { let fence: string | undefined; let removed = false; @@ -34,20 +50,19 @@ export function removeAgentCredits(message: string): string { } if (fence !== undefined) return true; const text = line.trim().replace(/^(?:[-*]\s+)/, ""); - const author = /^co-authored-by:\s*(.*?)\s*<([^<>]+)>\s*$/i.exec(text); - // GitHub's generic noreply address is also used by people; require Copilot's name. - const email = author?.[2]?.toLowerCase(); - const agentAuthor = - email !== undefined && - AGENT_EMAILS.has(email) && - (email !== "noreply@github.com" || /^github copilot$|^copilot$/i.test(author![1]!)); - const footer = text.replace( - /\[([^\]]+)\]\(https:\/\/(?:claude\.ai|claude\.com|www\.anthropic\.com|chatgpt\.com|openai\.com|cursor\.com|github\.com|opencode\.ai|x\.ai|antigravity\.google)(?:\/[^\s)]*)?\)/gi, - "$1", - ); - if (!agentAuthor && !GENERATED_BY_AGENT.test(footer)) return true; + if (!isAgentAuthor(text)) { + const footer = text.replace( + /\[([^[\]\r\n]+)\]\(https:\/\/(?:claude\.ai|claude\.com|www\.anthropic\.com|chatgpt\.com|openai\.com|cursor\.com|github\.com|opencode\.ai|x\.ai|antigravity\.google)(?:\/[^\s)[\]]*)?\)/gi, + "$1", + ); + if (!GENERATED_BY_AGENT.test(footer)) return true; + } removed = true; return false; }); - return removed ? lines.join(newline).replace(/(?:\r?\n[ \t]*)+$/, "") : message; + if (!removed) return message; + while (lines.length > 1 && /^[ \t]*$/.test(lines.at(-1)!)) { + lines.pop(); + } + return lines.join(newline); }