worktree-add 1.2.1 → 1.3.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.
Files changed (49) hide show
  1. package/README.md +57 -22
  2. package/dist/app/get-unsafe-app-name-reason.d.ts +11 -0
  3. package/dist/app/get-unsafe-app-name-reason.js +21 -0
  4. package/dist/app/resolve-apps.d.ts +6 -0
  5. package/dist/app/resolve-apps.js +31 -0
  6. package/dist/cli/cleanup-worktree.d.ts +2 -0
  7. package/dist/cli/cleanup-worktree.js +13 -0
  8. package/dist/cli/open-worktree-apps.d.ts +5 -0
  9. package/dist/cli/open-worktree-apps.js +33 -0
  10. package/dist/cli/register-sigint-handler.d.ts +8 -0
  11. package/dist/cli/register-sigint-handler.js +19 -0
  12. package/dist/cli/resolve-worktree-context.d.ts +6 -0
  13. package/dist/cli/resolve-worktree-context.js +22 -0
  14. package/dist/cli/run-worktree-add.d.ts +9 -0
  15. package/dist/cli/run-worktree-add.js +100 -0
  16. package/dist/cli.d.ts +0 -6
  17. package/dist/cli.js +38 -76
  18. package/dist/git/create-worktree.d.ts +9 -0
  19. package/dist/git/create-worktree.js +39 -0
  20. package/dist/git/extract-diagnostic-line.d.ts +1 -0
  21. package/dist/git/extract-diagnostic-line.js +13 -0
  22. package/dist/git/fetch-remote-branch.d.ts +9 -0
  23. package/dist/git/fetch-remote-branch.js +102 -0
  24. package/dist/git/git.d.ts +11 -5
  25. package/dist/git/git.js +25 -17
  26. package/dist/git/remove-worktree.d.ts +1 -0
  27. package/dist/git/remove-worktree.js +4 -0
  28. package/dist/output/create-status-logger.d.ts +13 -0
  29. package/dist/output/create-status-logger.js +32 -0
  30. package/dist/output/get-status-logger.d.ts +2 -0
  31. package/dist/output/get-status-logger.js +12 -0
  32. package/dist/project/install-commands.js +2 -2
  33. package/dist/project/next.d.ts +4 -1
  34. package/dist/project/next.js +2 -2
  35. package/dist/project/package-manager.d.ts +7 -1
  36. package/dist/project/package-manager.js +12 -3
  37. package/dist/project/setup.d.ts +5 -1
  38. package/dist/project/setup.js +15 -7
  39. package/dist/worktree/destination-directory.d.ts +10 -3
  40. package/dist/worktree/destination-directory.js +94 -16
  41. package/dist/worktree/untracked-file-copy.d.ts +5 -1
  42. package/dist/worktree/untracked-file-copy.js +51 -9
  43. package/package.json +4 -2
  44. package/dist/git/parse-branch-head-mismatch-choice.d.ts +0 -7
  45. package/dist/git/parse-branch-head-mismatch-choice.js +0 -26
  46. package/dist/git/resolve-branch-head-mismatch.d.ts +0 -6
  47. package/dist/git/resolve-branch-head-mismatch.js +0 -79
  48. package/dist/git/worktree-creation.d.ts +0 -13
  49. package/dist/git/worktree-creation.js +0 -35
@@ -7,29 +7,71 @@ import * as fs from "node:fs/promises";
7
7
  import path from "node:path";
8
8
  import { git } from "../git/git.js";
9
9
  import { EXTRA_IGNORED_PATTERNS, globToRegExp, toPosixPath, } from "./file-patterns.js";
10
+ import { getStatusLogger } from "../output/get-status-logger.js";
10
11
  /**
11
12
  * Copy untracked files from source to destination, excluding ignored patterns
12
13
  */
