From db131014cf9d40f526a42d14901f3c10cba27786 Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Tue, 28 Feb 2023 22:54:04 +0200 Subject: Git init --- Cargo.lock | 180 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + readme.org | 3 +- src/git.rs | 19 +++++++ src/main.rs | 6 +- tests/cli.rs | 11 +++- 6 files changed, 216 insertions(+), 4 deletions(-) create mode 100644 src/git.rs diff --git a/Cargo.lock b/Cargo.lock index 332101e..0a5c417 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -55,6 +55,15 @@ dependencies = [ "serde", ] +[[package]] +name = "cc" +version = "1.0.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" +dependencies = [ + "jobserver", +] + [[package]] name = "cfg-if" version = "1.0.0" @@ -94,6 +103,40 @@ dependencies = [ "instant", ] +[[package]] +name = "form_urlencoded" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9c384f161156f5260c24a097c56119f9be8c798586aecc13afbcbe7b7e26bf8" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "git2" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf7f68c2995f392c49fffb4f95ae2c873297830eb25c6bc4c114ce8f4562acc" +dependencies = [ + "bitflags", + "libc", + "libgit2-sys", + "log", + "openssl-probe", + "openssl-sys", + "url", +] + +[[package]] +name = "idna" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e14ddfc70884202db2244c223200c204c2bda1bc6e0998d11b5e024d657209e6" +dependencies = [ + "unicode-bidi", + "unicode-normalization", +] + [[package]] name = "instant" version = "0.1.12" @@ -112,12 +155,70 @@ dependencies = [ "either", ] +[[package]] +name = "jobserver" +version = "0.1.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "936cfd212a0155903bcbc060e316fb6cc7cbf2e1907329391ebadc1fe0ce77c2" +dependencies = [ + "libc", +] + [[package]] name = "libc" version = "0.2.139" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" +[[package]] +name = "libgit2-sys" +version = "0.14.2+1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f3d95f6b51075fe9810a7ae22c7095f12b98005ab364d8544797a825ce946a4" +dependencies = [ + "cc", + "libc", + "libssh2-sys", + "libz-sys", + "openssl-sys", + "pkg-config", +] + +[[package]] +name = "libssh2-sys" +version = "0.2.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b094a36eb4b8b8c8a7b4b8ae43b2944502be3e59cd87687595cf6b0a71b3f4ca" +dependencies = [ + "cc", + "libc", + "libz-sys", + "openssl-sys", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "libz-sys" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9702761c3935f8cc2f101793272e202c72b99da8f4224a19ddcf1279a6450bbf" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "log" +version = "0.4.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" +dependencies = [ + "cfg-if", +] + [[package]] name = "memchr" version = "2.5.0" @@ -169,12 +270,43 @@ version = "1.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" +[[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.80" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23bbbf7854cd45b83958ebe919f0e8e516793727652e27fda10a8384cfc790b7" +dependencies = [ + "autocfg", + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "percent-encoding" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e" + [[package]] name = "pin-utils" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "pkg-config" +version = "0.3.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160" + [[package]] name = "predicates" version = "2.1.5" @@ -286,6 +418,7 @@ version = "0.1.0" dependencies = [ "anyhow", "assert_cmd", + "git2", "nom", "rexpect", "thiserror", @@ -342,12 +475,59 @@ dependencies = [ "syn", ] +[[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.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d54675592c1dbefd78cbd98db9bacd89886e1ca50692a0692baefffdeb92dd58" + [[package]] name = "unicode-ident" version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" +[[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.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d68c799ae75762b8c3fe375feb6600ef5602c883c5d21eb51c09f22b83c4643" +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 = "wait-timeout" version = "0.2.0" diff --git a/Cargo.toml b/Cargo.toml index 26bdb76..98d243e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +git2 = "0.16.1" nom = "7.1.3" rexpect = "0.5.0" thiserror = "1.0.38" diff --git a/readme.org b/readme.org index 5dabeb9..4e71e8b 100644 --- a/readme.org +++ b/readme.org @@ -15,8 +15,9 @@ Pijul. - [X] interactive command prompt - [X] exit command -- [ ] git init of private repo +- [X] git init of private repo - [X] responds to unknown commands +- [ ] Isolation of workdir between tests - [ ] git fetch - git receive-pack - [ ] git push diff --git a/src/git.rs b/src/git.rs new file mode 100644 index 0000000..bd26568 --- /dev/null +++ b/src/git.rs @@ -0,0 +1,19 @@ +use crate::ShackleError; + +use git2::{Repository, RepositoryInitOptions}; +use std::path::PathBuf; + +pub fn init(repo_name: &str) -> Result<(), ShackleError> { + let mut path = PathBuf::from("git"); + path.push(repo_name); + path.set_extension("git"); + + Repository::init_opts( + path, + &RepositoryInitOptions::new() + .bare(true) + .mkdir(true) + .no_reinit(true), + )?; + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index 0df2754..142b4fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use std::{io, io::Write}; use thiserror::Error; +mod git; mod parser; use parser::Command; @@ -31,6 +32,7 @@ fn main() -> Result<(), ShackleError> { break; } Ok(Command::GitInit(repo_name)) => { + git::init(&repo_name)?; // TODO should report this error differently println!("Successfully created {}.git", repo_name); } } @@ -39,7 +41,9 @@ fn main() -> Result<(), ShackleError> { } #[derive(Error, Debug)] -enum ShackleError { +pub enum ShackleError { #[error(transparent)] IoError(#[from] io::Error), + #[error(transparent)] + GitError(#[from] git2::Error), } diff --git a/tests/cli.rs b/tests/cli.rs index fcf38e8..8b8aa82 100644 --- a/tests/cli.rs +++ b/tests/cli.rs @@ -1,5 +1,5 @@ use anyhow::Result; -use assert_cmd::cargo::cargo_bin; +use assert_cmd::{cargo::cargo_bin, Command}; use rexpect::{session::PtySession, spawn}; fn spawn_interactive_process() -> Result { @@ -70,6 +70,13 @@ fn can_init_a_new_git_repo() -> Result<()> { p.send_line("git-init my-new-repo")?; p.exp_string("Successfully created my-new-repo.git")?; expect_prompt(&mut p)?; - // TODO: assert that the repo is actually there? + + Command::new("git") + .arg("rev-list") + .arg("--all") + .current_dir("git/my-new-repo.git") + .assert() + .success() + .stdout(""); Ok(()) } -- cgit v1.2.3