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