repository.rs

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