step twenty nine: log, use iter_commits_and_parents
This commit is contained in:
parent
cb9b476ef9
commit
ba6bd7aac9
10
src/base.rs
10
src/base.rs
|
@ -1,4 +1,4 @@
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet, VecDeque};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
@ -109,13 +109,12 @@ pub fn get_commit(oid: String) -> Commit {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn iter_commits_and_parents(mut oids: HashSet<String>) -> Vec<String> {
|
pub fn iter_commits_and_parents(mut oids: VecDeque<String>) -> Vec<String> {
|
||||||
let mut visited: HashSet<String> = HashSet::new();
|
let mut visited: HashSet<String> = HashSet::new();
|
||||||
let mut oid_sequence = vec![];
|
let mut oid_sequence = vec![];
|
||||||
|
|
||||||
while !oids.is_empty() {
|
while !oids.is_empty() {
|
||||||
let oid = oids.iter().next().cloned().unwrap();
|
let oid = oids.pop_front().unwrap();
|
||||||
oids.remove(&oid);
|
|
||||||
if oid == "" || visited.contains(&oid) {
|
if oid == "" || visited.contains(&oid) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -123,7 +122,8 @@ pub fn iter_commits_and_parents(mut oids: HashSet<String>) -> Vec<String> {
|
||||||
oid_sequence.push(oid.clone());
|
oid_sequence.push(oid.clone());
|
||||||
|
|
||||||
let commit = get_commit(oid);
|
let commit = get_commit(oid);
|
||||||
oids.insert(commit.parent);
|
// Deal with parent next
|
||||||
|
oids.push_front(commit.parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
return oid_sequence;
|
return oid_sequence;
|
||||||
|
|
14
src/main.rs
14
src/main.rs
|
@ -1,5 +1,5 @@
|
||||||
use clap::{App, Arg, ArgMatches, SubCommand};
|
use clap::{App, Arg, ArgMatches, SubCommand};
|
||||||
use std::collections::HashSet;
|
use std::collections::{HashSet, VecDeque};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::process::{Command, Stdio};
|
use std::process::{Command, Stdio};
|
||||||
|
@ -120,9 +120,11 @@ fn commit(matches: ArgMatches) {
|
||||||
fn log_commits(matches: ArgMatches) {
|
fn log_commits(matches: ArgMatches) {
|
||||||
if let Some(cmd_matches) = matches.subcommand_matches("log") {
|
if let Some(cmd_matches) = matches.subcommand_matches("log") {
|
||||||
let provided_ref = cmd_matches.value_of("oid").unwrap().to_owned();
|
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());
|
let commit = base::get_commit(oid.clone());
|
||||||
|
|
||||||
println!("commit {}", oid);
|
println!("commit {}", oid);
|
||||||
|
@ -132,8 +134,6 @@ fn log_commits(matches: ArgMatches) {
|
||||||
if commit.parent == "" {
|
if commit.parent == "" {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
oid = commit.parent;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -156,11 +156,11 @@ fn tag(matches: ArgMatches) {
|
||||||
|
|
||||||
fn k() {
|
fn k() {
|
||||||
let mut dot = "digraph commits {\n".to_owned();
|
let mut dot = "digraph commits {\n".to_owned();
|
||||||
let mut oids = HashSet::new();
|
let mut oids = VecDeque::new();
|
||||||
for refinfo in data::iter_refs() {
|
for refinfo in data::iter_refs() {
|
||||||
dot.push_str(&format!("\"{}\" [shape=note]\n", refinfo.0));
|
dot.push_str(&format!("\"{}\" [shape=note]\n", refinfo.0));
|
||||||
dot.push_str(&format!("\"{}\" -> \"{}\"", refinfo.0, refinfo.1));
|
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) {
|
for oid in base::iter_commits_and_parents(oids) {
|
||||||
|
|
Loading…
Reference in New Issue