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