From ba6bd7aac95bf4e8ce9a21a6959f73f064167f10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20Val=C3=A9rio?= Date: Sat, 7 Nov 2020 22:06:42 +0000 Subject: [PATCH] step twenty nine: log, use iter_commits_and_parents --- src/base.rs | 10 +++++----- src/main.rs | 14 +++++++------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/base.rs b/src/base.rs index 0a5a4bc..53c3cd9 100644 --- a/src/base.rs +++ b/src/base.rs @@ -1,4 +1,4 @@ -use std::collections::{HashMap, HashSet}; +use std::collections::{HashMap, HashSet, VecDeque}; use std::fs; use std::io; use std::path::Path; @@ -109,13 +109,12 @@ pub fn get_commit(oid: String) -> Commit { }; } -pub fn iter_commits_and_parents(mut oids: HashSet) -> Vec { +pub fn iter_commits_and_parents(mut oids: VecDeque) -> Vec { let mut visited: HashSet = HashSet::new(); let mut oid_sequence = vec![]; while !oids.is_empty() { - let oid = oids.iter().next().cloned().unwrap(); - oids.remove(&oid); + let oid = oids.pop_front().unwrap(); if oid == "" || visited.contains(&oid) { continue; } @@ -123,7 +122,8 @@ pub fn iter_commits_and_parents(mut oids: HashSet) -> Vec { oid_sequence.push(oid.clone()); let commit = get_commit(oid); - oids.insert(commit.parent); + // Deal with parent next + oids.push_front(commit.parent); } return oid_sequence; diff --git a/src/main.rs b/src/main.rs index d6bf04f..51fa5e2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use clap::{App, Arg, ArgMatches, SubCommand}; -use std::collections::HashSet; +use std::collections::{HashSet, VecDeque}; use std::fs; use std::io::Write; use std::process::{Command, Stdio}; @@ -120,9 +120,11 @@ 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 oid = base::get_oid(provided_ref.to_owned()); + let initial_oid = base::get_oid(provided_ref.to_owned()); + let mut oids = VecDeque::new(); + oids.push_back(initial_oid); - loop { + for oid in base::iter_commits_and_parents(oids) { let commit = base::get_commit(oid.clone()); println!("commit {}", oid); @@ -132,8 +134,6 @@ fn log_commits(matches: ArgMatches) { if commit.parent == "" { break; } - - oid = commit.parent; } } } @@ -156,11 +156,11 @@ fn tag(matches: ArgMatches) { fn k() { let mut dot = "digraph commits {\n".to_owned(); - let mut oids = HashSet::new(); + let mut oids = VecDeque::new(); for refinfo in data::iter_refs() { dot.push_str(&format!("\"{}\" [shape=note]\n", refinfo.0)); dot.push_str(&format!("\"{}\" -> \"{}\"", refinfo.0, refinfo.1)); - oids.insert(refinfo.1); + oids.push_back(refinfo.1); } for oid in base::iter_commits_and_parents(oids) {