Skip to content

Commit

Permalink
Return the number of fetched commits
Browse files Browse the repository at this point in the history
  • Loading branch information
Notgnoshi committed Dec 29, 2024
1 parent f4bba61 commit a170e91
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion herostratus/src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn check(args: &CheckArgs) -> eyre::Result<()> {

pub fn check_all(args: &CheckAllArgs, config: &Config, data_dir: &Path) -> eyre::Result<()> {
if !args.no_fetch {
crate::commands::fetch_all(&args.into(), config, data_dir)?
let _fetched = crate::commands::fetch_all(&args.into(), config, data_dir)?;
}

tracing::info!("Checking repositories ...");
Expand Down
7 changes: 4 additions & 3 deletions herostratus/src/commands/fetch_all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ use crate::cli::FetchAllArgs;
use crate::config::Config;
use crate::git::clone::{clone_repository, fetch_remote, find_local_repository};

pub fn fetch_all(_args: &FetchAllArgs, config: &Config, _data_dir: &Path) -> eyre::Result<()> {
pub fn fetch_all(_args: &FetchAllArgs, config: &Config, _data_dir: &Path) -> eyre::Result<usize> {
tracing::info!("Fetching repositories ...");
let start = Instant::now();
let mut fetched_commits = 0;
for (name, config) in config.repositories.iter() {
let span = tracing::debug_span!("fetch", name = name);
let _enter = span.enter();
Expand All @@ -27,7 +28,7 @@ pub fn fetch_all(_args: &FetchAllArgs, config: &Config, _data_dir: &Path) -> eyr
};

if !skip_fetch {
fetch_remote(config, &repo)?
fetched_commits += fetch_remote(config, &repo)?
}
}
tracing::info!(
Expand All @@ -36,5 +37,5 @@ pub fn fetch_all(_args: &FetchAllArgs, config: &Config, _data_dir: &Path) -> eyr
start.elapsed()
);

Ok(())
Ok(fetched_commits)
}
11 changes: 8 additions & 3 deletions herostratus/src/git/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,13 @@ fn fetch_options(config: &crate::config::RepositoryConfig) -> git2::FetchOptions
pub fn fetch_remote(
config: &crate::config::RepositoryConfig,
repo: &git2::Repository,
) -> eyre::Result<()> {
) -> eyre::Result<usize> {
let mut remote = repo.find_remote("origin")?;
assert_eq!(
remote.url().unwrap_or_default(),
config.url.as_str(),
"RepositoryConfig and remote 'origin' don't agree on the URL"
);
let reference_name = config.branch.as_deref().unwrap_or("HEAD");
// If this is the first time this reference is being fetched, fetch it like
// git fetch origin branch:branch
Expand All @@ -145,11 +150,11 @@ pub fn fetch_remote(
let reference = repo.resolve_reference_from_short_name(reference_name)?;
let after = reference.peel_to_commit()?;

let mut new_commits: usize = 0;
if before.is_some() && before.as_ref().unwrap().id() == after.id() {
tracing::debug!("... done. No new commits");
} else {
let commits = crate::git::rev::walk(after.id(), repo)?;
let mut new_commits: usize = 0;
for commit_id in commits {
if let Some(before) = &before {
if commit_id? == before.id() {
Expand All @@ -161,7 +166,7 @@ pub fn fetch_remote(
tracing::debug!("... done. {new_commits} new commits");
}

Ok(())
Ok(new_commits)
}

pub fn clone_repository(
Expand Down

0 comments on commit a170e91

Please sign in to comment.