worktree.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. """``hermes worktree`` subcommand parser."""
  2. from __future__ import annotations
  3. def build_worktree_parser(subparsers) -> None:
  4. """Attach the ``worktree`` subcommand to ``subparsers``."""
  5. worktree_parser = subparsers.add_parser(
  6. "worktree", help="Audit and reclaim accumulated git worktrees and merged branches",
  7. description="Audit, reclaim and restore the .worktrees/ trees Hermes sessions "
  8. "accumulate. Idle trees with uncommitted changes or unpushed commits are "
  9. "archived to refs/hermes/archive/<tree> before removal "
  10. "(worktrees.archive_dirty); `restore` brings one back. In-use trees are "
  11. "never touched. See: "
  12. "https://hermes-agent.nousresearch.com/docs/user-guide/git-worktrees")
  13. worktree_subparsers = worktree_parser.add_subparsers(dest="worktree_action")
  14. worktree_list = worktree_subparsers.add_parser(
  15. "list", aliases=["ls", "audit"],
  16. help="Classify every tree: age, size, verdict, reason (default action)")
  17. worktree_list.add_argument("--repo", help="Repo root (default: current repo)")
  18. worktree_list.add_argument(
  19. "--json", action="store_true",
  20. help="Machine-readable audit output (trees, external trees, branches)")
  21. worktree_list.add_argument(
  22. "--older-than", type=float, metavar="DAYS", dest="older_than",
  23. help="Treat reapable trees younger than DAYS as keep")
  24. worktree_prune = worktree_subparsers.add_parser(
  25. "prune", help="Remove safe trees and delete fully-merged local branches")
  26. worktree_prune.add_argument("--repo", help="Repo root (default: current repo)")
  27. worktree_prune.add_argument(
  28. "--json", action="store_true",
  29. help="Machine-readable result (actions taken/planned, preserved trees)")
  30. worktree_prune.add_argument(
  31. "--older-than", type=float, metavar="DAYS", dest="older_than",
  32. help="Only reap trees idle for at least DAYS days (safety gates still apply)")
  33. worktree_prune.add_argument(
  34. "--dry-run", action="store_true", help="Show the plan without changing anything")
  35. worktree_prune.add_argument(
  36. "--trees-only", action="store_true",
  37. help="Only remove worktrees; leave local branches alone")
  38. worktree_prune.add_argument(
  39. "--branches-only", action="store_true",
  40. help="Only delete merged local branches; leave worktrees alone")
  41. worktree_restore = worktree_subparsers.add_parser(
  42. "restore", help="Recreate an archived worktree (tracked, staged and untracked files)")
  43. worktree_restore.add_argument("name", help="Archive name or tree name (see `hermes worktree list`)")
  44. worktree_restore.add_argument("--path", help="Restore here instead of the original path")
  45. worktree_restore.add_argument("--repo", help="Repo root (default: current repo)")
  46. def _dispatch_worktree(_args):
  47. from hermes_cli.worktree_cmd import cmd_worktree
  48. # argparse aliases set dest to the literal typed string ("ls"/"audit").
  49. action = getattr(_args, "worktree_action", None)
  50. if action in ("ls", "audit"):
  51. _args.worktree_action = "list"
  52. return cmd_worktree(_args)
  53. worktree_parser.set_defaults(func=_dispatch_worktree)