- Go 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
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.
|
||
| .gitignore | ||
| askpass.go | ||
| clone.go | ||
| forgejo.go | ||
| go.mod | ||
| main.go | ||
| main_test.go | ||
| readme.md | ||
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 checkoutup-to-date— fetch produced no new refsupdated— fetch or pull moved refsbehind (run with --pull)— origin has commits the working tree does not; without--pullthe checkout is fetched but deliberately not advancedfailed: ...— 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 tops) or in the clone URL - the token is never written to
.git/config -c credential.helper=is set for these invocations, so a storedcredential.helper=storecannot 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 (defaulthttps://git.mojosa.su, envFORGEJO_URL). A sub-path install such ashttps://host/giteais preserved in clone URLs.--token— API token (envFORGEJO_TOKEN).--http-user— username offered to HTTPS auth; the token is the password (defaultoauth2, envFORGEJO_USER).--ssh-user— SSH user for--protocol ssh(defaultgit).--dest— destination directory (default.).--jobs— parallel git operations (defaultmin(8, NumCPU)).--protocol—https(default) orssh.--layout—org(default,<dest>/<org>/<repo>) orflat(<dest>/<repo>, with an explicit error on name collisions).--pull— after fetching, fast-forward existing checkouts.--depth— shallow clone depth,0for full history.--dry-run— report actions, change nothing.--list— printorg/repolines 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, default15m),--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 flatrefuses ambiguous names instead of guessing.