diff options
Diffstat (limited to 'gitctl-helper')
| -rwxr-xr-x | gitctl-helper | 79 |
1 files changed, 79 insertions, 0 deletions
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=<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 <url>.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) |
