# madgit

`madgit` is a CLI tool for mass running commands distributed across multiple Git repositories.
Instead of navigating into each directory, you can run a single `madgit` command to perform operations on every repository within a specified directory.

## Commands

- `--dir <path>`: Top-level option that selects the directory containing repositories.
- `clone --file <path>`: Clones repositories listed in a file.
- `clone --github <owner>`: Clones repositories owned by a GitHub user or organization.
- `clone --gitlab <url-or-owner>`: Clones repositories from a GitLab owner or self-hosted instance.
- `clone --forgejo <url-or-owner>`: Clones repositories from a Forgejo owner or self-hosted instance.
- `repos --github <owner>`: Prints GitHub repository URLs for a user or organization.
- `repos --gitlab <url-or-owner>`: Prints GitLab repository URLs for an owner.
- `repos --forgejo <url-or-owner>`: Prints Forgejo repository URLs for an owner.
- `status`: Runs `git status` in each repository (headers are color-coded: red=needs-sync, yellow=changes, cyan=clean).
- `checkout-main`: Checks out `main` in each repository, falling back to `master` when needed.
- `exec -- <command>`: Executes any arbitrary command in each repository.
- `<git-subcommand> [args...]`: Unknown top-level commands are forwarded as `git <subcommand> [args...]` in each repository.
- `no-git`: Lists subdirectories which are not git repositories.

## Examples

```sh
# Clone repositories from a file to ~/projects
madgit --dir ~/projects clone --file repos.txt

# Clone all repos from a GitHub organization
madgit --dir ~/projects clone --github gotopkg

# Clone from a GitLab user
madgit --dir ~/projects clone --gitlab wykwit

# Print GitLab repositories for an owner
madgit repos --gitlab wykwit

# List all repositories for a Forgejo owner
madgit repos --forgejo https://git.coopcloud.tech/kotec

# Pull all repositories in current directory
madgit pull

# Stash worktree changes in every repository
madgit --dir ~/projects stash

# Check status of all repositories in current directory
madgit status

# Execute custom command in all repositories
madgit --dir ~/projects exec -- git log --oneline -5

# Forward an unsupported command to git in each repository
madgit --dir ~/projects rev-parse --show-toplevel

# Check out the primary branch across mixed repositories
madgit --dir ~/projects checkout-main
```

## Remote auth

Remote repository discovery works without credentials for public owners, but you can provide API tokens for private repositories or to avoid rate limits.

- `GITHUB_TOKEN`: GitHub API token used for `clone --github` and `repos --github`
- `GITLAB_TOKEN`: GitLab API token used for `clone --gitlab` and `repos --gitlab`
- `FORGEJO_TOKEN`: Forgejo API token used for `clone --forgejo` and `repos --forgejo`

With `GITHUB_TOKEN` set, GitHub discovery uses the authenticated repository listing and filters it to the requested owner, so private repositories should be included when your token can access them.

Example:

```sh
GITHUB_TOKEN=ghp_xxx madgit repos --github RustCrypto
```

## Installation

### with cargo

`madgit` is available on crates.io: <https://crates.io/crates/madgit>

```sh
cargo install madgit
```

### from AUR

```sh
paru -S madgit
```

### from source

To build `madgit` from source, you'll need the [Rust toolchain](https://www.rust-lang.org/tools/install).

1. Clone the repository:
    ```sh
    git clone https://gitlab.com/wykwit/madgit.git && cd madgit
    ```

2. Build the project:
    ```sh
    cargo build --release
    ```

3. You will find the executable in:
    ```sh
    ./target/release/madgit
    ```

## License

This project is licensed under the MIT License.

```
MIT License

Copyright (c) 2025 wykwit

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.
```
