This example will import Copybara source code to an internal git repository
under $GIT/third_party/copybara
.
Assuming you have an existing git repository. For the example in /tmp/foo
. But it could be
a remote one:
mkdir /tmp/foo
cd /tmp/foo
git init --bare .
Create a copy.bara.sky
config file like:
url = "https://github.com/google/copybara.git"
core.workflow(
name = "default",
origin = git.origin(
url = url,
ref = "master",
),
destination = git.destination(
url = "file:///tmp/foo",
fetch = "master",
push = "master",
),
# Copy everything but don't remove a README_INTERNAL.txt file if it exists.
destination_files = glob(["third_party/copybara/**"], exclude = ["README_INTERNAL.txt"]),
authoring = authoring.pass_thru("Default email <[email protected]>"),
transformations = [
core.move("", "third_party/copybara"),
],
)
Invoke the tool like:
copybara copy.bara.sky --force
--force
should only be needed for empty destination repositories or non-existent
branches in the destination. After the first import, it should be always invoked as:
copybara copy.bara.sky
This example will import private source code to an external GitHub repository, and uses SSH.
PROTIP: You will need to have an ssh key setup without a password to accomplish this, Copybara doesn't currently support ssh with a password.
Create a copy.bara.sky
config file like:
# Update these references to your orginzations repos
sourceUrl = "[email protected]:organization/internal-repo.git"
destinationUrl = "[email protected]:organization/external-repo.git"
core.workflow(
name = "default",
origin = git.origin(
url = sourceUrl,
ref = "master",
),
destination = git.destination(
url = destinationUrl,
fetch = "master",
push = "master",
),
# Change path to the folder you want to publish publicly
origin_files = glob(["path/to/folder/you/want/exported/**"]),
authoring = authoring.pass_thru("Default email <[email protected]>"),
# Change the path here to the folder you want to publish publicly
transformations = [
core.move("path/to/folder/you/want/exported", ""),
],
)
Invoke the tool like:
copybara copy.bara.sky --force
--force
should only be needed for empty destination repositories or non-existent
branches in the destination. After the first import, it should be always invoked as:
copybara copy.bara.sky
After running through this example, you should see all the source from the folder you selected in the external-repo at the root. This can be helpful if you are only trying to move a subdirectory in your git repo out for public use.
Let's say that we realized that we need to do some code transformations to the imported code.
We could use core.replace to do it. Here we look for //third_party/bazel/bashunit
text
and we replace it with the correct destination one just for BUILD files:
url = "https://github.com/google/copybara.git"
core.workflow(
name = "default",
origin = git.origin(
url = url,
ref = "master",
),
destination = git.destination(
url = "file:///tmp/foo",
fetch = "master",
push = "master",
),
# Copy everything but don't remove a README_INTERNAL.txt file if it exists.
destination_files = glob(["third_party/copybara/**"], exclude = ["README_INTERNAL.txt"]),
authoring = authoring.pass_thru("Default email <[email protected]>"),
transformations = [
core.replace(
before = "//third_party/bazel/bashunit",
after = "//another/path:bashunit",
paths = glob(["**/BUILD"]),
),
core.move("", "third_party/copybara")
],
)
The tool accepts different subcommands, à la Bazel. If no command is specified, migrate is executed by default. These two commands are equivalent:
$ copybara copy.bara.sky
$ copybara migrate copy.bara.sky
You can validate your configuration running:
$ copybara validate copy.bara.sky
Copybara source mover
INFO: Configuration validated.
And you can get information about a migration workflow by running:
$ copybara info copy.bara.sky
Copybara source mover
...
INFO: Workflow 'default': last_migrated_ref 4dd20b2...