Give the git step the optional `into` parameter to clone into a specific directory

This commit is contained in:
R Tyler Croy 2020-11-19 20:31:25 -08:00
parent 7b2dfdb749
commit 80425ec62b
3 changed files with 49 additions and 14 deletions

View File

@ -23,3 +23,10 @@ parameters:
type: string
description: |
A git branch to clone instead of the default.
- name: into
required: false
type: string
description: |
Path into which the clone should be performed, can be used as `.` to
clone into the current working directory

View File

@ -10,6 +10,7 @@ use url::Url;
struct Parameters {
url: Url,
branch: Option<String>,
into: Option<String>,
}
/**
@ -32,21 +33,23 @@ fn main() -> std::io::Result<()> {
let invoke: Invocation<Parameters> =
invocation_from_args(&args).expect("Failed to deserialize the invocation for the step");
if let Some(path) = repo_from_url(&invoke.parameters.url) {
println!("Clone!");
let mut builder = git2::build::RepoBuilder::new();
let clone_path = match invoke.parameters.into {
Some(into) => into,
None => repo_from_url(&invoke.parameters.url).expect("Failed to determine local path to clone"),
};
if let Some(branch) = &invoke.parameters.branch {
builder.branch(&branch);
}
let _repo = match builder.clone(&invoke.parameters.url.into_string(), Path::new(&path)) {
Ok(repo) => repo,
Err(e) => panic!("failed to clone: {}", e),
};
} else {
println!("Failed to determine the right local path to clone the repository into");
std::process::exit(1);
println!("Clone!");
let mut builder = git2::build::RepoBuilder::new();
if let Some(branch) = &invoke.parameters.branch {
builder.branch(&branch);
}
let _repo = match builder.clone(&invoke.parameters.url.into_string(), Path::new(&clone_path)) {
Ok(repo) => repo,
Err(e) => panic!("failed to clone: {}", e),
};
Ok(())
}

View File

@ -1,6 +1,6 @@
#!/bin/sh
INVOCATION_FILE=tmp_gittest_invocation_file.json
INVOCATION_FILE=$PWD/tmp_gittest_invocation_file.json
oneTimeTearDown() {
rm -f $INVOCATION_FILE
@ -52,4 +52,29 @@ EOF
rm -rf otto-test-repository
}
test_clone_into() {
cat > $INVOCATION_FILE<<EOF
{
"configuration" : {
"pipeline" : "2265b5d0-1f70-46de-bf50-f1050e9fac9a",
"uuid" : "5599cffb-f23a-4e0f-a0b9-f74654641b2b",
"ipc" : "unix:///dev/null",
"endpoints" : {
}
},
"parameters" : {
"url" : "https://git.brokenco.de/rtyler/otto-test-repository",
"into" : "."
}
}
EOF
mkdir work-dir
pushd work-dir
output=$(git-step $INVOCATION_FILE)
assertTrue "step should be able to clone the given url: ${output}" $?
assertTrue "step should have cloned the repo into $PWD" "test -f README.adoc"
popd
}
. $(dirname $0)/../../../contrib/shunit2/shunit2