step fourty: show refs that point to each commit

This commit is contained in:
Gonçalo Valério 2020-11-14 18:22:10 +00:00
parent 57d5f13acc
commit 7b46b2cbfa
1 changed files with 18 additions and 2 deletions

View File

@ -1,5 +1,5 @@
use clap::{App, Arg, ArgMatches, SubCommand};
use std::collections::{HashSet, VecDeque};
use std::collections::{HashMap, HashSet, VecDeque};
use std::fs;
use std::io::Write;
use std::process::{Command, Stdio};
@ -129,6 +129,16 @@ fn commit(matches: ArgMatches) {
fn log_commits(matches: ArgMatches) {
if let Some(cmd_matches) = matches.subcommand_matches("log") {
let provided_ref = cmd_matches.value_of("oid").unwrap().to_owned();
let mut refs: HashMap<String, Vec<String>> = HashMap::new();
for entry in data::iter_refs("", true) {
if refs.contains_key(&entry.1.value) {
refs.get_mut(&entry.1.value).unwrap().push(entry.0);
} else {
refs.insert(entry.1.value, vec![entry.0]);
}
}
let initial_oid = base::get_oid(provided_ref.to_owned());
let mut oids = VecDeque::new();
oids.push_back(initial_oid);
@ -136,7 +146,13 @@ fn log_commits(matches: ArgMatches) {
for oid in base::iter_commits_and_parents(oids) {
let commit = base::get_commit(oid.clone());
println!("commit {}", oid);
let ref_str = if refs.contains_key(&oid) {
refs.get_mut(&oid).unwrap().join(", ")
} else {
"".to_owned()
};
println!("commit {} {}", oid, ref_str);
println!("{}", commit.message);
println!("");