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