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