From bdb10fd54ad47cf3f57bd3c44f15b95c43bc9aae Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Sun, 7 Jul 2019 13:28:43 +0200 Subject: [PATCH 1/6] Add commit count to the cache struct --- src/cache.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/cache.rs b/src/cache.rs index fd0aacf..5795522 100644 --- a/src/cache.rs +++ b/src/cache.rs @@ -9,7 +9,7 @@ use std::{ /// Enum to indicate the state of the cache pub(crate) enum CacheState<'a> { /// Current head and cached head are the same - Current(u64), + Current { count: u64, commits: u64 }, /// Cached head is older than current head Old(Cache<'a>), /// No cache was found @@ -21,7 +21,10 @@ impl<'a> CacheState<'a> { if path.as_ref().exists() { let cache: Cache = serde_json::from_reader(BufReader::new(File::open(path)?))?; if cache.head == head { - Ok(CacheState::Current(cache.count)) + Ok(CacheState::Current { + count: cache.count, + commits: cache.commits, + }) } else { Ok(CacheState::Old(cache)) } @@ -30,22 +33,31 @@ impl<'a> CacheState<'a> { } } - pub(crate) fn calculate_new_cache(self, count: u64, head: Cow<'a, str>) -> Cache { + pub(crate) fn calculate_new_cache(self, count: u64, commits: u64, head: Cow<'a, str>) -> Cache { match self { CacheState::Old(mut cache) => { cache.head = head; cache.count += count; + cache.commits += commits; cache } - CacheState::No | CacheState::Current(_) => Cache { head, count }, + CacheState::No | CacheState::Current { .. } => Cache { + head, + count, + commits, + }, } } } #[derive(Serialize, Deserialize)] pub(crate) struct Cache<'a> { + /// HEAD commit ref pub head: Cow<'a, str>, + /// HoC value pub count: u64, + /// Number of commits + pub commits: u64, } impl<'a> Cache<'a> { From ed1cafafd0e405c6541b811c8730462ca5c7b771 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Sun, 7 Jul 2019 13:29:03 +0200 Subject: [PATCH 2/6] Show commit count in overview page --- templates/overview.rs.html | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/templates/overview.rs.html b/templates/overview.rs.html index 1a768a9..6b51ad2 100644 --- a/templates/overview.rs.html +++ b/templates/overview.rs.html @@ -1,12 +1,13 @@ @use super::base; @use crate::statics::VersionInfo; -@(version_info: VersionInfo, repo_count: usize, domain: &str, path: &str, url: &str, hoc: u64, hoc_pretty: &str, head: &str, commit_url: &str) +@(version_info: VersionInfo, repo_count: usize, domain: &str, path: &str, url: &str, hoc: u64, hoc_pretty: &str, head: &str, commit_url: &str, commits: u64) @:base("Hits-of-Code Badges", "Overview", {

-The project @url has @hoc_pretty (exactly @hoc) hits of code at @head. +The project @url has @hoc_pretty (exactly @hoc) hits of code at +@head. The repository contains @commits commits.

From 00b4e60341ba13ef16f626e77bb403151ba9f093 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Sun, 7 Jul 2019 13:29:26 +0200 Subject: [PATCH 3/6] Add parse error --- src/error.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/error.rs b/src/error.rs index 0f6edf2..49f70cf 100644 --- a/src/error.rs +++ b/src/error.rs @@ -16,6 +16,7 @@ pub(crate) enum Error { Io(std::io::Error), Log(log::SetLoggerError), LogBuilder(log4rs::config::Errors), + Parse(std::num::ParseIntError), Serial(serde_json::Error), } @@ -29,6 +30,7 @@ impl fmt::Display for Error { Error::Io(e) => write!(fmt, "Io({})", e), Error::Log(e) => write!(fmt, "Log({})", e), Error::LogBuilder(e) => write!(fmt, "LogBuilder({})", e), + Error::Parse(e) => write!(fmt, "Parse({})", e), Error::Serial(e) => write!(fmt, "Serial({})", e), } } @@ -91,3 +93,9 @@ impl From for Error { Error::LogBuilder(err) } } + +impl From for Error { + fn from(err: std::num::ParseIntError) -> Self { + Error::Parse(err) + } +} From a239a3f80bdd74a56e64cbcb69e686b8dc968c51 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Sun, 7 Jul 2019 13:30:17 +0200 Subject: [PATCH 4/6] Calculate number of commits The number of commits is shown in the overview page and returned in the JSON response. Closes #33 --- src/main.rs | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index 71adfa5..ba03cfe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,6 +60,7 @@ struct State { struct JsonResponse<'a> { head: &'a str, count: u64, + commits: u64, } fn pull(path: impl AsRef) -> Result<()> { @@ -69,17 +70,13 @@ fn pull(path: impl AsRef) -> Result<()> { Ok(()) } -fn hoc(repo: &str, repo_dir: &str, cache_dir: &str) -> Result<(u64, String)> { +fn hoc(repo: &str, repo_dir: &str, cache_dir: &str) -> Result<(u64, String, u64)> { let repo_dir = format!("{}/{}", repo_dir, repo); let cache_dir = format!("{}/{}.json", cache_dir, repo); let cache_dir = Path::new(&cache_dir); - let head = format!( - "{}", - Repository::open_bare(&repo_dir)? - .head()? - .target() - .ok_or(Error::Internal)? - ); + let repo = Repository::open_bare(&repo_dir)?; + let head = format!("{}", repo.head()?.target().ok_or(Error::Internal)?); + let mut arg_commit_count = vec!["rev-list".to_string(), "--count".to_string()]; let mut arg = vec![ "log".to_string(), "--pretty=tformat:".to_string(), @@ -94,16 +91,18 @@ fn hoc(repo: &str, repo_dir: &str, cache_dir: &str) -> Result<(u64, String)> { ]; let cache = CacheState::read_from_file(&cache_dir, &head)?; match &cache { - CacheState::Current(res) => { + CacheState::Current { count, commits } => { info!("Using cache for {}", repo_dir); - return Ok((*res, head)); + return Ok((*count, head, *commits)); } CacheState::Old(cache) => { info!("Updating cache for {}", repo_dir); arg.push(format!("{}..HEAD", cache.head)); + arg_commit_count.push(format!("{}..HEAD", cache.head)); } CacheState::No => { info!("Creating cache for {}", repo_dir); + arg_commit_count.push("HEAD".to_string()); } }; arg.push("--".to_string()); @@ -114,6 +113,13 @@ fn hoc(repo: &str, repo_dir: &str, cache_dir: &str) -> Result<(u64, String)> { .output()? .stdout; let output = String::from_utf8_lossy(&output); + let output_commits = Command::new("git") + .args(&arg_commit_count) + .current_dir(&repo_dir) + .output()? + .stdout; + let output_commits = String::from_utf8_lossy(&output_commits); + let commits: u64 = output_commits.trim().parse()?; let count: u64 = output .lines() .map(|s| { @@ -125,10 +131,10 @@ fn hoc(repo: &str, repo_dir: &str, cache_dir: &str) -> Result<(u64, String)> { }) .sum(); - let cache = cache.calculate_new_cache(count, (&head).into()); + let cache = cache.calculate_new_cache(count, commits, (&head).into()); cache.write_to_file(cache_dir)?; - Ok((cache.count, head)) + Ok((cache.count, head, commits)) } fn remote_exists(url: &str) -> Result { @@ -138,6 +144,7 @@ fn remote_exists(url: &str) -> Result { enum HocResult { Hoc { hoc: u64, + commits: u64, hoc_pretty: String, head: String, url: String, @@ -176,15 +183,16 @@ where REPO_COUNT.fetch_add(1, Ordering::Relaxed); } pull(&path)?; - let (hoc, head) = hoc(&service_path, &state.repos, &state.cache)?; + let (hoc, head, commits) = hoc(&service_path, &state.repos, &state.cache)?; let hoc_pretty = match NumberPrefix::decimal(hoc as f64) { Standalone(hoc) => hoc.to_string(), Prefixed(prefix, hoc) => format!("{:.1}{}", hoc, prefix), }; Ok(HocResult::Hoc { hoc, + commits, hoc_pretty, - head, + head: head.to_string(), url, repo, service_path, @@ -199,9 +207,12 @@ fn json_hoc( ) -> impl Future { let mapper = |r| match r { HocResult::NotFound => p404(), - HocResult::Hoc { hoc, head, .. } => Ok(HttpResponse::Ok().json(JsonResponse { + HocResult::Hoc { + hoc, head, commits, .. + } => Ok(HttpResponse::Ok().json(JsonResponse { head: &head, count: hoc, + commits, })), }; handle_hoc_request::(state, data, mapper) @@ -248,6 +259,7 @@ fn overview( HocResult::NotFound => p404(), HocResult::Hoc { hoc, + commits, hoc_pretty, url, head, @@ -266,6 +278,7 @@ fn overview( &hoc_pretty, &head, &T::commit_url(&repo, &head), + commits, )?; let (tx, rx_body) = mpsc::unbounded(); From c5bbd14a05b69d7824fe539403656a264d14748b Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Sun, 7 Jul 2019 13:36:14 +0200 Subject: [PATCH 5/6] Update documentation for the JSON endpoint --- templates/index.rs.html | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/templates/index.rs.html b/templates/index.rs.html index 5d23dd2..075b0c4 100644 --- a/templates/index.rs.html +++ b/templates/index.rs.html @@ -46,17 +46,18 @@ alt="example badge" />

