gwqadd 0.3.1 → 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.
Files changed (2) hide show
  1. package/bin/gwqadd.mjs +31 -4
  2. package/package.json +1 -1
package/bin/gwqadd.mjs CHANGED
@@ -941,9 +941,36 @@ function copyToClipboard(text) {
941
941
 
942
942
  // ── worktree creation ────────────────────────────────────────────────────────
943
943
 
944
- // gwq reports the git command it ran, so a collision's destination can be read
945
- // back out of its error text.
946
- const COLLISION = /git worktree add (?:-b [^ ]* )?(\/[^ :]*)/;
944
+ // A collision's destination has to be recovered from gwq's error text. Two
945
+ // sources, in order of reliability:
946
+ //
947
+ // fatal: '<path>' already exists <- git, quoted, unambiguous
948
+ // ...: git worktree add [-b <branch>] <path>: ...
949
+ //
950
+ // The quoted form is preferred because the command echo is not parseable in
951
+ // general: `-b` swaps the argument order (`add -b <branch> <path>` versus
952
+ // `add <path> <branch>`), and a path containing a space silently truncated the
953
+ // old pattern — a gwq basedir under a directory with a space made `-f` do
954
+ // nothing at all, without saying so.
955
+ const COLLISION_QUOTED = /fatal: '([^']+)' already exists/;
956
+
957
+ // The command echo needs to know which form was used, because `-b` swaps the
958
+ // argument order and both a path and a branch can follow `add`:
959
+ //
960
+ // gwq add -b <branch> -> git worktree add -b <branch> <path>: …
961
+ // gwq add <branch> -> git worktree add <path> <branch>: …
962
+ //
963
+ // Guessing cost real time once already: a pattern that stopped at the first
964
+ // space read the path-first form correctly by accident, and a pattern that ran
965
+ // to the colon swallowed the branch with it.
966
+ const collisionFromCmd = (out, withB) => (withB
967
+ ? out.match(/git worktree add -b \S+ (.+?): /)
968
+ : out.match(/git worktree add (.+?) \S+: /))?.[1];
969
+
970
+ function collisionPath(out, withB) {
971
+ const quoted = out.match(COLLISION_QUOTED)?.[1];
972
+ return (quoted ?? collisionFromCmd(out, withB) ?? '').trim();
973
+ }
947
974
 
948
975
  function timestamp() {
949
976
  const d = new Date();
@@ -1054,7 +1081,7 @@ async function main() {
1054
1081
  };
1055
1082
 
1056
1083
  if (status !== 0) {
1057
- const collide = out.match(COLLISION)?.[1] ?? '';
1084
+ const collide = collisionPath(out, addArgs[0] === '-b');
1058
1085
  // gwq's own -f does not reach `git worktree add` (verified against v0.1.1),
1059
1086
  // so a collision has to be cleared here or not at all.
1060
1087
  if (collide && existsSync(collide) && force) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gwqadd",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "description": "Create a branch and its gwq worktree in the repository you are in, and cd there.",
5
5
  "type": "module",
6
6
  "bin": {