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