git-worktree-utils 1.0.0 → 1.0.4
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 +32 -1
- package/package.json +1 -1
package/dist/add.js
CHANGED
|
@@ -14,10 +14,41 @@ if (checkedOut) {
|
|
|
14
14
|
console.error(location);
|
|
15
15
|
process.exit(1);
|
|
16
16
|
}
|
|
17
|
+
console.log("Fetching latest from remote...");
|
|
18
|
+
try {
|
|
19
|
+
execSync("git fetch", { stdio: "inherit" });
|
|
20
|
+
} catch {
|
|
21
|
+
console.warn("Warning: git fetch failed, continuing anyway...");
|
|
22
|
+
}
|
|
23
|
+
function branchExistsLocally(branchName2) {
|
|
24
|
+
try {
|
|
25
|
+
const result = execSync(`git branch --list ${branchName2}`, { encoding: "utf-8" });
|
|
26
|
+
return result.trim().length > 0;
|
|
27
|
+
} catch {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function branchExistsOnRemote(branchName2) {
|
|
32
|
+
try {
|
|
33
|
+
const result = execSync(`git branch -r --list origin/${branchName2}`, { encoding: "utf-8" });
|
|
34
|
+
return result.trim().length > 0;
|
|
35
|
+
} catch {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
17
39
|
const targetPath = `../${dirName}`;
|
|
18
40
|
try {
|
|
19
41
|
console.log(`Creating worktree at ${targetPath} for branch ${branch}...`);
|
|
20
|
-
|
|
42
|
+
if (branchExistsLocally(branch)) {
|
|
43
|
+
execSync(`git worktree add ${targetPath} ${branch}`, { stdio: "inherit" });
|
|
44
|
+
} else if (branchExistsOnRemote(branch)) {
|
|
45
|
+
execSync(`git worktree add -b ${branch} ${targetPath} origin/${branch}`, { stdio: "inherit" });
|
|
46
|
+
} else {
|
|
47
|
+
console.log(`Branch '${branch}' not found locally or on remote, creating new branch...`);
|
|
48
|
+
execSync(`git worktree add -b ${branch} ${targetPath}`, { stdio: "inherit" });
|
|
49
|
+
console.log(`
|
|
50
|
+
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.`);
|
|
51
|
+
}
|
|
21
52
|
} catch {
|
|
22
53
|
exitWithError("Failed to create worktree");
|
|
23
54
|
}
|