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