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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
//! Revision resolution metadata and typed object contents for display (`resolve` / `cat`).
//!
//! [`Repository::resolve`] resolves a user spec to an object id plus kind and size.
//! [`Repository::read_object_for_display`] loads and parses object bytes for programmatic use.
//!
//! ```rust
//! use grit_lib::object_inspect::ResolveError;
//! use grit_lib::repo::{InitOptions, Repository};
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let dir = tempfile::tempdir()?;
//! let repo = Repository::init(dir.path(), &InitOptions::default())?;
//! match repo.resolve("HEAD") {
//! Ok(_resolved) => {}
//! Err(ResolveError::UnknownRevision { .. }) => {}
//! Err(e) => return Err(e.into()),
//! }
//! # Ok(())
//! # }
//! ```
use thiserror::Error;
use crate::error::{Error, Result};
use crate::objects::{
parse_commit, parse_tag, parse_tree, CommitData, Object, ObjectId, ObjectKind, TagData,
TreeEntry,
};
use crate::repo::Repository;
use crate::rev_parse::{
abbreviate_object_id, ambiguous_object_hint_lines, parse_peel_suffix, resolve_revision,
symbolic_full_name,
};
use crate::rev_parse_error::{AmbiguousChangeIdHint, AmbiguousObjectHint, RevParseError};
/// Failure resolving a revision spec for object inspection.
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ResolveError {
/// The spec does not name a known revision or object.
#[error("unknown revision '{spec}'")]
UnknownRevision {
/// User revision string.
spec: String,
},
/// A short hex prefix matches more than one object and could not be disambiguated.
#[error("ambiguous short object id '{spec}'")]
AmbiguousShortObject {
/// User revision string.
spec: String,
/// Candidate objects in Git hint order (type, then hex).
candidates: Vec<AmbiguousObjectHint>,
},
/// A change-id prefix matches more than one commit.
#[error("ambiguous change id '{spec}'")]
AmbiguousChangeId {
/// User revision string.
spec: String,
/// Matching commits (oid, change id, subject).
candidates: Vec<AmbiguousChangeIdHint>,
},
/// A path component is missing from the tree named by the revision.
#[error("path '{path}' does not exist in '{revision}'")]
PathNotInTree {
/// Path that is missing from the tree.
path: String,
/// Revision label for the tree that was searched.
revision: String,
},
}
impl From<RevParseError> for ResolveError {
fn from(value: RevParseError) -> Self {
match value {
RevParseError::PathNotInRevision { path, revision }
| RevParseError::PathOnDiskNotInRevision { path, revision } => {
ResolveError::PathNotInTree { path, revision }
}
RevParseError::InvalidObjectName { name } => {
ResolveError::UnknownRevision { spec: name }
}
RevParseError::AmbiguousChangeId { prefix, candidates } => {
ResolveError::AmbiguousChangeId {
spec: prefix,
candidates,
}
}
other => ResolveError::UnknownRevision {
spec: other.to_string(),
},
}
}
}
/// Result of resolving one revision spec to a concrete object.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResolvedObject {
/// Resolved object id (after peel suffixes in the spec).
pub oid: ObjectId,
/// Kind of the resolved object.
pub kind: ObjectKind,
/// Uncompressed object payload size in bytes.
pub size: u64,
/// Short ref name when the spec named a ref directly (for example `main`, `v1.0`).
pub ref_name: Option<String>,
}
/// Parsed object payload for display or serialization.
#[derive(Debug, Clone)]
pub enum ObjectContents {
/// Raw blob bytes.
Blob(Vec<u8>),
/// Tree entries in Git sort order.
Tree(Vec<TreeEntry>),
/// Parsed commit headers and message.
Commit(CommitData),
/// Parsed annotated tag.
Tag(TagData),
}
impl Repository {
/// Resolve `spec` to an object id, kind, and size.
///
/// Supports the same revision forms as [`crate::revwalk::rev_parse::resolve_revision`]
/// (for example `HEAD~2`, `v1^{tree}`, `main:path`, `@{1}`, abbreviated oids).
///
/// # Errors
///
/// Returns [`ResolveError::UnknownRevision`] when the spec cannot be resolved,
/// [`ResolveError::AmbiguousShortObject`] when a short oid is ambiguous, and
/// [`ResolveError::PathNotInTree`] when a `rev:path` path is missing.
pub fn resolve(&self, spec: &str) -> std::result::Result<ResolvedObject, ResolveError> {
let oid = match resolve_revision(self, spec) {
Ok(o) => o,
Err(Error::ObjectNotFound(_)) => {
return Err(ResolveError::UnknownRevision {
spec: spec.to_owned(),
});
}
Err(Error::RevParse(RevParseError::AmbiguousShortObject { spec: prefix })) => {
let candidates =
ambiguous_object_hint_lines(self, &prefix, None).unwrap_or_default();
return Err(ResolveError::AmbiguousShortObject {
spec: spec.to_owned(),
candidates,
});
}
Err(Error::RevParse(RevParseError::AmbiguousChangeId { prefix, candidates })) => {
return Err(ResolveError::AmbiguousChangeId {
spec: prefix,
candidates,
});
}
Err(Error::RevParse(e)) => return Err(e.into()),
Err(_) => {
return Err(ResolveError::UnknownRevision {
spec: spec.to_owned(),
});
}
};
let info = self
.read_info_replaced(&oid)
.map_err(|_| ResolveError::UnknownRevision {
spec: spec.to_owned(),
})?;
Ok(ResolvedObject {
oid,
kind: info.kind,
size: info.size,
ref_name: infer_ref_name(self, spec, oid),
})
}
/// Read uncompressed object bytes for display, following replace refs when enabled.
///
/// # Errors
///
/// Returns [`Error::ObjectNotFound`] when the object is missing.
pub fn read_object_raw_for_display(&self, oid: &ObjectId) -> Result<Object> {
self.read_replaced(oid)
}
/// Read and parse the object named by `oid` for display.
///
/// # Errors
///
/// Returns [`Error::ObjectNotFound`] when the object is missing and
/// [`Error::CorruptObject`] when parsing fails.
pub fn read_object_for_display(&self, oid: &ObjectId) -> Result<ObjectContents> {
let obj = self.read_object_raw_for_display(oid)?;
match obj.kind {
ObjectKind::Blob => Ok(ObjectContents::Blob(obj.data)),
ObjectKind::Tree => Ok(ObjectContents::Tree(parse_tree(&obj.data)?)),
ObjectKind::Commit => Ok(ObjectContents::Commit(parse_commit(&obj.data)?)),
ObjectKind::Tag => Ok(ObjectContents::Tag(parse_tag(&obj.data)?)),
}
}
}
/// Format a tree entry mode for `git cat-file -p` output (six-digit tree modes).
#[must_use]
pub fn tree_entry_mode_cat_file_p(mode: u32) -> String {
if mode == 0o040000 {
"040000".to_owned()
} else {
format!("{:o}", mode)
}
}
/// Object type label for one tree entry in `git cat-file -p` / `ls-tree` output.
///
/// Git uses mode for trees and gitlinks; other entries use the stored object kind when present.
#[must_use]
pub fn tree_entry_cat_file_type(repo: &Repository, mode: u32, oid: &ObjectId) -> &'static str {
if mode == 0o040000 {
return "tree";
}
if mode == 0o160000 {
return "commit";
}
if let Ok(info) = repo.odb().read_info(oid) {
return object_kind_cat_file_label(info.kind);
}
"blob"
}
/// Format a tree object's payload like `git cat-file -p` (one line per entry).
///
/// # Errors
///
/// Returns [`Error::CorruptObject`] when `data` is not a valid tree.
pub fn format_tree_cat_file_p(repo: &Repository, data: &[u8]) -> Result<Vec<u8>> {
let entries = parse_tree(data)?;
let mut out = Vec::new();
for e in &entries {
out.extend_from_slice(tree_entry_mode_cat_file_p(e.mode).as_bytes());
out.push(b' ');
out.extend_from_slice(tree_entry_cat_file_type(repo, e.mode, &e.oid).as_bytes());
out.push(b' ');
out.extend_from_slice(e.oid.to_hex().as_bytes());
out.push(b'\t');
out.extend_from_slice(&e.name);
out.push(b'\n');
}
Ok(out)
}
fn object_kind_cat_file_label(kind: ObjectKind) -> &'static str {
match kind {
ObjectKind::Blob => "blob",
ObjectKind::Tree => "tree",
ObjectKind::Commit => "commit",
ObjectKind::Tag => "tag",
}
}
/// Abbreviate `oid` for human output using repository uniqueness rules.
pub fn abbreviate_for_display(repo: &Repository, oid: ObjectId, short: bool) -> Result<String> {
if short {
abbreviate_object_id(repo, oid, 7)
} else {
Ok(oid.to_hex())
}
}
fn infer_ref_name(repo: &Repository, spec: &str, oid: ObjectId) -> Option<String> {
let rev_part = spec.split(':').next().unwrap_or(spec);
let (base, _) = parse_peel_suffix(rev_part);
if base.is_empty() || looks_like_abbrev_hex(base, repo) {
return None;
}
if symbolic_full_name(repo, base).is_some() {
return Some(base.to_owned());
}
for prefix in ["refs/heads/", "refs/tags/"] {
let full = format!("{prefix}{base}");
if repo.resolve_ref_name(&full).ok() == Some(oid) {
return Some(base.to_owned());
}
}
None
}
fn looks_like_abbrev_hex(base: &str, repo: &Repository) -> bool {
if base.len() >= repo.odb().hash_algo().hex_len() {
return base.chars().all(|c| c.is_ascii_hexdigit());
}
base.len() >= 4 && base.chars().all(|c| c.is_ascii_hexdigit())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn resolve_error_from_path_not_in_revision() {
let err = ResolveError::from(RevParseError::PathNotInRevision {
path: "missing.txt".into(),
revision: "HEAD".into(),
});
assert!(matches!(err, ResolveError::PathNotInTree { .. }));
}
#[test]
fn tree_entry_mode_cat_file_p_uses_six_digit_tree_mode() {
assert_eq!(tree_entry_mode_cat_file_p(0o040000), "040000");
assert_eq!(tree_entry_mode_cat_file_p(0o100644), "100644");
}
}