git-project-core-plugins 0.1.1__tar.gz → 0.1.3__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 (44) hide show
  1. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/PKG-INFO +11 -9
  2. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/_version.py +2 -2
  3. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/docs/changelog.rst +22 -1
  4. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/requirements.txt +1 -2
  5. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/src/git_project_core_plugins/clone.py +2 -2
  6. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/src/git_project_core_plugins/multirepo.py +2 -3
  7. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/src/git_project_core_plugins/worktree.py +114 -8
  8. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/tests/test_worktree.py +402 -0
  9. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/.gitignore +0 -0
  10. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/COPYING +0 -0
  11. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/MANIFEST.in +0 -0
  12. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/changelog.d/README.rst +0 -0
  13. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/changelog.py +0 -0
  14. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/docs/Makefile +0 -0
  15. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/docs/authors.rst +0 -0
  16. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/docs/commands.rst +0 -0
  17. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/docs/conf.py +0 -0
  18. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/docs/contributing.rst +0 -0
  19. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/docs/index.rst +0 -0
  20. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/docs/intro.rst +0 -0
  21. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/docs/license.rst +0 -0
  22. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/docs/make.bat +0 -0
  23. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/hatch_build.py +0 -0
  24. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/pyproject.toml +0 -0
  25. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/src/git_project_core_plugins/__init__.py +0 -0
  26. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/src/git_project_core_plugins/_commanddocs.py +0 -0
  27. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/src/git_project_core_plugins/artifact.py +0 -0
  28. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/src/git_project_core_plugins/branch.py +0 -0
  29. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/src/git_project_core_plugins/common.py +0 -0
  30. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/src/git_project_core_plugins/config.py +0 -0
  31. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/src/git_project_core_plugins/help.py +0 -0
  32. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/src/git_project_core_plugins/init.py +0 -0
  33. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/src/git_project_core_plugins/run.py +0 -0
  34. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/tests/common.py +0 -0
  35. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/tests/conftest.py +0 -0
  36. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/tests/test_artifact.py +0 -0
  37. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/tests/test_branch.py +0 -0
  38. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/tests/test_changelog.py +0 -0
  39. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/tests/test_clone.py +0 -0
  40. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/tests/test_config.py +0 -0
  41. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/tests/test_help.py +0 -0
  42. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/tests/test_init.py +0 -0
  43. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/tests/test_multirepo.py +0 -0
  44. {git_project_core_plugins-0.1.1 → git_project_core_plugins-0.1.3}/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.1
3
+ Version: 0.1.3
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,9 +14,8 @@ 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.1
17
+ Requires-Dist: git-project>=0.1.2
18
18
  Requires-Dist: pygit2>=1.18.2
19
- Requires-Dist: pytest-console-scripts
20
19
  Provides-Extra: test
21
20
  Requires-Dist: git-project; extra == 'test'
22
21
  Requires-Dist: pytest; extra == 'test'
@@ -258,8 +257,8 @@ Summary::
258
257
  By itself clone does a basic clone, then sets the project's defaults in
259
258
  the new repository. With no <path> it clones into the last component of
260
259
  <url> under the current directory, keeping any ``.git`` suffix, where
261
- ``git clone`` drops it. It offers no ssh key, so an ssh url fails to
262
- authenticate.
260
+ ``git clone`` drops it. An ssh url clones through git, so it uses your
261
+ ssh agent and ``~/.ssh/config`` as ``git clone`` does.
263
262
 
264
263
  Plugins add options to clone. For example, the worktree command adds a
265
264
  --worktree option to have clone create an ``umbrella layout``.
@@ -621,9 +620,8 @@ it over the file, so a failure leaves the file as it was. A symlink to
621
620
  the set file stays a symlink.
622
621
 
623
622
  ``set clone`` clones each member whose root does not exist, as ``clone
624
- --worktree <url> <root>`` does, one at a time. Like ``clone``, it offers
625
- no ssh key, so give a member it must clone an https url. A member whose
626
- root is already an umbrella cloned from its url is left alone. A root
623
+ --worktree <url> <root>`` does, one at a time. A member whose root is
624
+ already an umbrella cloned from its url is left alone. A root
627
625
  that exists and is anything else stops the command before anything is
628
626
  cloned, so a clone that failed partway is never taken for a finished
629
627
  one. A failed clone does not stop the rest. Ctrl-C, SIGTERM and SIGHUP
@@ -688,7 +686,11 @@ project remote, unless a flag says otherwise: ``--keep-branch`` leaves the
688
686
  branch alone everywhere, and ``--keep-remote-branch`` deletes only the
689
687
  local copy. A branch the project configures is never deleted, whatever the
