git-project-core-plugins 0.1.3__tar.gz → 0.1.4__tar.gz

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 (45) hide show
  1. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/PKG-INFO +7 -3
  2. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/_version.py +2 -2
  3. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/docs/changelog.rst +11 -1
  4. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/docs/index.rst +1 -0
  5. git_project_core_plugins-0.1.4/docs/umbrella.rst +78 -0
  6. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/requirements.txt +1 -1
  7. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/src/git_project_core_plugins/worktree.py +252 -37
  8. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/tests/test_worktree.py +625 -0
  9. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/.gitignore +0 -0
  10. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/COPYING +0 -0
  11. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/MANIFEST.in +0 -0
  12. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/changelog.d/README.rst +0 -0
  13. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/changelog.py +0 -0
  14. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/docs/Makefile +0 -0
  15. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/docs/authors.rst +0 -0
  16. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/docs/commands.rst +0 -0
  17. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/docs/conf.py +0 -0
  18. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/docs/contributing.rst +0 -0
  19. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/docs/intro.rst +0 -0
  20. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/docs/license.rst +0 -0
  21. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/docs/make.bat +0 -0
  22. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/hatch_build.py +0 -0
  23. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/pyproject.toml +0 -0
  24. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/src/git_project_core_plugins/__init__.py +0 -0
  25. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/src/git_project_core_plugins/_commanddocs.py +0 -0
  26. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/src/git_project_core_plugins/artifact.py +0 -0
  27. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/src/git_project_core_plugins/branch.py +0 -0
  28. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/src/git_project_core_plugins/clone.py +0 -0
  29. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/src/git_project_core_plugins/common.py +0 -0
  30. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/src/git_project_core_plugins/config.py +0 -0
  31. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/src/git_project_core_plugins/help.py +0 -0
  32. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/src/git_project_core_plugins/init.py +0 -0
  33. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/src/git_project_core_plugins/multirepo.py +0 -0
  34. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/src/git_project_core_plugins/run.py +0 -0
  35. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/tests/common.py +0 -0
  36. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/tests/conftest.py +0 -0
  37. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/tests/test_artifact.py +0 -0
  38. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/tests/test_branch.py +0 -0
  39. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/tests/test_changelog.py +0 -0
  40. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/tests/test_clone.py +0 -0
  41. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/tests/test_config.py +0 -0
  42. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/tests/test_help.py +0 -0
  43. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/tests/test_init.py +0 -0
  44. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/tests/test_multirepo.py +0 -0
  45. {git_project_core_plugins-0.1.3 → git_project_core_plugins-0.1.4}/tests/test_run.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: git-project-core-plugins
3
- Version: 0.1.3
3
+ Version: 0.1.4
4
4
  Summary: The extensible stupid project manager - core functionality
5
5
  Author-email: "David A. Greene" <dag@obbligato.org>
6
6
  License-Expression: AGPL-3.0-or-later
@@ -14,7 +14,7 @@ Classifier: Programming Language :: Python :: 3.10
14
14
  Classifier: Programming Language :: Python :: 3.11
15
15
  Classifier: Topic :: Software Development :: Version Control :: Git
16
16
  Requires-Python: >=3.10
17
- Requires-Dist: git-project>=0.1.2
17
+ Requires-Dist: git-project>=0.1.4
18
18
  Requires-Dist: pygit2>=1.18.2
19
19
  Provides-Extra: test
20
20
  Requires-Dist: git-project; extra == 'test'
@@ -690,7 +690,11 @@ unless ``--keep-branch`` means the branch survives anyway. When <name> has
690
690
  no worktree record, as after an ``rm`` that stopped part way, ``worktree
691
691
  rm`` removes each path record that names <name> and changes nothing else.
692
692
  When the worktree record of <name> has no path, it removes that record and
693
- each path record that names <name>, and again changes nothing else.
693
+ each path record that names <name>, and again changes nothing else. When
694
+ the record has a path but no branch, ``worktree rm`` removes the worktree
695
+ and keeps every branch. While the directory exists, it needs ``-f`` or
696
+ ``--keep-branch``, unless ``--multirepo`` already keeps the branch.
697
+ ``worktree rm`` refuses a worktree moved with ``git worktree move``.
694
698
 
