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