Moving Files
Moving Files
Section titled “Moving Files”Use core.move and core.copy to restructure files during synchronization.
core.move
Section titled “core.move”Move or rename files and directories:
core.move("source_path", "destination_path")Move Directory
Section titled “Move Directory”# Move entire directorycore.move("src/", "lib/")# src/foo.py → lib/foo.py# src/bar/baz.py → lib/bar/baz.pyMove to Root
Section titled “Move to Root”# Flatten a subdirectory to rootcore.move("project/src/", "")# project/src/main.py → main.py# project/src/lib/util.py → lib/util.pyRename File
Section titled “Rename File”# Rename a single filecore.move("README.internal.md", "README.md")With Glob Filter
Section titled “With Glob Filter”# Only move matching filescore.move("src/", "lib/", paths = glob(["**/*.py"]))# src/main.py → lib/main.py# src/config.json → stays at src/config.jsoncore.copy
Section titled “core.copy”Copy files (keeping originals):
core.copy("source", "destination")Examples
Section titled “Examples”# Copy a filecore.copy("LICENSE", "docs/LICENSE")
# Copy directorycore.copy("templates/", "examples/")Overwrite Behavior
Section titled “Overwrite Behavior”By default, moves fail if the destination exists:
# Fail if destination existscore.move("src/", "lib/")
# Overwrite if destination existscore.move("src/", "lib/", overwrite = True)Common Patterns
Section titled “Common Patterns”Flatten Monorepo Package
Section titled “Flatten Monorepo Package”# packages/my-lib/src/* → src/*core.move("packages/my-lib/", "")Restructure for Publishing
Section titled “Restructure for Publishing”transformations = [ # Move source to lib core.move("src/", "lib/"),
# Move internal docs to public core.move("docs/public/", "docs/"),
# Remove internal docs core.remove(glob(["docs/internal/**"])),]Rename for Convention
Section titled “Rename for Convention”transformations = [ core.move("README.internal.md", "README.md"), core.move("CHANGELOG.internal.md", "CHANGELOG.md"),]Conditional Move
Section titled “Conditional Move”# Only move if file matches patterncore.move( "config/", "settings/", paths = glob(["**/*.yaml", "**/*.yml"]),)Order of Operations
Section titled “Order of Operations”When combining moves, order matters:
# CORRECT: nested moves from deepest to shallowesttransformations = [ core.move("a/b/c/", "x/y/z/"), core.move("a/b/", "x/y/"), core.move("a/", "x/"),]
# INCORRECT: parent moved first, children paths no longer existtransformations = [ core.move("a/", "x/"), # a/b/c/ is now x/b/c/ core.move("a/b/", "x/y/"), # ERROR: a/b/ doesn't exist]Trailing Slashes
Section titled “Trailing Slashes”Trailing slashes indicate directories:
# Move directory contentscore.move("src/", "lib/")# src/a.py → lib/a.py
# Move directory itself (rename)core.move("src", "lib")# src → lib (same effect for directories)
# Move filecore.move("src/main.py", "lib/app.py")# src/main.py → lib/app.pyDebugging Moves
Section titled “Debugging Moves”Test with folder destination:
java -jar copybara.jar migrate copy.bara.sky test \ --folder-origin /path/to/source \ --folder-destination /tmp/output
# Check the structurefind /tmp/output -type f