Skip to content
Closed
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
16 changes: 6 additions & 10 deletions backend/src/system-prompt/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,28 +278,24 @@ When the user requests a new git commit, please follow these steps closely:
Generated with Codebuff 🤖
Co-Authored-By: Codebuff <noreply@codebuff.com>
\`\`\`
To maintain proper formatting, use cross-platform compatible commit messages:
To maintain proper formatting and ensure cross-platform compatibility, use the commit helper script:

**For Unix/bash shells:**
\`\`\`
git commit -m "$(cat <<'EOF'
Your commit message here.
npm run commit "Your commit message here.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
EOF
)"
Co-Authored-By: Codebuff <noreply@codebuff.com>"
\`\`\`

**For Windows Command Prompt:**
Or directly with bun:
\`\`\`
git commit -m "Your commit message here.
bun scripts/commit-helper.ts "Your commit message here.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>"
\`\`\`

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.
**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.

**Important details**

Expand Down
10 changes: 8 additions & 2 deletions npm-app/src/terminal/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
backgroundProcesses,
spawnAndTrack,
} from '../background-process-manager'
import { detectShell } from '../utils/detect-shell'

import type { BackgroundProcessInfo } from '../background-process-manager'
import type { CodebuffToolOutput } from '@codebuff/common/tools/list'
Expand All @@ -28,8 +29,13 @@ export function runBackgroundCommand(
): void {
const { toolCallId, command, mode, cwd, stdoutFile, stderrFile } = options
const isWindows = os.platform() === 'win32'
const shell = isWindows ? 'cmd.exe' : 'bash'
const shellArgs = isWindows ? ['/c'] : ['-c']
const detectedShell = detectShell()
const shell = isWindows
? (detectedShell === 'powershell' ? 'powershell.exe' : 'cmd.exe')
: 'bash'
const shellArgs = isWindows
? (detectedShell === 'powershell' ? ['-Command'] : ['/c'])
: ['-c']

if (mode === 'assistant') {
console.log(green(`Running background process...\n> ${command}`))
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"test": "bun --filter='{@codebuff/backend,@codebuff/common,@codebuff/npm-app,@codebuff/agents}' run test",
"init-worktree": "bun scripts/init-worktree.ts",
"cleanup-worktree": "bash scripts/cleanup-worktree.sh",
"generate-tool-definitions": "bun scripts/generate-tool-definitions.ts"
"generate-tool-definitions": "bun scripts/generate-tool-definitions.ts",
"commit": "bun scripts/commit-helper.ts"
},
"dependencies": {
"@t3-oss/env-nextjs": "^0.7.3",
Expand Down
98 changes: 98 additions & 0 deletions scripts/commit-helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/usr/bin/env bun

/**
* Cross-platform Git commit helper that properly handles multi-line commit messages
*
* This script solves the issue where heredoc syntax (<<'EOF') fails on Windows systems.
* It creates a temporary file to hold the commit message, which works consistently
* across all platforms.
*
* Usage: bun scripts/commit-helper.ts "Your multi-line commit message here"
*/

import { execSync } from 'child_process'
import { writeFileSync, unlinkSync, existsSync } from 'fs'
import { tmpdir } from 'os'
import { join } from 'path'

function createCommit(message: string): void {
// Validate that we're in a git repository
try {
execSync('git rev-parse --git-dir', { stdio: 'ignore' })
} catch (error) {
console.error('❌ Error: Not in a git repository')
process.exit(1)
}

// Check if there are changes to commit
try {
const status = execSync('git status --porcelain', { encoding: 'utf8' })
if (status.trim() === '') {
console.error('❌ Error: No changes to commit')
process.exit(1)
}
} catch (error) {
console.error('❌ Error: Failed to check git status')
process.exit(1)
}

// Create a temporary file to hold the commit message
const tempFile = join(tmpdir(), `git-commit-msg-${Date.now()}-${Math.random().toString(36).substring(7)}.txt`)

try {
// Write the message to the temp file
writeFileSync(tempFile, message.trim(), 'utf8')

// Use git commit with -F flag to read from file
execSync(`git commit -F "${tempFile}"`, {
stdio: 'inherit',
encoding: 'utf8'
})

console.log('✅ Commit created successfully!')
} catch (error) {
if (error instanceof Error) {
console.error('❌ Commit failed:', error.message)
} else {
console.error('❌ Commit failed with unknown error')
}
process.exit(1)
} finally {
// Clean up temp file
try {
if (existsSync(tempFile)) {
unlinkSync(tempFile)
}
} catch (cleanupError) {
// Ignore cleanup errors, but warn
console.warn('⚠️ Warning: Failed to clean up temporary file:', tempFile)
}
}
}

// Main execution
function main(): void {
// Get commit message from command line argument
const message = process.argv[2]

if (!message) {
console.error('Usage: bun scripts/commit-helper.ts "Your commit message here"')
console.error(' npm run commit "Your commit message here"')
console.error('')
console.error('Example:')
console.error(' bun scripts/commit-helper.ts "fix: resolve authentication issue')
console.error('')
console.error(' Fixes login flow by updating token validation logic')
console.error('')
console.error(' 🤖 Generated with Codebuff')
console.error(' Co-Authored-By: Codebuff <noreply@codebuff.com>"')
process.exit(1)
}

createCommit(message)
}

// Execute if running as main module
if (import.meta.main) {
main()
}
10 changes: 8 additions & 2 deletions sdk/src/tools/run-terminal-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as os from 'os'
import * as path from 'path'

import type { CodebuffToolOutput } from '../../../common/src/tools/list'
import { detectShell } from '../../../npm-app/src/utils/detect-shell'

export function runTerminalCommand({
command,
Expand All @@ -21,8 +22,13 @@ export function runTerminalCommand({

return new Promise((resolve, reject) => {
const isWindows = os.platform() === 'win32'
const shell = isWindows ? 'cmd.exe' : 'bash'
const shellArgs = isWindows ? ['/c'] : ['-c']
const detectedShell = detectShell()
const shell = isWindows
? (detectedShell === 'powershell' ? 'powershell.exe' : 'cmd.exe')
: 'bash'
const shellArgs = isWindows
? (detectedShell === 'powershell' ? ['-Command'] : ['/c'])
: ['-c']

// Resolve cwd to absolute path
const resolvedCwd = path.resolve(cwd)
Expand Down
Loading