Simple Forgejo cloning, fetching and pulling tool
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
surdeus 3d7d186d79 refactor: move to the go.mojosa.su/util/fjclone module path
Published as git.mojosa.su/util/fjclone. Code lives on the Forgejo instance and go.mojosa.su
answers discovery for it, so the module path follows the code:

    git.mojosa.su/util/fjclone -> go.mojosa.su/util/fjclone

That is what the vanity path is for: the next time this repository moves,
nothing changes for anyone importing it. go.mojosa.su/util/fjclone keeps resolving
and only the redirect target behind it changes.

Changed: the module line and every internal import. No behaviour change.
2026-10-31 15:02:23 +05:00
.gitignore Init 2026-10-31 10:34:06 +05:00
askpass.go Init 2026-10-31 10:34:06 +05:00
clone.go Init 2026-10-31 10:34:06 +05:00
forgejo.go Init 2026-10-31 10:34:06 +05:00
go.mod refactor: move to the go.mojosa.su/util/fjclone module path 2026-10-31 15:02:23 +05:00
main.go refactor: move to the go.mojosa.su/util/fjclone module path 2026-10-31 15:02:23 +05:00
main_test.go Init 2026-10-31 10:34:06 +05:00
readme.md refactor: move to the go.mojosa.su/util/fjclone module path 2026-10-31 15:02:23 +05:00

fjclone

Mirror every repository of a Forgejo instance into a local tree that keeps the instance's own grouping:

<dest>/<org>/<repo>
<dest>/lib/goss
<dest>/plugin/nextcloud-chat-kanso-app
<dest>/util/xgo

One static Go binary, standard library only. No go get, no runtime deps.

Why the API instead of a plain loop

The org list is the source of truth. Asking /api/v1/orgs and /api/v1/orgs/{org}/repos means:

  • new orgs and new repos are picked up automatically on the next run
  • one request per org instead of scraping the web UI or guessing names
  • archived / fork / mirror / empty repositories can be filtered by metadata the API already returns
  • private repositories appear as soon as a token is supplied

Build

go build -o fjclone .
# or
go install go.mojosa.su/util/fjclone@latest

Use

# everything, into the current directory
fjclone

# everything into ~/dev
fjclone --dest ~/dev

# only selected orgs
fjclone --dest ~/dev lib util plugin

# see what would happen without touching the disk
fjclone --dest ~/dev --dry-run

# just print the repository names
fjclone --list

# refresh existing checkouts, fast-forward only
fjclone --dest ~/dev --pull

Re-running is safe: an existing work tree is fetched (and optionally fast-forwarded) instead of being cloned again. Nothing is ever deleted, and --pull uses --ff-only, so local commits are never rewritten or lost.

Repository state is classified per run:

  • cloned — new checkout
  • up-to-date — fetch produced no new refs
  • updated — fetch or pull moved refs
  • behind (run with --pull) — origin has commits the working tree does not; without --pull the checkout is fetched but deliberately not advanced
  • failed: ... — exit status 1, with git's own error text

Authentication

git.mojosa.su serves public org repositories anonymously, so a bare fjclone already works. For private repositories, create a token at Settings → Applications → Access Tokens (scope read:repository) and pass it via the environment:

export FORGEJO_TOKEN=...        # keep it out of shell history
fjclone --dest ~/dev

The token is handed to git through GIT_ASKPASS: this binary re-executes itself as the askpass helper and prints the token only when git asks for a password. Consequences:

  • the token never appears in argv (visible to ps) or in the clone URL
  • the token is never written to .git/config
  • -c credential.helper= is set for these invocations, so a stored credential.helper=store cannot shadow the explicit token

For SSH instead:

fjclone --protocol ssh --dest ~/dev     # git@git.mojosa.su:lib/x.git
fjclone --protocol ssh --ssh-user git --dest ~/dev

Flags

  • --url — Forgejo base URL (default https://git.mojosa.su, env FORGEJO_URL). A sub-path install such as https://host/gitea is preserved in clone URLs.
  • --token — API token (env FORGEJO_TOKEN).
  • --http-user — username offered to HTTPS auth; the token is the password (default oauth2, env FORGEJO_USER).
  • --ssh-user — SSH user for --protocol ssh (default git).
  • --dest — destination directory (default .).
  • --jobs — parallel git operations (default min(8, NumCPU)).
  • --protocol — https (default) or ssh.
  • --layout — org (default, <dest>/<org>/<repo>) or flat (<dest>/<repo>, with an explicit error on name collisions).
  • --pull — after fetching, fast-forward existing checkouts.
  • --depth — shallow clone depth, 0 for full history.
  • --dry-run — report actions, change nothing.
  • --list — print org/repo lines and exit.
  • --match / --exclude — regexps over the full repository name.
  • --skip-archived, --skip-forks, --skip-mirrors, --skip-empty.
  • --orgs — comma-separated orgs, equivalent to positional arguments.
  • --users — comma-separated users whose repositories to include.
  • --self — include the token owner's repositories, private ones too.
  • --quiet, --verbose, --timeout (per repository, default 15m), --version.

Exit codes: 0 success, 1 at least one repository failed, 2 usage or API error.

Tests

go test ./...

The end-to-end test builds the binary, stands up a stub Forgejo API, serves real bare repositories over dumb HTTP and exercises listing, dry-run, cloning, idempotent re-runs, fast-forward pulls, filtering and flat-layout collisions against the actual git executable.

Limits worth knowing

  • Deleted repositories are not removed locally; the tool never deletes.
  • A directory that exists but is not a git work tree is reported as a failure rather than overwritten.
  • --layout flat refuses ambiguous names instead of guessing.