Skip to content

Commit

Permalink
feature: add current branch commits reader
Browse files Browse the repository at this point in the history
  • Loading branch information
ScarletFlash committed Jan 16, 2022
1 parent 91cd828 commit cb30531
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions src/subcommands/commit_hashes.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use clap::{App, AppSettings, Arg, ArgMatches};
use git2::{Repository, RepositoryState};
use git2::{Branch, Odb, Reference, Repository};
use std::env::current_dir;
use std::path::PathBuf;

pub fn get_app() -> App<'static> {
return App::new("commit-hashes")
Expand Down Expand Up @@ -42,19 +44,33 @@ pub fn get_app() -> App<'static> {

pub fn handle_matches(matches: &ArgMatches) -> () {
match matches.subcommand() {
| Some(("commit-hashes", _sub_matches)) => {
println!("commit-hashes");
| Some(("commit-hashes", _sub_matches)) => get_commit_hashes(),
| _ => (),
}
}

let repository = match Repository::open("./") {
| Ok(repository) => repository,
| Err(error) => panic!("failed to open: {}", error),
};
fn get_commit_hashes() {
let current_dir_path: PathBuf = current_dir().unwrap();
let current_path: String = current_dir_path.to_str().unwrap().to_owned();

let path: &str = repository.path().to_str().unwrap();
println!("{}", String::from(path));
let repository: Repository = match Repository::open(current_path) {
| Ok(repository) => repository,
| Err(error) => panic!("Git repository not found: {}", error),
};

let state: RepositoryState = repository.state();
},
| _ => (),
let is_head_detached: bool = repository.head_detached().unwrap_or(false);
if is_head_detached {
panic!("Can not access Git commits on detached HEAD")
}

let head: Reference = repository.head().unwrap();
if !head.is_branch() {
panic!("HEAD should be on branch to make commits interaction available");
}

let branch: Branch = Branch::wrap(head);

println!("{}", branch.name().unwrap().unwrap().to_owned());

let _object_database: Odb = repository.odb().unwrap();
}

0 comments on commit cb30531

Please sign in to comment.