-You can also request the HoC as JSON by appending /json to the request path. This will return a JSON -object with two fields: count and head with count being the HoC value and head being the -commit ref of HEAD. Requesting -https://@domain/github/vbrandl/hoc/json might return something -along the lines of +You can also request the HoC as JSON by appending /json to the request path. This will return a JSON object +with three fields: count (the HoC value), commits (the number of commits) and +head (the commit ref of HEAD). Requesting https://@domain/github/vbrandl/hoc/json might return something along +the lines of

 {
-    "head" : "05736ee3ba256ec9a7227c436aef2bf43db109ab",
-    "count": 7582
+    "head": "1f01c3b964b018fb0c0c2c5b572bf4ace2968546",
+    "count": 8324,
+    "commits": 223
 }
 
From c71925f61e23a9599d7e5a8d2022803f172da499 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Sun, 7 Jul 2019 14:52:42 +0200 Subject: [PATCH 6/6] Add migration subcommand --- src/config.rs | 8 ++++++++ src/main.rs | 43 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/config.rs b/src/config.rs index e0a848c..66d02a3 100644 --- a/src/config.rs +++ b/src/config.rs @@ -46,6 +46,14 @@ pub(crate) struct Opt { )] /// The logfile pub(crate) logfile: PathBuf, + #[structopt(subcommand)] + pub(crate) migrate: Option, +} + +#[derive(StructOpt, Debug, Clone, Copy)] +pub(crate) enum Migration { + #[structopt(name = "migrate-commit-count")] + CacheCommitCount, } pub(crate) fn init() -> Result<()> { diff --git a/src/main.rs b/src/main.rs index ba03cfe..ca755d7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,7 @@ mod statics; use crate::{ cache::CacheState, + config::Migration, error::{Error, Result}, service::{Bitbucket, FormService, GitHub, Gitlab, Service}, statics::{CLIENT, CSS, FAVICON, OPT, REPO_COUNT, VERSION_INFO}, @@ -34,7 +35,7 @@ use git2::Repository; use number_prefix::{NumberPrefix, Prefixed, Standalone}; use std::{ borrow::Cow, - fs::create_dir_all, + fs::{create_dir_all, read_dir, rename}, path::Path, process::Command, sync::atomic::Ordering, @@ -341,8 +342,7 @@ fn favicon32() -> HttpResponse { HttpResponse::Ok().content_type("image/png").body(FAVICON) } -fn main() -> Result<()> { - config::init()?; +fn start_server() -> Result<()> { let interface = format!("{}:{}", OPT.host, OPT.port); let state = Arc::new(State { repos: OPT.outdir.display().to_string(), @@ -371,3 +371,40 @@ fn main() -> Result<()> { .bind(interface)? .run()?) } + +fn migrate_cache() -> Result<()> { + let mut backup_cache = OPT.cachedir.clone(); + backup_cache.set_extension("bak"); + rename(&OPT.cachedir, backup_cache)?; + let outdir = OPT.outdir.display().to_string(); + let cachedir = OPT.cachedir.display().to_string(); + for service in read_dir(&OPT.outdir)? { + let service = service?; + for namespace in read_dir(service.path())? { + let namespace = namespace?; + for repo in read_dir(namespace.path())? { + let repo_path = repo?.path().display().to_string(); + let repo_path: String = + repo_path + .split(&outdir) + .fold(String::new(), |mut acc, next| { + acc.push_str(next); + acc + }); + println!("{}", repo_path); + hoc(&repo_path, &outdir, &cachedir)?; + } + } + } + Ok(()) +} + +fn main() -> Result<()> { + config::init()?; + match &OPT.migrate { + None => start_server(), + Some(migration) => match migration { + Migration::CacheCommitCount => migrate_cache(), + }, + } +}