695
699
  With ``--check``, ``worktree add`` and ``worktree rm`` make the checks
696
700
  they would make before changing anything, report the result and change
@@ -18,7 +18,7 @@ version_tuple: tuple[int | str, ...]
18
18
  commit_id: str | None
19
19
  __commit_id__: str | None
20
20
 
21
- __version__ = version = '0.1.3'
22
- __version_tuple__ = version_tuple = (0, 1, 3)
21
+ __version__ = version = '0.1.4'
22
+ __version_tuple__ = version_tuple = (0, 1, 4)
23
23
 
24
24
  __commit_id__ = commit_id = None
@@ -31,6 +31,15 @@ ChangeLog
31
31
  `Unreleased`_
32
32
  -------------
33
33
 
34
+ `0.1.4`_ - 2026-11-21
35
+ ---------------------
36
+ Fixed
37
+ .....
38
+ - ``clone --worktree`` of an empty repository now adds the main worktree on an unborn branch, the one the remote's HEAD names. Before, it repeated a prompt for a main branch that no answer could match. A ``clone --worktree`` that fails partway now removes what it made. A ``clone --worktree`` into a directory that already has a ``.git`` now fails, where before it overwrote the ``.git``. With no input to read, the prompt for a main branch now fails with a one-line error rather than a traceback.
39
+ - ``clone --worktree`` and ``init --worktree`` now check out a main branch with a ``/`` in its name, such as ``release/1``, in the main worktree. Before, they created a new branch named after the last part of the name, ``1``, and checked that out.
40
+ - ``worktree rm <name>`` now finishes for a worktree that ``git worktree remove`` has already removed. Before, it removed the records, then stopped with a ``NotFoundError`` and left the branch. When the branch is gone too, it no longer needs ``-f``, and it says that any copies of the branch on remotes are kept.
41
+ - ``worktree rm <name>`` now removes a worktree whose record has a path but no branch, such as one edited by hand, and keeps every branch. While the worktree's directory exists, it needs ``-f`` or ``--keep-branch``. Before, it stopped with an ``AttributeError``. ``worktree rm`` now refuses a worktree moved with ``git worktree move``, before it changes anything. Before, it removed the records and then failed, leaving the moved directory.
42
+
34
43
  `0.1.3`_ - 2026-11-17
35
44
  ---------------------
36
45
  Fixed
@@ -306,7 +315,8 @@ Fixed
306
315
  404. Issues named ``unknown/greened``, a leftover from hatch's project
307
316
  template, and Documentation misspelled the package name.
308
317
 
309
- .. _Unreleased: https://github.com/greened/git-project-core-plugins/compare/v0.1.3...HEAD
318
+ .. _Unreleased: https://github.com/greened/git-project-core-plugins/compare/v0.1.4...HEAD
319
+ .. _0.1.4: https://github.com/greened/git-project-core-plugins/compare/v0.1.3...v0.1.4
310
320
  .. _0.1.3: https://github.com/greened/git-project-core-plugins/compare/v0.1.2...v0.1.3
311
321
  .. _0.1.2: https://github.com/greened/git-project-core-plugins/compare/v0.1.1...v0.1.2
312
322
  .. _0.1.1: https://github.com/greened/git-project-core-plugins/compare/v0.1.0...v0.1.1
@@ -35,6 +35,7 @@ Welcome to git-project-core-plugins' documentation!
35
35
 
36
36
  intro.rst
37
37
  commands.rst
38
+ umbrella.rst
38
39
  contributing.rst
39
40
  authors.rst
40
41
  changelog.rst
