From 049e4b778a229e0e06f4d5f8c41d3d5c42030613 Mon Sep 17 00:00:00 2001 From: Daniil Demidko Date: Fri, 29 Mar 2024 03:11:28 +0600 Subject: [PATCH] =?UTF-8?q?=D0=A1=D1=83=D1=89=D0=BD=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D1=8C=20MyGit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 242 +++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 + src/arch.rs | 16 +++- src/git.rs | 26 ------ src/main.rs | 12 ++- src/mygit.rs | 68 +++++++++++++++ 6 files changed, 331 insertions(+), 35 deletions(-) delete mode 100644 src/git.rs create mode 100644 src/mygit.rs diff --git a/Cargo.lock b/Cargo.lock index 40859f9..440479e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,3 +5,245 @@ version = 3 [[package]] name = "archdiff" version = "2024.3.12" +dependencies = [ + "git2", + "walkdir", +] + +[[package]] +name = "bitflags" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" + +[[package]] +name = "cc" +version = "1.0.90" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cd6604a82acf3039f1144f54b8eb34e91ffba622051189e71b781822d5ee1f5" +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.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "232e6a7bfe35766bf715e55a88b39a700596c0ccfd88cd3680b4cdb40d66ef70" +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.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab46a6e9526ddef3ae7f787c06f0f2600639ba80ea3eade3d8e670a2230f51d6" +dependencies = [ + "libc", +] + +[[package]] +name = "libc" +version = "0.2.153" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" + +[[package]] +name = "libgit2-sys" +version = "0.16.2+1.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee4126d8b4ee5c9d9ea891dd875cfdc1e9d0950437179104b183d7d8a74d24e8" +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.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e143b5e666b2695d28f6bca6497720813f699c9602dd7f5cac91008b8ada7f9" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "log" +version = "0.4.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" + +[[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.101" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dda2b0f344e78efc2facf7d195d098df0dd72151b26ab98da807afc26c198dff" +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.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" + +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + +[[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.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08f95100a766bf4f8f28f90d77e0a5461bbdb219042e7679bebe79004fed8d75" + +[[package]] +name = "unicode-normalization" +version = "0.1.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a56d1686db2308d901306f92a263857ef59ea39678a5458e7cb17f01415101f5" +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" + +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml index 5f75d75..c0ad678 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,5 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +git2 = "0.18.3" +walkdir = "2.5.0" diff --git a/src/arch.rs b/src/arch.rs index 0ed8293..6eb8ced 100644 --- a/src/arch.rs +++ b/src/arch.rs @@ -1,3 +1,15 @@ +use walkdir::WalkDir; + pub fn read() -> String { - todo!() -} \ No newline at end of file + todo!("") +} + +#[cfg(test)] +mod tests { + use crate::arch::read; + + #[test] + fn it_works() { + read(); + } +} diff --git a/src/git.rs b/src/git.rs deleted file mode 100644 index 6e74928..0000000 --- a/src/git.rs +++ /dev/null @@ -1,26 +0,0 @@ -use std::process::Command; - -pub fn co(branch: &str) { - let status = - Command::new("git") - .arg("checkout") - .arg(branch) - .status() - .expect("failed to find git"); - assert!(status.success()) -} - -pub fn current_branch() -> String { - let output = - Command::new("git") - .arg("branch") - .arg("--show-current") - .output() - .expect("failed to find git") - .stdout; - String::from_utf8(output).expect("failed to find current branch") -} - -pub fn diff(from: &str, into: &str) -> String { - todo!() -} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index a6c1168..d915fff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,19 +1,17 @@ use std::env::args; -use clap::Parser; - -mod git; +mod mygit; mod arch; fn main() { let from_branch = args().nth(1).expect("expected source branch name"); - let into_branch = git::current_branch(); + let into_branch = mygit::current_branch(); - git::co(&from_branch); + mygit::co(&from_branch); let from_branch = arch::read(); - git::co(&into_branch); + mygit::co(&into_branch); let into_branch = arch::read(); - println!("{}", git::diff(&from_branch, &into_branch)); + println!("{}", mygit::diff(&from_branch, &into_branch)); } \ No newline at end of file diff --git a/src/mygit.rs b/src/mygit.rs new file mode 100644 index 0000000..b663fce --- /dev/null +++ b/src/mygit.rs @@ -0,0 +1,68 @@ +use std::process::Command; +use std::str; + +use git2::{BranchType, Repository}; + +struct MyGit { + repo: Repository, +} + +impl MyGit { + pub fn new() -> Self { + match Repository::init(".") { + Ok(repo) => Self { repo }, + Err(e) => panic!("failed to init: {}", e) + } + } + + pub fn current_branch_name(&self) -> String { + let repo = &self.repo; + let head = repo.head().unwrap(); + let name = head.name().unwrap(); + let name = name.trim_start_matches("refs/heads/"); + name.to_string() + } + + pub fn checkout(&self, branch_name: &str) { + let repo = &self.repo; + } +} + +pub fn co(branch: &str) { + let status = + Command::new("git") + .arg("checkout") + .arg(branch) + .status() + .expect("failed to find 'git' command"); + assert!(status.success()) +} + +pub fn current_branch() -> String { + let output = + Command::new("git") + .arg("branch") + .arg("--show-current") + .output() + .expect("failed to find 'git' command") + .stdout; + str::from_utf8(&output) + .expect("failed to find current branch") + .trim() + .to_string() +} + +pub fn diff(from: &str, into: &str) -> String { + todo!() +} + +#[cfg(test)] +mod tests { + use crate::mygit::{current_branch, MyGit}; + + #[test] + fn it_works() { + let branch = MyGit::new().current_branch_name(); + assert_eq!(branch, "main") + } +} \ No newline at end of file