From 49520d1f03ee1d011f6e9f2eb07084d327fe95ae Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Wed, 22 Feb 2023 13:09:48 +0100 Subject: [PATCH] feat: `gix tree entries` with rev-spec support. Previously it wanted a tree-id, now it can derive it itself. --- gitoxide-core/src/repository/tree.rs | 13 +++++-------- src/plumbing/options/mod.rs | 4 ++-- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/gitoxide-core/src/repository/tree.rs b/gitoxide-core/src/repository/tree.rs index cbe9b913352..0f5aec74a72 100644 --- a/gitoxide-core/src/repository/tree.rs +++ b/gitoxide-core/src/repository/tree.rs @@ -2,7 +2,7 @@ use std::{borrow::Cow, io}; use anyhow::bail; -use gix::{prelude::ObjectIdExt, Tree}; +use gix::Tree; use crate::OutputFormat; @@ -176,13 +176,10 @@ pub fn entries( } fn treeish_to_tree<'repo>(treeish: Option<&str>, repo: &'repo gix::Repository) -> anyhow::Result> { - Ok(match treeish { - Some(hex) => gix::hash::ObjectId::from_hex(hex.as_bytes()) - .map(|id| id.attach(repo))? - .object()? - .try_into_tree()?, - None => repo.head()?.peel_to_commit_in_place()?.tree()?, - }) + let spec = treeish + .map(|spec| format!("{spec}^{{tree}}")) + .unwrap_or_else(|| "@^{tree}".into()); + Ok(repo.rev_parse_single(spec.as_str())?.object()?.into_tree()) } fn format_entry( diff --git a/src/plumbing/options/mod.rs b/src/plumbing/options/mod.rs index 2f8617382b1..858ad45b4c3 100644 --- a/src/plumbing/options/mod.rs +++ b/src/plumbing/options/mod.rs @@ -253,7 +253,7 @@ pub mod tree { #[clap(long, short = 'e')] extended: bool, - /// The tree to traverse, or the tree at `HEAD` if unspecified. + /// The revspec of the tree to traverse, or the tree at `HEAD` if unspecified. treeish: Option, }, /// Provide information about a tree. @@ -261,7 +261,7 @@ pub mod tree { /// Provide files size as well. This is expensive as the object is decoded entirely. #[clap(long, short = 'e')] extended: bool, - /// The tree to traverse, or the tree at `HEAD` if unspecified. + /// The revspec of the tree to traverse, or the tree at `HEAD` if unspecified. treeish: Option, }, }