From a239a3f80bdd74a56e64cbcb69e686b8dc968c51 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Sun, 7 Jul 2019 13:30:17 +0200 Subject: [PATCH] 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();