diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c3a1f43 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +_build/ +_opam/ +_install/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0e1c8ab --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright (c) 2026 Braeden Sowinski + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index 38322df..4003b16 100644 --- a/README.md +++ b/README.md @@ -1,63 +1,65 @@ # git-fleet -Small collection of Git utilities I use to keep my local repositories configured between my self-hosted Gitea instance and GitHub. +A Git extension for managing repositories across multiple Git hosts and destinations. -Gitea is my primary Git server. GitHub is kept as a secondary/public copy of my repositories. +`git-fleet` is a native CLI I'm building to simplify the Git workflows I use across self-hosted Git services and public hosts. The project started as a few personal scripts for configuring dual-push repositories and is being rebuilt in OCaml as a single, reusable command. -## `setup_dual_push.py` +## Status -Scans my local Git repositories and updates repos owned by `SowinskiBraeden` to use: +**v0.1.0: CLI foundation** -- **Fetch:** `git.sowinski.dev` -- **Push:** `git.sowinski.dev` -- **Push:** `github.com` +Initial release is just figuring out the project structure and command-line interface: -Other repositories and repositories without a recognized origin are skipped. +- Native OCaml executable +- Dune build system +- Cmdliner command interface +- `git fleet` Git subcommand integration +- Automated tests -Useful for configuring existing projects or setting up a new machine. +Remote and multi-destination management will begin in v0.2.0. -## `git-new` +## Usage -Small command for initializing new projects with my normal Git configuration. +Once `git-fleet` is installed somewhere on your `PATH`, Git automatically exposes it as a subcommand: -Run it from inside the project directory: - -```bash -git-new +```sh +git fleet --help +git fleet --version ``` -It will: +You can also invoke the executable directly: -Refuse to run if the directory is already inside a Git repository -Initialize a new Git repository -Set the default branch to main -Set Gitea as the fetch origin -Set Gitea and GitHub as push origins - -The repo name does not need to match the local directory name. - -## Setup - -Make the script executable: - -```bash -chmod +x git-new +```sh +git-fleet --help ``` -Symlink it into /usr/local/bin: +## Development -```bash -sudo ln -s "$(pwd)/git-new" /usr/local/bin/git-new +Install the project dependencies: + +```sh +opam install . --deps-only --with-test ``` -It can then be used from any directory: +Build: -```bash -mkdir my-project -cd my-project - -git-new my-repo-name +```sh +dune build ``` -The repositories must already exist on Gitea and GitHub before pushing. +Run the tests: +```sh +dune runtest +``` + +Run the development executable: + +```sh +dune exec git-fleet -- --help +dune exec git-fleet -- --version +``` + +## License + +MIT diff --git a/bin/dune b/bin/dune new file mode 100644 index 0000000..08793a2 --- /dev/null +++ b/bin/dune @@ -0,0 +1,5 @@ +(executable + (name main) + (public_name git-fleet) + (package git-fleet) + (libraries git_fleet cmdliner)) diff --git a/bin/main.ml b/bin/main.ml new file mode 100644 index 0000000..f7a093a --- /dev/null +++ b/bin/main.ml @@ -0,0 +1,2 @@ +let () = + exit (Cmdliner.Cmd.eval Git_fleet.Cli.cmd) diff --git a/dune-project b/dune-project new file mode 100644 index 0000000..3854561 --- /dev/null +++ b/dune-project @@ -0,0 +1,27 @@ +(lang dune 3.16) + +(name git-fleet) +(version 0.1.0) + +(generate_opam_files true) + +(source + (github SowinskiBraeden/git-fleet)) + +(authors "Braeden Sowinski") +(maintainers "Braeden Sowinski ") + +(license MIT) + +(package + (name git-fleet) + (synopsis "Manage Git repositories across multiple destinations") + (description + "git-fleet is a Git extension for managing repositories across multiple Git hosts and push destinations.") + (depends + (ocaml (>= 5.1)) + (dune (>= 3.16)) + (cmdliner (>= 1.3.0)) + (alcotest :with-test)) + (tags + ("git" "cli" "developer-tools"))) diff --git a/git-fleet.opam b/git-fleet.opam new file mode 100644 index 0000000..25853d2 --- /dev/null +++ b/git-fleet.opam @@ -0,0 +1,34 @@ +# This file is generated by dune, edit dune-project instead +opam-version: "2.0" +version: "0.1.0" +synopsis: "Manage Git repositories across multiple destinations" +description: + "git-fleet is a Git extension for managing repositories across multiple Git hosts and push destinations." +maintainer: ["Braeden Sowinski "] +authors: ["Braeden Sowinski"] +license: "MIT" +tags: ["git" "cli" "developer-tools"] +homepage: "https://github.com/SowinskiBraeden/git-fleet" +bug-reports: "https://github.com/SowinskiBraeden/git-fleet/issues" +depends: [ + "ocaml" {>= "5.1"} + "dune" {>= "3.16" & >= "3.16"} + "cmdliner" {>= "1.3.0"} + "alcotest" {with-test} + "odoc" {with-doc} +] +build: [ + ["dune" "subst"] {dev} + [ + "dune" + "build" + "-p" + name + "-j" + jobs + "@install" + "@runtest" {with-test} + "@doc" {with-doc} + ] +] +dev-repo: "git+https://github.com/SowinskiBraeden/git-fleet.git" diff --git a/git-new b/git-new deleted file mode 100755 index b27c216..0000000 --- a/git-new +++ /dev/null @@ -1,87 +0,0 @@ -#!/usr/bin/env python3 - -import subprocess -import sys -from pathlib import Path - -OWNER = "SowinskiBraeden" -GITEA_HOST = "git.sowinski.dev" -GITHUB_HOST = "github.com" - - -def git(*args: str, check: bool = True) -> subprocess.CompletedProcess: - return subprocess.run( - ["git", *args], - text=True, - capture_output=True, - check=check, - ) - - -def main(): - if len(sys.argv) != 2: - print("Usage: git-new ") - sys.exit(1) - - repo_name = sys.argv[1].strip() - - if not repo_name: - print("Error: repository name cannot be empty.") - sys.exit(1) - - current_dir = Path.cwd() - - # Refuse to run anywhere inside an existing Git repository. - existing_repo = git("rev-parse", "--show-toplevel", check=False) - - if existing_repo.returncode == 0: - repo_root = existing_repo.stdout.strip() - - print("Error: already inside a Git repository.") - print(f"Repository root: {repo_root}") - sys.exit(1) - - gitea_url = f"git@{GITEA_HOST}:{OWNER}/{repo_name}.git" - github_url = f"git@{GITHUB_HOST}:{OWNER}/{repo_name}.git" - - print(f"Initializing: {current_dir}") - print(f"Remote repo: {OWNER}/{repo_name}") - print() - - # Initialize repository. - git("init") - - # Use main as the initial branch. - git("branch", "-m", "main") - - # Gitea is canonical: fetch + first push destination. - git("remote", "add", "origin", gitea_url) - - git( - "remote", - "set-url", - "--push", - "origin", - gitea_url, - ) - - # GitHub is the secondary push destination. - git( - "remote", - "set-url", - "--add", - "--push", - "origin", - github_url, - ) - - print("Repository initialized.") - print() - print("Remotes:") - print(f" fetch -> {gitea_url}") - print(f" push -> {gitea_url}") - print(f" push -> {github_url}") - - -if __name__ == "__main__": - main() diff --git a/lib/cli.ml b/lib/cli.ml new file mode 100644 index 0000000..cafede2 --- /dev/null +++ b/lib/cli.ml @@ -0,0 +1,7 @@ +open Cmdliner + +let cmd = + let doc = "Manage Git repositories across multiple destinations." in + Cmd.v + (Cmd.info "git-fleet" ~version:Version.value ~doc) + (Term.const ()) diff --git a/lib/dune b/lib/dune new file mode 100644 index 0000000..ad624e0 --- /dev/null +++ b/lib/dune @@ -0,0 +1,4 @@ +(library + (name git_fleet) + (public_name git-fleet.lib) + (libraries cmdliner)) diff --git a/lib/version.ml b/lib/version.ml new file mode 100644 index 0000000..0ccbc3e --- /dev/null +++ b/lib/version.ml @@ -0,0 +1 @@ +let value = "0.1.0" diff --git a/setup_dual_push.py b/setup_dual_push.py deleted file mode 100755 index 1b86444..0000000 --- a/setup_dual_push.py +++ /dev/null @@ -1,210 +0,0 @@ -#!/usr/bin/env python3 - -import re -import subprocess -from pathlib import Path - -PROJECTS_DIR = Path.home() / "Projects" - -OWNER = "SowinskiBraeden" -GITHUB_HOST = "github.com" -GITEA_HOST = "git.sowinski.dev" - - -def git(repo: Path, *args: str, check: bool = True) -> subprocess.CompletedProcess: - return subprocess.run( - ["git", "-C", str(repo), *args], - text=True, - capture_output=True, - check=check, - ) - - -def find_git_repositories(root: Path) -> list[Path]: - """Find all Git repositories recursively below root.""" - repositories = set() - - for git_path in root.rglob(".git"): - if git_path.is_dir() or git_path.is_file(): - repositories.add(git_path.parent) - - return sorted(repositories) - - -def parse_repo_name(url: str) -> str | None: - """ - Extract the repository name if the URL belongs to OWNER on either - GitHub or our Gitea instance. - - Supported examples: - - git@github.com:SowinskiBraeden/example.git - https://github.com/SowinskiBraeden/example.git - - git@git.sowinski.dev:SowinskiBraeden/example.git - https://git.sowinski.dev/SowinskiBraeden/example.git - """ - - hosts = [ - re.escape(GITHUB_HOST), - re.escape(GITEA_HOST), - ] - - host_pattern = "(?:" + "|".join(hosts) + ")" - - patterns = [ - rf"^git@{host_pattern}:{re.escape(OWNER)}/([^/]+?)(?:\.git)?$", - rf"^https?://{host_pattern}/{re.escape(OWNER)}/([^/]+?)(?:\.git)?/?$", - rf"^ssh://git@{host_pattern}/{re.escape(OWNER)}/([^/]+?)(?:\.git)?/?$", - ] - - for pattern in patterns: - match = re.match(pattern, url) - - if match: - return match.group(1) - - return None - - -def get_origin(repo: Path) -> str | None: - result = git( - repo, - "remote", - "get-url", - "origin", - check=False, - ) - - if result.returncode != 0: - return None - - return result.stdout.strip() - - -def configure_repository(repo: Path) -> bool: - origin = get_origin(repo) - - if origin is None: - print(f"SKIP {repo}") - print(" No origin remote") - return False - - repo_name = parse_repo_name(origin) - - if repo_name is None: - print(f"SKIP {repo}") - print(f" Unrecognized/non-{OWNER} origin:") - print(f" {origin}") - return False - - gitea_url = f"git@{GITEA_HOST}:{OWNER}/{repo_name}.git" - github_url = f"git@{GITHUB_HOST}:{OWNER}/{repo_name}.git" - - print(f"UPDATE {repo}") - print(f" Repository: {OWNER}/{repo_name}") - - # - # FETCH - # - # Gitea is authoritative. - # - git( - repo, - "remote", - "set-url", - "origin", - gitea_url, - ) - - print(f" fetch -> {gitea_url}") - - # - # PUSH - # - # Reset the push URL list so running this utility repeatedly cannot - # create duplicate push destinations. - # - current_push_urls = git( - repo, - "remote", - "get-url", - "--all", - "--push", - "origin", - ).stdout.splitlines() - - # Delete existing explicit push URLs. - # - # Git returns an error if there are no explicit push URLs matching, - # which is harmless here. - for push_url in current_push_urls: - git( - repo, - "remote", - "set-url", - "--delete", - "--push", - "origin", - re.escape(push_url), - check=False, - ) - - # Establish Gitea as the primary push destination. - git( - repo, - "remote", - "set-url", - "--push", - "origin", - gitea_url, - ) - - # GitHub remains the public/legacy destination. - git( - repo, - "remote", - "set-url", - "--add", - "--push", - "origin", - github_url, - ) - - print(f" push -> {gitea_url}") - print(f" push -> {github_url}") - print() - - return True - - -def main(): - if not PROJECTS_DIR.exists(): - raise SystemExit( - f"Projects directory does not exist: {PROJECTS_DIR}" - ) - - repositories = find_git_repositories(PROJECTS_DIR) - - print(f"Searching: {PROJECTS_DIR}") - print(f"Found {len(repositories)} Git repositories.") - print() - - configured = 0 - skipped = 0 - - for repo in repositories: - if configure_repository(repo): - configured += 1 - else: - skipped += 1 - - print("=" * 60) - print("Finished") - print(f"Configured: {configured}") - print(f"Skipped: {skipped}") - print(f"Total: {len(repositories)}") - - -if __name__ == "__main__": - main() diff --git a/test/dune b/test/dune new file mode 100644 index 0000000..6ece985 --- /dev/null +++ b/test/dune @@ -0,0 +1,3 @@ +(test + (name test_git_fleet) + (libraries git_fleet alcotest)) diff --git a/test/test_git_fleet.ml b/test/test_git_fleet.ml new file mode 100644 index 0000000..fad4ec8 --- /dev/null +++ b/test/test_git_fleet.ml @@ -0,0 +1,18 @@ +let test_version () = + Alcotest.(check string) + "version" + "0.1.0" + Git_fleet.Version.value + +let () = + Alcotest.run + "git-fleet" + [ + ( "version", + [ + Alcotest.test_case + "reports 0.1.0" + `Quick + test_version; + ] ); + ]