13
- export async function copyUntrackedFiles(repoRoot, destinationDirectory) {
14
+ export async function copyUntrackedFiles(repoRoot, destinationDirectory, options) {
15
+ const logger = getStatusLogger(options?.logger);
16
+ const dryRun = options?.dryRun ?? false;
14
17
  const extraIgnoredRegexes = EXTRA_IGNORED_PATTERNS.map((pattern) => globToRegExp(pattern));
15
- const untrackedEntries = git("ls-files", "--others", // untracked files
16
- "--ignored", // ignored files
17
- "--exclude-standard", // respect .gitignore rules
18
- "-z")
19
- .split("\0")
20
- .filter(Boolean);
18
+ const untrackedEntries = new Set();
19
+ // Combine untracked-not-ignored (`--others`) and untracked-ignored (`--others --ignored`) entries.
20
+ // These sets should be disjoint, but we dedupe defensively.
21
+ const untrackedPaths = [
22
+ ...git("ls-files", "--others", "--exclude-standard", "--full-name", "-z", // null-separated output for safe parsing
23
+ { cwd: repoRoot })
24
+ .split("\0")
25
+ .filter(Boolean),
26
+ ...git("ls-files", "--others", "--ignored", "--exclude-standard", "--full-name", "-z", // null-separated output for safe parsing
27
+ { cwd: repoRoot })
28
+ .split("\0")
29
+ .filter(Boolean),
30
+ ];
31
+ for (const entry of untrackedPaths) {
32
+ untrackedEntries.add(entry);
33
+ }
21
34
  for (const relativePath of untrackedEntries) {
35
+ const resolvedSourcePath = path.resolve(repoRoot, relativePath);
36
+ const relativeToRoot = path.relative(repoRoot, resolvedSourcePath);
37
+ const escapesRepoRoot = path.isAbsolute(relativeToRoot) ||
38
+ relativeToRoot === ".." ||
39
+ relativeToRoot.startsWith(`..${path.sep}`);
40
+ if (escapesRepoRoot) {
41
+ logger.warn(`Skipping ${relativePath} (path escapes repo root).`);
42
+ continue;
43
+ }
22
44
  const posixPath = toPosixPath(relativePath);
23
45
  if (extraIgnoredRegexes.some((regex) => regex.test(posixPath))) {
24
46
  continue;
25
47
  }
26
- const sourcePath = path.resolve(repoRoot, relativePath);
48
+ const sourcePath = resolvedSourcePath;
27
49
  const destinationPath = path.join(destinationDirectory, relativePath);
50
+ const destinationExists = await fs
51
+ .stat(destinationPath)
52
+ .then(() => true)
53
+ .catch((error) => {
54
+ if (error &&
55
+ typeof error === "object" &&
56
+ "code" in error &&
57
+ error.code === "ENOENT") {
58
+ return false;
59
+ }
60
+ throw error;
61
+ });
62
+ if (destinationExists) {
63
+ logger.detail(`Skipped ${relativePath} (destination already exists).`);
64
+ continue;
65
+ }
66
+ if (dryRun) {
67
+ logger.detail(`Would copy ${relativePath}`);
68
+ continue;
69
+ }
28
70
  await fs.mkdir(path.dirname(destinationPath), { recursive: true });
29
71
  await fs.cp(sourcePath, destinationPath, {
30
72
  recursive: true,
31
73
  errorOnExist: false,
32
74
  });
33
- console.log(` • copied ${relativePath}`);
75
+ logger.detail(`Copied ${relativePath}`);
34
76
  }
35
77
  }
package/package.json CHANGED
@@ -2,8 +2,8 @@
2
2
  "name": "worktree-add",
3
3
  "author": "Łukasz Jerciński",
4
4
  "license": "MIT",
5
- "version": "1.2.1",
6
- "description": "Create a Git worktree next to your current repo for a branch, copy useful local files, install deps, and open it in your editor.",
5
+ "version": "1.3.0",
6
+ "description": "Create a Git worktree next to your current repo for a branch, copy useful local files, install deps, and open it in your apps.",
7
7
  "repository": {
8
8
  "type": "git",
9
9
  "url": "git+https://github.com/Jercik/worktree-add.git"
@@ -52,7 +52,9 @@
52
52
  "node": ">=22.14.0"
53
53
  },
54
54
  "dependencies": {
55
+ "@commander-js/extra-typings": "^14.0.0",
55
56
  "commander": "^14.0.2",
57
+ "open": "^11.0.0",
56
58
  "package-manager-detector": "^1.6.0",
57
59
  "trash": "^10.0.1"
58
60
  },
@@ -1,7 +0,0 @@
1
- /**
2
- * parse-branch-head-mismatch-choice.ts
3
- *
4
- * Pure helpers for parsing interactive choices in the branch-head mismatch flow.
5
- */
6
- export type ResolutionChoice = "keep-local" | "update-local" | "abort";
7
- export declare function parseResolutionChoice(input: string, defaultChoice: ResolutionChoice): ResolutionChoice | undefined;
@@ -1,26 +0,0 @@
1
- /**
2
- * parse-branch-head-mismatch-choice.ts
3
- *
4
- * Pure helpers for parsing interactive choices in the branch-head mismatch flow.
5
- */
6
- export function parseResolutionChoice(input, defaultChoice) {
7
- const normalized = input.trim().toLowerCase();
8
- if (normalized === "")
9
- return defaultChoice;
10
- if (normalized === "1" || normalized === "keep" || normalized === "local") {
11
- return "keep-local";
12
- }
13
- if (normalized === "2" ||
14
- normalized === "update" ||
15
- normalized === "remote") {
16
- return "update-local";
17
- }
18
- if (normalized === "3" ||
19
- normalized === "abort" ||
20
- normalized === "a" ||
21
- normalized === "q" ||
22
- normalized === "quit") {
23
- return "abort";
24
- }
25
- return undefined;
26
- }
@@ -1,6 +0,0 @@
1
- /**
2
- * resolve-branch-head-mismatch.ts
3
- *
4
- * Warn and resolve mismatched local/remote branch heads.
5
- */
6
- export declare function resolveBranchHeadMismatch(branch: string): Promise<boolean>;
@@ -1,79 +0,0 @@
1
- /**
2
- * resolve-branch-head-mismatch.ts
3
- *
4
- * Warn and resolve mismatched local/remote branch heads.
5
- */
6
- import { confirm, fetchOriginBranch, getLocalBranchHead, getRemoteBranchHead, git, localBranchExists, normalizeBranchName, prompt, remoteBranchExists, } from "./git.js";
7
- import { parseResolutionChoice } from "./parse-branch-head-mismatch-choice.js";
8
- const shortHash = (value) => value.slice(0, 7);
9
- const getAheadBehindCounts = (localHead, remoteHead) => {
10
- const output = git("rev-list", "--left-right", "--count", `${localHead}...${remoteHead}`);
11
- const [aheadRaw, behindRaw] = output.split(/\s+/u);
12
- const ahead = Number.parseInt(aheadRaw ?? "0");
13
- const behind = Number.parseInt(behindRaw ?? "0");
14
- return {
15
- ahead: Number.isNaN(ahead) ? 0 : ahead,
16
- behind: Number.isNaN(behind) ? 0 : behind,
17
- };
18
- };
19
- const promptResolution = async (branch, localHead, remoteHead, ahead, behind) => {
20
- const updateLabel = ahead > 0
21
- ? `Update local branch to match origin (drops ${ahead} local commit${ahead === 1 ? "" : "s"})`
22
- : "Update local branch to match origin (fast-forward)";
23
- const defaultChoice = ahead === 0 && behind > 0 ? "update-local" : "keep-local";
24
- const defaultIndex = defaultChoice === "update-local" ? "2" : "1";
25
- console.warn(`⚠️ Local branch '${branch}' and origin/${branch} point to different commits.`);
26
- console.warn(` local: ${shortHash(localHead)}`);
27
- console.warn(` origin: ${shortHash(remoteHead)}`);
28
- if (ahead > 0 || behind > 0) {
29
- const descriptors = [];
30
- if (ahead > 0)
31
- descriptors.push(`ahead by ${ahead}`);
32
- if (behind > 0)
33
- descriptors.push(`behind by ${behind}`);
34
- console.warn(` local is ${descriptors.join(" and ")} relative to origin`);
35
- }
36
- console.warn("Choose how to proceed:");
37
- console.warn(" 1) Keep local branch (use local head for the worktree)");
38
- console.warn(` 2) ${updateLabel}`);
39
- console.warn(" 3) Abort");
40
- for (;;) {
41
- const answer = await prompt(`Select an option (default: ${defaultIndex}): `);
42
- const choice = parseResolutionChoice(answer, defaultChoice);
43
- if (choice)
44
- return choice;
45
- console.warn("Please enter 1, 2, or 3.");
46
- }
47
- };
48
- export async function resolveBranchHeadMismatch(branch) {
49
- const normalized = normalizeBranchName(branch);
50
- if (!localBranchExists(normalized) || !remoteBranchExists(normalized)) {
51
- return true;
52
- }
53
- console.warn(`➤ Fetching origin/${normalized} to check for divergence …`);
54
- fetchOriginBranch(normalized);
55
- const localHead = getLocalBranchHead(normalized);
56
- const remoteHead = getRemoteBranchHead(normalized);
57
- if (!localHead || !remoteHead || localHead === remoteHead) {
58
- return true;
59
- }
60
- const { ahead, behind } = getAheadBehindCounts(localHead, remoteHead);
61
- const resolution = await promptResolution(normalized, localHead, remoteHead, ahead, behind);
62
- if (resolution === "abort") {
63
- console.warn("Operation cancelled.");
64
- return false;
65
- }
66
- if (resolution === "keep-local") {
67
- return true;
68
- }
69
- if (ahead > 0) {
70
- const confirmed = await confirm(`Updating local '${normalized}' will discard ${ahead} local commit${ahead === 1 ? "" : "s"} not on origin. Continue?`);
71
- if (!confirmed) {
72
- console.warn("Operation cancelled.");
73
- return false;
74
- }
75
- }
76
- console.warn(`➤ Updating local branch '${normalized}' to origin/${normalized} …`);
77
- git("branch", "-f", "--", normalized, `origin/${normalized}`);
78
- return true;
79
- }
@@ -1,13 +0,0 @@
1
- /**
2
- * worktree-creation.ts
3
- *
4
- * Utilities for creating Git worktrees
5
- */
6
- /**
7
- * Fetch a remote branch if it exists remotely but not locally.
8
- */
9
- export declare function fetchRemoteBranch(branch: string): void;
10
- /**
11
- * Create a worktree for the given branch at the specified destination
12
- */
13
- export declare function createWorktree(branch: string, destinationDirectory: string): void;
@@ -1,35 +0,0 @@
1
- /**
2
- * worktree-creation.ts
3
- *
4
- * Utilities for creating Git worktrees
5
- */
6
- import { fetchOriginBranch, git, localBranchExists, remoteBranchExists, } from "./git.js";
7
- /**
8
- * Fetch a remote branch if it exists remotely but not locally.
9
- */
10
- export function fetchRemoteBranch(branch) {
11
- if (!localBranchExists(branch) && remoteBranchExists(branch)) {
12
- console.log(`➤ Fetching origin/${branch} …`);
13
- fetchOriginBranch(branch);
14
- }
15
- }
16
- /**
17
- * Create a worktree for the given branch at the specified destination
18
- */
19
- export function createWorktree(branch, destinationDirectory) {
20
- if (localBranchExists(branch)) {
21
- // Reuse existing local branch
22
- console.log(`➤ git worktree add ${destinationDirectory} ${branch}`);
23
- git("worktree", "add", destinationDirectory, branch);
24
- }
25
- else if (remoteBranchExists(branch)) {
26
- // Create new local branch tracking the remote branch
27
- console.log(`➤ git worktree add --track -b ${branch} ${destinationDirectory} origin/${branch}`);
28
- git("worktree", "add", "--track", "-b", branch, destinationDirectory, `origin/${branch}`);
29
- }
30
- else {
31
- // Create new branch in the worktree from current HEAD
32
- console.log(`➤ git worktree add -b ${branch} ${destinationDirectory}`);
33
- git("worktree", "add", "-b", branch, destinationDirectory);
34
- }
35
- }