1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""Command-line entry point for pygittools (pgt)."""
from __future__ import annotations
import argparse
import sys
from pathlib import Path
import pygittools
from pygittools.tags_sync import delete_task_remote, format_sync_result, sync_board_tags
from pygittools.tasks import TASK_REF_PREFIX, delete_task_local
from pygittools.tasks_query import open_repository, resolve_repo_location
from pygittools.tui import run_tui
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
prog="pgt",
description=pygittools.__description__,
)
parser.add_argument(
"--repo",
type=Path,
help="Path to a git repository (default: discover from the current directory)",
)
parser.add_argument(
"--version",
action="version",
version=f"%(prog)s {pygittools.__version__}",
)
subparsers = parser.add_subparsers(dest="command")
tags_parser = subparsers.add_parser("tags", help="Task tag operations")
tags_subparsers = tags_parser.add_subparsers(dest="tags_command", required=True)
sync_parser = tags_subparsers.add_parser("sync", help="Pull and push board task tags with a remote")
sync_parser.add_argument(
"--remote",
default="origin",
help="Remote name to sync with (for example origin)",
)
sync_parser.add_argument(
"--board",
default="Tasks",
help="Board name under refs/tags/boards/ (default: Tasks)",
)
delete_parser = tags_subparsers.add_parser("delete-task", help="Delete a task tag locally and on a remote")
delete_parser.add_argument(
"task",
help="Task ref or ID (for example refs/tags/tasks/alpha or alpha)",
)
delete_parser.add_argument(
"--remote",
default="origin",
help="Remote name to delete from (for example origin)",
)
return parser
def _run_tags_sync(repo_path: Path | None, remote: str, board: str) -> int:
location = resolve_repo_location(repo_path=repo_path)
repo = open_repository(location)
try:
result = sync_board_tags(repo, remote, board)
except (KeyError, RuntimeError, ValueError) as exc:
print(exc, file=sys.stderr)
return 1
print(format_sync_result(result))
return 0
def _normalize_task_ref(task: str) -> str:
if task.startswith("refs/"):
return task
return f"{TASK_REF_PREFIX}{task}"
def _run_task_delete(repo_path: Path | None, remote: str | None, task: str) -> int:
location = resolve_repo_location(repo_path=repo_path)
repo = open_repository(location)
task_ref = _normalize_task_ref(task)
delete_task_local(repo, task_ref)
if not remote:
return 0
try:
delete_task_remote(repo, remote, task_ref)
except (KeyError, RuntimeError, ValueError) as exc:
print(exc, file=sys.stderr)
return 1
return 0
def main(argv: list[str] | None = None) -> int:
args_list = sys.argv[1:] if argv is None else argv
if not args_list:
return run_tui()
parser = build_parser()
args = parser.parse_args(args_list)
if args.command == "tags" and args.tags_command == "sync":
return _run_tags_sync(args.repo, args.remote, args.board)
if args.command == "tags" and args.tags_command == "delete-task":
return _run_task_delete(args.repo, args.remote, args.task)
return run_tui(args.repo)
if __name__ == "__main__":
raise SystemExit(main())