From bf9721d4ee878b212a87aa4c59126647cba998cc Mon Sep 17 00:00:00 2001 From: YeonGyu-Kim Date: Wed, 11 Mar 2026 17:07:33 +0900 Subject: [PATCH] fix(git-master): prefix git commands in injected templates --- .../git-master-template-injection.ts | 49 ++++++++++++------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/src/features/opencode-skill-loader/git-master-template-injection.ts b/src/features/opencode-skill-loader/git-master-template-injection.ts index daec2d31a..812fa228f 100644 --- a/src/features/opencode-skill-loader/git-master-template-injection.ts +++ b/src/features/opencode-skill-loader/git-master-template-injection.ts @@ -1,30 +1,31 @@ -import type { GitMasterConfig } from "../../config/schema" +import { assertValidGitEnvPrefix, type GitMasterConfig } from "../../config/schema" + +const BASH_CODE_BLOCK_PATTERN = /```bash\r?\n([\s\S]*?)```/g +const LEADING_GIT_COMMAND_PATTERN = /^([ \t]*(?:[A-Za-z_][A-Za-z0-9_]*=[^ \t]+\s+)*)git(?=[ \t]|$)/gm +const INLINE_GIT_COMMAND_PATTERN = /([;&|()][ \t]*)git(?=[ \t]|$)/g export function injectGitMasterConfig(template: string, config?: GitMasterConfig): string { const commitFooter = config?.commit_footer ?? true const includeCoAuthoredBy = config?.include_co_authored_by ?? true - const gitEnvPrefix = config?.git_env_prefix ?? "GIT_MASTER=1" + const gitEnvPrefix = assertValidGitEnvPrefix(config?.git_env_prefix ?? "GIT_MASTER=1") let result = gitEnvPrefix ? injectGitEnvPrefix(template, gitEnvPrefix) : template - if (!commitFooter && !includeCoAuthoredBy) { - return result + if (commitFooter || includeCoAuthoredBy) { + const injection = buildCommitFooterInjection(commitFooter, includeCoAuthoredBy, gitEnvPrefix) + const insertionPoint = result.indexOf("```\n") + + result = + insertionPoint !== -1 + ? result.slice(0, insertionPoint) + + "```\n\n" + + injection + + "\n" + + result.slice(insertionPoint + "```\n".length) + : result + "\n\n" + injection } - const injection = buildCommitFooterInjection(commitFooter, includeCoAuthoredBy, gitEnvPrefix) - - const insertionPoint = result.indexOf("```\n") - if (insertionPoint !== -1) { - return ( - result.slice(0, insertionPoint) + - "```\n\n" + - injection + - "\n" + - result.slice(insertionPoint + "```\n".length) - ) - } - - return result + "\n\n" + injection + return gitEnvPrefix ? prefixGitCommandsInBashCodeBlocks(result, gitEnvPrefix) : result } function injectGitEnvPrefix(template: string, prefix: string): string { @@ -63,6 +64,18 @@ function injectGitEnvPrefix(template: string, prefix: string): string { return envPrefixSection + "\n\n---\n\n" + template } +function prefixGitCommandsInBashCodeBlocks(template: string, prefix: string): string { + return template.replace(BASH_CODE_BLOCK_PATTERN, (block, codeBlock: string) => { + return block.replace(codeBlock, prefixGitCommandsInCodeBlock(codeBlock, prefix)) + }) +} + +function prefixGitCommandsInCodeBlock(codeBlock: string, prefix: string): string { + return codeBlock + .replace(LEADING_GIT_COMMAND_PATTERN, `$1${prefix} git`) + .replace(INLINE_GIT_COMMAND_PATTERN, `$1${prefix} git`) +} + function buildCommitFooterInjection( commitFooter: boolean | string, includeCoAuthoredBy: boolean,