Skip to content

Commit 2d1be13

Browse files
aster-voidclaude
andcommitted
git: only log when new commits are pulled
Show commit range (e.g. "Pulled abc123..def456") instead of logging every sync attempt. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent b5291a2 commit 2d1be13

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

src/git.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@ use anyhow::{Context, Result};
22
use std::path::{Path, PathBuf};
33
use std::process::Command;
44

5-
/// Ensures repo is cloned/synced to cache. Returns cache path.
6-
pub fn ensure_repo(source: &str) -> Result<PathBuf> {
5+
/// Ensures repo is cloned/synced to cache. Returns (cache path, commit_range if updated).
6+
pub fn ensure_repo(source: &str) -> Result<(PathBuf, Option<String>)> {
77
let cache_dir = get_cache_dir(source)?;
88
if let Some(parent) = cache_dir.parent() {
99
std::fs::create_dir_all(parent)?;
1010
}
1111

12-
if cache_dir.exists() {
13-
sync_repo(&cache_dir)?;
12+
let update_info = if cache_dir.exists() {
13+
sync_repo(&cache_dir)?
1414
} else {
1515
clone_repo(source, &cache_dir)?;
16-
}
16+
Some("initial".to_string())
17+
};
1718

18-
Ok(cache_dir)
19+
Ok((cache_dir, update_info))
1920
}
2021

2122
fn clone_repo(source: &str, dest: &Path) -> Result<()> {
@@ -34,7 +35,8 @@ fn clone_repo(source: &str, dest: &Path) -> Result<()> {
3435
Ok(())
3536
}
3637

37-
fn sync_repo(dest: &Path) -> Result<()> {
38+
/// Returns commit range (e.g. "abc123..def456") if new commits were fetched
39+
fn sync_repo(dest: &Path) -> Result<Option<String>> {
3840
// git clone sets up tracking branches for both local and remote repos
3941
let has_upstream = Command::new("git")
4042
.args(["rev-parse", "--abbrev-ref", "@{upstream}"])
@@ -53,9 +55,23 @@ fn sync_repo(dest: &Path) -> Result<()> {
5355
let stderr = String::from_utf8_lossy(&output.stderr);
5456
anyhow::bail!("git pull failed: {}", stderr);
5557
}
58+
59+
let stdout = String::from_utf8_lossy(&output.stdout);
60+
if stdout.contains("Already up to date") {
61+
return Ok(None);
62+
}
63+
64+
// Extract commit range from "Updating abc123..def456"
65+
let range = stdout
66+
.lines()
67+
.find(|l| l.starts_with("Updating "))
68+
.and_then(|l| l.strip_prefix("Updating "))
69+
.map(|s| s.to_string());
70+
71+
return Ok(range.or_else(|| Some("updated".to_string())));
5672
}
5773

58-
Ok(())
74+
Ok(None)
5975
}
6076

6177
fn get_cache_dir(source: &str) -> Result<PathBuf> {

src/main.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ async fn main() -> Result<()> {
4949
println!("[rollcron] Pull interval: {}s", args.pull_interval);
5050

5151
// Initial sync
52-
let sot_path = git::ensure_repo(&source)?;
52+
let (sot_path, _) = git::ensure_repo(&source)?;
5353
println!("[rollcron] Cache: {}", sot_path.display());
5454

5555
let (initial_runner, initial_jobs) = load_config(&sot_path)?;
@@ -72,14 +72,19 @@ async fn main() -> Result<()> {
7272
loop {
7373
ticker.tick().await;
7474

75-
let sot = match git::ensure_repo(&source_clone) {
76-
Ok(p) => p,
75+
let (sot, update_info) = match git::ensure_repo(&source_clone) {
76+
Ok(r) => r,
7777
Err(e) => {
7878
eprintln!("[rollcron] Sync failed: {}", e);
7979
continue;
8080
}
8181
};
82-
println!("[rollcron] Synced from upstream");
82+
83+
let Some(range) = update_info else {
84+
continue;
85+
};
86+
87+
println!("[rollcron] Pulled {}", range);
8388

8489
match load_config(&sot) {
8590
Ok((runner, jobs)) => {
@@ -90,7 +95,6 @@ async fn main() -> Result<()> {
9095
}
9196
drop(running);
9297
let _ = tx.send((runner, jobs));
93-
println!("[rollcron] Synced job directories");
9498
}
9599
Err(e) => eprintln!("[rollcron] Failed to reload config: {}", e),
96100
}

0 commit comments

Comments
 (0)