- Go 96.3%
- Shell 2.5%
- Just 1.2%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
|
|
||
| .crow | ||
| .gitsv | ||
| cmd | ||
| examples | ||
| pkg | ||
| tools | ||
| .commitlintrc.json | ||
| .editorconfig | ||
| .editorconfig-checker.json | ||
| .gitignore | ||
| .golangci.yaml | ||
| .markdownlint.yaml | ||
| .markdownlintignore | ||
| .pre-commit-config.yaml | ||
| .prettierignore | ||
| .prettierrc.json | ||
| .yamllint.yaml | ||
| go.mod | ||
| go.sum | ||
| install.sh | ||
| Justfile | ||
| LICENSE.md | ||
| README.md | ||
| renovate.json | ||
forgejo-batch-migrate
Batch-migrate repositories from GitHub, GitLab or another Forgejo/Gitea instance into a target Forgejo instance.
The tool wraps Forgejo's own migration API (POST /api/v1/repos/migrate) and exposes every option the web migration assistant offers.
It can migrate a whole source organization/group or user in one go, or a hand-picked list of repositories, and it makes conservative rate-limiting assumptions so large batches do not overwhelm either forge.
Features
- Sources: GitHub, GitLab, Forgejo, Gitea, and plain
gitclone URLs. - Migrate an entire org/group (
--source-org), a whole user (--source-user), or individual repos (--source-repo, repeatable). - All assistant options: issues, pull requests, labels, milestones, releases, wiki, LFS, mirror, private, and mirror interval.
- Content options default to on; pass
--issues=false(and friends) to skip. - Private source repositories are always created private on the target, regardless of the
--privatedefault. - Graceful degradation: options are automatically skipped for repositories whose source has that unit disabled (e.g. requesting releases when the source repo has releases turned off), so one disabled unit does not fail the whole migration.
- Interrupted migrations are recovered rather than guessed at: a migration whose call times out or fails with a 5xx is reconciled against the target, and the leftovers a failed migration leaves behind can be cleared with
--replace-incomplete,--adopt-existing-filesor--replace-existing. - A 5xx a proxy in front of the target produced is read as the interruption it is, not as a failed migration, and
--migrate-retriesgives an attempt that left nothing behind another go. - Merged pull requests from a Forgejo or Gitea source are checked after the migration, and a warning names every one the target imported with the wrong merge base (see Merged pull requests showing no commits).
- Progress while a migration runs: the target repository is polled and each new batch of milestones, labels, releases, issues and pull requests is reported as it lands.
- Smart rate limiting: a global token bucket, a bounded worker pool, and automatic backoff on HTTP 429 (honoring
Retry-After, as seconds or as a date, and GitHub'sX-RateLimit-*). - One-shot flags for quick runs, or a YAML config describing many jobs for batch/cron use.
--dry-runresolves and prints the full plan without writing anything.
Install
Install script (recommended)
The install script detects your OS and architecture, downloads and verifies the matching archive from the latest release, extracts the binary into a local bin directory (~/.local/bin by default), and creates an fbm alias next to it:
curl -fsSL https://codefloe.com/codefloe/forgejo-batch-migrate/raw/branch/main/install.sh | sh
Override the defaults with environment variables:
# pin a specific version
curl -fsSL https://codefloe.com/codefloe/forgejo-batch-migrate/raw/branch/main/install.sh | VERSION=v0.1.0 sh
# install somewhere else
curl -fsSL https://codefloe.com/codefloe/forgejo-batch-migrate/raw/branch/main/install.sh | INSTALL_DIR=/usr/local/bin sh
# skip creating the `fbm` alias
curl -fsSL https://codefloe.com/codefloe/forgejo-batch-migrate/raw/branch/main/install.sh | NO_ALIAS=1 sh
If the install directory is not on your PATH, the script prints the line to add.
Manual download
Download a pre-built archive from the latest release.
Archives are published for Linux, macOS, and Windows (amd64 and arm64) as .tar.gz (Unix) and .zip (Windows), alongside a SHA256SUMS.txt for verification:
# Linux (amd64)
curl -LO https://codefloe.com/codefloe/forgejo-batch-migrate/releases/download/v0.1.0/forgejo-batch-migrate-linux-amd64.tar.gz
# Linux (arm64)
# curl -LO https://codefloe.com/codefloe/forgejo-batch-migrate/releases/download/v0.1.0/forgejo-batch-migrate-linux-arm64.tar.gz
# macOS (Intel)
# curl -LO https://codefloe.com/codefloe/forgejo-batch-migrate/releases/download/v0.1.0/forgejo-batch-migrate-darwin-amd64.tar.gz
# macOS (Apple Silicon)
# curl -LO https://codefloe.com/codefloe/forgejo-batch-migrate/releases/download/v0.1.0/forgejo-batch-migrate-darwin-arm64.tar.gz
tar -xzf forgejo-batch-migrate-linux-amd64.tar.gz
chmod +x forgejo-batch-migrate
# Windows (amd64): download the .zip and extract it
# curl -LO https://codefloe.com/codefloe/forgejo-batch-migrate/releases/download/v0.1.0/forgejo-batch-migrate-windows-amd64.zip
Or install with the Go toolchain:
go install codefloe.com/codefloe/forgejo-batch-migrate/cmd@latest
Build from source
Build from source with just:
just build
# binary at build/forgejo-batch-migrate
Or with the Go toolchain directly:
go build -o forgejo-batch-migrate ./cmd
Authentication
Provide a token for both forges. In order of preference:
- Environment variables
SOURCE_TOKEN/FORGEJO_TARGET_TOKEN(recommended). - Token files:
--source-token-file/--target-token-file(e.g. a mounted secret). - Literal flags
--source-token/--target-token(discouraged: the value is visible in the process list and shell history, so the tool warns when you use them).
- The target token is required for any real migration (a run without it fails fast;
--dry-runis exempt). - A source token is strongly recommended even for public repositories: unauthenticated API access is rate-limited aggressively (e.g. GitHub allows only 60 requests/hour), so listing or migrating more than a few repos will otherwise stall. The tool warns when a source token is missing.
- Scopes on the target token: migrating needs
write:repository(pluswrite:organizationfor--create-org). Reading back what a migration has imported additionally needsread:issue, which Forgejo requires on the issue, label and milestone endpoints. Without it the migration still runs; the tool warns once and reports no issue progress. See Progress reporting.
Usage
Migrate a whole GitHub organization
export SOURCE_TOKEN=ghp_...
export FORGEJO_TARGET_TOKEN=...
forgejo-batch-migrate \
--source-type github \
--source-org acme \
--target-url https://codefloe.com \
--target-owner acme-mirror \
--create-org
Migrate a whole GitLab group (self-hosted)
Groups are addressed by their full path; subgroups are included automatically.
export SOURCE_TOKEN=glpat-...
forgejo-batch-migrate \
--source-type gitlab \
--source-url https://gitlab.example.com \
--source-org platform/backend \
--target-url https://codefloe.com \
--target-owner backend \
--create-org
Migrate a whole Forgejo organization
export SOURCE_TOKEN=...
export FORGEJO_TARGET_TOKEN=...
forgejo-batch-migrate \
--source-type forgejo \
--source-url https://codeberg.org \
--source-org acme \
--target-url https://codefloe.com \
--target-owner acme \
--create-org
Mirror selected repos from a single user
Pick individual repositories owned by one user with --source-repo, and use --mirror to keep the destination in sync with the source instead of a one-time copy.
forgejo-batch-migrate \
--source-type forgejo \
--source-url https://codefloe.com \
--source-repo alice/dotfiles \
--source-repo alice/notes \
--target-url https://codefloe.com \
--target-owner alice \
--mirror --mirror-interval 24h0m0s
Migrate hand-picked repositories, code only
Pass --source-repo multiple times and switch off the metadata you do not want.
forgejo-batch-migrate \
--source-type github \
--source-repo acme/api \
--source-repo acme/web \
--target-url https://codefloe.com \
--target-owner mirrors \
--issues=false --pull-requests=false --wiki=false
Migrate arbitrary Git URLs
--source-type git mirrors any plain Git repository; pass full clone URLs.
forgejo-batch-migrate \
--source-type git \
--source-repo https://git.example.com/team/tool.git \
--target-url https://codefloe.com \
--target-owner mirrors
Preview without migrating
Add --dry-run to any invocation to list the resolved repositories and per-repo options without contacting the target for writes.
This is the safe way to check what a whole-org migration would do before running it.
forgejo-batch-migrate \
--source-type github --source-org acme \
--target-url https://codefloe.com --target-owner mirrors \
--dry-run
Options
| Flag | Config key | Default | Meaning |
|---|---|---|---|
--issues |
issues |
true |
Migrate issues. |
--pull-requests |
pull_requests |
true |
Migrate pull requests. |
--labels |
labels |
true |
Migrate labels. |
--milestones |
milestones |
true |
Migrate milestones. |
--releases |
releases |
true |
Migrate releases. |
--wiki |
wiki |
true |
Migrate the wiki. |
--lfs |
lfs |
false |
Migrate Git LFS objects. |
--mirror |
mirror |
false |
Set up the repo as a pull mirror. |
--private |
private |
false |
Force the destination repo to be private. |
--mirror-interval |
mirror_interval |
8h0m0s |
Mirror sync interval. |
--concurrency |
run.concurrency |
2 |
Concurrent migrations. |
--rate-limit |
run.rate_limit |
30 |
Max API requests per minute. |
--skip-existing |
run.skip_existing |
true |
Skip repos that already exist on the target. |
| Flag | Config key | Default | Meaning |
|---|---|---|---|
--migrate-timeout |
run.migrate_timeout |
1h |
Budget for one migration, and for reconciling it if the call times out. |
--reconcile-interval |
run.reconcile_interval |
30s |
How often the target is polled while reconciling. |
--progress-interval |
run.progress_interval |
30s |
How often a running migration reports what it has imported; 0 is off. |
--migrate-retries |
run.migrate_retries |
0 |
Extra attempts for a migration that was cut short and left nothing behind. |
--replace-incomplete |
run.replace_incomplete |
false |
Delete an empty leftover from an interrupted migration, then migrate again. |
--adopt-existing-files |
run.adopt_existing_files |
false |
Adopt repository files left on the target's disk. |
--replace-existing |
run.replace_existing |
false |
Delete a repository that already holds content, then migrate again. |
Progress reporting
A migration of a busy repository holds one request open for minutes, and Forgejo says nothing until it returns.
It does track its own progress internally, but only hands those messages to the web UI's task list: the API handler passes the progress callback a nil messenger, and no endpoint exposes it.
So the tool watches the destination fill up instead.
Every --progress-interval (30s by default, 0 turns it off) it looks at the target repository and logs a line whenever something has moved:
3:02PM INF git data imported, importing content repo=mirrors/acme-api size=187
3:04PM INF import progress issues=150 labels=12 milestones=3 phase=issues pull_requests=0 releases=8 repo=mirrors/acme-api
The first line marks the end of the clone, which on a large repository is the longest silence of the run.
Afterwards phase names the stage the server has reached.
Forgejo imports in a fixed order, milestones, then labels, releases, issues with their comments, and finally pull requests with their reviews, so the furthest unit that is still growing says where the migration is.
Each pass is one request for the repository itself, and while it is still being cloned that is the only one.
Once content starts arriving, the units are counted with a single-item page read for its X-Total-Count header; units the run does not migrate are never counted, and a stage the import has already passed is dropped from the polling.
Three limits are worth knowing.
The counts are observed rather than reported, so they lag by up to one server-side batch, and a counter that stops moving cannot tell a slow source apart from a stalled import.
Polling draws on the same --rate-limit budget as everything else, and that budget is spent one request at a time.
At the default of 30 requests per minute a pass takes up to ten seconds, so --progress-interval is a floor rather than a promise: raise --rate-limit for livelier reporting, or lengthen the interval to tread more lightly.
Counting issues needs a target token with the read:issue scope.
A token without it can still migrate, but Forgejo answers the issue, label and milestone counts with HTTP 403; the tool warns once, naming the units it cannot read, and stops asking for them.
Interrupted migrations
Forgejo's migration endpoint is synchronous: POST /api/v1/repos/migrate creates the repository, then clones it and imports its content before responding.
A large repository, or a source that rate-limits the import, can hold that request open for a long time.
The server runs the migration on a context of its own, so it carries on even after the client hangs up.
This tool therefore gives a migration an hour by default (--migrate-timeout), and when the call does time out it does not report a failure outright.
It polls the target until the repository stops changing and reports what actually happened: the repository filled up (migrated), the target deleted it again (the migration failed server-side), or it is still empty and no longer changing (the migration stalled).
A 5xx from the migration endpoint is treated the same way, because it says just as little about what the server ended up doing. The migration is never re-sent while its fate is unknown: the request is not idempotent, and re-sending it can start a second migration alongside the first or collide with the repository the first one already created. Only once the target has been asked, and has shown that nothing usable was left behind, is another attempt considered.
Not every 5xx comes from Forgejo. The migration request stays open for the whole clone and import, which is long enough for a reverse proxy in front of the target to give up on it and answer 502, 503 or 504 on the target's behalf:
5:48PM ERR the target returned a server error during the migration target_error="<html><body><h1>504 Gateway Time-out</h1>"
Such an answer is told apart by its body, since Forgejo answers an API error with a JSON envelope and a proxy never does, and it is read like a timeout rather than like a failure. The target never learned that the connection was closed and carries on importing, so what the repository settles in is what decides the outcome. A 5xx that Forgejo produced itself means something else: the target is reporting a failure of its own, and a repository left holding content after one is reported as a failure rather than a success, since issues, pull requests and releases may all be missing.
Requests rejected with 429 are retried whatever their method, because a rate limiter turns them away before any work begins, and the wait honors Retry-After in both its forms, a number of seconds or an HTTP date.
Every other call the tool makes is a GET, PUT or DELETE, which are safe to repeat and are retried up to five times with an exponential backoff.
What the recovery flags cover
A migration that fails can leave three different things on the target, and each has its own remedy.
| What the target holds | Remedy |
|---|---|
| An empty repository, stamped with this source | --replace-incomplete deletes it and migrates again. |
| Bare repository files on disk, with no repository attached | --adopt-existing-files turns them into a repository, or --replace-incomplete deletes them and migrates again. |
| A repository holding content, stamped with this source | --replace-existing deletes it and migrates again. |
| Anything else: a repository recording no source, or one migrated from a different one | Nothing. The tool leaves it alone and says so. |
Two of these surface as an HTTP 409 from the migration endpoint.
The repository with the same name already exists. means a repository is still registered on the target.
With --replace-incomplete the tool deletes it and migrates again, but only once it has established that the repository holds no git data, that its original_url is the very source being migrated from, and that nothing has touched it between two polls.
Files already exist for this repository. Adopt them or delete them. means bare repository files are on disk with no repository attached.
--adopt-existing-files turns them into a repository, recovering the git data but no issues or pull requests; --replace-incomplete deletes them and migrates again.
Forgejo restricts both remedies to site administrators, so they need a target token belonging to one.
The third is usually settled before the migration endpoint is reached.
With --skip-existing (the default) the tool looks the repository up first, so a repository that is already there is skipped rather than attempted, and the log line says which of the rows above it matched.
That is also why --adopt-existing-files can never help when a repository of that name exists: unadopted files are by definition files with no repository attached, so a repository being there rules them out.
Without these flags the tool never deletes anything on the target: it reports what it found and names the flag that resolves it.
Replacing a repository that holds content
A migration that clones the git data and then dies during the issue or pull request import leaves a repository with content behind.
--replace-incomplete will not touch it, because the emptiness check is what tells a leftover apart from a repository someone is using.
--replace-existing drops that check and keeps the other two: the repository's original_url must be exactly the source being migrated from, and it must be unchanged across two polls --reconcile-interval apart, so a migration still running server-side is never deleted out from under itself.
It is the wider of the two flags and does everything --replace-incomplete does, so there is no reason to pass both.
The caveat is worth reading twice.
The target records nothing about whether a migration finished, so a repository migrated successfully last week and one that died halfway through the import look exactly alike: non-empty, stamped with the source.
--replace-existing will delete and re-migrate both.
On a batch it is worth running once without the flag first and reading the skip lines, which name what the target holds for each repository, or restricting the run to the repositories that actually failed.
Retrying a migration
--migrate-retries gives a repository more than one attempt.
It is off by default, because every attempt costs another full clone and import.
An attempt is repeated only when both halves of the question are answered the right way.
The call has to have been cut short by something that says nothing about the migration, a timeout or a proxy, since a failure the target reported itself would only be reported again.
And the target has to have been left with nothing usable: either no repository at all, or the empty shell a stalled migration leaves behind, which needs --replace-incomplete because clearing it means deleting it.
Anything else, above all a repository that holds content, is left exactly where it is.
The pause before each new attempt starts at one --reconcile-interval and doubles, up to five minutes.
Pull request import failing with a 500
A migration from a Forgejo or Gitea source can fail with a 500 whose message is error while listing pull requests.
This is a source-compatibility problem rather than a fault in the migration itself: Forgejo falls back to resolving each pull request's head commit through the source's refs whenever it decides the source instance is too old, which it also does for any instance reporting a pre-release version such as Codeberg's 16.0.0-dev-....
Merged pull requests whose refs have since been pruned answer 404 to that lookup, and the whole migration fails.
The tool reports the target's own message and suggests the workaround: re-run with --pull-requests=false to migrate everything else.
Merged pull requests showing no commits
After a migration from a Forgejo or Gitea source, merged pull requests on the target can show "merged 0 commits", an empty commits tab and "Diff content is not available", even though the pull request's patch is there.
The cause is in Forgejo's importer, so the web migration page has the same problem.
For every pull request, the importer stores the source's base.sha as the merge base, but Forgejo's API fills base.sha with the current tip of the base branch rather than the commit the pull request started from, which is merge_base.
A merged pull request then compares a commit that already contains it against its own head, which yields nothing.
Whether a target is affected depends only on the Forgejo version it runs.
The API cannot correct a stored merge base, so the tool cannot repair this.
It compares the merge base of each merged pull request on the target with the source's after every migration that includes pull requests, and warns with the numbers of those that differ.
No warning means the target stored them correctly.
If the target later runs a Forgejo that fixes the importer, migrate the affected repositories again with --replace-existing.
Development
just vendor # download + vendor + tidy modules
just fmt # prettier + gofmt + gci
just lint # golangci-lint, pinned in tools/go.mod
just test # go test ./...
just build # vendor, then build the binary
just compile # build the binary against whatever is already vendored
Pre-commit hooks mirror the linters; run them with prek run -a.