1use crate::status::FileStatus;
2use crate::{blame::Blame, status::GitStatus};
3use crate::{GitHostingProviderRegistry, COMMIT_MESSAGE};
4use anyhow::{anyhow, Context as _, Result};
5use collections::{HashMap, HashSet};
6use git2::BranchType;
7use gpui::SharedString;
8use parking_lot::Mutex;
9use rope::Rope;
10use std::borrow::Borrow;
11use std::sync::LazyLock;
12use std::{
13 cmp::Ordering,
14 path::{Component, Path, PathBuf},
15 sync::Arc,
16};
17use sum_tree::MapSeekTarget;
18use util::command::new_std_command;
19use util::ResultExt;
20
21#[derive(Clone, Debug, Hash, PartialEq)]
22pub struct Branch {
23 pub is_head: bool,
24 pub name: SharedString,
25 /// Timestamp of most recent commit, normalized to Unix Epoch format.
26 pub unix_timestamp: Option<i64>,
27}
28
29pub trait GitRepository: Send + Sync {
30 fn reload_index(&self);
31
32 /// Returns the contents of an entry in the repository's index, or None if there is no entry for the given path.
33 ///
34 /// Note that for symlink entries, this will return the contents of the symlink, not the target.
35 fn load_index_text(&self, path: &RepoPath) -> Option<String>;
36
37 /// Returns the contents of an entry in the repository's HEAD, or None if HEAD does not exist or has no entry for the given path.
38 ///
39 /// Note that for symlink entries, this will return the contents of the symlink, not the target.
40 fn load_committed_text(&self, path: &RepoPath) -> Option<String>;
41
42 /// Returns the URL of the remote with the given name.
43 fn remote_url(&self, name: &str) -> Option<String>;
44 fn branch_name(&self) -> Option<String>;
45
46 /// Returns the SHA of the current HEAD.
47 fn head_sha(&self) -> Option<String>;
48
49 /// Returns the list of git statuses, sorted by path
50 fn status(&self, path_prefixes: &[RepoPath]) -> Result<GitStatus>;
51
52 fn branches(&self) -> Result<Vec<Branch>>;
53 fn change_branch(&self, _: &str) -> Result<()>;
54 fn create_branch(&self, _: &str) -> Result<()>;
55 fn branch_exits(&self, _: &str) -> Result<bool>;
56
57 fn blame(&self, path: &Path, content: Rope) -> Result<crate::blame::Blame>;
58
59 /// Returns the path to the repository, typically the `.git` folder.
60 fn dot_git_dir(&self) -> PathBuf;
61
62 /// Updates the index to match the worktree at the given paths.
63 ///
64 /// If any of the paths have been deleted from the worktree, they will be removed from the index if found there.
65 fn stage_paths(&self, paths: &[RepoPath]) -> Result<()>;
66 /// Updates the index to match HEAD at the given paths.
67 ///
68 /// If any of the paths were previously staged but do not exist in HEAD, they will be removed from the index.
69 fn unstage_paths(&self, paths: &[RepoPath]) -> Result<()>;
70
71 fn commit(&self, name_and_email: Option<(&str, &str)>) -> Result<()>;
72}
73
74impl std::fmt::Debug for dyn GitRepository {
75 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76 f.debug_struct("dyn GitRepository<...>").finish()
77 }
78}
79
80pub struct RealGitRepository {
81 pub repository: Mutex<git2::Repository>,
82 pub git_binary_path: PathBuf,
83 hosting_provider_registry: Arc<GitHostingProviderRegistry>,
84}
85
86impl RealGitRepository {
87 pub fn new(
88 repository: git2::Repository,
89 git_binary_path: Option<PathBuf>,
90 hosting_provider_registry: Arc<GitHostingProviderRegistry>,
91 ) -> Self {
92 Self {
93 repository: Mutex::new(repository),
94 git_binary_path: git_binary_path.unwrap_or_else(|| PathBuf::from("git")),
95 hosting_provider_registry,
96 }
97 }
98}
99
100// https://git-scm.com/book/en/v2/Git-Internals-Git-Objects
101const GIT_MODE_SYMLINK: u32 = 0o120000;
102
103impl GitRepository for RealGitRepository {
104 fn reload_index(&self) {
105 if let Ok(mut index) = self.repository.lock().index() {
106 _ = index.read(false);
107 }
108 }
109
110 fn dot_git_dir(&self) -> PathBuf {
111 let repo = self.repository.lock();
112 repo.path().into()
113 }
114
115 fn load_index_text(&self, path: &RepoPath) -> Option<String> {
116 fn logic(repo: &git2::Repository, path: &RepoPath) -> Result<Option<String>> {
117 const STAGE_NORMAL: i32 = 0;
118 let index = repo.index()?;
119
120 // This check is required because index.get_path() unwraps internally :(
121 check_path_to_repo_path_errors(path)?;
122
123 let oid = match index.get_path(path, STAGE_NORMAL) {
124 Some(entry) if entry.mode != GIT_MODE_SYMLINK => entry.id,
125 _ => return Ok(None),
126 };
127
128 let content = repo.find_blob(oid)?.content().to_owned();
129 Ok(Some(String::from_utf8(content)?))
130 }
131
132 match logic(&self.repository.lock(), path) {
133 Ok(value) => return value,
134 Err(err) => log::error!("Error loading index text: {:?}", err),
135 }
136 None
137 }
138
139 fn load_committed_text(&self, path: &RepoPath) -> Option<String> {
140 let repo = self.repository.lock();
141 let head = repo.head().ok()?.peel_to_tree().log_err()?;
142 let oid = head.get_path(path).ok()?.id();
143 let content = repo.find_blob(oid).log_err()?.content().to_owned();
144 let content = String::from_utf8(content).log_err()?;
145 Some(content)
146 }
147
148 fn remote_url(&self, name: &str) -> Option<String> {
149 let repo = self.repository.lock();
150 let remote = repo.find_remote(name).ok()?;
151 remote.url().map(|url| url.to_string())
152 }
153
154 fn branch_name(&self) -> Option<String> {
155 let repo = self.repository.lock();
156 let head = repo.head().log_err()?;
157 let branch = String::from_utf8_lossy(head.shorthand_bytes());
158 Some(branch.to_string())
159 }
160
161 fn head_sha(&self) -> Option<String> {
162 Some(self.repository.lock().head().ok()?.target()?.to_string())
163 }
164
165 fn status(&self, path_prefixes: &[RepoPath]) -> Result<GitStatus> {
166 let working_directory = self
167 .repository
168 .lock()
169 .workdir()
170 .context("failed to read git work directory")?
171 .to_path_buf();
172 GitStatus::new(&self.git_binary_path, &working_directory, path_prefixes)
173 }
174
175 fn branch_exits(&self, name: &str) -> Result<bool> {
176 let repo = self.repository.lock();
177 let branch = repo.find_branch(name, BranchType::Local);
178 match branch {
179 Ok(_) => Ok(true),
180 Err(e) => match e.code() {
181 git2::ErrorCode::NotFound => Ok(false),
182 _ => Err(anyhow!(e)),
183 },
184 }
185 }
186
187 fn branches(&self) -> Result<Vec<Branch>> {
188 let repo = self.repository.lock();
189 let local_branches = repo.branches(Some(BranchType::Local))?;
190 let valid_branches = local_branches
191 .filter_map(|branch| {
192 branch.ok().and_then(|(branch, _)| {
193 let is_head = branch.is_head();
194 let name = branch
195 .name()
196 .ok()
197 .flatten()
198 .map(|name| name.to_string().into())?;
199 let timestamp = branch.get().peel_to_commit().ok()?.time();
200 let unix_timestamp = timestamp.seconds();
201 let timezone_offset = timestamp.offset_minutes();
202 let utc_offset =
203 time::UtcOffset::from_whole_seconds(timezone_offset * 60).ok()?;
204 let unix_timestamp =
205 time::OffsetDateTime::from_unix_timestamp(unix_timestamp).ok()?;
206 Some(Branch {
207 is_head,
208 name,
209 unix_timestamp: Some(unix_timestamp.to_offset(utc_offset).unix_timestamp()),
210 })
211 })
212 })
213 .collect();
214 Ok(valid_branches)
215 }
216
217 fn change_branch(&self, name: &str) -> Result<()> {
218 let repo = self.repository.lock();
219 let revision = repo.find_branch(name, BranchType::Local)?;
220 let revision = revision.get();
221 let as_tree = revision.peel_to_tree()?;
222 repo.checkout_tree(as_tree.as_object(), None)?;
223 repo.set_head(
224 revision
225 .name()
226 .ok_or_else(|| anyhow!("Branch name could not be retrieved"))?,
227 )?;
228 Ok(())
229 }
230
231 fn create_branch(&self, name: &str) -> Result<()> {
232 let repo = self.repository.lock();
233 let current_commit = repo.head()?.peel_to_commit()?;
234 repo.branch(name, ¤t_commit, false)?;
235 Ok(())
236 }
237
238 fn blame(&self, path: &Path, content: Rope) -> Result<crate::blame::Blame> {
239 let working_directory = self
240 .repository
241 .lock()
242 .workdir()
243 .with_context(|| format!("failed to get git working directory for file {:?}", path))?
244 .to_path_buf();
245
246 const REMOTE_NAME: &str = "origin";
247 let remote_url = self.remote_url(REMOTE_NAME);
248
249 crate::blame::Blame::for_path(
250 &self.git_binary_path,
251 &working_directory,
252 path,
253 &content,
254 remote_url,
255 self.hosting_provider_registry.clone(),
256 )
257 }
258
259 fn stage_paths(&self, paths: &[RepoPath]) -> Result<()> {
260 let working_directory = self
261 .repository
262 .lock()
263 .workdir()
264 .context("failed to read git work directory")?
265 .to_path_buf();
266
267 if !paths.is_empty() {
268 let status = new_std_command(&self.git_binary_path)
269 .current_dir(&working_directory)
270 .args(["update-index", "--add", "--remove", "--"])
271 .args(paths.iter().map(|p| p.as_ref()))
272 .status()?;
273 if !status.success() {
274 return Err(anyhow!("Failed to stage paths: {status}"));
275 }
276 }
277 Ok(())
278 }
279
280 fn unstage_paths(&self, paths: &[RepoPath]) -> Result<()> {
281 let working_directory = self
282 .repository
283 .lock()
284 .workdir()
285 .context("failed to read git work directory")?
286 .to_path_buf();
287
288 if !paths.is_empty() {
289 let cmd = new_std_command(&self.git_binary_path)
290 .current_dir(&working_directory)
291 .args(["reset", "--quiet", "--"])
292 .args(paths.iter().map(|p| p.as_ref()))
293 .status()?;
294 if !cmd.success() {
295 return Err(anyhow!("Failed to unstage paths: {cmd}"));
296 }
297 }
298 Ok(())
299 }
300
301 fn commit(&self, name_and_email: Option<(&str, &str)>) -> Result<()> {
302 let working_directory = self
303 .repository
304 .lock()
305 .workdir()
306 .context("failed to read git work directory")?
307 .to_path_buf();
308 let commit_file = self.dot_git_dir().join(*COMMIT_MESSAGE);
309 let commit_file_path = commit_file.to_string_lossy();
310 let mut args = vec![
311 "commit",
312 "--quiet",
313 "-F",
314 commit_file_path.as_ref(),
315 "--cleanup=strip",
316 ];
317 let author = name_and_email.map(|(name, email)| format!("{name} <{email}>"));
318 if let Some(author) = author.as_deref() {
319 args.push("--author");
320 args.push(author);
321 }
322
323 let cmd = new_std_command(&self.git_binary_path)
324 .current_dir(&working_directory)
325 .args(args)
326 .status()?;
327 if !cmd.success() {
328 return Err(anyhow!("Failed to commit: {cmd}"));
329 }
330 Ok(())
331 }
332}
333
334#[derive(Debug, Clone)]
335pub struct FakeGitRepository {
336 state: Arc<Mutex<FakeGitRepositoryState>>,
337}
338
339#[derive(Debug, Clone)]
340pub struct FakeGitRepositoryState {
341 pub dot_git_dir: PathBuf,
342 pub event_emitter: smol::channel::Sender<PathBuf>,
343 pub head_contents: HashMap<RepoPath, String>,
344 pub index_contents: HashMap<RepoPath, String>,
345 pub blames: HashMap<RepoPath, Blame>,
346 pub statuses: HashMap<RepoPath, FileStatus>,
347 pub current_branch_name: Option<String>,
348 pub branches: HashSet<String>,
349}
350
351impl FakeGitRepository {
352 pub fn open(state: Arc<Mutex<FakeGitRepositoryState>>) -> Arc<dyn GitRepository> {
353 Arc::new(FakeGitRepository { state })
354 }
355}
356
357impl FakeGitRepositoryState {
358 pub fn new(dot_git_dir: PathBuf, event_emitter: smol::channel::Sender<PathBuf>) -> Self {
359 FakeGitRepositoryState {
360 dot_git_dir,
361 event_emitter,
362 head_contents: Default::default(),
363 index_contents: Default::default(),
364 blames: Default::default(),
365 statuses: Default::default(),
366 current_branch_name: Default::default(),
367 branches: Default::default(),
368 }
369 }
370}
371
372impl GitRepository for FakeGitRepository {
373 fn reload_index(&self) {}
374
375 fn load_index_text(&self, path: &RepoPath) -> Option<String> {
376 let state = self.state.lock();
377 state.index_contents.get(path.as_ref()).cloned()
378 }
379
380 fn load_committed_text(&self, path: &RepoPath) -> Option<String> {
381 let state = self.state.lock();
382 state.head_contents.get(path.as_ref()).cloned()
383 }
384
385 fn remote_url(&self, _name: &str) -> Option<String> {
386 None
387 }
388
389 fn branch_name(&self) -> Option<String> {
390 let state = self.state.lock();
391 state.current_branch_name.clone()
392 }
393
394 fn head_sha(&self) -> Option<String> {
395 None
396 }
397
398 fn dot_git_dir(&self) -> PathBuf {
399 let state = self.state.lock();
400 state.dot_git_dir.clone()
401 }
402
403 fn status(&self, path_prefixes: &[RepoPath]) -> Result<GitStatus> {
404 let state = self.state.lock();
405
406 let mut entries = state
407 .statuses
408 .iter()
409 .filter_map(|(repo_path, status)| {
410 if path_prefixes
411 .iter()
412 .any(|path_prefix| repo_path.0.starts_with(path_prefix))
413 {
414 Some((repo_path.to_owned(), *status))
415 } else {
416 None
417 }
418 })
419 .collect::<Vec<_>>();
420 entries.sort_unstable_by(|(a, _), (b, _)| a.cmp(&b));
421
422 Ok(GitStatus {
423 entries: entries.into(),
424 })
425 }
426
427 fn branches(&self) -> Result<Vec<Branch>> {
428 let state = self.state.lock();
429 let current_branch = &state.current_branch_name;
430 Ok(state
431 .branches
432 .iter()
433 .map(|branch_name| Branch {
434 is_head: Some(branch_name) == current_branch.as_ref(),
435 name: branch_name.into(),
436 unix_timestamp: None,
437 })
438 .collect())
439 }
440
441 fn branch_exits(&self, name: &str) -> Result<bool> {
442 let state = self.state.lock();
443 Ok(state.branches.contains(name))
444 }
445
446 fn change_branch(&self, name: &str) -> Result<()> {
447 let mut state = self.state.lock();
448 state.current_branch_name = Some(name.to_owned());
449 state
450 .event_emitter
451 .try_send(state.dot_git_dir.clone())
452 .expect("Dropped repo change event");
453 Ok(())
454 }
455
456 fn create_branch(&self, name: &str) -> Result<()> {
457 let mut state = self.state.lock();
458 state.branches.insert(name.to_owned());
459 state
460 .event_emitter
461 .try_send(state.dot_git_dir.clone())
462 .expect("Dropped repo change event");
463 Ok(())
464 }
465
466 fn blame(&self, path: &Path, _content: Rope) -> Result<crate::blame::Blame> {
467 let state = self.state.lock();
468 state
469 .blames
470 .get(path)
471 .with_context(|| format!("failed to get blame for {:?}", path))
472 .cloned()
473 }
474
475 fn stage_paths(&self, _paths: &[RepoPath]) -> Result<()> {
476 unimplemented!()
477 }
478
479 fn unstage_paths(&self, _paths: &[RepoPath]) -> Result<()> {
480 unimplemented!()
481 }
482
483 fn commit(&self, _name_and_email: Option<(&str, &str)>) -> Result<()> {
484 unimplemented!()
485 }
486}
487
488fn check_path_to_repo_path_errors(relative_file_path: &Path) -> Result<()> {
489 match relative_file_path.components().next() {
490 None => anyhow::bail!("repo path should not be empty"),
491 Some(Component::Prefix(_)) => anyhow::bail!(
492 "repo path `{}` should be relative, not a windows prefix",
493 relative_file_path.to_string_lossy()
494 ),
495 Some(Component::RootDir) => {
496 anyhow::bail!(
497 "repo path `{}` should be relative",
498 relative_file_path.to_string_lossy()
499 )
500 }
501 Some(Component::CurDir) => {
502 anyhow::bail!(
503 "repo path `{}` should not start with `.`",
504 relative_file_path.to_string_lossy()
505 )
506 }
507 Some(Component::ParentDir) => {
508 anyhow::bail!(
509 "repo path `{}` should not start with `..`",
510 relative_file_path.to_string_lossy()
511 )
512 }
513 _ => Ok(()),
514 }
515}
516
517pub static WORK_DIRECTORY_REPO_PATH: LazyLock<RepoPath> =
518 LazyLock::new(|| RepoPath(Path::new("").into()));
519
520#[derive(Clone, Debug, Ord, Hash, PartialOrd, Eq, PartialEq)]
521pub struct RepoPath(pub Arc<Path>);
522
523impl RepoPath {
524 pub fn new(path: PathBuf) -> Self {
525 debug_assert!(path.is_relative(), "Repo paths must be relative");
526
527 RepoPath(path.into())
528 }
529
530 pub fn from_str(path: &str) -> Self {
531 let path = Path::new(path);
532 debug_assert!(path.is_relative(), "Repo paths must be relative");
533
534 RepoPath(path.into())
535 }
536
537 pub fn to_proto(&self) -> String {
538 self.0.to_string_lossy().to_string()
539 }
540}
541
542impl std::fmt::Display for RepoPath {
543 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
544 self.0.to_string_lossy().fmt(f)
545 }
546}
547
548impl From<&Path> for RepoPath {
549 fn from(value: &Path) -> Self {
550 RepoPath::new(value.into())
551 }
552}
553
554impl From<Arc<Path>> for RepoPath {
555 fn from(value: Arc<Path>) -> Self {
556 RepoPath(value)
557 }
558}
559
560impl From<PathBuf> for RepoPath {
561 fn from(value: PathBuf) -> Self {
562 RepoPath::new(value)
563 }
564}
565
566impl From<&str> for RepoPath {
567 fn from(value: &str) -> Self {
568 Self::from_str(value)
569 }
570}
571
572impl Default for RepoPath {
573 fn default() -> Self {
574 RepoPath(Path::new("").into())
575 }
576}
577
578impl AsRef<Path> for RepoPath {
579 fn as_ref(&self) -> &Path {
580 self.0.as_ref()
581 }
582}
583
584impl std::ops::Deref for RepoPath {
585 type Target = Path;
586
587 fn deref(&self) -> &Self::Target {
588 &self.0
589 }
590}
591
592impl Borrow<Path> for RepoPath {
593 fn borrow(&self) -> &Path {
594 self.0.as_ref()
595 }
596}
597
598#[derive(Debug)]
599pub struct RepoPathDescendants<'a>(pub &'a Path);
600
601impl<'a> MapSeekTarget<RepoPath> for RepoPathDescendants<'a> {
602 fn cmp_cursor(&self, key: &RepoPath) -> Ordering {
603 if key.starts_with(self.0) {
604 Ordering::Greater
605 } else {
606 self.0.cmp(key)
607 }
608 }
609}