Skip to content

Commit

Permalink
Merge pull request #24 from cosmonaut-nz/re-arch
Browse files Browse the repository at this point in the history
feat: added initial code frequency data into new module "git".
  • Loading branch information
avastmick committed Dec 8, 2023
2 parents c9e8b5f + a59703c commit 6ab9061
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 10 deletions.
17 changes: 15 additions & 2 deletions src/dev_mode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,26 @@ pub mod comment_summary {

#[cfg(debug_assertions)]
pub mod _code_frequency {
use crate::settings::Settings;
use crate::{retrieval::git::file::get_file_change_frequency, settings::Settings};

pub(crate) fn test_code_frequency(
_settings: &Settings,
settings: &Settings,
) -> Result<(), Box<dyn std::error::Error>> {
log::info!("Mod: Testing code frequency.");

let repo_path = settings.repository_path.clone();
let file_path = "src/review/mod.rs";

let (file_commits, total_commits, frequency) =
get_file_change_frequency(&repo_path, file_path)?;

log::info!(
"File commits: {}, total commits: {}, frequency: {}",
file_commits,
total_commits,
frequency
);

Ok(())
}
}
50 changes: 49 additions & 1 deletion src/retrieval/git.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//! Functions to gather data on the 'git' repository, files and contributors

/// Functions to gather data on the 'git' repository
pub(crate) mod repository {
use log::{debug, warn};
use std::fs;
Expand Down Expand Up @@ -52,10 +54,56 @@ pub(crate) mod repository {
}
}

/// Functions to gather data on the 'git' files
pub(crate) mod file {
// TODO add in the code frequency functions
use git2::Repository;

pub(crate) fn get_file_change_frequency(
repo_path: &str,
file_path: &str,
) -> Result<(usize, usize, f32), git2::Error> {
let repo = Repository::open(repo_path)?;
let mut revwalk = repo.revwalk()?;
revwalk.push_head()?;

let mut total_commits = 0;
let mut file_commits = 0;

for commit_id in revwalk {
let commit = repo.find_commit(commit_id?)?;
total_commits += 1;

if commit.parent_count() > 0 {
let parent = commit.parent(0)?;
let commit_tree = commit.tree()?;
let parent_tree = parent.tree()?;

let diff = repo.diff_tree_to_tree(Some(&parent_tree), Some(&commit_tree), None)?;
diff.foreach(
&mut |delta, _| {
let filepath = delta
.new_file()
.path()
.unwrap_or(delta.old_file().path().unwrap());
if filepath.to_str() == Some(file_path) {
file_commits += 1;
}
true
},
None,
None,
None,
)?;
}
}

let frequency = file_commits as f32 / total_commits as f32;

Ok((file_commits, total_commits, frequency))
}
}

/// Functions to gather data on the 'git' contributors
pub(crate) mod contributor {
use crate::review::data::Contributor;
use chrono::{DateTime, NaiveDateTime, Utc};
Expand Down
10 changes: 3 additions & 7 deletions src/review/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ pub(crate) async fn assess_codebase(
) -> Result<String, Box<dyn std::error::Error>> {
let mut review = initialise_repository_review(&settings)?;
let repository_root = validate_repository(PathBuf::from(&settings.repository_path))?;

review.generative_ai_service_and_model(get_service_and_model(&settings));

info!(
"Reviewing: {}, with {}",
review.repository_name,
Expand Down Expand Up @@ -125,18 +125,14 @@ pub(crate) async fn assess_codebase(
}
}

match finalise_review(
finalise_review(
&mut review,
overall_file_count,
&mut review_breakdown,
&breakdown,
&settings,
)
.await
{
Ok(_) => (),
Err(e) => return Err(e),
};
.await?;

create_report(&settings, &review)
}
Expand Down

0 comments on commit 6ab9061

Please sign in to comment.