Skip to content
Open
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
41 changes: 39 additions & 2 deletions packages/core/src/tools/shell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,10 @@ describe('ShellTool', () => {
await promise;

const tmpFile = path.join(os.tmpdir(), 'shell_pgrep_abcdef.tmp');
const wrappedCommand = `{ npm start & }; __code=$?; pgrep -g 0 >${tmpFile} 2>&1; exit $__code;`;
const logFile = path.resolve(
'/test/dir/.blackbox/tmp/shell_tool_abcdef.log',
);
const wrappedCommand = `{ npm start > "${logFile}" 2>&1 & }; __code=$?; pgrep -g 0 >${tmpFile} 2>&1; exit $__code;`;
expect(mockShellExecutionService).toHaveBeenCalledWith(
wrappedCommand,
expect.any(String),
Expand All @@ -233,7 +236,10 @@ describe('ShellTool', () => {
await promise;

const tmpFile = path.join(os.tmpdir(), 'shell_pgrep_abcdef.tmp');
const wrappedCommand = `{ npm start & }; __code=$?; pgrep -g 0 >${tmpFile} 2>&1; exit $__code;`;
const logFile = path.resolve(
'/test/dir/.blackbox/tmp/shell_tool_abcdef.log',
);
const wrappedCommand = `{ npm start > "${logFile}" 2>&1 & }; __code=$?; pgrep -g 0 >${tmpFile} 2>&1; exit $__code;`;
expect(mockShellExecutionService).toHaveBeenCalledWith(
wrappedCommand,
expect.any(String),
Expand Down Expand Up @@ -773,6 +779,37 @@ describe('ShellTool', () => {
undefined,
);
});

it('should add co-author to git commit with unquoted message', async () => {
const command = 'git commit -m Fix_bug_unquoted';
const invocation = shellTool.build({ command, is_background: false });
const promise = invocation.execute(mockAbortSignal);

resolveExecutionPromise({
rawOutput: Buffer.from(''),
output: '',
exitCode: 0,
signal: null,
error: null,
aborted: false,
pid: 12345,
executionMethod: 'child_process',
});

await promise;

expect(mockShellExecutionService).toHaveBeenCalledWith(
expect.stringContaining(
'Co-authored-by: BlackboxAI <code@blackbox.ai>',
),
expect.any(String),
expect.any(Function),
mockAbortSignal,
false,
undefined,
undefined,
);
});
});
});

Expand Down
26 changes: 19 additions & 7 deletions packages/core/src/tools/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,16 +374,28 @@ class ShellToolInvocation extends BaseToolInvocation<
Co-authored-by: ${gitCoAuthorSettings.name} <${gitCoAuthorSettings.email}>`;

// Handle different git commit patterns
// Match -m "message" or -m 'message'
const messagePattern = /(-m\s+)(['"])((?:\\.|[^\\])*?)(\2)/;
const messagePattern = /(-m\s+)(?:(['"])((?:\\.|[^\\])*?)(\2)|([^\s]+))/;
const match = command.match(messagePattern);

if (match) {
const [fullMatch, prefix, quote, existingMessage, closingQuote] = match;
const newMessage = existingMessage + coAuthor;
const replacement = prefix + quote + newMessage + closingQuote;

return command.replace(fullMatch, replacement);
const [
fullMatch,
prefix,
quote,
quotedMessage,
_closingQuote,
unquotedMessage,
] = match;

if (quote) {
const newMessage = quotedMessage + coAuthor;
const replacement = prefix + quote + newMessage + quote;
return command.replace(fullMatch, replacement);
} else {
const newMessage = unquotedMessage + coAuthor;
const replacement = prefix + '"' + newMessage + '"';
return command.replace(fullMatch, replacement);
}
}

// If no -m flag found, the command might open an editor
Expand Down