-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy pathgit-utils.ts
More file actions
30 lines (27 loc) · 880 Bytes
/
git-utils.ts
File metadata and controls
30 lines (27 loc) · 880 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { execFileSync } from 'child_process';
/**
* Check if the given directory is inside a git repository.
* Uses `git rev-parse --git-dir` which exits nonzero when not in a git repo.
*/
export function isGitRepository(cwd: string): boolean {
try {
execFileSync('git', ['rev-parse', '--git-dir'], { cwd, stdio: 'pipe' });
return true;
} catch {
return false;
}
}
/**
* Create a new git branch and check it out.
* Throws if the branch already exists or git is not available.
*/
export function createAndCheckoutBranch(cwd: string, branchName: string): void {
execFileSync('git', ['checkout', '-b', branchName], { cwd, stdio: 'pipe' });
}
/**
* Derive the git branch name for a given change name.
* Always returns `openspec/<changeName>`.
*/
export function getBranchNameForChange(changeName: string): string {
return `openspec/${changeName}`;
}