Skip to content

Commit

Permalink
Add the resolution of the working directory +
Browse files Browse the repository at this point in the history
Solve the case of file deletion / creation
Refactor main.rs
  • Loading branch information
menoude committed Sep 12, 2019
1 parent 6e992f7 commit 5b24983
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 49 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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).
14 changes: 9 additions & 5 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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<Commit<'a>> {
let commit = repo.find_commit(Oid::from_str(candidate)?)?;
Ok(commit)
}
21 changes: 16 additions & 5 deletions src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
8 changes: 0 additions & 8 deletions src/commit.rs

This file was deleted.

3 changes: 2 additions & 1 deletion src/dependencies.rs
Original file line number Diff line number Diff line change
@@ -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<HashMap<String, Vec<String>>> {
pub fn create_dep_map<P: AsRef<Path>>(working_dir: P) -> Result<HashMap<String, Vec<String>>> {
let configs = WalkDir::new(working_dir).into_iter().filter_entry(|e| {
e.file_name()
.to_str()
Expand Down
10 changes: 6 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ 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 {}

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)
}
Expand All @@ -25,4 +27,4 @@ impl From<git2::Error> for ModifError {
fn from(err: git2::Error) -> Self {
ModifError::CommitError(err)
}
}
}
44 changes: 18 additions & 26 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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<T> = std::result::Result<T, error::ModifError>;

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);
}
};
})
}

0 comments on commit 5b24983

Please sign in to comment.