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