-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathls_tree.rs
44 lines (40 loc) · 1.18 KB
/
ls_tree.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use anyhow::Result;
use clap::Args;
use crate::{
context::Context,
objects::{find_hash, object::Contents, ObjectFile},
};
#[derive(Args, Debug)]
pub(crate) struct LsTreeOptions {
#[arg(long, alias = "")]
name_only: bool,
/// The object name (currently only object hash is supported)
object: String,
}
pub(crate) fn ls_tree(context: Context, options: LsTreeOptions) -> Result<()> {
let hash = find_hash(&context, &options.object)?;
let file = ObjectFile::new(&context, &hash);
let object = file.parse()?;
match object.contents {
Contents::Blob(_) => eprintln!("fatal: not a tree object"),
Contents::Tree(tree) => {
if options.name_only {
for line in tree.lines {
println!("{}", line.name);
}
} else {
for line in tree.lines {
println!("{line}");
}
}
}
Contents::Commit(commit) => {
let options = LsTreeOptions {
name_only: options.name_only,
object: commit.tree,
};
return ls_tree(context, options);
}
};
Ok(())
}