aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorValentin Popov <valentin@popov.link>2024-01-11 21:03:42 +0300
committerValentin Popov <valentin@popov.link>2024-01-11 21:03:42 +0300
commitfdd3eb83229bfe8032f87b570d24d6b91d581b31 (patch)
tree140a844297b04711d77e9069415b2147a91156c9
parent4cbb43c1e2db4c90293e55f2497ae0d0d841c9bd (diff)
downloadgitlab-duplicator-fdd3eb83229bfe8032f87b570d24d6b91d581b31.tar.xz
gitlab-duplicator-fdd3eb83229bfe8032f87b570d24d6b91d581b31.zip
Added a method for cloning a repository
Signed-off-by: Valentin Popov <valentin@popov.link>
-rw-r--r--Cargo.lock112
-rw-r--r--Cargo.toml1
-rw-r--r--src/main.rs55
3 files changed, 155 insertions, 13 deletions
diff --git a/Cargo.lock b/Cargo.lock
index e819af5..eb663fe 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -137,6 +137,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
+name = "bitflags"
+version = "2.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07"
+
+[[package]]
name = "bumpalo"
version = "3.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -160,6 +166,7 @@ version = "1.0.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0"
dependencies = [
+ "jobserver",
"libc",
]
@@ -184,9 +191,9 @@ dependencies = [
[[package]]
name = "clap"
-version = "4.4.14"
+version = "4.4.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "33e92c5c1a78c62968ec57dbc2440366a2d6e5a23faf829970ff1585dc6b18e2"
+checksum = "c12ed66a79a555082f595f7eb980d08669de95009dd4b3d61168c573ebe38fc9"
dependencies = [
"clap_builder",
"clap_derive",
@@ -194,9 +201,9 @@ dependencies = [
[[package]]
name = "clap_builder"
-version = "4.4.14"
+version = "4.4.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f4323769dc8a61e2c39ad7dc26f6f2800524691a44d74fe3d1071a5c24db6370"
+checksum = "0f4645eab3431e5a8403a96bea02506a8b35d28cd0f0330977dd5d22f9c84f43"
dependencies = [
"anstream",
"anstyle",
@@ -482,12 +489,28 @@ dependencies = [
"clap",
"dotenv",
"dotenv_codegen",
+ "git2",
"gitlab",
"serde",
"serde_json",
]
[[package]]
+name = "git2"
+version = "0.18.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fbf97ba92db08df386e10c8ede66a2a0369bd277090afd8710e19e38de9ec0cd"
+dependencies = [
+ "bitflags 2.4.1",
+ "libc",
+ "libgit2-sys",
+ "log",
+ "openssl-probe",
+ "openssl-sys",
+ "url",
+]
+
+[[package]]
name = "gitlab"
version = "0.1408.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -759,6 +782,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c"
[[package]]
+name = "jobserver"
+version = "0.1.27"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8c37f63953c4c63420ed5fd3d6d398c719489b9f872b9fa683262f8edd363c7d"
+dependencies = [
+ "libc",
+]
+
+[[package]]
name = "js-sys"
version = "0.3.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -780,6 +812,46 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13e3bf6590cbc649f4d1a3eefc9d5d6eb746f5200ffb04e5e142700b8faa56e7"
[[package]]
+name = "libgit2-sys"
+version = "0.16.1+1.7.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f2a2bb3680b094add03bb3732ec520ece34da31a8cd2d633d1389d0f0fb60d0c"
+dependencies = [
+ "cc",
+ "libc",
+ "libssh2-sys",
+ "libz-sys",
+ "openssl-sys",
+ "pkg-config",
+]
+
+[[package]]
+name = "libssh2-sys"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2dc8a030b787e2119a731f1951d6a773e2280c660f8ec4b0f5e1505a386e71ee"
+dependencies = [
+ "cc",
+ "libc",
+ "libz-sys",
+ "openssl-sys",
+ "pkg-config",
+ "vcpkg",
+]
+
+[[package]]
+name = "libz-sys"
+version = "1.1.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "295c17e837573c8c821dbaeb3cceb3d745ad082f7572191409e69cbc1b3fd050"
+dependencies = [
+ "cc",
+ "libc",
+ "pkg-config",
+ "vcpkg",
+]
+
+[[package]]
name = "log"
version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -852,6 +924,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]]
+name = "openssl-probe"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf"
+
+[[package]]
+name = "openssl-sys"
+version = "0.9.98"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c1665caf8ab2dc9aef43d1c0023bd904633a6a05cb30b0ad59bec2ae986e57a7"
+dependencies = [
+ "cc",
+ "libc",
+ "pkg-config",
+ "vcpkg",
+]
+
+[[package]]
name = "percent-encoding"
version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -870,6 +960,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]]
+name = "pkg-config"
+version = "0.3.28"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "69d3587f8a9e599cc7ec2c00e331f71c4e69a5f9a4b8a6efd5b07466b9736f9a"
+
+[[package]]
name = "proc-macro-hack"
version = "0.5.20+deprecated"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1114,7 +1210,7 @@ version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba3a3adc5c275d719af8cb4272ea1c4a6d668a777f37e115f6d11ddbc1c8e0e7"
dependencies = [
- "bitflags",
+ "bitflags 1.3.2",
"core-foundation",
"system-configuration-sys",
]
@@ -1301,6 +1397,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
+name = "vcpkg"
+version = "0.2.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
+
+[[package]]
name = "void"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index 6c66722..0f55099 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -12,6 +12,7 @@ strip = true
clap = { version = "4.4.0", features = ["derive"] }
dotenv = "0.15.0"
dotenv_codegen = "0.15.0"
+git2 = "0.18.1"
gitlab = "=0.1408.0"
serde = "1.0.195"
serde_json = "1.0.111"
diff --git a/src/main.rs b/src/main.rs
index 48d1772..aa2f233 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,7 +1,11 @@
#[macro_use]
extern crate dotenv_codegen;
+extern crate git2;
+
+use std::path::Path;
use clap::Parser;
+use git2::{Cred, RemoteCallbacks};
use gitlab::api::{projects, Query};
use gitlab::Gitlab;
use serde::Deserialize;
@@ -21,25 +25,60 @@ struct Args {
/// The URL of the repository to clone
#[arg(short, long)]
- url: String,
+ repo: String,
+
+ /// The username to use for authentication
+ #[arg(long)]
+ username: String,
+
+ /// The password to use for authentication
+ #[arg(long)]
+ password: String,
}
/* Get project info from Gitlab */
fn get_project(url: &str) -> Project {
- let gl_client = Gitlab::new(dotenv!("GITLAB_URL"), dotenv!("GITLAB_TOKEN")).unwrap();
+ let client = Gitlab::new(dotenv!("GITLAB_URL"), dotenv!("GITLAB_TOKEN")).unwrap();
+
+ let endpoint = projects::Project::builder().project(url).build().unwrap();
+ let project: Project = endpoint.query(&client).unwrap();
+
+ project
+}
+
+/* Clone project from Gitlab */
+fn clone_project(repo: &str, path: &str, username: &str, password: &str) {
+ // Callbacks for authentication
+ let mut cb = RemoteCallbacks::new();
+ cb.credentials(|_url, _username_from_url, _allowed_types| {
+ Cred::userpass_plaintext(username, password)
+ });
+
+ // Prepare fetch options
+ let mut fetch_options = git2::FetchOptions::new();
+ fetch_options.remote_callbacks(cb);
+
+ // Prepare builder
+ let mut builder = git2::build::RepoBuilder::new();
+ builder.remote_create(|repo, name, url| repo.remote_with_fetch(name, url, "+refs/*:refs/*"));
- let gl_endpoint = projects::Project::builder().project(url).build().unwrap();
- let gl_project: Project = gl_endpoint.query(&gl_client).unwrap();
+ builder.fetch_options(fetch_options);
+ builder.bare(true);
- gl_project
+ // Clone
+ builder.clone(repo, Path::new(&path)).unwrap();
}
fn main() {
dotenv::dotenv().ok();
let args = Args::parse();
- println!("{:?}", args);
+ let project = get_project(&args.repo);
- let project = get_project(&args.url);
- dbg!(project);
+ clone_project(
+ &project.http_url_to_repo,
+ &args.path,
+ &args.username,
+ &args.password,
+ );
}