690
688
  flags say. Removing a worktree whose branch is unmerged requires ``-f``,
691
- unless ``--keep-branch`` means the branch survives anyway.
689
+ unless ``--keep-branch`` means the branch survives anyway. When <name> has
690
+ no worktree record, as after an ``rm`` that stopped part way, ``worktree
691
+ rm`` removes each path record that names <name> and changes nothing else.
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.
692
694
 
693
695
  With ``--check``, ``worktree add`` and ``worktree rm`` make the checks
694
696
  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.1'
22
- __version_tuple__ = version_tuple = (0, 1, 1)
21
+ __version__ = version = '0.1.3'
22
+ __version_tuple__ = version_tuple = (0, 1, 3)
23
23
 
24
24
  __commit_id__ = commit_id = None
@@ -31,6 +31,25 @@ ChangeLog
31
31
  `Unreleased`_
32
32
  -------------
33
33
 
34
+ `0.1.3`_ - 2026-11-17
35
+ ---------------------
36
+ Fixed
37
+ .....
38
+ - ``Worktree.rm`` now leaves a branch alone on every remote when the local branch is gone, since a remote may hold the only copy of its commits. Before, it raised ``NotFoundError`` or deleted the remote copies, depending on the project's other branches.
39
+ - ``worktree rm -f <name>`` now removes a worktree whose branch is gone, and leaves the branch alone on every remote, saying so. Without ``-f`` or ``--keep-branch`` it fails with a one-line error that names the branch. Before, both stopped with a traceback.
40
+ - ``worktree rm <name>`` now removes the worktree record of ``<name>`` when that record has no path, as after an ``rm`` that stopped part way through the record's keys, along with each path record that names ``<name>``. It changes no other config and removes no files or branches. Before, it failed with a ``TypeError`` and left both records in place. With ``--multirepo`` it does so only when the record still names the set, and otherwise stops with an error.
41
+
42
+ `0.1.2`_ - 2026-10-22
43
+ ---------------------
44
+ Changed
45
+ .......
46
+ - The help for ``clone`` and ``set clone`` no longer says an ssh url fails to authenticate. Both now need git-project 0.1.2, which clones an ssh url through git, with your ssh agent and ``~/.ssh/config``.
47
+
48
+ Fixed
49
+ .....
50
+ - Installing git-project-core-plugins no longer installs ``pytest-console-scripts``. Only the tests use it, and the ``test`` extra already lists it.
51
+ - ``worktree rm <name>`` now removes each path record that names ``<name>`` when the worktree record of ``<name>`` is gone, as after an ``rm`` that stopped part way. It changes no other config and removes no files. Before, it said git-project had no record of the worktree and left the path record in place.
52
+
34
53
  `0.1.1`_ - 2026-10-19
35
54
  ---------------------
36
55
  Added
@@ -287,7 +306,9 @@ Fixed
287
306
  404. Issues named ``unknown/greened``, a leftover from hatch's project
288
307
  template, and Documentation misspelled the package name.
289
308
 
290
- .. _Unreleased: https://github.com/greened/git-project-core-plugins/compare/v0.1.1...HEAD
309
+ .. _Unreleased: https://github.com/greened/git-project-core-plugins/compare/v0.1.3...HEAD
310
+ .. _0.1.3: https://github.com/greened/git-project-core-plugins/compare/v0.1.2...v0.1.3
311
+ .. _0.1.2: https://github.com/greened/git-project-core-plugins/compare/v0.1.1...v0.1.2
291
312
  .. _0.1.1: https://github.com/greened/git-project-core-plugins/compare/v0.1.0...v0.1.1
292
313
  .. _0.1.0: https://github.com/greened/git-project-core-plugins/compare/v0.0.33...v0.1.0
293
314
  .. _0.0.33: https://github.com/greened/git-project-core-plugins/compare/v0.0.32...v0.0.33
@@ -19,6 +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.1
22
+ git-project >= 0.1.2
23
23
  pygit2 >= 1.18.2
24
- pytest-console-scripts
@@ -62,8 +62,8 @@ class ClonePlugin(Plugin):
62
62
  By itself clone does a basic clone, then sets the project's defaults in
63
63
  the new repository. With no <path> it clones into the last component of
64
64
  <url> under the current directory, keeping any ``.git`` suffix, where
65
- ``git clone`` drops it. It offers no ssh key, so an ssh url fails to
66
- authenticate.
65
+ ``git clone`` drops it. An ssh url clones through git, so it uses your
66
+ ssh agent and ``~/.ssh/config`` as ``git clone`` does.
67
67
 
68
68
  Plugins add options to clone. For example, the worktree command adds a
69
69
  --worktree option to have clone create an ``umbrella layout``.
