-
-
Notifications
You must be signed in to change notification settings - Fork 259
/
resolve.rs
58 lines (52 loc) · 1.74 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
use crate::OutputFormat;
pub struct Options {
pub format: OutputFormat,
pub explain: bool,
}
pub(crate) mod function {
use anyhow::bail;
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 }: 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)?.detach();
writeln!(out, "{spec}")?;
}
}
#[cfg(feature = "serde1")]
OutputFormat::Json => {
if explain {
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(())
}
}