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