git.rs

  1mod hosting_provider;
  2
  3use anyhow::{anyhow, Context, Result};
  4use serde::{Deserialize, Serialize};
  5use std::ffi::OsStr;
  6use std::fmt;
  7use std::str::FromStr;
  8use std::sync::LazyLock;
  9
 10pub use git2 as libgit;
 11
 12pub use crate::hosting_provider::*;
 13
 14pub mod blame;
 15pub mod commit;
 16pub mod diff;
 17pub mod repository;
 18pub mod status;
 19
 20pub static DOT_GIT: LazyLock<&'static OsStr> = LazyLock::new(|| OsStr::new(".git"));
 21pub static GITIGNORE: LazyLock<&'static OsStr> = LazyLock::new(|| OsStr::new(".gitignore"));
 22
 23#[derive(Clone, Copy, Eq, Hash, PartialEq)]
 24pub struct Oid(libgit::Oid);
 25
 26impl Oid {
 27    pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
 28        let oid = libgit::Oid::from_bytes(bytes).context("failed to parse bytes into git oid")?;
 29        Ok(Self(oid))
 30    }
 31
 32    pub fn as_bytes(&self) -> &[u8] {
 33        self.0.as_bytes()
 34    }
 35
 36    pub(crate) fn is_zero(&self) -> bool {
 37        self.0.is_zero()
 38    }
 39
 40    /// Returns this [`Oid`] as a short SHA.
 41    pub fn display_short(&self) -> String {
 42        self.to_string().chars().take(7).collect()
 43    }
 44}
 45
 46impl FromStr for Oid {
 47    type Err = anyhow::Error;
 48
 49    fn from_str(s: &str) -> std::prelude::v1::Result<Self, Self::Err> {
 50        libgit::Oid::from_str(s)
 51            .map_err(|error| anyhow!("failed to parse git oid: {}", error))
 52            .map(|oid| Self(oid))
 53    }
 54}
 55
 56impl fmt::Debug for Oid {
 57    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 58        fmt::Display::fmt(self, f)
 59    }
 60}
 61
 62impl fmt::Display for Oid {
 63    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 64        self.0.fmt(f)
 65    }
 66}
 67
 68impl Serialize for Oid {
 69    fn serialize<S>(&self, serializer: S) -> std::prelude::v1::Result<S::Ok, S::Error>
 70    where
 71        S: serde::Serializer,
 72    {
 73        serializer.serialize_str(&self.0.to_string())
 74    }
 75}
 76
 77impl<'de> Deserialize<'de> for Oid {
 78    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
 79    where
 80        D: serde::Deserializer<'de>,
 81    {
 82        let s = String::deserialize(deserializer)?;
 83        s.parse::<Oid>().map_err(serde::de::Error::custom)
 84    }
 85}
 86
 87impl Default for Oid {
 88    fn default() -> Self {
 89        Self(libgit::Oid::zero())
 90    }
 91}
 92
 93impl From<Oid> for u32 {
 94    fn from(oid: Oid) -> Self {
 95        let bytes = oid.0.as_bytes();
 96        debug_assert!(bytes.len() > 4);
 97
 98        let mut u32_bytes: [u8; 4] = [0; 4];
 99        u32_bytes.copy_from_slice(&bytes[..4]);
100
101        u32::from_ne_bytes(u32_bytes)
102    }
103}
104
105impl From<Oid> for usize {
106    fn from(oid: Oid) -> Self {
107        let bytes = oid.0.as_bytes();
108        debug_assert!(bytes.len() > 8);
109
110        let mut u64_bytes: [u8; 8] = [0; 8];
111        u64_bytes.copy_from_slice(&bytes[..8]);
112
113        u64::from_ne_bytes(u64_bytes) as usize
114    }
115}