@lakakala/kgit 0.3.1 → 0.3.3
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/config.js +1 -0
- package/dist/git.js +14 -13
- package/package.json +1 -1
package/dist/config.js
CHANGED
package/dist/git.js
CHANGED
|
@@ -1,13 +1,22 @@
|
|
|
1
1
|
import { execa } from 'execa';
|
|
2
2
|
import fs from 'node:fs';
|
|
3
3
|
import path from 'node:path';
|
|
4
|
+
async function run(args) {
|
|
5
|
+
console.log(` $ ${args.join(' ')}`);
|
|
6
|
+
const result = await execa(args[0], args.slice(1), { all: true }).catch(err => {
|
|
7
|
+
if (err.all)
|
|
8
|
+
process.stderr.write(err.all + '\n');
|
|
9
|
+
throw err;
|
|
10
|
+
});
|
|
11
|
+
if (result.all)
|
|
12
|
+
process.stdout.write(result.all + '\n');
|
|
13
|
+
}
|
|
4
14
|
async function localBranchExists(repoPath, branch) {
|
|
5
15
|
const { stdout } = await execa('git', ['-C', repoPath, 'branch', '--list', branch], { stdio: 'pipe' });
|
|
6
16
|
return stdout.trim().length > 0;
|
|
7
17
|
}
|
|
8
18
|
async function remoteBranchExists(repoPath, branch) {
|
|
9
19
|
try {
|
|
10
|
-
// Query remote directly — more reliable than local cached refs
|
|
11
20
|
const { stdout } = await execa('git', ['-C', repoPath, 'ls-remote', '--heads', 'origin', branch], { stdio: 'pipe' });
|
|
12
21
|
if (stdout.trim().length === 0)
|
|
13
22
|
return null;
|
|
@@ -20,38 +29,30 @@ async function remoteBranchExists(repoPath, branch) {
|
|
|
20
29
|
export async function addWorktree(repoPath, worktreePath, newBranch, baseBranch) {
|
|
21
30
|
await fs.promises.mkdir(path.dirname(worktreePath), { recursive: true });
|
|
22
31
|
if (await localBranchExists(repoPath, newBranch)) {
|
|
23
|
-
// Branch exists locally — use it directly
|
|
24
32
|
console.log(` Branch "${newBranch}" exists locally, using it directly.`);
|
|
25
|
-
await
|
|
33
|
+
await run(['git', '-C', repoPath, 'worktree', 'add', worktreePath, newBranch]);
|
|
26
34
|
return;
|
|
27
35
|
}
|
|
28
36
|
const remoteBranch = await remoteBranchExists(repoPath, newBranch);
|
|
29
37
|
if (remoteBranch) {
|
|
30
|
-
// Branch exists on remote — create a tracking local branch
|
|
31
38
|
console.log(` Branch "${newBranch}" found on remote (${remoteBranch}), creating tracking branch.`);
|
|
32
|
-
await
|
|
39
|
+
await run(['git', '-C', repoPath, 'worktree', 'add', '--track', '-b', newBranch, worktreePath, remoteBranch]);
|
|
33
40
|
return;
|
|
34
41
|
}
|
|
35
|
-
|
|
36
|
-
await execa('git', ['-C', repoPath, 'worktree', 'add', '-b', newBranch, worktreePath, baseBranch], { stdio: 'inherit' });
|
|
42
|
+
await run(['git', '-C', repoPath, 'worktree', 'add', '-b', newBranch, worktreePath, baseBranch]);
|
|
37
43
|
}
|
|
38
44
|
export async function isBranchSyncedToRemote(worktreePath) {
|
|
39
45
|
try {
|
|
40
|
-
// Check if there is an upstream tracking branch
|
|
41
46
|
await execa('git', ['-C', worktreePath, 'rev-parse', '--abbrev-ref', '@{u}'], { stdio: 'pipe' });
|
|
42
47
|
}
|
|
43
48
|
catch {
|
|
44
|
-
// No upstream configured — treat as unsynced
|
|
45
49
|
return false;
|
|
46
50
|
}
|
|
47
|
-
// Check for commits not yet pushed to upstream
|
|
48
51
|
const { stdout } = await execa('git', ['-C', worktreePath, 'rev-list', '--count', '@{u}..HEAD'], { stdio: 'pipe' });
|
|
49
52
|
return parseInt(stdout.trim(), 10) === 0;
|
|
50
53
|
}
|
|
51
54
|
export async function removeWorktree(repoPath, worktreePath) {
|
|
52
|
-
await
|
|
53
|
-
stdio: 'inherit',
|
|
54
|
-
});
|
|
55
|
+
await run(['git', '-C', repoPath, 'worktree', 'remove', worktreePath, '--force']);
|
|
55
56
|
}
|
|
56
57
|
export async function isGitRepo(dirPath) {
|
|
57
58
|
try {
|