git.rs

  1pub mod blame;
  2pub mod commit;
  3mod hosting_provider;
  4mod remote;
  5pub mod repository;
  6pub mod status;
  7
  8use anyhow::{anyhow, Context as _, Result};
  9use gpui::actions;
 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 GITIGNORE: LazyLock<&'static OsStr> = LazyLock::new(|| OsStr::new(".gitignore"));
 23pub static FSMONITOR_DAEMON: LazyLock<&'static OsStr> =
 24    LazyLock::new(|| OsStr::new("fsmonitor--daemon"));
 25pub static COMMIT_MESSAGE: LazyLock<&'static OsStr> =
 26    LazyLock::new(|| OsStr::new("COMMIT_EDITMSG"));
 27pub static INDEX_LOCK: LazyLock<&'static OsStr> = LazyLock::new(|| OsStr::new("index.lock"));
 28
 29actions!(
 30    git,
 31    [
 32        StageFile,
 33        UnstageFile,
 34        ToggleStaged,
 35        // Revert actions are currently in the editor crate:
 36        // editor::RevertFile,
 37        // editor::RevertSelectedHunks
 38        StageAll,
 39        UnstageAll,
 40        DiscardTrackedChanges,
 41        TrashUntrackedFiles,
 42        Uncommit,
 43        Commit,
 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}