diff options
| author | 2026-02-28 10:59:06 -0500 | |
|---|---|---|
| committer | 2026-02-28 10:59:06 -0500 | |
| commit | d6ef99e9efbc25b86c1c31bb8fcc8d1f03c1edef (patch) | |
| tree | a16e728c749f8a96c34233acb9c76ba815f0c8e1 /src | |
| parent | add package collect helpers to reduce duplicity (diff) | |
add short hash to list log
Diffstat (limited to 'src')
| -rw-r--r-- | src/action.rs | 13 | ||||
| -rw-r--r-- | src/util.rs | 12 |
2 files changed, 19 insertions, 6 deletions
diff --git a/src/action.rs b/src/action.rs index 5ebf34a..3041c0b 100644 --- a/src/action.rs +++ b/src/action.rs @@ -195,14 +195,21 @@ fn remove(packages: Vec<String>) -> Result<(), String> { } fn list() -> Result<(), String> { - for entry in fs::read_dir(BASE_CONFIG_PATH) + if !nix::unistd::geteuid().is_root() { + return Err("list must be run as root".to_string()); + } + + for entry in fs::read_dir(BASE_REPO_PATH) .map_err(|e| format!("failed to iterate package directory: {}", e))? { let entry = entry.map_err(|e| e.to_string())?; let path = entry.path(); - if path.is_file() { + if path.is_dir() { + let oid = util::get_commit_hash(&path) + .map_err(|e| format!("failed to get commit hash: {e}"))?; + let oid = oid.as_str().unwrap(); if let Some(stem) = path.file_stem() { - println!("{}", stem.to_string_lossy()); + println!("{} ({})", stem.to_string_lossy(), oid); } } } diff --git a/src/util.rs b/src/util.rs index 8508377..7bd2142 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,4 +1,4 @@ -use git2::{Cred, FetchOptions, RemoteCallbacks, Repository, build::CheckoutBuilder}; +use git2::{Buf, Cred, FetchOptions, RemoteCallbacks, Repository, build::CheckoutBuilder}; use std::env; use std::fs; use std::io; @@ -71,6 +71,14 @@ pub fn dir_size(path: &Path) -> std::io::Result<u64> { Ok(size) } +pub fn get_commit_hash(path: &Path) -> Result<Buf, git2::Error> { + let repo = Repository::open(path)?; + let head = repo.head()?; + + let commit = head.peel_to_commit()?; + Ok(repo.find_object(commit.id(), None)?.short_id()?) +} + pub fn get_editor() -> String { env::var("VISUAL") .or_else(|_| env::var("EDITOR")) @@ -147,11 +155,9 @@ pub fn yn_prompt(prompt: &str) -> bool { print!("{} [y/n]: ", prompt); io::stdout().flush().unwrap(); - // Read input from user let mut input = String::new(); io::stdin().read_line(&mut input).unwrap(); - // Normalize input let input = input.trim().to_lowercase(); match input.as_str() { |
