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