@@ -0,0 +1,78 @@
1
+ ..
2
+ SPDX-FileCopyrightText: 2026-present David A. Greene <dag@obbligato.org>
3
+
4
+ ..
5
+ SPDX-License-Identifier: AGPL-3.0-or-later
6
+
7
+ ..
8
+ Copyright 2026 David A. Greene
9
+
10
+ ..
11
+ This file is part of git-project
12
+
13
+ ..
14
+ git-project is free software: you can redistribute it and/or modify it under
15
+ the terms of the GNU Affero General Public License as published by the Free
16
+ Software Foundation, either version 3 of the License, or (at your option)
17
+ any later version.
18
+
19
+ ..
20
+ This program is distributed in the hope that it will be useful, but WITHOUT
21
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
23
+ for more details.
24
+
25
+ ..
26
+ You should have received a copy of the GNU Affero General Public License
27
+ along with git-project. If not, see <https://www.gnu.org/licenses/>.
28
+
29
+ Umbrellas for other tools
30
+ =========================
31
+
32
+ This page is for a tool that works with an umbrella made by
33
+ git-project-core-plugins but does not use git-project itself. The
34
+ :doc:`intro` describes the layout. ``clone --worktree``, ``init
35
+ --worktree`` and ``worktree migrate`` make an umbrella, and ``git <project>
36
+ help worktree`` describes each.
37
+
38
+ An umbrella is one directory that holds a bare clone and its worktrees side
39
+ by side::
40
+
41
+ myrepo/
42
+ .git a file holding "gitdir: .myrepo.git"
43
+ .myrepo.git/ the bare repository
44
+ master/ a worktree on the main branch
45
+ topic/ a worktree on branch topic
46
+
47
+ The bare repository is named for the last component of the remote's url,
48
+ with a leading ``.`` and a ``.git`` suffix added if the url has none. So
49
+ ``git@host:me/myrepo.git`` and ``https://host/me/myrepo`` both give
50
+ ``.myrepo.git``. It has ``core.bare`` set to true.
51
+
52
+ The ``.git`` file lets git run from the umbrella itself, where it sees the
53
+ bare repository. ``git log`` and ``git worktree list`` work there, but
54
+ ``git status`` does not, since the umbrella is not a checkout. The ``.git``
55
+ is a file, never a directory or a symlink.
56
+
57
+ Finding a checkout
58
+ ------------------
59
+
60
+ A tool can find its way around an umbrella without git-project:
61
+
62
+ - A directory ``D`` is an umbrella when ``D/.git`` is a file whose whole
63
+ content is ``gitdir: <name>`` and a newline, and ``D/<name>`` is the bare
64
+ repository.
65
+ - Inside a worktree, ``git rev-parse --path-format=absolute
66
+ --git-common-dir`` prints the bare repository's path. Its parent is the
67
+ umbrella if the test above passes.
68
+ - In the umbrella, ``git rev-parse --is-bare-repository`` prints ``true``
69
+ and ``git rev-parse --show-toplevel`` fails. Do not treat the umbrella as
70
+ a checkout.
71
+ - To find the checkout of a branch, read ``git -C D worktree list
72
+ --porcelain``. Skip the record marked ``bare``, which is the repository,
73
+ and match the line ``branch refs/heads/<branch>``. Do not build the path
74
+ from the branch name. A worktree lives wherever it was added, which can
75
+ be outside the umbrella.
76
+ - git-project records each worktree it makes in the repository's config, as
77
+ ``<project>.worktree.<name>.path``. A plain ``git worktree add`` leaves no
78
+ record, so ``git worktree list`` is the complete answer.
@@ -19,5 +19,5 @@
19
19
  # You should have received a copy of the GNU Affero General Public License along
20
20
  # with git-project. If not, see <https://www.gnu.org/licenses/>.
21
21
 
22
- git-project >= 0.1.2
22
+ git-project >= 0.1.4
23
23
  pygit2 >= 1.18.2
@@ -22,6 +22,7 @@
22
22
  # with git-project. If not, see <https://www.gnu.org/licenses/>.
23
23
 
24
24
  import argparse
25
+ import contextlib
25
26
  import json
26
27
  import os
27
28
  import re
@@ -219,14 +220,17 @@ class _AddPlan:
219
220
  create_branch: bool
220
221
 
221
222
 
222
- def _plan_worktree_add(git, project, clargs):
223
+ def _plan_worktree_add(git, project, clargs, path_branch=None):
223
224
  """Work out what worktree add will do, changing nothing. Raise
224
225
  GitProjectError for each reason the add would fail that can be seen now.
226
+ A path_branch replaces the branch named after the path.
225
227
 
226
228
  """
