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