| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | #![expect( |
| 6 | clippy::let_underscore_must_use, |
| 7 | reason = "rendering an advisory-gate verdict to a writer is best-effort; a broken pipe here \ |
| 8 | is not actionable" |
| 9 | )] |
| 10 | |
| 11 | pub mod account; |
| 12 | pub mod bootstrap; |
| 13 | pub mod comment; |
| 14 | pub mod effect; |
| 15 | pub mod inbox; |
| 16 | pub mod issue; |
| 17 | pub mod login; |
| 18 | pub mod lsp; |
| 19 | pub mod members; |
| 20 | pub mod redact; |
| 21 | pub mod review; |
| 22 | pub mod serve; |
| 23 | pub mod setup; |
| 24 | pub mod toolchain; |
| 25 | |
| 26 | use std::path::PathBuf; |
| 27 | |
| 28 | use gix_hash::ObjectId; |
| 29 | use gix_object::{CommitRef, Find, Kind}; |
| 30 | |
| 31 | use crate::error::{Error, Result}; |
| 32 | use crate::root::LocalRoot; |
| 33 | use crate::sign::Signer; |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | pub(crate) fn commit_tree(objects: &impl Find, oid: ObjectId) -> Result<ObjectId> { |
| 44 | let mut buf = Vec::new(); |
| 45 | let data = objects |
| 46 | .try_find(&oid, &mut buf) |
| 47 | .map_err(|source| Error::InvalidArgument(source.to_string()))? |
| 48 | .ok_or_else(|| Error::NotFound { |
| 49 | what: oid.to_string(), |
| 50 | })?; |
| 51 | if data.kind != Kind::Commit { |
| 52 | return Err(Error::NotFound { |
| 53 | what: oid.to_string(), |
| 54 | }); |
| 55 | } |
| 56 | let commit = CommitRef::from_bytes(data.data, oid.kind()) |
| 57 | .map_err(|source| Error::InvalidArgument(source.to_string()))?; |
| 58 | Ok(commit.tree()) |
| 59 | } |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | pub fn signer(root: &LocalRoot, key: Option<PathBuf>) -> Result<Signer> { |
| 69 | let repo = gix::open(&root.path)?; |
| 70 | let path = crate::sign::resolve_key_path(&repo, key.as_deref())?; |
| 71 | Signer::load(&path) |
| 72 | } |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | #[must_use] |
| 80 | pub fn actor(signer: &Signer) -> gix::actor::Signature { |
| 81 | let seconds = std::time::SystemTime::now() |
| 82 | .duration_since(std::time::UNIX_EPOCH) |
| 83 | .map(|d| i64::try_from(d.as_secs()).unwrap_or(i64::MAX)) |
| 84 | .unwrap_or_default(); |
| 85 | gix::actor::Signature { |
| 86 | name: "git-ents".into(), |
| 87 | email: format!("{}@git-ents.local", short_fingerprint(signer)).into(), |
| 88 | time: gix::date::Time { seconds, offset: 0 }, |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | fn short_fingerprint(signer: &Signer) -> String { |
| 93 | let key = signer.public_openssh(); |
| 94 | let hex = key |
| 95 | .split_whitespace() |
| 96 | .nth(1) |
| 97 | .unwrap_or(&key) |
| 98 | .chars() |
| 99 | .take(12) |
| 100 | .collect::<String>(); |
| 101 | if hex.is_empty() { |
| 102 | "member".to_owned() |
| 103 | } else { |
| 104 | hex |
| 105 | } |
| 106 | } |