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