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/// The length of a Git short SHA.
 49pub const SHORT_SHA_LENGTH: usize = 7;
 50
 51#[derive(Clone, Copy, Eq, Hash, PartialEq)]
 52pub struct Oid(libgit::Oid);
 53
 54impl Oid {
 55    pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
 56        let oid = libgit::Oid::from_bytes(bytes).context("failed to parse bytes into git oid")?;
 57        Ok(Self(oid))
 58    }
 59
 60    pub fn as_bytes(&self) -> &[u8] {
 61        self.0.as_bytes()
 62    }
 63
 64    pub(crate) fn is_zero(&self) -> bool {
 65        self.0.is_zero()
 66    }
 67
 68    /// Returns this [`Oid`] as a short SHA.
 69    pub fn display_short(&self) -> String {
 70        self.to_string().chars().take(SHORT_SHA_LENGTH).collect()
 71    }
 72}
 73
 74impl FromStr for Oid {
 75    type Err = anyhow::Error;
 76
 77    fn from_str(s: &str) -> std::prelude::v1::Result<Self, Self::Err> {
 78        libgit::Oid::from_str(s)
 79            .map_err(|error| anyhow!("failed to parse git oid: {}", error))
 80            .map(Self)
 81    }
 82}
 83
 84impl fmt::Debug for Oid {
 85    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 86        fmt::Display::fmt(self, f)
 87    }
 88}
 89
 90impl fmt::Display for Oid {
 91    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 92        self.0.fmt(f)
 93    }
 94}
 95
 96impl Serialize for Oid {
 97    fn serialize<S>(&self, serializer: S) -> std::prelude::v1::Result<S::Ok, S::Error>
 98    where
 99        S: serde::Serializer,
100    {
101        serializer.serialize_str(&self.0.to_string())
102    }
103}
104
105impl<'de> Deserialize<'de> for Oid {
106    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
107    where
108        D: serde::Deserializer<'de>,
109    {
110        let s = String::deserialize(deserializer)?;
111        s.parse::<Oid>().map_err(serde::de::Error::custom)
112    }
113}
114
115impl Default for Oid {
116    fn default() -> Self {
117        Self(libgit::Oid::zero())
118    }
119}
120
121impl From<Oid> for u32 {
122    fn from(oid: Oid) -> Self {
123        let bytes = oid.0.as_bytes();
124        debug_assert!(bytes.len() > 4);
125
126        let mut u32_bytes: [u8; 4] = [0; 4];
127        u32_bytes.copy_from_slice(&bytes[..4]);
128
129        u32::from_ne_bytes(u32_bytes)
130    }
131}
132
133impl From<Oid> for usize {
134    fn from(oid: Oid) -> Self {
135        let bytes = oid.0.as_bytes();
136        debug_assert!(bytes.len() > 8);
137
138        let mut u64_bytes: [u8; 8] = [0; 8];
139        u64_bytes.copy_from_slice(&bytes[..8]);
140
141        u64::from_ne_bytes(u64_bytes) as usize
142    }
143}