227
229
  name, newbranch, path, refname = get_name_branch_path_and_refname(
228
230
  git, project, clargs
229
231
  )
232
+ if path_branch is not None:
233
+ newbranch = path_branch
230
234
 
231
235
  branch = git.refname_to_branch_name(refname)
232
236
  branch_point = refname
@@ -392,6 +396,79 @@ def _apply_worktree_add(git, project, plan, multirepo=None):
392
396
  return worktree
393
397
 
394
398
 
399
+ def _unborn_head_refname(git):
400
+ """Return the refname HEAD names when the repository has no branches
401
+ and HEAD is unborn, as in a clone of an empty repository. Return None
402
+ otherwise.
403
+
404
+ """
405
+ if next(iter(git.iterrefnames(["refs/heads"])), None) is not None:
406
+ return None
407
+ repo = pygit2.Repository(git.get_git_common_dir())
408
+ if not repo.head_is_unborn:
409
+ return None
410
+ return repo.references["HEAD"].target
411
+
412
+
413
+ def _add_orphan_worktree(git, project, path, branch):
414
+ """Add a worktree at path on branch, which has no commit yet, and record
415
+ it as worktree add does. Raise GitProjectError if git cannot add it.
416
+
417
+ """
418
+ path = Path(path).resolve()
419
+ # libgit2 cannot add a worktree on an unborn branch.
420
+ result = subprocess.run(
421
+ [
422
+ "git",
423
+ "--git-dir",
424
+ git.get_git_common_dir(),
425
+ "worktree",
426
+ "add",
427
+ "--quiet",
428
+ "--orphan",
429
+ "-b",
430
+ branch,
431
+ str(path),
432
+ ],
433
+ stdin=subprocess.DEVNULL,
434
+ capture_output=True,
435
+ text=True,
436
+ check=False,
437
+ )
438
+ if result.returncode != 0:
439
+ first_line = next(iter(result.stderr.strip().splitlines()), "")
440
+ raise GitProjectError(
441
+ f"Cannot add worktree {path} on unborn branch {branch}: "
442
+ f"{first_line} (an unborn branch needs git 2.42 or later)"
443
+ )
444
+ return Worktree.get(
445
+ git, project, path.name, path=str(path), committish=branch
446
+ )
447
+
448
+
449
+ def _undo_umbrella_clone(made, made_dirs):
450
+ """Remove what a failed clone --worktree made: the paths in made, then
451
+ the directories in made_dirs, newest first, if they are empty. Report,
452
+ rather than raise, what cannot be removed, so the caller's error is the
453
+ one the user sees.
454
+
455
+ """
456
+ for path in reversed(made):
457
+ try:
458
+ if path.is_dir() and not path.is_symlink():
459
+ shutil.rmtree(path)
460
+ elif os.path.lexists(path):
461
+ path.unlink()
462
+ except OSError as exception:
463
+ print(
464
+ f"warning: could not remove {path}: {exception}",
465
+ file=sys.stderr,
466
+ )
467
+ for directory in reversed(made_dirs):
468
+ with contextlib.suppress(OSError):
469
+ directory.rmdir()
470
+
471
+
395
472
  # worktree add
396
473
  def command_worktree_add(git, gitproject, project, clargs):
397
474
  """Implement git-project worktree add."""
@@ -444,6 +521,24 @@ def _path_sections_naming(git, project, name):
444
521
  return sections
445
522
 
446
523
 
524
+ def _moved_worktree_path(git, worktree):
525
+ """Return the path of git's worktree of the same name as worktree when a
526
+ directory is there and it is not the recorded path, as after a git
527
+ worktree move. Return None otherwise.
528
+
529
+ """
530
+ ident = worktree.get_ident()
531
+ repo = pygit2.Repository(git.get_git_common_dir())
532
+ if ident not in repo.list_worktrees():
533
+ return None
534
+ path = repo.lookup_worktree(ident).path
535
+ if not os.path.lexists(path):
536
+ return None
537
+ if os.path.realpath(path) == os.path.realpath(worktree.path):
538
+ return None
539
+ return path
540
+
541
+
447
542
  def _plan_worktree_rm(git, project, clargs):
