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