git.rs

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