From c37bd2c64db41b3dd70ada080abe50130928484e Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 6 Aug 2024 20:48:53 +0100 Subject: [PATCH] WIP: add log graph to notifications --- Cargo.lock | 7 ++++ Cargo.toml | 3 ++ crates/core/Cargo.toml | 3 ++ crates/core/src/config/graphs.rs | 3 ++ crates/core/src/config/mod.rs | 1 + crates/core/src/git/graph.rs | 58 ++++++++++++++++++++++++++++++++ crates/core/src/git/mod.rs | 1 + 7 files changed, 76 insertions(+) create mode 100644 crates/core/src/config/graphs.rs create mode 100644 crates/core/src/git/graph.rs diff --git a/Cargo.lock b/Cargo.lock index d87b59e..ec22243 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -993,6 +993,7 @@ dependencies = [ "secrecy", "serde", "serde_json", + "take-until", "test-log", "thiserror", "time", @@ -3635,6 +3636,12 @@ dependencies = [ "libc", ] +[[package]] +name = "take-until" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8bdb6fa0dfa67b38c1e66b7041ba9dcf23b99d8121907cd31c807a332f7a0bbb" + [[package]] name = "tempfile" version = "3.10.1" diff --git a/Cargo.toml b/Cargo.toml index 98d5cae..215b768 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -84,6 +84,9 @@ anyhow = "1.0" thiserror = "1.0" pike = "0.1" +# iters +take-until = "0.2" + # file watcher notify = "6.1" diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index ae3cc40..f798ea8 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -57,6 +57,9 @@ serde_json = { workspace = true } mockall = { workspace = true } +#iters +take-until = { workspace = true } + [dev-dependencies] # Testing assert2 = { workspace = true } diff --git a/crates/core/src/config/graphs.rs b/crates/core/src/config/graphs.rs new file mode 100644 index 0000000..890d53c --- /dev/null +++ b/crates/core/src/config/graphs.rs @@ -0,0 +1,3 @@ +use crate::newtype; + +newtype!(LogGraph, Vec, "Git log showing branch positions"); diff --git a/crates/core/src/config/mod.rs b/crates/core/src/config/mod.rs index e1d9f7f..4f1e479 100644 --- a/crates/core/src/config/mod.rs +++ b/crates/core/src/config/mod.rs @@ -7,6 +7,7 @@ mod forge_config; mod forge_details; mod forge_type; pub mod git_dir; +mod graphs; mod host_name; mod registered_webhook; mod remote_url; diff --git a/crates/core/src/git/graph.rs b/crates/core/src/git/graph.rs new file mode 100644 index 0000000..4fd61a6 --- /dev/null +++ b/crates/core/src/git/graph.rs @@ -0,0 +1,58 @@ +// + +use std::borrow::ToOwned; + +use take_until::TakeUntilExt; + +use crate::{GitDir, RepoBranches}; + +// create a graph to log relative positions +// +// ANCESTOR=$(git merge-base --octopus origin/main origin/next origin/dev) +// SHORT_ANCESTOR=$(echo $ANCESTOR | cut -b -7) +fn ancesstor(gitdir: &GitDir, branches: &RepoBranches) -> Option { + let result = std::process::Command::new("/usr/bin/git") + .current_dir(gitdir.to_path_buf()) + .args([ + "merge-base", + "--octopus", + format!("origin/{}", branches.main()).as_str(), + format!("origin/{}", branches.next()).as_str(), + format!("origin/{}", branches.dev()).as_str(), + ]) + .output(); + if let Ok(output) = result { + return String::from_utf8_lossy(output.stdout.as_slice()) + .split('\n') + .take(1) + .map(|line| line.chars().take(7).collect::()) + .collect::>() + .first() + .cloned(); + } + None +} + +// git log --oneline --graph --decorate origin/main origin/dev origin/next | awk "1;/$SHORT_ANCESTOR/{exit}" +fn log(gitdir: &GitDir, branches: &RepoBranches, ancestor: &str) -> Vec { + let result = std::process::Command::new("/usr/bin/git") + .current_dir(gitdir.to_path_buf()) + .args([ + "log", + "--oneline", + "--graph", + "--decorate", + format!("origin/{}", branches.main()).as_str(), + format!("origin/{}", branches.next()).as_str(), + format!("origin/{}", branches.dev()).as_str(), + ]) + .output(); + if let Ok(output) = result { + return String::from_utf8_lossy(output.stdout.as_slice()) + .split('\n') + .take_until(|line| line.starts_with(ancestor)) + .map(ToOwned::to_owned) + .collect(); + } + vec![] +} diff --git a/crates/core/src/git/mod.rs b/crates/core/src/git/mod.rs index d13411a..2c29f03 100644 --- a/crates/core/src/git/mod.rs +++ b/crates/core/src/git/mod.rs @@ -6,6 +6,7 @@ pub mod forge; mod generation; mod git_ref; mod git_remote; +pub mod graph; pub mod push; mod repo_details; pub mod repository;