From 98f7dde49951062c16f260467f4a421c2b2ca0a3 Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Fri, 30 Oct 2026 17:44:35 +0100 Subject: Add repo move: refile a public repo under another section There was no way to change a repo's cgit section short of deleting and recreating it or hand-editing cgitrc, which gitctl exists to prevent. The helper gains move-repo, built from the existing pair: the block's repo.* lines (desc included) are lifted out, remove_repo_block drops them, insert_repo_block files them at the end of the target section's span, so spacing follows the same rules as add and delete. It refuses a repo not in cgit and a missing section, and is a no-op when the repo is already there. The client's repo move ensures the section exists first. Self-tests cover the block move, the file round trip and both refusals. Co-Authored-By: Claude Opus 5.5 --- README.md | 6 +++- completions/gitctl.bash | 7 +++-- gitctl | 11 +++++++ gitctl-helper | 79 +++++++++++++++++++++++++++++++++++++++++++++++++ skills/gitctl/SKILL.md | 2 ++ 5 files changed, 101 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 01b5af7..6168d6b 100644 --- a/README.md +++ b/README.md @@ -160,7 +160,7 @@ ssh git_push info # gitolite access check Or source it from `~/.bashrc`. It completes subcommands and flags, and pulls live values from the server (one ssh call each, only at the relevant position): section names after `--section`, and repo names for the `desc`, - `delete`, and `add-remote` arguments. + `move`, `delete`, and `add-remote` arguments. 6. (Optional) If you drive gitctl through a Claude Code agent, install the bundled skill so the agent knows when and how to use it: ``` @@ -177,6 +177,7 @@ gitctl repo create secrets --private gitctl repo list gitctl repo desc publisher "a new description" gitctl repo add-remote publisher --remote-name origin +gitctl repo move publisher --section Archived gitctl repo delete publisher gitctl sync ``` @@ -207,6 +208,9 @@ for the SSH key, so it is not fully unattended. private repos show too. A `VIS` column marks each `PUB` (present in cgitrc) or `PRIV`; on a terminal public rows are green and private dim. The description comes from each repo's `description` file, truncated to 60 chars. +- `repo move` refiles a public repo under another cgit section, creating the + section first if it is missing. Only cgitrc changes: the gitolite stanza and + the bare repo stay as they are. Idempotent. - `repo delete` removes a repo from all three places it lives: the gitolite stanza (committed and pushed first), the cgit block, and the bare repo, which is MOVED to the server's trash dir (`/var/lib/gitolite3/trash`, diff --git a/completions/gitctl.bash b/completions/gitctl.bash index 497d0a8..520d842 100644 --- a/completions/gitctl.bash +++ b/completions/gitctl.bash @@ -29,13 +29,13 @@ _gitctl() { return fi - # complete repo names for the name argument of desc/delete/add-remote (one + # complete repo names for the name argument of desc/move/delete/add-remote (one # ssh call). Only at the name position and not while typing a flag, so a # stray Tab elsewhere never hits the network. `repo list` prints an aligned # table (no color when not a TTY, as here); the name is the first field, and # we drop the NAME header row. if [[ $cword -eq 3 && $group == repo && $cur != -* \ - && ( $action == desc || $action == delete || $action == add-remote ) ]]; then + && ( $action == desc || $action == move || $action == delete || $action == add-remote ) ]]; then local repos repos=$(gitctl repo list 2>/dev/null | awk 'NR>1 {print $1}') local IFS=$'\n' @@ -50,7 +50,7 @@ _gitctl() { 2) case $group in sections) COMPREPLY=($(compgen -W "list add" -- "$cur")) ;; - repo) COMPREPLY=($(compgen -W "create list desc add-remote delete" -- "$cur")) ;; + repo) COMPREPLY=($(compgen -W "create list desc move add-remote delete" -- "$cur")) ;; esac return ;; esac @@ -59,6 +59,7 @@ _gitctl() { if [[ $cur == -* ]]; then case "$group $action" in "repo create") COMPREPLY=($(compgen -W "--section --private --desc --owner" -- "$cur")) ;; + "repo move") COMPREPLY=($(compgen -W "--section" -- "$cur")) ;; "repo add-remote") COMPREPLY=($(compgen -W "--remote-name" -- "$cur")) ;; esac return diff --git a/gitctl b/gitctl index 7293cf1..5e4e8a7 100755 --- a/gitctl +++ b/gitctl @@ -441,6 +441,14 @@ def build_parser(): "cgit-exposed ones). A VIS column marks each public (in cgit) or " "private; on a terminal public rows are green, private dim.") + rm_ = rep.add_parser( + "move", help="move a public repo to another cgit section", + description="Refile a repo already in cgit under another section, created " + "first if missing. Only cgitrc changes; gitolite and the bare repo " + "are untouched. Idempotent.") + rm_.add_argument("name", help="repo name") + rm_.add_argument("--section", required=True, help="cgit section to move it to") + rdel = rep.add_parser( "delete", help="delete a repo (gitolite + cgit + move bare repo to trash)", description="Remove a repo from all three places it lives: its gitolite stanza " @@ -479,6 +487,9 @@ def main(): return cmd_add_remote(cfg, a.name, a.remote_name) if a.group == "repo" and a.action == "create": return cmd_create(cfg, a) + if a.group == "repo" and a.action == "move": + call_helper(cfg, ["add-section", "--name", a.section]) + call_helper(cfg, ["move-repo", "--url", a.name, "--section", a.section]); return 0 if a.group == "repo" and a.action == "delete": return cmd_delete(cfg, a) return 1 diff --git a/gitctl-helper b/gitctl-helper index f71763e..bc9eaa4 100755 --- a/gitctl-helper +++ b/gitctl-helper @@ -194,6 +194,23 @@ def remove_repo_block(lines, url): return lines[:start] + lines[end:] +def move_repo_block(lines, url, section): + """Refile the repo.url= block under `section`, keeping every repo.*= + line it carries (path, owner, desc). remove_repo_block then + insert_repo_block, so spacing follows the same rules as add and delete. + Raise KeyError(url) if the block is absent, KeyError(section) if the + section is.""" + start = next((i for i, line in enumerate(lines) + if (m := URL_LINE_RE.match(line.rstrip("\n"))) and m.group(1) == url), None) + if start is None: + raise KeyError(url) + end = start + 1 + while end < len(lines) and REPO_KV_RE.match(lines[end].rstrip("\n")) \ + and not URL_LINE_RE.match(lines[end].rstrip("\n")): + end += 1 + return insert_repo_block(remove_repo_block(lines, url), section, lines[start:end]) + + def write_cgitrc_lines(path, lines, backup_dir=None): """Back up then write `path` in place. Not atomic: the helper runs as the git user, which owns /etc/cgitrc but cannot create temp inodes in /etc, so @@ -303,6 +320,28 @@ def do_delete_repo(cgitrc, url, repo_base, trash_dir, run_sync, backup_dir=None) return 0 +def do_move_repo(cgitrc, url, section, run_sync, backup_dir=None): + """Move a repo already in cgit to another existing section. Idempotent: a + repo already under `section` is reported, not rewritten.""" + with open(cgitrc) as f: + lines = f.readlines() + existing = find_repo_section(lines, url) + if existing is None: + print(f"error: repo {url} is not in cgit (private or absent)", file=sys.stderr) + return 1 + if existing == section: + print(f"repo {url} already under section {section}") + return 0 + if section not in list_sections_from(lines): + print(f"error: section {section!r} does not exist; run add-section first", + file=sys.stderr) + return 1 + write_cgitrc_lines(cgitrc, move_repo_block(lines, url, section), backup_dir) + print(f"moved repo {url} from {existing} to {section}") + run_sync() + return 0 + + def _self_test(): assert validate_url("publisher") == "publisher" assert validate_url("a.b_c-1") == "a.b_c-1" @@ -517,6 +556,33 @@ def _self_test(): assert calls == [1] # missing repo -> non-zero, no sync + # move_repo_block: the block, desc line included, lands under the new + # section; the sibling stays; unknown url or section raise. + fx = list(fixture) + fx.insert(3, "repo.desc=CAD work\n") + moved = move_repo_block(fx, "cad-projects", "SlackBuilds") + assert find_repo_section(moved, "cad-projects") == "SlackBuilds" + assert find_repo_section(moved, "my-slackbuilds") == "SlackBuilds" + assert "".join(fx[1:4]) in "".join(moved) + assert remove_repo_block(moved, "cad-projects") == remove_repo_block(fx, "cad-projects") + for args in [("ghost", "SlackBuilds"), ("cad-projects", "Nope")]: + try: + move_repo_block(fx, *args); assert False, args + except KeyError: + pass + # do_move_repo on a real file: moves and syncs once, then is a no-op. + cg = os.path.join(d, "cgitrc") + with open(cg, "w") as f: + f.writelines(fx) + calls.clear() + assert do_move_repo(cg, "cad-projects", "SlackBuilds", lambda: calls.append(1), d) == 0 + assert do_move_repo(cg, "cad-projects", "SlackBuilds", lambda: calls.append(1), d) == 0 + assert calls == [1] + with open(cg) as f: + assert find_repo_section(f.readlines(), "cad-projects") == "SlackBuilds" + assert do_move_repo(cg, "ghost", "SlackBuilds", lambda: calls.append(1), d) == 1 + assert do_move_repo(cg, "cad-projects", "Nope", lambda: calls.append(1), d) == 1 + calls.clear() rc = do_set_desc(os.path.join(d, "ghost.git", "description"), "x", run_sync=lambda: calls.append(1), url="ghost") @@ -607,6 +673,16 @@ def main(): "Idempotent: a piece already gone is skipped, not an error.") dr.add_argument("--url", required=True, help="repo name to delete (the bare repo is .git)") + mv = sub.add_parser("move-repo", + help="move a repo's cgit block to another section, then sync", + description="Refile an existing repo under another existing " + "section in /etc/cgitrc, keeping its repo.* lines. " + "Idempotent: a repo already under the section is " + "left alone.") + mv.add_argument("--url", required=True, help="repo name as it appears in cgit") + mv.add_argument("--section", required=True, + help="existing section to move it to; create it first with add-section") + sub.add_parser("list-repos", help="list every bare repo, tagged public/private", description="Scan the repository base for all bare repos (the full " @@ -643,6 +719,9 @@ def main(): for name, pub, section, desc in list_repos(REPO_BASE, cgit): print(f"{name}\t{'PUB' if pub else 'PRIV'}\t{section}\t{desc}") return 0 + if a.verb == "move-repo": + validate_url(a.url); validate_section(a.section) + return do_move_repo(CGITRC, a.url, a.section, run_sync, BACKUP_DIR) if a.verb == "delete-repo": validate_url(a.url) return do_delete_repo(CGITRC, a.url, REPO_BASE, TRASH_DIR, run_sync, BACKUP_DIR) diff --git a/skills/gitctl/SKILL.md b/skills/gitctl/SKILL.md index 726ce33..674af0d 100644 --- a/skills/gitctl/SKILL.md +++ b/skills/gitctl/SKILL.md @@ -17,6 +17,8 @@ setup" and "Client install") rather than guessing. ## When to use - User wants a NEW repo on the server -> `gitctl repo create` +- MOVE a public repo to another cgit section -> `gitctl repo move --section ` + (creates the section if missing; cgitrc only, idempotent) - DELETE a repo from the server -> `gitctl repo delete` (destructive, always confirms) - List every repo (public AND private) -> `gitctl repo list` - List/add cgit sections (categories) -> `gitctl sections list|add` -- cgit v1.2.3