Skip to content
Merged
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
8 changes: 0 additions & 8 deletions packages/cli/src/commands/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,6 @@ const validations = (

// Check if git repository has at least one commit
if (!git.hasCommits()) {
if (!isJsonMode) {
console.log(colors.red('✗ No commits found in git repository'));
console.log(
colors.gray(
' Git worktree requires at least one commit in the repository'
)
);
}
return {
error: 'No commits found in git repository',
tips: ['Git worktree requires at least one commit in the repository'],
Expand Down
49 changes: 26 additions & 23 deletions packages/common/src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,34 +50,36 @@ export type GitRemoteUrlOptions = {
*/
export class Git {
constructor() {
// Check docker is available
if (launchSync('git', ['--version']).exitCode !== 0) {
// Check git is available
if (launchSync('git', ['--version'], { reject: false }).exitCode !== 0) {
throw new GitError('Git is not installed.');
}
}

isGitRepo(): boolean {
const result = launchSync('git', ['rev-parse', '--is-inside-work-tree']);
const result = launchSync('git', ['rev-parse', '--is-inside-work-tree'], {
reject: false,
});
return result.exitCode === 0;
}

/**
* Get the root directory of the Git repository
*/
getRepositoryRoot(): string | null {
try {
const result = launchSync('git', ['rev-parse', '--show-toplevel']);
if (result.exitCode === 0) {
return result.stdout?.toString().trim() || null;
}
return null;
} catch {
return null;
const result = launchSync('git', ['rev-parse', '--show-toplevel'], {
reject: false,
});
if (result.exitCode === 0) {
return result.stdout?.toString().trim() || null;
}
return null;
}

hasCommits(): boolean {
const result = launchSync('git', ['rev-list', '--count', 'HEAD']);
const result = launchSync('git', ['rev-list', '--count', 'HEAD'], {
reject: false,
});
return result.exitCode === 0;
}

Expand Down Expand Up @@ -110,6 +112,7 @@ export class Git {
['ls-files', '--others', '--exclude-standard'],
{
cwd: options.worktreePath,
reject: false,
}
);

Expand Down Expand Up @@ -210,9 +213,7 @@ export class Git {
cwd: options.worktreePath,
});

return result?.exitCode == 0
? result?.stdout?.toString().trim() || ''
: '';
return result?.stdout?.toString().trim() || '';
} catch (_err) {
return '';
}
Expand Down Expand Up @@ -402,12 +403,11 @@ export class Git {
branchExists(branch: string): boolean {
try {
return (
launchSync('git', [
'show-ref',
'--verify',
'--quiet',
`refs/heads/${branch}`,
]).exitCode === 0
launchSync(
'git',
['show-ref', '--verify', '--quiet', `refs/heads/${branch}`],
{ reject: false }
).exitCode === 0
);
} catch (error) {
return false;
Expand All @@ -424,14 +424,16 @@ export class Git {
): boolean {
if (this.branchExists(branchName)) {
return (
launchSync('git', ['worktree', 'add', path, branchName]).exitCode == 0
launchSync('git', ['worktree', 'add', path, branchName], {
reject: false,
}).exitCode == 0
);
}
// Create new branch from base branch if specified, otherwise from current branch
const args = baseBranch
? ['worktree', 'add', path, '-b', branchName, baseBranch]
: ['worktree', 'add', path, '-b', branchName];
return launchSync('git', args).exitCode == 0;
return launchSync('git', args, { reject: false }).exitCode == 0;
}

/**
Expand Down Expand Up @@ -524,6 +526,7 @@ export class Git {

const result = launchSync('git', args, {
cwd: options.worktreePath,
reject: false,
});

if (result.exitCode !== 0) {
Expand Down
Loading