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