diff --git a/README.md b/README.md new file mode 100644 index 0000000..d53b2de --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# Package modifications controller + +Go to the `working_repository` directory and initialize a git repository. + +Changes that you commit will be tracked (on a commit basis) when executing this program. Pass a --reference (-r) argument to indicate the oldest commit, and an optional --last (-l) for the most recent one (HEAD commit by default). diff --git a/src/args.rs b/src/args.rs index 7d831f6..eeabc6d 100644 --- a/src/args.rs +++ b/src/args.rs @@ -1,8 +1,7 @@ use clap::{App, Arg}; -use crate::commit; -use crate::{error::ModifError, Result}; -use git2::{Commit, Repository}; +use crate::Result; +use git2::{Commit, Oid, Repository}; pub fn get_args<'a>(repo: &'a Repository) -> Result<(Commit<'a>, Commit<'a>)> { let matches = App::new("Modif-finder") @@ -29,14 +28,19 @@ pub fn get_args<'a>(repo: &'a Repository) -> Result<(Commit<'a>, Commit<'a>)> { .get_matches(); let first = match matches.value_of("last-commit") { - Some(param) => commit::valid_commit(param, repo)?, + Some(param) => valid_commit(param, repo)?, None => repo.head()?.peel_to_commit()?, }; let second = match matches.value_of("reference") { - Some(param) => commit::valid_commit(param, repo)?, + Some(param) => valid_commit(param, repo)?, None => panic!(), }; Ok((first, second)) } + +pub fn valid_commit<'a>(candidate: &str, repo: &'a Repository) -> Result> { + let commit = repo.find_commit(Oid::from_str(candidate)?)?; + Ok(commit) +} diff --git a/src/check.rs b/src/check.rs index ed5886d..7ab331a 100644 --- a/src/check.rs +++ b/src/check.rs @@ -10,15 +10,26 @@ pub fn check_modifs<'a>(repo: &Repository, last: Commit<'a>, reference: Commit<' let diff = repo.diff_tree_to_tree(Some(&old_tree), Some(&new_tree), None)?; let mut changed_files = Vec::new(); for delta in diff.deltas() { - if let Some(path) = delta.new_file().path() { - changed_files.push(path); - } + match (delta.old_file().path(), delta.new_file().path()) { + (Some(old_path), Some(new_path)) if old_path != new_path => { + changed_files.push(old_path); + changed_files.push(new_path); + } + (Some(_), Some(new_path)) => changed_files.push(new_path), + (Some(path), _) => changed_files.push(path), + (_, Some(path)) => changed_files.push(path), + (_, _) => panic!( + "Can't find the delta files for {} vs {}", + last.id(), + reference.id() + ), + }; } - find_packets(changed_files)?; + find_package(changed_files)?; Ok(()) } -fn find_packets(modified_files: Vec<&Path>) -> Result<()> { +fn find_package(modified_files: Vec<&Path>) -> Result<()> { for file in modified_files { println!("{:?}", file); } diff --git a/src/commit.rs b/src/commit.rs deleted file mode 100644 index aebbdad..0000000 --- a/src/commit.rs +++ /dev/null @@ -1,8 +0,0 @@ -use crate::error::ModifError; -use crate::Result; -use git2::{Commit, Oid, Repository}; - -pub fn valid_commit<'a>(candidate: &str, repo: &'a Repository) -> Result> { - let commit = repo.find_commit(Oid::from_str(candidate)?)?; - Ok(commit) -} diff --git a/src/dependencies.rs b/src/dependencies.rs index d09f57a..e780942 100644 --- a/src/dependencies.rs +++ b/src/dependencies.rs @@ -1,10 +1,11 @@ use crate::Result; use std::collections::HashMap; +use std::path::Path; use walkdir::{DirEntry, WalkDir}; -pub fn create_dep_map(working_dir: &str) -> Result>> { +pub fn create_dep_map>(working_dir: P) -> Result>> { let configs = WalkDir::new(working_dir).into_iter().filter_entry(|e| { e.file_name() .to_str() diff --git a/src/error.rs b/src/error.rs index 9c37f74..bf9f10a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -6,7 +6,8 @@ use std::{ #[derive(Debug)] pub enum ModifError { CommitError(git2::Error), - NoRepo(String) + NoRepo(git2::Error), + IoError(std::io::Error), } impl std::error::Error for ModifError {} @@ -14,8 +15,9 @@ impl std::error::Error for ModifError {} impl Display for ModifError { fn fmt(&self, f: &mut Formatter) -> fmt::Result { let message = match self { - ModifError::CommitError(e) => e.message(), - ModifError::NoRepo(error_message) => error_message + ModifError::CommitError(e) => format!("Commit error: {}", e), + ModifError::NoRepo(e) => format!("Repository Error: {}", e), + ModifError::IoError(io_err) => io_err.to_string(), }; write!(f, "{}", message) } @@ -25,4 +27,4 @@ impl From for ModifError { fn from(err: git2::Error) -> Self { ModifError::CommitError(err) } -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index 3295d40..df630ca 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,41 +1,33 @@ pub mod args; pub mod check; -pub mod commit; pub mod dependencies; pub mod error; use args::get_args; use git2::Repository; +use std::env::current_dir; use std::process::exit; type Result = std::result::Result; fn main() { - let repo = match Repository::open(".").map_err(|e| error::ModifError::NoRepo(format!("{}", e))) - { - Ok(repository) => repository, - Err(e) => { - println!("{}", e); - exit(1); - } - }; - let map = match dependencies::create_dep_map(".") { - Ok(map) => map, - Err(e) => { - println!("{}", e); - exit(1); - } - }; - match get_args(&repo) { - Ok((last, reference)) => { - if let Err(e) = check::check_modifs(&repo, last, reference) { - println!("{}", e); - exit(1); - } - } - Err(e) => { + let working_dir = current_dir().unwrap_or_else(|e| { + println!("{}", e); + exit(1); + }); + + let repo = Repository::open(&working_dir).unwrap_or_else(|e| { + println!("{}", e); + exit(1); + }); + let map = dependencies::create_dep_map(&working_dir).unwrap_or_else(|e| { + println!("{}", e); + exit(1); + }); + get_args(&repo) + .and_then(|(last, reference)| check::check_modifs(&repo, last, reference)) + .unwrap_or_else(|e| { println!("{}", e); exit(1); - } - }; + }) }