From 7b46b2cbfa153118037dc3eadc693b6329cddb1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20Val=C3=A9rio?= Date: Sat, 14 Nov 2020 18:22:10 +0000 Subject: [PATCH] step fourty: show refs that point to each commit --- src/main.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4b61090..4c587f3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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> = 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!("");