From ac9edd48cff10b84c3e45ecc6bfc069921e24b95 Mon Sep 17 00:00:00 2001 From: holly sparkles Date: Sun, 7 Jan 2024 19:39:17 +0100 Subject: [PATCH] feat: add listing repository files --- Cargo.lock | 191 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/lib.rs | 1 + src/main.rs | 16 ++++++++++++++-- src/repository/mod.rs | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 274 insertions(+), 2 deletions(-) create mode 100644 src/repository/mod.rs diff --git a/Cargo.lock b/Cargo.lock index dfb7cda..44b4fd4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,5 +3,196 @@ version = 3 [[package]] +name = "bitflags" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07" + +[[package]] +name = "cc" +version = "1.0.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" +dependencies = [ + "jobserver", + "libc", +] + +[[package]] +name = "form_urlencoded" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "git2" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbf97ba92db08df386e10c8ede66a2a0369bd277090afd8710e19e38de9ec0cd" +dependencies = [ + "bitflags", + "libc", + "libgit2-sys", + "log", + "openssl-probe", + "openssl-sys", + "url", +] + +[[package]] +name = "idna" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "634d9b1461af396cad843f47fdba5597a4f9e6ddd4bfb6ff5d85028c25cb12f6" +dependencies = [ + "unicode-bidi", + "unicode-normalization", +] + +[[package]] +name = "jobserver" +version = "0.1.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c37f63953c4c63420ed5fd3d6d398c719489b9f872b9fa683262f8edd363c7d" +dependencies = [ + "libc", +] + +[[package]] +name = "libc" +version = "0.2.152" +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.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d97137b25e321a73eef1418d1d5d2eda4d77e12813f8e6dead84bc52c5870a7b" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "log" +version = "0.4.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" + +[[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" +checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" + +[[package]] +name = "pkg-config" +version = "0.3.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69d3587f8a9e599cc7ec2c00e331f71c4e69a5f9a4b8a6efd5b07466b9736f9a" + +[[package]] name = "smolguess" version = "0.1.0" +dependencies = [ + "git2", +] + +[[package]] +name = "tinyvec" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87cc5ceb3875bb20c2890005a4e226a4651264a5c75edb2421b52861a0a0cb50" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + +[[package]] +name = "unicode-bidi" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f2528f27a9eb2b21e69c95319b30bd0efd85d09c379741b0f78ea1d86be2416" + +[[package]] +name = "unicode-normalization" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "url" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31e6302e3bb753d46e83516cae55ae196fc0c309407cf11ab35cc51a4c2a4633" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", +] + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" diff --git a/Cargo.toml b/Cargo.toml index ea3249c..a64a203 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +git2 = "0.18.1" diff --git a/src/lib.rs b/src/lib.rs index 5a7ca06..33185cb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,2 @@ pub mod core; +pub mod repository; diff --git a/src/main.rs b/src/main.rs index eb4fb61..b4a601c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,21 @@ -use smolguess::core::LanguageDefinitions; +use std::{env, path::PathBuf}; + +use smolguess::{core::LanguageDefinitions, repository}; fn main() { + let args: Vec = env::args().collect(); + if args.len() < 2 || args[1].is_empty() { + eprintln!("Usage: {} ", args[0]); + std::process::exit(1); + } + + let files = repository::get_bare_repository_files(PathBuf::from(&args[1])); + + println!("{:?}", files); + LanguageDefinitions::default() .load_builtins() .items .iter() - .for_each(|def| println!("{} has the extension {}", def.name, def.extension)) + .for_each(|def| println!("{} has the extension {}", def.name, def.extension)); } diff --git a/src/repository/mod.rs b/src/repository/mod.rs new file mode 100644 index 0000000..b4d6359 --- /dev/null +++ b/src/repository/mod.rs @@ -0,0 +1,67 @@ +use std::path::PathBuf; + +use git2::{Repository, TreeWalkMode}; + +/// Retrieves a list of file paths from a bare Git repository. +/// +/// The `get_bare_repository_files` function takes a `PathBuf` representing the path to a +/// bare Git repository and returns a vector of `PathBuf` representing the file paths in the repository. +/// +/// # Parameters +/// +/// - `path`: The path to the bare Git repository. +/// +/// # Returns +/// +/// A vector of `PathBuf` containing the file paths in the bare Git repository. +/// +/// # Examples +/// +/// ``` +/// use std::path::PathBuf; +/// use smolguess::repository::get_bare_repository_files; +/// +/// let repo_path = PathBuf::from("/path/to/bare/repo.git"); +/// let files = get_bare_repository_files(repo_path); +/// +/// for file in files { +/// println!("File: {}", file.display()); +/// } +/// ``` +pub fn get_bare_repository_files(path: PathBuf) -> Vec { + if let Ok(repo) = Repository::open_bare(path) { + if let Ok(current_tree) = repo.head().map(|head| head.peel_to_tree()) { + let mut files: Vec = Vec::new(); + + match current_tree { + Ok(tree) => { + tree.walk(TreeWalkMode::PreOrder, |root, entry| { + if entry + .kind() + .map_or(false, |kind| kind.eq(&git2::ObjectType::Blob)) + { + files.push(PathBuf::from(format!( + "{}{}", + root, + entry.name().expect("Error getting file name") + ))); + } + git2::TreeWalkResult::Ok + }) + .expect("Error walking through tree"); + } + Err(e) => { + println!("{:?}", e); + } + }; + + files + } else { + println!("Error retrieving tree"); + Vec::new() + } + } else { + println!("Could not open repository"); + Vec::new() + } +} -- libgit2 1.7.2