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