@lakakala/kgit 0.1.3 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/git.js +29 -3
- package/package.json +1 -1
package/dist/git.js
CHANGED
|
@@ -1,11 +1,37 @@
|
|
|
1
1
|
import { execa } from 'execa';
|
|
2
2
|
import fs from 'node:fs';
|
|
3
3
|
import path from 'node:path';
|
|
4
|
+
async function localBranchExists(repoPath, branch) {
|
|
5
|
+
const { stdout } = await execa('git', ['-C', repoPath, 'branch', '--list', branch], { stdio: 'pipe' });
|
|
6
|
+
return stdout.trim().length > 0;
|
|
7
|
+
}
|
|
8
|
+
async function remoteBranchExists(repoPath, branch) {
|
|
9
|
+
try {
|
|
10
|
+
const { stdout } = await execa('git', ['-C', repoPath, 'branch', '-r', '--list', `*/${branch}`], { stdio: 'pipe' });
|
|
11
|
+
const match = stdout.trim().split('\n').find(l => l.trim().length > 0);
|
|
12
|
+
return match ? match.trim() : null;
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
4
18
|
export async function addWorktree(repoPath, worktreePath, newBranch, baseBranch) {
|
|
5
19
|
await fs.promises.mkdir(path.dirname(worktreePath), { recursive: true });
|
|
6
|
-
await
|
|
7
|
-
|
|
8
|
-
|
|
20
|
+
if (await localBranchExists(repoPath, newBranch)) {
|
|
21
|
+
// Branch exists locally — use it directly
|
|
22
|
+
console.log(` Branch "${newBranch}" exists locally, using it directly.`);
|
|
23
|
+
await execa('git', ['-C', repoPath, 'worktree', 'add', worktreePath, newBranch], { stdio: 'inherit' });
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const remoteBranch = await remoteBranchExists(repoPath, newBranch);
|
|
27
|
+
if (remoteBranch) {
|
|
28
|
+
// Branch exists on remote — create a tracking local branch
|
|
29
|
+
console.log(` Branch "${newBranch}" found on remote (${remoteBranch}), creating tracking branch.`);
|
|
30
|
+
await execa('git', ['-C', repoPath, 'worktree', 'add', '--track', '-b', newBranch, worktreePath, remoteBranch], { stdio: 'inherit' });
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
// Branch does not exist — create from base branch
|
|
34
|
+
await execa('git', ['-C', repoPath, 'worktree', 'add', '-b', newBranch, worktreePath, baseBranch], { stdio: 'inherit' });
|
|
9
35
|
}
|
|
10
36
|
export async function removeWorktree(repoPath, worktreePath) {
|
|
11
37
|
await execa('git', ['-C', repoPath, 'worktree', 'remove', worktreePath, '--force'], {
|