Skip to content

Commit f5af7b7

Browse files
fix: add cross-platform commit helper to resolve heredoc issues
Resolves the issue where git commit commands with heredoc syntax fail on Windows systems. The heredoc approach (<<'EOF') only works in bash/Unix shells and causes "pathspec '<<'EOF'' did not match any file(s)" errors on Windows. Changes: - Add scripts/commit-helper.ts: Cross-platform script using temporary files - Add npm script "commit" for easy access: npm run commit "message" - Update system prompt to instruct AI agents to use commit helper - Replace platform-specific heredoc instructions with single helper approach The commit helper validates git repository state, handles multi-line messages correctly, and works consistently across Windows, macOS, and Linux. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
1 parent 2c0e9aa commit f5af7b7

File tree

3 files changed

+106
-11
lines changed

3 files changed

+106
-11
lines changed

backend/src/system-prompt/prompts.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -278,28 +278,24 @@ When the user requests a new git commit, please follow these steps closely:
278278
Generated with Codebuff 🤖
279279
Co-Authored-By: Codebuff <noreply@codebuff.com>
280280
\`\`\`
281-
To maintain proper formatting, use cross-platform compatible commit messages:
281+
To maintain proper formatting and ensure cross-platform compatibility, use the commit helper script:
282282
283-
**For Unix/bash shells:**
284283
\`\`\`
285-
git commit -m "$(cat <<'EOF'
286-
Your commit message here.
284+
npm run commit "Your commit message here.
287285
288286
🤖 Generated with Codebuff
289-
Co-Authored-By: Codebuff <noreply@codebuff.com>
290-
EOF
291-
)"
287+
Co-Authored-By: Codebuff <noreply@codebuff.com>"
292288
\`\`\`
293289
294-
**For Windows Command Prompt:**
290+
Or directly with bun:
295291
\`\`\`
296-
git commit -m "Your commit message here.
292+
bun scripts/commit-helper.ts "Your commit message here.
297293
298294
🤖 Generated with Codebuff
299295
Co-Authored-By: Codebuff <noreply@codebuff.com>"
300296
\`\`\`
301297
302-
Always detect the platform and use the appropriate syntax. HEREDOC syntax (\`<<'EOF'\`) only works in bash/Unix shells and will fail on Windows Command Prompt.
298+
**IMPORTANT**: Always use the commit helper script instead of direct git commit commands with heredoc syntax, as heredocs (\`<<'EOF'\`) fail on Windows systems. The commit helper handles multi-line messages correctly on all platforms.
303299
304300
**Important details**
305301

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"test": "bun --filter='{@codebuff/backend,@codebuff/common,@codebuff/npm-app,@codebuff/agents}' run test",
3636
"init-worktree": "bun scripts/init-worktree.ts",
3737
"cleanup-worktree": "bash scripts/cleanup-worktree.sh",
38-
"generate-tool-definitions": "bun scripts/generate-tool-definitions.ts"
38+
"generate-tool-definitions": "bun scripts/generate-tool-definitions.ts",
39+
"commit": "bun scripts/commit-helper.ts"
3940
},
4041
"dependencies": {
4142
"@t3-oss/env-nextjs": "^0.7.3",

scripts/commit-helper.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/usr/bin/env bun
2+
3+
/**
4+
* Cross-platform Git commit helper that properly handles multi-line commit messages
5+
*
6+
* This script solves the issue where heredoc syntax (<<'EOF') fails on Windows systems.
7+
* It creates a temporary file to hold the commit message, which works consistently
8+
* across all platforms.
9+
*
10+
* Usage: bun scripts/commit-helper.ts "Your multi-line commit message here"
11+
*/
12+
13+
import { execSync } from 'child_process'
14+
import { writeFileSync, unlinkSync, existsSync } from 'fs'
15+
import { tmpdir } from 'os'
16+
import { join } from 'path'
17+
18+
function createCommit(message: string): void {
19+
// Validate that we're in a git repository
20+
try {
21+
execSync('git rev-parse --git-dir', { stdio: 'ignore' })
22+
} catch (error) {
23+
console.error('❌ Error: Not in a git repository')
24+
process.exit(1)
25+
}
26+
27+
// Check if there are changes to commit
28+
try {
29+
const status = execSync('git status --porcelain', { encoding: 'utf8' })
30+
if (status.trim() === '') {
31+
console.error('❌ Error: No changes to commit')
32+
process.exit(1)
33+
}
34+
} catch (error) {
35+
console.error('❌ Error: Failed to check git status')
36+
process.exit(1)
37+
}
38+
39+
// Create a temporary file to hold the commit message
40+
const tempFile = join(tmpdir(), `git-commit-msg-${Date.now()}-${Math.random().toString(36).substring(7)}.txt`)
41+
42+
try {
43+
// Write the message to the temp file
44+
writeFileSync(tempFile, message.trim(), 'utf8')
45+
46+
// Use git commit with -F flag to read from file
47+
execSync(`git commit -F "${tempFile}"`, {
48+
stdio: 'inherit',
49+
encoding: 'utf8'
50+
})
51+
52+
console.log('✅ Commit created successfully!')
53+
} catch (error) {
54+
if (error instanceof Error) {
55+
console.error('❌ Commit failed:', error.message)
56+
} else {
57+
console.error('❌ Commit failed with unknown error')
58+
}
59+
process.exit(1)
60+
} finally {
61+
// Clean up temp file
62+
try {
63+
if (existsSync(tempFile)) {
64+
unlinkSync(tempFile)
65+
}
66+
} catch (cleanupError) {
67+
// Ignore cleanup errors, but warn
68+
console.warn('⚠️ Warning: Failed to clean up temporary file:', tempFile)
69+
}
70+
}
71+
}
72+
73+
// Main execution
74+
function main(): void {
75+
// Get commit message from command line argument
76+
const message = process.argv[2]
77+
78+
if (!message) {
79+
console.error('Usage: bun scripts/commit-helper.ts "Your commit message here"')
80+
console.error(' npm run commit "Your commit message here"')
81+
console.error('')
82+
console.error('Example:')
83+
console.error(' bun scripts/commit-helper.ts "fix: resolve authentication issue')
84+
console.error('')
85+
console.error(' Fixes login flow by updating token validation logic')
86+
console.error('')
87+
console.error(' 🤖 Generated with Codebuff')
88+
console.error(' Co-Authored-By: Codebuff <noreply@codebuff.com>"')
89+
process.exit(1)
90+
}
91+
92+
createCommit(message)
93+
}
94+
95+
// Execute if running as main module
96+
if (import.meta.main) {
97+
main()
98+
}

0 commit comments

Comments
 (0)