Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add current branch commits reader #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 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,29 @@ 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(true);
assert!(!is_head_detached, "Can not access Git commits on detached HEAD");

let head: Reference = repository.head().unwrap();
assert!(head.is_branch(), "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();
}