git.rs

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