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 status = 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 .status()?;
301 if !status.success() {
302 return Err(anyhow!("Failed to stage paths: {status}"));
303 }
304 }
305 Ok(())
306 }
307
308 fn unstage_paths(&self, paths: &[RepoPath]) -> Result<()> {
309 let working_directory = self
310 .repository
311 .lock()
312 .workdir()
313 .context("failed to read git work directory")?
314 .to_path_buf();
315
316 if !paths.is_empty() {
317 let cmd = new_std_command(&self.git_binary_path)
318 .current_dir(&working_directory)
319 .args(["reset", "--quiet", "--"])
320 .args(paths.iter().map(|p| p.as_ref()))
321 .status()?;
322 if !cmd.success() {
323 return Err(anyhow!("Failed to unstage paths: {cmd}"));
324 }
325 }
326 Ok(())
327 }
328
329 fn commit(&self, message: &str, name_and_email: Option<(&str, &str)>) -> Result<()> {
330 let working_directory = self
331 .repository
332 .lock()
333 .workdir()
334 .context("failed to read git work directory")?
335 .to_path_buf();
336 let mut args = vec!["commit", "--quiet", "-m", message, "--cleanup=strip"];
337 let author = name_and_email.map(|(name, email)| format!("{name} <{email}>"));
338 if let Some(author) = author.as_deref() {
339 args.push("--author");
340 args.push(author);
341 }
342
343 let cmd = new_std_command(&self.git_binary_path)
344 .current_dir(&working_directory)
345 .args(args)
346 .status()?;
347 if !cmd.success() {
348 return Err(anyhow!("Failed to commit: {cmd}"));
349 }
350 Ok(())
351 }
352}
353
354#[derive(Debug, Clone)]
355pub struct FakeGitRepository {
356 state: Arc<Mutex<FakeGitRepositoryState>>,
357}
358
359#[derive(Debug, Clone)]
360pub struct FakeGitRepositoryState {
361 pub path: PathBuf,
362 pub event_emitter: smol::channel::Sender<PathBuf>,
363 pub head_contents: HashMap<RepoPath, String>,
364 pub index_contents: HashMap<RepoPath, String>,
365 pub blames: HashMap<RepoPath, Blame>,
366 pub statuses: HashMap<RepoPath, FileStatus>,
367 pub current_branch_name: Option<String>,
368 pub branches: HashSet<String>,
369}
370
371impl FakeGitRepository {
372 pub fn open(state: Arc<Mutex<FakeGitRepositoryState>>) -> Arc<dyn GitRepository> {
373 Arc::new(FakeGitRepository { state })
374 }
375}
376
377impl FakeGitRepositoryState {
378 pub fn new(path: PathBuf, event_emitter: smol::channel::Sender<PathBuf>) -> Self {
379 FakeGitRepositoryState {
380 path,
381 event_emitter,
382 head_contents: Default::default(),
383 index_contents: Default::default(),
384 blames: Default::default(),
385 statuses: Default::default(),
386 current_branch_name: Default::default(),
387 branches: Default::default(),
388 }
389 }
390}
391
392impl GitRepository for FakeGitRepository {
393 fn reload_index(&self) {}
394
395 fn load_index_text(&self, path: &RepoPath) -> Option<String> {
396 let state = self.state.lock();
397 state.index_contents.get(path.as_ref()).cloned()
398 }
399
400 fn load_committed_text(&self, path: &RepoPath) -> Option<String> {
401 let state = self.state.lock();
402 state.head_contents.get(path.as_ref()).cloned()
403 }
404
405 fn remote_url(&self, _name: &str) -> Option<String> {
406 None
407 }
408
409 fn branch_name(&self) -> Option<String> {
410 let state = self.state.lock();
411 state.current_branch_name.clone()
412 }
413
414 fn head_sha(&self) -> Option<String> {
415 None
416 }
417
418 fn merge_head_shas(&self) -> Vec<String> {
419 vec![]
420 }
421
422 fn path(&self) -> PathBuf {
423 let state = self.state.lock();
424 state.path.clone()
425 }
426
427 fn main_repository_path(&self) -> PathBuf {
428 self.path()
429 }
430
431 fn status(&self, path_prefixes: &[RepoPath]) -> Result<GitStatus> {
432 let state = self.state.lock();
433
434 let mut entries = state
435 .statuses
436 .iter()
437 .filter_map(|(repo_path, status)| {
438 if path_prefixes
439 .iter()
440 .any(|path_prefix| repo_path.0.starts_with(path_prefix))
441 {
442 Some((repo_path.to_owned(), *status))
443 } else {
444 None
445 }
446 })
447 .collect::<Vec<_>>();
448 entries.sort_unstable_by(|(a, _), (b, _)| a.cmp(&b));
449
450 Ok(GitStatus {
451 entries: entries.into(),
452 })
453 }
454
455 fn branches(&self) -> Result<Vec<Branch>> {
456 let state = self.state.lock();
457 let current_branch = &state.current_branch_name;
458 Ok(state
459 .branches
460 .iter()
461 .map(|branch_name| Branch {
462 is_head: Some(branch_name) == current_branch.as_ref(),
463 name: branch_name.into(),
464 unix_timestamp: None,
465 })
466 .collect())
467 }
468
469 fn branch_exits(&self, name: &str) -> Result<bool> {
470 let state = self.state.lock();
471 Ok(state.branches.contains(name))
472 }
473
474 fn change_branch(&self, name: &str) -> Result<()> {
475 let mut state = self.state.lock();
476 state.current_branch_name = Some(name.to_owned());
477 state
478 .event_emitter
479 .try_send(state.path.clone())
480 .expect("Dropped repo change event");
481 Ok(())
482 }
483
484 fn create_branch(&self, name: &str) -> Result<()> {
485 let mut state = self.state.lock();
486 state.branches.insert(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 blame(&self, path: &Path, _content: Rope) -> Result<crate::blame::Blame> {
495 let state = self.state.lock();
496 state
497 .blames
498 .get(path)
499 .with_context(|| format!("failed to get blame for {:?}", path))
500 .cloned()
501 }
502
503 fn stage_paths(&self, _paths: &[RepoPath]) -> Result<()> {
504 unimplemented!()
505 }
506
507 fn unstage_paths(&self, _paths: &[RepoPath]) -> Result<()> {
508 unimplemented!()
509 }
510
511 fn commit(&self, _message: &str, _name_and_email: Option<(&str, &str)>) -> Result<()> {
512 unimplemented!()
513 }
514}
515
516fn check_path_to_repo_path_errors(relative_file_path: &Path) -> Result<()> {
517 match relative_file_path.components().next() {
518 None => anyhow::bail!("repo path should not be empty"),
519 Some(Component::Prefix(_)) => anyhow::bail!(
520 "repo path `{}` should be relative, not a windows prefix",
521 relative_file_path.to_string_lossy()
522 ),
523 Some(Component::RootDir) => {
524 anyhow::bail!(
525 "repo path `{}` should be relative",
526 relative_file_path.to_string_lossy()
527 )
528 }
529 Some(Component::CurDir) => {
530 anyhow::bail!(
531 "repo path `{}` should not start with `.`",
532 relative_file_path.to_string_lossy()
533 )
534 }
535 Some(Component::ParentDir) => {
536 anyhow::bail!(
537 "repo path `{}` should not start with `..`",
538 relative_file_path.to_string_lossy()
539 )
540 }
541 _ => Ok(()),
542 }
543}
544
545pub static WORK_DIRECTORY_REPO_PATH: LazyLock<RepoPath> =
546 LazyLock::new(|| RepoPath(Path::new("").into()));
547
548#[derive(Clone, Debug, Ord, Hash, PartialOrd, Eq, PartialEq)]
549pub struct RepoPath(pub Arc<Path>);
550
551impl RepoPath {
552 pub fn new(path: PathBuf) -> Self {
553 debug_assert!(path.is_relative(), "Repo paths must be relative");
554
555 RepoPath(path.into())
556 }
557
558 pub fn from_str(path: &str) -> Self {
559 let path = Path::new(path);
560 debug_assert!(path.is_relative(), "Repo paths must be relative");
561
562 RepoPath(path.into())
563 }
564
565 pub fn to_proto(&self) -> String {
566 self.0.to_string_lossy().to_string()
567 }
568}
569
570impl std::fmt::Display for RepoPath {
571 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
572 self.0.to_string_lossy().fmt(f)
573 }
574}
575
576impl From<&Path> for RepoPath {
577 fn from(value: &Path) -> Self {
578 RepoPath::new(value.into())
579 }
580}
581
582impl From<Arc<Path>> for RepoPath {
583 fn from(value: Arc<Path>) -> Self {
584 RepoPath(value)
585 }
586}
587
588impl From<PathBuf> for RepoPath {
589 fn from(value: PathBuf) -> Self {
590 RepoPath::new(value)
591 }
592}
593
594impl From<&str> for RepoPath {
595 fn from(value: &str) -> Self {
596 Self::from_str(value)
597 }
598}
599
600impl Default for RepoPath {
601 fn default() -> Self {
602 RepoPath(Path::new("").into())
603 }
604}
605
606impl AsRef<Path> for RepoPath {
607 fn as_ref(&self) -> &Path {
608 self.0.as_ref()
609 }
610}
611
612impl std::ops::Deref for RepoPath {
613 type Target = Path;
614
615 fn deref(&self) -> &Self::Target {
616 &self.0
617 }
618}
619
620impl Borrow<Path> for RepoPath {
621 fn borrow(&self) -> &Path {
622 self.0.as_ref()
623 }
624}
625
626#[derive(Debug)]
627pub struct RepoPathDescendants<'a>(pub &'a Path);
628
629impl<'a> MapSeekTarget<RepoPath> for RepoPathDescendants<'a> {
630 fn cmp_cursor(&self, key: &RepoPath) -> Ordering {
631 if key.starts_with(self.0) {
632 Ordering::Greater
633 } else {
634 self.0.cmp(key)
635 }
636 }
637}