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