repository.rs

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