mirror of
https://seed.radicle.dev/rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5.git
synced 2026-11-29 14:00:31 +08:00
cli: Allow git revision parameters as COB IDs
This commit allows any git revision to be used instead of the full SHA-1, when specifying COBs. Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
//! Git-related functions and types.
|
||||
use std::collections::HashSet;
|
||||
use std::fmt::Display;
|
||||
use std::fs::{File, OpenOptions};
|
||||
use std::io;
|
||||
use std::io::Write;
|
||||
@@ -10,6 +11,7 @@ use std::str::FromStr;
|
||||
use anyhow::anyhow;
|
||||
use anyhow::Context as _;
|
||||
|
||||
use radicle::cob::ObjectId;
|
||||
use radicle::crypto::ssh;
|
||||
use radicle::git;
|
||||
use radicle::git::raw as git2;
|
||||
@@ -27,6 +29,35 @@ pub const CONFIG_GPG_FORMAT: &str = "gpg.format";
|
||||
pub const CONFIG_GPG_SSH_PROGRAM: &str = "gpg.ssh.program";
|
||||
pub const CONFIG_GPG_SSH_ALLOWED_SIGNERS: &str = "gpg.ssh.allowedSignersFile";
|
||||
|
||||
/// Git revision parameter. Supports extended SHA-1 syntax.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Rev(String);
|
||||
|
||||
impl Rev {
|
||||
/// Return the revision as a string.
|
||||
pub fn as_str(&self) -> &str {
|
||||
&self.0
|
||||
}
|
||||
|
||||
/// Resolve the revision to an [`ObjectId`].
|
||||
pub fn resolve(&self, repo: &git2::Repository) -> Result<ObjectId, git2::Error> {
|
||||
let object = repo.revparse_single(self.as_str())?;
|
||||
Ok(ObjectId::from(object.id()))
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Rev {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for Rev {
|
||||
fn from(value: String) -> Self {
|
||||
Rev(value)
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the git repository in the current directory.
|
||||
pub fn repository() -> Result<Repository, anyhow::Error> {
|
||||
match Repository::open(".") {
|
||||
|
||||
Reference in New Issue
Block a user