@@ -1331,9 +1331,8 @@ class SetPlugin(Plugin):
1331
1331
  the set file stays a symlink.
1332
1332
 
1333
1333
  ``set clone`` clones each member whose root does not exist, as ``clone
1334
- --worktree <url> <root>`` does, one at a time. Like ``clone``, it offers
1335
- no ssh key, so give a member it must clone an https url. A member whose
1336
- root is already an umbrella cloned from its url is left alone. A root
1334
+ --worktree <url> <root>`` does, one at a time. A member whose root is
1335
+ already an umbrella cloned from its url is left alone. A root
1337
1336
  that exists and is anything else stops the command before anything is
1338
1337
  cloned, so a clone that failed partway is never taken for a finished
1339
1338
  one. A failed clone does not stop the rest. Ctrl-C, SIGTERM and SIGHUP
@@ -418,6 +418,32 @@ def _record_values(git, worktree, key):
418
418
  )
419
419
 
420
420
 
421
+ def _path_sections_naming(git, project, name):
422
+ """Return the section and path of each path record that names worktree
423
+ name.
424
+
425
+ """
426
+ section = project.get_section()
427
+ # A live worktree's own path record is never an orphan, even if its
428
+ # worktree key was edited to name this one.
429
+ live = set()
430
+ for ident in ConfigObject.iter_idents(git, section, Worktree.subsection()):
431
+ worktree = Worktree.get(git, project, ident)
432
+ project.pop_scope()
433
+ live.update(_record_values(git, worktree, "path"))
434
+ sections = []
435
+ for path in ConfigObject.iter_idents(
436
+ git, section, Worktree.Path.subsection()
437
+ ):
438
+ if path in live:
439
+ continue
440
+ record = Worktree.Path.get(git, section, path)
441
+ # Skip a record with several worktree values rather than guess.
442
+ if _record_values(git, record, "worktree") == [name]:
443
+ sections.append((record.get_section(), path))
444
+ return sections
445
+
446
+
421
447
  def _plan_worktree_rm(git, project, clargs):
