Core Concepts
Core Concepts
Section titled “Core Concepts”Before diving into Copybara configurations, it’s important to understand the key concepts that drive how Copybara works.
Workflows
Section titled “Workflows”A workflow is the fundamental unit of Copybara. It defines:
- Where to read code from (origin)
- Where to write code to (destination)
- What transformations to apply
- How to handle authorship and commit history
core.workflow( name = "my-workflow", # Unique identifier origin = ..., # Source of code destination = ..., # Target for code transformations = [...], # Changes to apply authoring = ..., # Author handling mode = "SQUASH", # How to create commits)You can define multiple workflows in a single config file:
core.workflow(name = "export", ...) # Internal → Externalcore.workflow(name = "import", ...) # External → InternalOrigins
Section titled “Origins”An origin is where Copybara reads code from. The most common origins are:
| Origin | Use Case |
|---|---|
git.origin | Generic Git repository |
git.github_origin | GitHub with PR/issue integration |
git.github_pr_origin | Read from GitHub PRs |
git.gerrit_origin | Gerrit with CL integration |
folder.origin | Local filesystem |
origin = git.github_origin( url = "https://github.com/org/repo", ref = "main",)Destinations
Section titled “Destinations”A destination is where Copybara writes code to:
| Destination | Behavior |
|---|---|
git.destination | Push directly to a branch |
git.github_destination | Push to GitHub |
git.github_pr_destination | Create GitHub PRs |
git.gerrit_destination | Create Gerrit CLs |
folder.destination | Write to local filesystem |
# Direct pushdestination = git.destination( url = "https://github.com/org/repo", push = "main",)
# Create PRs insteaddestination = git.github_pr_destination( url = "https://github.com/org/repo", destination_ref = "main",)Transformations
Section titled “Transformations”Transformations modify code during the sync process. They’re applied in order:
transformations = [ core.move("src/", ""), # First: move files core.replace("foo", "bar"), # Then: replace text core.verify_match("SECRET", ...), # Finally: verify]Common transformation types:
| Transformation | Purpose |
|---|---|
core.move | Move/rename files or directories |
core.copy | Copy files (keep original) |
core.remove | Delete files matching a pattern |
core.replace | Text replacement (literal or regex) |
core.verify_match | Fail if pattern exists (or doesn’t) |
Globs define which files to include or exclude:
# Include specific directoriesorigin_files = glob(["src/**", "docs/**"])
# Exclude internal filesorigin_files = glob( include = ["**"], exclude = ["**/internal/**", "**/*.secret"],)Pattern syntax:
*matches any characters except/**matches any characters including/?matches a single character
Authoring
Section titled “Authoring”Authoring controls how commit authors are handled:
# Keep original authorsauthoring = authoring.pass_thru("Default <default@example.com>")
# Replace all authorsauthoring = authoring.overwrite("Bot <bot@example.com>")
# Allow only specific authorsauthoring = authoring.allowed( default = "Bot <bot@example.com>", allowlist = ["alice@example.com", "bob@example.com"],)Workflow Modes
Section titled “Workflow Modes”The mode determines how commits are created in the destination:
| Mode | Behavior |
|---|---|
SQUASH | All origin changes → one destination commit |
ITERATIVE | Preserve individual commits |
CHANGE_REQUEST | Create PR/CL for review |
CHANGE_REQUEST_FROM_SOT | PR from source of truth |
mode = "SQUASH", # Recommended for most workflowsState Tracking
Section titled “State Tracking”Copybara tracks state to know what’s already been synced:
-
After syncing, Copybara adds a marker to commit messages:
GitOrigin-RevId: abc123def456 -
On the next run, Copybara reads this marker to find where to resume
-
Only new commits since the last sync are processed
Overriding State
Section titled “Overriding State”# Start from a specific commitjava -jar copybara.jar migrate copy.bara.sky --last-rev abc123
# Process all history (initial sync)java -jar copybara.jar migrate copy.bara.sky --init-historySource of Truth (SOT)
Section titled “Source of Truth (SOT)”The Source of Truth is the authoritative repository:
- For export workflows: Internal repo is SOT
- For import workflows: External repo is SOT
Copybara always reads from the SOT and writes to the mirror.
Labels
Section titled “Labels”Labels are metadata extracted from commit messages:
# Expose labels for use in transformationsmetadata.expose_label("TESTED_BY")metadata.expose_label("BUG")
# Add labels to commit messagesmetadata.add_header("Synced-From: internal")Labels can be used to:
- Track relationships between commits
- Add metadata to synced commits
- Filter or route based on commit properties