step twenty six: print refs

This commit is contained in:
Gonçalo Valério 2020-11-07 13:25:11 +00:00
parent cf4e05e325
commit a555ebf6c0
4 changed files with 60 additions and 0 deletions

30
Cargo.lock generated
View File

@ -108,6 +108,16 @@ version = "0.1.0"
dependencies = [
"clap",
"sha-1",
"walkdir",
]
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]]
@ -162,6 +172,17 @@ version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"
[[package]]
name = "walkdir"
version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "777182bc735b6424e1a57516d35ed72cb8019d85c8c9bf536dccb3445c1a2f7d"
dependencies = [
"same-file",
"winapi",
"winapi-util",
]
[[package]]
name = "winapi"
version = "0.3.9"
@ -178,6 +199,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"

View File

@ -9,3 +9,4 @@ edition = "2018"
[dependencies]
clap = "2.33.3"
sha-1 = "0.9.1"
walkdir = "2"

View File

@ -2,6 +2,7 @@ use sha1::{Digest, Sha1};
use std::fs;
use std::path::Path;
use std::str;
use walkdir::WalkDir;
static RGIT_DIR: &'static str = ".rgit";
@ -57,3 +58,23 @@ pub fn get_ref(reference: String) -> Result<String, Box<dyn std::error::Error +
let oid = fs::read_to_string(format!("{}/{}", RGIT_DIR, reference))?;
return Ok(oid);
}
pub fn iter_refs() -> Vec<(String, String)> {
let mut refs: Vec<(String, String)> = vec![];
refs.push(("HEAD".to_owned(), get_ref("HEAD".to_owned()).unwrap()));
for entry in WalkDir::new(format!("{}/refs/", RGIT_DIR)) {
let item = entry.unwrap();
let metadata = item.metadata().unwrap();
if metadata.is_file() {
let relative_path = item.path().strip_prefix(RGIT_DIR).unwrap();
refs.push((
relative_path.to_str().unwrap().to_owned(),
get_ref(relative_path.to_str().unwrap().to_owned()).unwrap(),
));
}
}
return refs;
}

View File

@ -54,6 +54,7 @@ fn main() {
.arg(Arg::with_name("name").index(1).required(true))
.arg(Arg::with_name("oid").index(2).default_value("@")),
)
.subcommand(SubCommand::with_name("k").about("visualize refs and commits"))
.get_matches();
match matches.subcommand_name() {
@ -66,6 +67,7 @@ fn main() {
Some("log") => log_commits(matches),
Some("checkout") => checkout(matches),
Some("tag") => tag(matches),
Some("k") => k(),
_ => println!("unknown sub command"),
}
}
@ -148,3 +150,9 @@ fn tag(matches: ArgMatches) {
base::create_tag(name, oid);
}
}
fn k() {
for refinfo in data::iter_refs() {
println!("{} {}", refinfo.0, refinfo.1);
}
}