takt/src/infra/task/git.ts
nrslib b9a2a0329b auto PR のベースブランチをブランチ作成前の現在ブランチに設定
createPullRequest の全呼び出し箇所で base が未指定だったため、
PR が常にリポジトリデフォルトブランチ(main)向けに作成されていた。
ブランチ作成/clone作成の直前に getCurrentBranch() で元ブランチを
取得し、PR作成時に base として渡すように修正。
2026-02-08 07:51:03 +09:00

43 lines
1012 B
TypeScript

/**
* Shared git operations for task execution
*/
import { execFileSync } from 'node:child_process';
/**
* Get the current branch name.
*/
export function getCurrentBranch(cwd: string): string {
return execFileSync('git', ['rev-parse', '--abbrev-ref', 'HEAD'], {
cwd,
encoding: 'utf-8',
stdio: 'pipe',
}).trim();
}
/**
* Stage all changes and create a commit.
* Returns the short commit hash if changes were committed, undefined if no changes.
*/
export function stageAndCommit(cwd: string, message: string): string | undefined {
execFileSync('git', ['add', '-A'], { cwd, stdio: 'pipe' });
const statusOutput = execFileSync('git', ['status', '--porcelain'], {
cwd,
stdio: 'pipe',
encoding: 'utf-8',
});
if (!statusOutput.trim()) {
return undefined;
}
execFileSync('git', ['commit', '-m', message], { cwd, stdio: 'pipe' });
return execFileSync('git', ['rev-parse', '--short', 'HEAD'], {
cwd,
stdio: 'pipe',
encoding: 'utf-8',
}).trim();
}