448
543
  """Check that worktree rm can go ahead, changing nothing. Return whether
449
544
  the worktree's branch stays.
@@ -457,6 +552,23 @@ def _plan_worktree_rm(git, project, clargs):
457
552
  worktree = Worktree.get(git, project, name)
458
553
  project.pop_scope()
459
554
 
555
+ # A relative path would resolve against the current directory.
556
+ if worktree.path and not os.path.isabs(worktree.path):
557
+ raise GitProjectError(
558
+ f"The record of worktree {name} has the relative path "
559
+ f"{worktree.path}, set it to an absolute path to remove the "
560
+ "worktree"
561
+ )
562
+ # Worktree.rm removes the recorded path and prunes git's worktree of the
563
+ # same name, so the two must agree.
564
+ moved = _moved_worktree_path(git, worktree)
565
+ if moved:
566
+ raise GitProjectError(
567
+ f"git's worktree {worktree.get_ident()} is at {moved}, not at "
568
+ f"the recorded path {worktree.path}, so worktree {name} cannot "
569
+ "be removed"
570
+ )
571
+
460
572
  keep_branch = clargs.keep_branch
461
573
  multirepo = getattr(clargs, "multirepo", None)
462
574
  if multirepo is not None:
@@ -475,14 +587,37 @@ def _plan_worktree_rm(git, project, clargs):
475
587
  f"{', '.join(created)}, not true"
476
588
  )
477
589
 
590
+ # A record can lack a committish, as after a hand edit. Then rm cannot
591
+ # tell which branch to delete, so it keeps them all, as when the branch
592
+ # is gone. -f guards only uncommitted work in the directory.
593
+ if not keep_branch and not _record_values(git, worktree, "committish"):
594
+ if not clargs.force and os.path.lexists(worktree.path):
595
+ raise GitProjectError(
596
+ f"The record of worktree {name} has no branch, use -f or "
597
+ "--keep-branch to remove the worktree"
598
+ )
599
+ keep_branch = True
600
+ check = getattr(clargs, "check", False)
601
+ kept = "would be kept" if check else "are kept"
602
+ print(
603
+ f"The record of worktree {name} has no branch, so all branches "
604
+ f"{kept}"
605
+ )
606
+
478
607
  # The merge check protects the branch's commits, so it applies only when we
479
608
  # are going to delete the branch. --keep-branch leaves the branch in place
480
609
  # both locally and on remotes, so the merge state cannot cost anything.
481
610
  # --keep-remote-branch still deletes the local branch, so it still applies.
482
611
  # The record holds the branch name, and a tag of that name would win.
483
- refname = git.branch_name_to_refname(worktree.committish)
612
+ refname = (
613
+ None
614
+ if keep_branch
615
+ else git.branch_name_to_refname(worktree.committish)
616
+ )
484
617
  if not keep_branch and not git.committish_exists(refname):
485
- if not clargs.force:
618
+ # The branch is gone, so -f guards only uncommitted work in the
619
+ # directory.
620
+ if not clargs.force and os.path.lexists(worktree.path):
486
621
  raise GitProjectError(
487
622
  f"Worktree branch {worktree.committish} does not exist, "
488
623
  "use -f or --keep-branch to remove the worktree"
@@ -758,7 +893,10 @@ class Worktree(ScopedConfigObject):
758
893
  if tree:
759
894
  shutil.rmtree(tree, ignore_errors=True)
760
895
 
761
- self._git.prune_worktree(ident)
896
+ # A plain git worktree remove prunes git's record itself.
897
+ repo = pygit2.Repository(self._git.get_git_common_dir())
898
+ if ident in repo.list_worktrees():
899
+ self._git.prune_worktree(ident)
762
900
 
763
901
  project = Project.get(self._git, self._project_section)
764
902
 
@@ -2081,7 +2219,11 @@ class WorktreePlugin(Plugin):
2081
2219
  no worktree record, as after an ``rm`` that stopped part way, ``worktree
2082
2220
  rm`` removes each path record that names <name> and changes nothing else.
2083
2221
  When the worktree record of <name> has no path, it removes that record and
2084
- each path record that names <name>, and again changes nothing else.
2222
+ each path record that names <name>, and again changes nothing else. When
2223
+ the record has a path but no branch, ``worktree rm`` removes the worktree
2224
+ and keeps every branch. While the directory exists, it needs ``-f`` or
2225
+ ``--keep-branch``, unless ``--multirepo`` already keeps the branch.
2226
+ ``worktree rm`` refuses a worktree moved with ``git worktree move``.
2085
2227
 
2086
2228
  With ``--check``, ``worktree add`` and ``worktree rm`` make the checks
2087
2229
  they would make before changing anything, report the result and change
@@ -2436,7 +2578,7 @@ class WorktreePlugin(Plugin):
2436
2578
  "-f",
2437
2579
  "--force",
2438
2580
  action="store_true",
2439
- help="Remove even if branch is not merged",
2581
+ help="Remove even if the branch is not merged or is missing",
2440
2582
  )
2441
2583
  worktree_rm_parser.add_argument(
2442
2584
  "--keep-branch",
@@ -2506,12 +2648,21 @@ class WorktreePlugin(Plugin):
2506
2648
  return git.branch_name_to_refname(main)
2507
2649
 
2508
2650
  branches = list(git.iterrefnames(["refs/heads"]))
2651
+ if not branches:
2652
+ raise GitProjectError(
2653
+ "Cannot choose a main branch, the repository has no branches"
2654
+ )
2509
2655
  while True:
2510
2656
  for refname in branches:
2511
2657
  print(git.refname_to_branch_name(refname))
2512
- main = input(
2513
- "No unique main branch found, enter branch to use as main: "
2514
- )
2658
+ try:
2659
+ main = input(
2660
+ "No unique main branch found, enter branch to use as main: "
2661
+ )
2662
+ except EOFError:
2663
+ raise GitProjectError(
2664
+ "No unique main branch found and no input to read one from"
2665
+ ) from None
2515
2666
  if git.branch_name_to_refname(main) in branches:
2516
2667
  break
2517
2668
 
@@ -2529,6 +2680,14 @@ class WorktreePlugin(Plugin):
2529
2680
  )
2530
2681
  p_git.fetch_remote("origin")
2531
2682
 
2683
+ unborn = _unborn_head_refname(p_git)
2684
+ if unborn:
2685
+ # There is no branch yet to set an upstream on.
2686
+ section = f"branch.{p_git.refname_to_branch_name(unborn)}"
2687
+ p_git.config.set_item(section, "remote", "origin")
2688
+ p_git.config.set_item(section, "merge", unborn)
2689
+ return unborn
2690
+
2532
2691
  main = self._choose_main_branch(p_git)
2533
2692
 
2534
2693
  for refname in p_git.iterrefnames(["refs/heads"]):
@@ -2553,7 +2712,10 @@ class WorktreePlugin(Plugin):
2553
2712
  clargs.committish = main_branch
2554
2713
  clargs.path = str(main_path)
2555
2714
 
2556
- command_worktree_add(p_git, p_gitproject, p_project, clargs)
2715
+ # From an absolute path, worktree add would name a new branch after
2716
+ # only the last component of a main branch such as release/1.
2717
+ plan = _plan_worktree_add(p_git, p_project, clargs, main_branch)
2718
+ _apply_worktree_add(p_git, p_project, plan)
2557
2719
 
2558
2720
  def modify_arguments(
2559
2721
  self, git, gitproject, project, parser_manager, plugin_manager
@@ -2573,41 +2735,94 @@ class WorktreePlugin(Plugin):
2573
2735
 
2574
2736
  assert hasattr(clargs, "url")
2575
2737
 
2576
- path = path / get_hidden_gitdir_name(clargs.url)
2738
+ # Resolved before anything is recorded, so a '..' past a
2739
+ # directory the clone creates cannot name an existing one
2740
+ # that the undo would then remove.
2741
+ path = (
2742
+ path / get_hidden_gitdir_name(clargs.url)
2743
+ ).resolve()
2744
+ umbrella = path.parent
2745
+
2746
+ # Writing the pointer would overwrite it, and an undo
2747
+ # could not bring it back.
2748
+ if os.path.lexists(umbrella / ".git"):
2749
+ raise GitProjectError(
2750
+ f"Cannot clone into {umbrella}, it already has a .git"
2751
+ )
2577
2752
 
2578
2753
  bare_specified = clargs.bare
2579
2754
 
2580
2755
  clargs.path = str(path)
2581
2756
  clargs.bare = True
2582
2757
 
2583
- # Bare clone to the hidden directory.
2584
- path = command_clone(
2585
- p_git, p_gitproject, p_project, clargs
2586
- )
2587
-
2588
- # If the user did not ask for a bare repo, rewrite refspecs,
2589
- # fetch remote refs and rewrite existing refs (just main)
2590
- # to track the remote ref. Delete other "local" branches.
2591
- main = ""
2592
- if not bare_specified:
2593
- main = self._rewrite_bare_refspects(p_git)
2758
+ # What this clone makes, to remove if a later step fails.
2759
+ made_dirs = []
2760
+ directory = umbrella
2761
+ while not os.path.lexists(directory):
2762
+ made_dirs.insert(0, directory)
2763
+ directory = directory.parent
2764
+ made = [
2765
+ made_path
2766
+ for made_path in (path, umbrella / ".git")
2767
+ if not os.path.lexists(made_path)
2768
+ ]
2594
2769
 
2595
- if not main:
2596
- main = self._choose_main_branch(p_git)
2597
-
2598
- write_umbrella_gitdir(Path(path).parent, Path(path).name)
2599
-
2600
- # Detach HEAD so we can worktree main.
2601
- p_git.detach_head()
2770
+ try:
2771
+ # Bare clone to the hidden directory.
2772
+ path = command_clone(
2773
+ p_git, p_gitproject, p_project, clargs
2774
+ )
2602
2775
 
2603
- self._setup_main_worktree(
2604
- main,
2605
- p_git,
2606
- p_gitproject,
2607
- p_project,
2608
- Path(path).parent,
2609
- clargs,
2610
- )
2776
+ # If the user did not ask for a bare repo, rewrite
2777
+ # refspecs, fetch remote refs and rewrite existing
2778
+ # refs (just main) to track the remote ref. Delete
2779
+ # other "local" branches.
2780
+ main = ""
2781
+ if not bare_specified:
2782
+ main = self._rewrite_bare_refspects(p_git)
2783
+
2784
+ unborn = _unborn_head_refname(p_git)
2785
+ if not main:
2786
+ main = unborn or self._choose_main_branch(p_git)
2787
+
2788
+ main_branch = p_git.refname_to_branch_name(main)
2789
+ main_top = None
2790
+ directory = umbrella / main_branch
2791
+ while directory != umbrella and not os.path.lexists(
2792
+ directory
2793
+ ):
2794
+ main_top = directory
2795
+ directory = directory.parent
2796
+ if main_top is not None:
2797
+ made.append(main_top)
2798
+
2799
+ write_umbrella_gitdir(umbrella, Path(path).name)
2800
+
2801
+ if unborn:
2802
+ if not p_project.has_item("branch"):
2803
+ p_project.branch = main_branch
2804
+ _add_orphan_worktree(
2805
+ p_git,
2806
+ p_project,
2807
+ umbrella / main_branch,
2808
+ main_branch,
2809
+ )
2810
+ else:
2811
+ # Detach HEAD so we can worktree main.
2812
+ p_git.detach_head()
2813
+
2814
+ self._setup_main_worktree(
2815
+ main,
2816
+ p_git,
2817
+ p_gitproject,
2818
+ p_project,
2819
+ umbrella,
2820
+ clargs,
2821
+ )
2822
+ # A Ctrl-C raises KeyboardInterrupt, so undo on that too.
2823
+ except BaseException:
2824
+ _undo_umbrella_clone(made, made_dirs)
2825
+ raise
2611
2826
  else:
2612
2827
  path = command_clone(
2613
2828
  p_git, p_gitproject, p_project, clargs