@lakakala/kgit 0.3.0 → 0.3.2

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