422
448
  """Check that worktree rm can go ahead, changing nothing. Return whether
423
449
  the worktree's branch stays.
@@ -455,10 +481,24 @@ def _plan_worktree_rm(git, project, clargs):
455
481
  # --keep-remote-branch still deletes the local branch, so it still applies.
456
482
  # The record holds the branch name, and a tag of that name would win.
457
483
  refname = git.branch_name_to_refname(worktree.committish)
484
+ if not keep_branch and not git.committish_exists(refname):
485
+ if not clargs.force:
486
+ raise GitProjectError(
487
+ f"Worktree branch {worktree.committish} does not exist, "
488
+ "use -f or --keep-branch to remove the worktree"
489
+ )
490
+ # The remote branch may now hold the only copy of the commits.
491
+ keep_branch = True
492
+ check = getattr(clargs, "check", False)
493
+ kept = "would be kept" if check else "are kept"
494
+ print(
495
+ f"Branch {worktree.committish} no longer exists locally, so any "
496
+ f"copies on remotes {kept}"
497
+ )
458
498
  if (
459
499
  not keep_branch
460
- and not project.branch_is_merged(refname)
461
500
  and not clargs.force
501
+ and not project.branch_is_merged(refname)
462
502
  ):
463
503
  raise GitProjectError(
464
504
  f"Worktree branch {worktree.committish} is not merged, use -f to force"
@@ -478,13 +518,71 @@ def _plan_worktree_rm(git, project, clargs):
478
518
 
479
519
  def command_worktree_rm(git, gitproject, project, clargs):
480
520
  """Implement git-project worktree rm."""
521
+ name = clargs.name
522
+ multirepo = getattr(clargs, "multirepo", None)
523
+ check = getattr(clargs, "check", False)
524
+ record = None
525
+ orphans = []
526
+ # A path record can outlive its worktree record, as when an rm stops
527
+ # between its two section removals. Remove such a record, config only.
528
+ # With --multirepo, refuse, since the record that names the set is gone.
529
+ if not Worktree.exists(
530
+ git, project.get_section(), Worktree.subsection(), name
531
+ ):
532
+ if multirepo is None:
533
+ orphans = _path_sections_naming(git, project, name)
534
+ else:
535
+ worktree = Worktree.get(git, project, name)
536
+ project.pop_scope()
537
+ # An rm that stops partway through the worktree record's keys can
538
+ # leave a record with no path. Such a record cannot vouch for a
539
+ # directory, so remove the config only. With --multirepo, the
540
+ # record must still name the set, as for any other rm.
541
+ if not _record_values(git, worktree, "path"):
542
+ if multirepo is not None and _record_values(
543
+ git, worktree, "multirepo"
544
+ ) != [multirepo]:
545
+ raise GitProjectError(
546
+ f"The record of worktree {name} has no path and does "
547
+ f"not name multirepo set {multirepo}. Run worktree rm "
548
+ f"{name} in this repository, outside multirepo, to "
549
+ f"remove it"
550
+ )
551
+ record = worktree.get_section()
552
+ orphans = _path_sections_naming(git, project, name)
553
+ if record is not None or orphans:
554
+ if record is not None:
555
+ if check:
556
+ print(
557
+ f"Would remove the worktree record {record}, which has "
558
+ f"no path"
559
+ )
560
+ else:
561
+ _rm_config_section(git, record)
562
+ print(
563
+ f"Removed the worktree record {record}, which had no path"
564
+ )
565
+ for section, path in orphans:
566
+ if check:
567
+ print(
568
+ f"Would remove the orphan path record {section}, "
569
+ f"and keep the worktree directory {path}"
570
+ )
571
+ else:
572
+ _rm_config_section(git, section)
573
+ print(
574
+ f"Removed the orphan path record {section}, and "
575
+ f"kept the worktree directory {path}"
576
+ )
577
+ return
578
+
481
579
  keep_branch = _plan_worktree_rm(git, project, clargs)
482
580
 
483
- if getattr(clargs, "check", False):
484
- print(f"Would remove worktree {clargs.name}")
581
+ if check:
582
+ print(f"Would remove worktree {name}")
485
583
  return
486
584
 
487
- Worktree.get(git, project, clargs.name).rm(
585
+ Worktree.get(git, project, name).rm(
488
586
  keep_branch=keep_branch,
489
587
  keep_remote_branch=clargs.keep_remote_branch,
490
588
  )
@@ -664,12 +762,16 @@ class Worktree(ScopedConfigObject):
664
762
 
665
763
  project = Project.get(self._git, self._project_section)
666
764
 
667
- if not keep_branch:
765
+ # With the local branch gone, the remote branch may hold the only copy
766
+ # of the commits.
767
+ if not keep_branch and self._git.committish_exists(
768
+ self._git.branch_name_to_refname(committish)
769
+ ):
668
770
  for branch in project.iterbranches():
669
771
  if branch not in self._git.iterbranches():
670
772
  continue
671
- branch_name = self._git.committish_to_refname(branch)
672
- committish_name = self._git.committish_to_refname(committish)
773
+ branch_name = self._git.branch_name_to_refname(branch)
774
+ committish_name = self._git.branch_name_to_refname(committish)
673
775
  if branch_name == committish_name:
674
776
  break
675
777
  else:
@@ -1975,7 +2077,11 @@ class WorktreePlugin(Plugin):
1975
2077
  branch alone everywhere, and ``--keep-remote-branch`` deletes only the
1976
2078
  local copy. A branch the project configures is never deleted, whatever the
1977
2079
  flags say. Removing a worktree whose branch is unmerged requires ``-f``,
1978
- unless ``--keep-branch`` means the branch survives anyway.
2080
+ unless ``--keep-branch`` means the branch survives anyway. When <name> has
2081
+ no worktree record, as after an ``rm`` that stopped part way, ``worktree
2082
+ rm`` removes each path record that names <name> and changes nothing else.
2083
+ 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.
1979
2085
 
1980
2086
  With ``--check``, ``worktree add`` and ``worktree rm`` make the checks
1981
2087
  they would make before changing anything, report the result and change
@@ -1718,6 +1718,41 @@ def test_worktree_migrate_existing_worktree_config(flat_clone, script_runner):
1718
1718
  )
1719
1719
 
1720
1720
 
1721
+ def test_worktree_rm_after_migrate_removes_the_stale_path_record(
1722
+ flat_clone, script_runner
1723
+ ):
1724
+ fc = flat_clone
1725
+ top = str(fc.top)
1726
+ old = f"project.worktreepath.{fc.feature}.worktree"
1727
+ _out("-C", top, "config", "project.worktree.feat.path", str(fc.feature))
1728
+ _out(
1729
+ "-C", top, "config", "project.worktree.feat.committish", "user/feature"
1730
+ )
1731
+ _out("-C", top, "config", old, "feat")
1732
+ assert _migrate(script_runner, fc.top, "--apply").success
1733
+ new = fc.top / "user-feature"
1734
+
1735
+ def rm():
1736
+ return script_runner.run(
1737
+ ["git-project", "worktree", "rm", "-f", "feat"],
1738
+ cwd=str(fc.top / "master"),
1739
+ )
1740
+
1741
+ result = rm()
1742
+ assert result.success, result.stdout + result.stderr
1743
+ # A stale record names a path that no worktree owns, so it survives
1744
+ # the rm of the live worktree.
1745
+ assert not new.exists()
1746
+ assert _out("-C", top, "config", old) == "feat\n"
1747
+
1748
+ result = rm()
1749
+ assert result.success, result.stdout + result.stderr
1750
+ assert f"Removed the orphan path record {old[: -len('.worktree')]}" in (
1751
+ result.stdout
1752
+ )
1753
+ assert "=feat\n" not in _out("-C", top, "config", "--list", "--local")
1754
+
1755
+
1721
1756
  def _unpushed(result):
1722
1757
  return [
1723
1758
  line
@@ -3295,6 +3330,286 @@ def test_worktree_rm_multirepo_deletes_a_branch_it_created(
3295
3330
  assert not (workarea / "wt-d").exists()
3296
3331
 
3297
3332
 
3333
+ def test_worktree_rm_keeps_the_remote_branch_when_local_is_gone(
3334
+ git, gitproject, project, monkeypatch
3335
+ ):
3336
+ # This test pushes, so it reaches a pre-push hook. Ignore the user's git
3337
+ # configuration to keep it hermetic.
3338
+ monkeypatch.setenv("GIT_CONFIG_GLOBAL", "/dev/null")
3339
+ monkeypatch.setenv("GIT_CONFIG_NOSYSTEM", "1")
3340
+
3341
+ _add(git, gitproject, project, "../wt-p")
3342
+ git_project.capture_command("git push origin wt-p:refs/heads/wt-p")
3343
+ assert git.remote_branch_exists("wt-p", "origin")
3344
+ # Deleted by hand, as a user might outside git-project.
3345
+ repo = pygit2.Repository(git.get_git_common_dir())
3346
+ repo.references.delete("refs/heads/wt-p")
3347
+
3348
+ # Worktree.rm directly, since the rm command stops before this case.
3349
+ worktree = Worktree.get(git, project, "wt-p")
3350
+ project.pop_scope()
3351
+ worktree.rm()
3352
+
3353
+ assert _record(git, "wt-p") == {}
3354
+ assert git.remote_branch_exists("wt-p", "origin")
3355
+
3356
+
3357
+ def _path_records(git):
3358
+ """Return each path record on disk, as path -> worktree name."""
3359
+ config = pygit2.Config(str(Path(git.get_git_common_dir()) / "config"))
3360
+ prefix = "project.worktreepath."
3361
+ return {
3362
+ entry.name[len(prefix) : -len(".worktree")]: entry.value
3363
+ for entry in config
3364
+ if entry.name.startswith(prefix) and entry.name.endswith(".worktree")
3365
+ }
3366
+
3367
+
3368
+ def _orphan(git, gitproject, project, workarea, path):
3369
+ """Add a worktree, then remove only its worktree record, as an rm that
3370
+ stopped between its two section removals would.
3371
+
3372
+ """
3373
+ _add(git, gitproject, project, path)
3374
+ name = Path(path).name
3375
+ for key in list(_record(git, name)):
3376
+ git.config.rm_items(f"project.worktree.{name}", key)
3377
+ assert _record(git, name) == {}
3378
+ return str(workarea / name)
3379
+
3380
+
3381
+ def test_worktree_rm_removes_an_orphan_path_record(
3382
+ git, gitproject, project, capsys
3383
+ ):
3384
+ workarea = _base()
3385
+ _add(git, gitproject, project, "../wt-keep")
3386
+ path = _orphan(git, gitproject, project, workarea, "../wt-o")
3387
+ assert _path_records(git)[path] == "wt-o"
3388
+ repo = pygit2.Repository(git.get_git_common_dir())
3389
+ refs = sorted(repo.references)
3390
+ worktrees = sorted(repo.list_worktrees())
3391
+
3392
+ command_worktree_rm(git, gitproject, project, _rm_clargs("wt-o"))
3393
+
3394
+ assert (
3395
+ f"Removed the orphan path record project.worktreepath.{path}"
3396
+ in capsys.readouterr().out
3397
+ )
3398
+ # Config only, and another worktree's records stay.
3399
+ assert _path_records(git) == {str(workarea / "wt-keep"): "wt-keep"}
3400
+ assert _record(git, "wt-keep")["path"] == [str(workarea / "wt-keep")]
3401
+ assert (workarea / "wt-o").is_dir()
3402
+ repo = pygit2.Repository(git.get_git_common_dir())
3403
+ assert sorted(repo.references) == refs
3404
+ assert sorted(repo.list_worktrees()) == worktrees
3405
+
3406
+
3407
+ def test_worktree_rm_check_keeps_an_orphan_path_record(
3408
+ git, gitproject, project, capsys
3409
+ ):
3410
+ workarea = _base()
3411
+ path = _orphan(git, gitproject, project, workarea, "../wt-o")
3412
+ before = _repo_state(git)
3413
+
3414
+ command_worktree_rm(
3415
+ git, gitproject, project, _rm_clargs("wt-o", check=True)
3416
+ )
3417
+
3418
+ assert (
3419
+ f"Would remove the orphan path record project.worktreepath.{path}"
3420
+ in capsys.readouterr().out
3421
+ )
3422
+ assert _repo_state(git) == before
3423
+
3424
+
3425
+ def test_worktree_rm_multirepo_refuses_an_orphan_path_record(
3426
+ git, gitproject, project
3427
+ ):
3428
+ workarea = _base()
3429
+ _orphan(git, gitproject, project, workarea, "../wt-o")
3430
+ before = _repo_state(git)
3431
+
3432
+ with pytest.raises(git_project.GitProjectError) as error:
3433
+ command_worktree_rm(
3434
+ git, gitproject, project, _rm_clargs("wt-o", multirepo="tools")
3435
+ )
3436
+
3437
+ assert "git-project has no record of worktree wt-o" in error.value.message
3438
+ assert _repo_state(git) == before
3439
+
3440
+
3441
+ def test_worktree_rm_removes_every_orphan_path_record(
3442
+ git, gitproject, project, capsys
3443
+ ):
3444
+ workarea = _base()
3445
+ path = _orphan(git, gitproject, project, workarea, "../wt-o")
3446
+ other = str(workarea / "wt-o-old")
3447
+ git.config.set_item(f"project.worktreepath.{other}", "worktree", "wt-o")
3448
+ assert _path_records(git) == {path: "wt-o", other: "wt-o"}
3449
+
3450
+ command_worktree_rm(git, gitproject, project, _rm_clargs("wt-o"))
3451
+
3452
+ out = capsys.readouterr().out
3453
+ assert (
3454
+ f"Removed the orphan path record project.worktreepath.{path}, and "
3455
+ f"kept the worktree directory {path}"
3456
+ ) in out
3457
+ assert f"project.worktreepath.{other}," in out
3458
+ assert _path_records(git) == {}
3459
+ assert (workarea / "wt-o").is_dir()
3460
+
3461
+ # Nothing is left to remove, so a second rm reports no record.
3462
+ with pytest.raises(git_project.GitProjectError) as error:
3463
+ command_worktree_rm(git, gitproject, project, _rm_clargs("wt-o"))
3464
+ assert "git-project has no record of worktree wt-o" in error.value.message
3465
+
3466
+
3467
+ def test_worktree_rm_keeps_a_live_path_record_that_names_an_orphan(
3468
+ git, gitproject, project
3469
+ ):
3470
+ workarea = _base()
3471
+ _add(git, gitproject, project, "../wt-live")
3472
+ live = str(workarea / "wt-live")
3473
+ # A hand edit points the live worktree's path record at a gone name.
3474
+ git.config.set_item(f"project.worktreepath.{live}", "worktree", "wt-gone")
3475
+ before = _repo_state(git)
3476
+
3477
+ with pytest.raises(git_project.GitProjectError) as error:
3478
+ command_worktree_rm(git, gitproject, project, _rm_clargs("wt-gone"))
3479
+
3480
+ assert (
3481
+ "git-project has no record of worktree wt-gone" in error.value.message
3482
+ )
3483
+ assert _repo_state(git) == before
3484
+
3485
+
3486
+ def test_worktree_rm_skips_a_multivalued_path_record(git, gitproject, project):
3487
+ workarea = _base()
3488
+ path = str(workarea / "wt-multi")
3489
+ git.config.add_item(f"project.worktreepath.{path}", "worktree", "wt-a")
3490
+ git.config.add_item(f"project.worktreepath.{path}", "worktree", "wt-b")
3491
+ before = _repo_state(git)
3492
+
3493
+ for name in ("wt-none", "wt-a"):
3494
+ with pytest.raises(git_project.GitProjectError) as error:
3495
+ command_worktree_rm(git, gitproject, project, _rm_clargs(name))
3496
+ assert (
3497
+ f"git-project has no record of worktree {name}"
3498
+ in error.value.message
3499
+ )
3500
+ assert _repo_state(git) == before
3501
+
3502
+
3503
+ def test_worktree_rm_removes_a_record_with_no_path(
3504
+ git, gitproject, project, capsys
3505
+ ):
3506
+ workarea = _base()
3507
+ _add(git, gitproject, project, "../wt-keep")
3508
+ _add(git, gitproject, project, "../wt-p")
3509
+ path = str(workarea / "wt-p")
3510
+ # As an rm that stopped part way through the worktree record's keys.
3511
+ git.config.rm_items("project.worktree.wt-p", "path")
3512
+ assert "committish" in _record(git, "wt-p")
3513
+ repo = pygit2.Repository(git.get_git_common_dir())
3514
+ refs = sorted(repo.references)
3515
+ worktrees = sorted(repo.list_worktrees())
3516
+
3517
+ command_worktree_rm(git, gitproject, project, _rm_clargs("wt-p"))
3518
+
3519
+ out = capsys.readouterr().out
3520
+ assert "Removed the worktree record project.worktree.wt-p" in out
3521
+ assert f"Removed the orphan path record project.worktreepath.{path}" in out
3522
+ assert _record(git, "wt-p") == {}
3523
+ assert _path_records(git) == {str(workarea / "wt-keep"): "wt-keep"}
3524
+ # Config only, so the directory, the branch and git's worktree stay.
3525
+ assert (workarea / "wt-p").is_dir()
3526
+ repo = pygit2.Repository(git.get_git_common_dir())
3527
+ assert sorted(repo.references) == refs
3528
+ assert sorted(repo.list_worktrees()) == worktrees
3529
+
3530
+ with pytest.raises(git_project.GitProjectError) as error:
3531
+ command_worktree_rm(git, gitproject, project, _rm_clargs("wt-p"))
3532
+ assert "git-project has no record of worktree wt-p" in error.value.message
3533
+
3534
+
3535
+ def test_worktree_rm_check_keeps_a_record_with_no_path(
3536
+ git, gitproject, project, capsys
3537
+ ):
3538
+ _add(git, gitproject, project, "../wt-p")
3539
+ git.config.rm_items("project.worktree.wt-p", "path")
3540
+ before = _repo_state(git)
3541
+
3542
+ command_worktree_rm(
3543
+ git, gitproject, project, _rm_clargs("wt-p", check=True)
3544
+ )
3545
+
3546
+ assert (
3547
+ "Would remove the worktree record project.worktree.wt-p"
3548
+ in capsys.readouterr().out
3549
+ )
3550
+ assert _repo_state(git) == before
3551
+
3552
+
3553
+ def test_worktree_rm_multirepo_removes_a_record_with_no_path(
3554
+ git, gitproject, project, capsys
3555
+ ):
3556
+ workarea = _base()
3557
+ _add(git, gitproject, project, "../wt-p", multirepo="tools")
3558
+ git.config.rm_items("project.worktree.wt-p", "path")
3559
+ repo = pygit2.Repository(git.get_git_common_dir())
3560
+ refs = sorted(repo.references)
3561
+
3562
+ command_worktree_rm(
3563
+ git, gitproject, project, _rm_clargs("wt-p", multirepo="tools")
3564
+ )
3565
+
3566
+ assert (
3567
+ "Removed the worktree record project.worktree.wt-p"
3568
+ in capsys.readouterr().out
3569
+ )
3570
+ assert _record(git, "wt-p") == {}
3571
+ assert _path_records(git) == {}
3572
+ assert (workarea / "wt-p").is_dir()
3573
+ repo = pygit2.Repository(git.get_git_common_dir())
3574
+ assert sorted(repo.references) == refs
3575
+
3576
+
3577
+ @pytest.mark.parametrize("multirepo", [None, "other"])
3578
+ def test_worktree_rm_multirepo_refuses_a_record_with_no_path(
3579
+ git, gitproject, project, multirepo
3580
+ ):
3581
+ _add(git, gitproject, project, "../wt-p", multirepo="tools")
3582
+ git.config.rm_items("project.worktree.wt-p", "path")
3583
+ if multirepo is None:
3584
+ git.config.rm_items("project.worktree.wt-p", "multirepo")
3585
+ else:
3586
+ git.config.set_item("project.worktree.wt-p", "multirepo", multirepo)
3587
+ before = _repo_state(git)
3588
+
3589
+ with pytest.raises(git_project.GitProjectError) as error:
3590
+ command_worktree_rm(
3591
+ git, gitproject, project, _rm_clargs("wt-p", multirepo="tools")
3592
+ )
3593
+
3594
+ assert (
3595
+ "has no path and does not name multirepo set tools"
3596
+ in error.value.message
3597
+ )
3598
+ assert _repo_state(git) == before
3599
+
3600
+
3601
+ def test_worktree_rm_removes_both_records(git, gitproject, project):
3602
+ workarea = _base()
3603
+ _add(git, gitproject, project, "../wt-a")
3604
+ assert _path_records(git) == {str(workarea / "wt-a"): "wt-a"}
3605
+
3606
+ command_worktree_rm(git, gitproject, project, _rm_clargs("wt-a"))
3607
+
3608
+ assert _record(git, "wt-a") == {}
3609
+ assert _path_records(git) == {}
3610
+ assert not (workarea / "wt-a").exists()
3611
+
3612
+
3298
3613
  def _tag(git, name, committish):
3299
3614
  repo = pygit2.Repository(git.get_git_common_dir())
3300
3615
  repo.create_reference(
@@ -3341,6 +3656,93 @@ def test_worktree_rm_check_changes_nothing(git, gitproject, project):
3341
3656
  assert _repo_state(git) == before
3342
3657
 
3343
3658
 
3659
+ def _drop_branch(git, name):
3660
+ """Delete a branch ref by hand, as a user might outside git-project."""
3661
+ repo = pygit2.Repository(git.get_git_common_dir())
3662
+ repo.references.delete(f"refs/heads/{name}")
3663
+
3664
+
3665
+ def test_worktree_rm_force_removes_a_worktree_whose_branch_is_gone(
3666
+ git, gitproject, project
3667
+ ):
3668
+ workarea = _base()
3669
+ _add(git, gitproject, project, "../wt-g")
3670
+ _drop_branch(git, "wt-g")
3671
+
3672
+ command_worktree_rm(
3673
+ git, gitproject, project, _rm_clargs("wt-g", force=True)
3674
+ )
3675
+
3676
+ assert _record(git, "wt-g") == {}
3677
+ assert _path_records(git) == {}
3678
+ assert not (workarea / "wt-g").exists()
3679
+ repo = pygit2.Repository(git.get_git_common_dir())
3680
+ assert "wt-g" not in repo.list_worktrees()
3681
+
3682
+
3683
+ def test_worktree_rm_force_keeps_the_remote_branch_when_local_is_gone(
3684
+ git, gitproject, project, monkeypatch, capsys
3685
+ ):
3686
+ # This test pushes, so it reaches a pre-push hook. Ignore the user's git
3687
+ # configuration to keep it hermetic.
3688
+ monkeypatch.setenv("GIT_CONFIG_GLOBAL", "/dev/null")
3689
+ monkeypatch.setenv("GIT_CONFIG_NOSYSTEM", "1")
3690
+
3691
+ _add(git, gitproject, project, "../wt-p")
3692
+ # The remote copy may now hold the only copy of the commits.
3693
+ git_project.capture_command("git push origin wt-p:refs/heads/wt-p")
3694
+ assert git.remote_branch_exists("wt-p", "origin")
3695
+ _drop_branch(git, "wt-p")
3696
+ capsys.readouterr()
3697
+
3698
+ command_worktree_rm(
3699
+ git, gitproject, project, _rm_clargs("wt-p", force=True)
3700
+ )
3701
+
3702
+ assert (
3703
+ "Branch wt-p no longer exists locally, so any copies on remotes are "
3704
+ "kept" in capsys.readouterr().out
3705
+ )
3706
+ assert _record(git, "wt-p") == {}
3707
+ assert git.remote_branch_exists("wt-p", "origin")
3708
+
3709
+
3710
+ def test_worktree_rm_check_force_says_remote_copies_would_be_kept(
3711
+ git, gitproject, project, capsys
3712
+ ):
3713
+ _add(git, gitproject, project, "../wt-c")
3714
+ _drop_branch(git, "wt-c")
3715
+ before = _repo_state(git)
3716
+ capsys.readouterr()
3717
+
3718
+ command_worktree_rm(
3719
+ git, gitproject, project, _rm_clargs("wt-c", force=True, check=True)
3720
+ )
3721
+
3722
+ assert (
3723
+ "Branch wt-c no longer exists locally, so any copies on remotes "
3724
+ "would be kept" in capsys.readouterr().out
3725
+ )
3726
+ assert _repo_state(git) == before
3727
+
3728
+
3729
+ def test_worktree_rm_refuses_a_worktree_whose_branch_is_gone(
3730
+ git, gitproject, project
3731
+ ):
3732
+ _add(git, gitproject, project, "../wt-g")
3733
+ _drop_branch(git, "wt-g")
3734
+ before = _repo_state(git)
3735
+
3736
+ with pytest.raises(
3737
+ git_project.GitProjectError,
3738
+ match="Worktree branch wt-g does not exist, "
3739
+ "use -f or --keep-branch to remove the worktree",
3740
+ ):
3741
+ command_worktree_rm(git, gitproject, project, _rm_clargs("wt-g"))
3742
+
3743
+ assert _repo_state(git) == before
3744
+
3745
+
3344
3746
  def test_worktree_menu_hints(git, git_project_runner, script_runner):
3345
3747
  workarea = git.get_working_copy_root()
3346
3748
  os.chdir(workarea)