From 738f4a9add6e956b64bb12b70a75bdf4a9965fb6 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sun, 6 Nov 2022 16:08:01 +0100 Subject: [PATCH] Add basic gitlog --- Cargo.lock | 237 ++++++++++++++++++++++++++++++++++ Cargo.toml | 16 ++- crates/gitlog_core/.gitignore | 2 + crates/gitlog_core/Cargo.toml | 12 ++ crates/gitlog_core/src/lib.rs | 78 +++++++++++ src/main.rs | 27 ++-- 6 files changed, 355 insertions(+), 17 deletions(-) create mode 100644 crates/gitlog_core/.gitignore create mode 100644 crates/gitlog_core/Cargo.toml create mode 100644 crates/gitlog_core/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index ff166df..4a168e7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,17 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.1.0" @@ -29,6 +40,43 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "4.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e67816e006b17427c9b4386915109b494fec2d929c63e3bd3561234cbf1bf1e" +dependencies = [ + "atty", + "bitflags", + "clap_derive", + "clap_lex", + "once_cell", + "strsim", + "termcolor", +] + +[[package]] +name = "clap_derive" +version = "4.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16a1b0f6422af32d5da0c58e2703320f379216ee70198241c84173a8c5ac28f3" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d4198f73e42b4936b35b5bb248d81d2b595ecb170da0bac7655c54eedfa8da8" +dependencies = [ + "os_str_bytes", +] + [[package]] name = "eyre" version = "0.6.8" @@ -52,8 +100,12 @@ dependencies = [ name = "git-log" version = "0.1.0" dependencies = [ + "clap", "eyre", "git2", + "gitlog_core", + "serde", + "serde_json", ] [[package]] @@ -71,6 +123,31 @@ dependencies = [ "url", ] +[[package]] +name = "gitlog_core" +version = "0.1.0" +dependencies = [ + "eyre", + "git2", + "serde", + "serde_json", +] + +[[package]] +name = "heck" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "idna" version = "0.3.0" @@ -87,6 +164,12 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683" +[[package]] +name = "itoa" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4217ad341ebadf8d8e724e264f13e593e0648f5b3e94b3896a5df283be015ecc" + [[package]] name = "jobserver" version = "0.1.25" @@ -176,6 +259,12 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "os_str_bytes" +version = "6.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3baf96e39c5359d2eb0dd6ccb42c62b91d9678aa68160d261b9e0ccbf9e9dea9" + [[package]] name = "percent-encoding" version = "2.2.0" @@ -188,6 +277,111 @@ version = "0.3.26" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160" +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro2" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "ryu" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" + +[[package]] +name = "serde" +version = "1.0.147" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d193d69bae983fc11a79df82342761dfbf28a99fc8d203dca4c3c1b590948965" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.147" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f1d362ca8fc9c3e3a7484440752472d68a6caa98f1ab81d99b5dfe517cec852" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.87" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce777b7b150d76b9cf60d28b55f5847135a003f7d7350c6be7a773508ce7d45" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + +[[package]] +name = "syn" +version = "1.0.103" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a864042229133ada95abf3b54fdc62ef5ccabe9515b64717bcb9a1919e59445d" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "termcolor" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" +dependencies = [ + "winapi-util", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -209,6 +403,12 @@ version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "099b7128301d285f79ddd55b9a83d5e6b9e97c92e0ea0daebee7263e932de992" +[[package]] +name = "unicode-ident" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" + [[package]] name = "unicode-normalization" version = "0.1.22" @@ -234,3 +434,40 @@ name = "vcpkg" version = "0.2.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[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.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +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 117bb8e..6b5eb08 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,20 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html -[dependencies] +[workspace] +members = ["crates/gitlog_core"] + +[workspace.dependencies] eyre = "0.6.8" git2 = "0.15.0" +serde = { version = "1.0.147", features = ["derive"] } +serde_json = "1.0.87" + +[dependencies] +gitlog_core = { path = "crates/gitlog_core" } + +eyre = { workspace = true } +git2 = { workspace = true } +serde = { workspace = true } +serde_json = { workspace = true } +clap = { version = "4.0.19", features = ["env", "derive"] } diff --git a/crates/gitlog_core/.gitignore b/crates/gitlog_core/.gitignore new file mode 100644 index 0000000..4fffb2f --- /dev/null +++ b/crates/gitlog_core/.gitignore @@ -0,0 +1,2 @@ +/target +/Cargo.lock diff --git a/crates/gitlog_core/Cargo.toml b/crates/gitlog_core/Cargo.toml new file mode 100644 index 0000000..b85acd2 --- /dev/null +++ b/crates/gitlog_core/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "gitlog_core" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +eyre = { workspace = true } +git2 = { workspace = true } +serde = { workspace = true } +serde_json = { workspace = true } diff --git a/crates/gitlog_core/src/lib.rs b/crates/gitlog_core/src/lib.rs new file mode 100644 index 0000000..aeeda1e --- /dev/null +++ b/crates/gitlog_core/src/lib.rs @@ -0,0 +1,78 @@ +use git2::Repository; +use serde::Serialize; + +#[allow(dead_code)] +#[derive(Debug, Serialize)] +struct CommitAuthor { + email: String, + name: String, +} + +#[allow(dead_code)] +#[derive(Debug, Serialize)] +struct Commit { + title: String, + #[serde(skip_serializing_if = "Option::is_none")] + message: Option, + author: CommitAuthor, + time: i64, +} + +pub fn get_git_log(path: String) -> eyre::Result { + let repo = Repository::open(path)?; + let mut revwalk = repo.revwalk()?; + revwalk.set_sorting(git2::Sort::TIME)?; + + let revspec = repo.revparse("HEAD")?; + let from = revspec.from().ok_or(eyre::anyhow!("could not get HEAD"))?; + revwalk.push(from.id())?; + + let mut commits: Vec = Vec::new(); + for rev in revwalk.enumerate() { + let oid = rev.1?; + let commit = repo.find_commit(oid)?; + let message = commit + .message() + .ok_or(eyre::anyhow!("could not find message from commit"))?; + let timestamp = commit.time(); + let author = commit.author(); + + let message_content = message.split("\n"); + let message_heading = message_content + .clone() + .take(1) + .collect::>() + .join("\n"); + let message_content = message_content + .clone() + .skip(1) + .collect::>() + .join("\n"); + + let commit = Commit { + title: message_heading, + message: if message_content == "" { + None + } else { + Some(message_content) + }, + author: CommitAuthor { + email: author + .email() + .ok_or(eyre::anyhow!("could not get authors email"))? + .to_string(), + name: author + .name() + .ok_or(eyre::anyhow!("could not get authors name"))? + .to_string(), + }, + time: timestamp.seconds(), + }; + + commits.push(commit); + } + + let commits_str = serde_json::to_string(&commits)?; + + Ok(commits_str) +} diff --git a/src/main.rs b/src/main.rs index ad67c21..d44c407 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,22 +1,17 @@ -use git2::Repository; +use clap::Parser; + +#[derive(Parser, Debug)] +#[command(author, version, about, long_about = None)] +struct Cli { + #[arg(short, long, required = true)] + path: String, +} fn main() -> eyre::Result<()> { - let repo = Repository::open(".git")?; - let mut revwalk = repo.revwalk()?; - revwalk.set_sorting(git2::Sort::TIME)?; + let args = Cli::parse(); - let revspec = repo.revparse("HEAD")?; - let from = revspec.from().ok_or(eyre::anyhow!("could not get HEAD"))?; - revwalk.push(from.id())?; - - for rev in revwalk.enumerate() { - let oid = rev.1?; - let commit = repo.find_commit(oid)?; - let message = commit - .message() - .ok_or(eyre::anyhow!("could not find message from commit"))?; - println!("rev: {}", message) - } + let log = gitlog_core::get_git_log(args.path)?; + println!("{}", log); Ok(()) }