step twenty seven: iterate commits and parents

This commit is contained in:
Gonçalo Valério 2020-11-07 19:45:58 +00:00
parent a555ebf6c0
commit e0467dec69
2 changed files with 33 additions and 1 deletions

View File

@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::fs;
use std::io;
use std::path::Path;
@ -109,6 +109,26 @@ pub fn get_commit(oid: String) -> Commit {
};
}
pub fn iter_commits_and_parents(mut oids: HashSet<String>) -> Vec<String> {
let mut visited: HashSet<String> = HashSet::new();
let mut oid_sequence = vec![];
while !oids.is_empty() {
let oid = oids.iter().next().cloned().unwrap();
oids.remove(&oid);
if oid == "" || visited.contains(&oid) {
continue;
}
visited.insert(oid.clone());
oid_sequence.push(oid.clone());
let commit = get_commit(oid);
oids.insert(commit.parent);
}
return oid_sequence;
}
pub fn checkout(oid: String) {
let commit = get_commit(oid.clone());
read_tree(commit.tree);

View File

@ -1,4 +1,5 @@
use clap::{App, Arg, ArgMatches, SubCommand};
use std::collections::HashSet;
use std::fs;
mod base;
mod data;
@ -152,7 +153,18 @@ fn tag(matches: ArgMatches) {
}
fn k() {
let mut oids = HashSet::new();
for refinfo in data::iter_refs() {
println!("{} {}", refinfo.0, refinfo.1);
oids.insert(refinfo.1);
}
for oid in base::iter_commits_and_parents(oids) {
let commit = base::get_commit(oid.clone());
println!("{}", oid);
if commit.parent != "" {
println!("Parent: {}", commit.parent);
}
}
// TODO visualize refs
}