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