git-worktree-utils 1.0.3 → 1.1.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/add.js +39 -7
- package/dist/cli.js +4 -4
- package/package.json +1 -1
package/dist/add.js
CHANGED
|
@@ -2,26 +2,58 @@
|
|
|
2
2
|
import { execSync } from "child_process";
|
|
3
3
|
import { p as parseArgs, e as exitWithError, i as isBranchCheckedOut, g as getMainRepoRoot, c as copyEnvFiles } from "./utils-D1mQhe1g.js";
|
|
4
4
|
const args = parseArgs(process.argv.slice(2));
|
|
5
|
-
const dirName = args["dirName"];
|
|
6
5
|
const branchName = args["branchName"];
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
const dirName = args["dirName"];
|
|
7
|
+
if (!branchName) {
|
|
8
|
+
exitWithError("Missing --branchName=...");
|
|
9
9
|
}
|
|
10
|
-
const branch = branchName
|
|
10
|
+
const branch = branchName;
|
|
11
|
+
const dir = dirName || branchName;
|
|
11
12
|
const { checkedOut, location } = isBranchCheckedOut(branch);
|
|
12
13
|
if (checkedOut) {
|
|
13
14
|
console.error(`Branch '${branch}' is already checked out at:`);
|
|
14
15
|
console.error(location);
|
|
15
16
|
process.exit(1);
|
|
16
17
|
}
|
|
17
|
-
|
|
18
|
+
console.log("Fetching latest from remote...");
|
|
19
|
+
try {
|
|
20
|
+
execSync("git fetch", { stdio: "inherit" });
|
|
21
|
+
} catch {
|
|
22
|
+
console.warn("Warning: git fetch failed, continuing anyway...");
|
|
23
|
+
}
|
|
24
|
+
function branchExistsLocally(branchName2) {
|
|
25
|
+
try {
|
|
26
|
+
const result = execSync(`git branch --list ${branchName2}`, { encoding: "utf-8" });
|
|
27
|
+
return result.trim().length > 0;
|
|
28
|
+
} catch {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function branchExistsOnRemote(branchName2) {
|
|
33
|
+
try {
|
|
34
|
+
const result = execSync(`git branch -r --list origin/${branchName2}`, { encoding: "utf-8" });
|
|
35
|
+
return result.trim().length > 0;
|
|
36
|
+
} catch {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const targetPath = `../${dir}`;
|
|
18
41
|
try {
|
|
19
42
|
console.log(`Creating worktree at ${targetPath} for branch ${branch}...`);
|
|
20
|
-
|
|
43
|
+
if (branchExistsLocally(branch)) {
|
|
44
|
+
execSync(`git worktree add ${targetPath} ${branch}`, { stdio: "inherit" });
|
|
45
|
+
} else if (branchExistsOnRemote(branch)) {
|
|
46
|
+
execSync(`git worktree add -b ${branch} ${targetPath} origin/${branch}`, { stdio: "inherit" });
|
|
47
|
+
} else {
|
|
48
|
+
console.log(`Branch '${branch}' not found locally or on remote, creating new branch...`);
|
|
49
|
+
execSync(`git worktree add -b ${branch} ${targetPath}`, { stdio: "inherit" });
|
|
50
|
+
console.log(`
|
|
51
|
+
Tip: Run \`git push -u origin ${branch}\` to push and set up tracking, otherwise you'll get an error about setting upstream on first push.`);
|
|
52
|
+
}
|
|
21
53
|
} catch {
|
|
22
54
|
exitWithError("Failed to create worktree");
|
|
23
55
|
}
|
|
24
56
|
console.log("\nCopying .env files...");
|
|
25
57
|
const mainRoot = getMainRepoRoot();
|
|
26
|
-
const count = copyEnvFiles(mainRoot, `../${
|
|
58
|
+
const count = copyEnvFiles(mainRoot, `../${dir}`);
|
|
27
59
|
console.log(`Done copying ${count} .env file(s)`);
|
package/dist/cli.js
CHANGED
|
@@ -32,8 +32,8 @@ Commands:
|
|
|
32
32
|
sync-env Sync .env files to other worktrees
|
|
33
33
|
|
|
34
34
|
Options for 'add':
|
|
35
|
-
--
|
|
36
|
-
--
|
|
35
|
+
--branchName=<name> Branch name (required)
|
|
36
|
+
--dirName=<name> Directory name for the worktree (defaults to branchName)
|
|
37
37
|
|
|
38
38
|
Options for 'find':
|
|
39
39
|
--search=<term> Search term (case-insensitive)
|
|
@@ -57,8 +57,8 @@ Safety Features:
|
|
|
57
57
|
- Automatically copies .env files when creating worktrees
|
|
58
58
|
|
|
59
59
|
Examples:
|
|
60
|
-
wt add --
|
|
61
|
-
wt add --dirName=
|
|
60
|
+
wt add --branchName=feature/my-feature
|
|
61
|
+
wt add --branchName=feature/my-feature --dirName=my-feature
|
|
62
62
|
wt find --search=feature
|
|
63
63
|
wt sync-env --all
|
|
64
64
|
wt remove --dirName=old-feature
|