/
resolve.rs
80 lines (73 loc) · 2.45 KB
/
resolve.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use crate::OutputFormat;
pub struct Options {
pub format: OutputFormat,
pub explain: bool,
pub cat_file: bool,
}
pub(crate) mod function {
use anyhow::Context;
use std::ffi::OsString;
use git_repository as git;
use super::Options;
use crate::repository::revision;
use crate::OutputFormat;
pub fn resolve(
mut repo: git::Repository,
specs: Vec<OsString>,
mut out: impl std::io::Write,
Options {
format,
explain,
cat_file,
}: Options,
) -> anyhow::Result<()> {
repo.object_cache_size_if_unset(1024 * 1024);
match format {
OutputFormat::Human => {
for spec in specs {
if explain {
return revision::explain(spec, out);
}
let spec = git::path::os_str_into_bstr(&spec)?;
let spec = repo.rev_parse(spec)?;
if cat_file {
return display_object(spec, out);
}
writeln!(out, "{spec}", spec = spec.detach())?;
}
}
#[cfg(feature = "serde1")]
OutputFormat::Json => {
if explain {
anyhow::bail!("Explanations are only for human consumption")
}
serde_json::to_writer_pretty(
&mut out,
&specs
.into_iter()
.map(|spec| {
git::path::os_str_into_bstr(&spec)
.map_err(anyhow::Error::from)
.and_then(|spec| repo.rev_parse(spec).map_err(Into::into))
.map(|spec| spec.detach())
})
.collect::<Result<Vec<_>, _>>()?,
)?;
}
}
Ok(())
}
fn display_object(spec: git::revision::Spec<'_>, mut out: impl std::io::Write) -> anyhow::Result<()> {
let id = spec.single().context("rev-spec must resolve to a single object")?;
let object = id.object()?;
match object.kind {
git::object::Kind::Tree => {
for entry in object.into_tree().iter() {
writeln!(out, "{}", entry?)?;
}
}
_ => out.write_all(&object.data)?,
}
Ok(())
}
}