1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use super::Repository;
use crate::{Error, ErrorKind};
use std::path::Path;

/// A path *relative to the root of the git repository* that is guaranteed to be tracked by Git.
///
/// This type is immutable.
#[cfg_attr(docsrs, doc(cfg(feature = "osv-export")))]
pub struct GitPath<'a> {
    repo: &'a Repository,
    path: &'a Path,
}

impl<'a> GitPath<'a> {
    /// Creates a new `GitPath`, validating that this file is tracked in Git
    pub fn new(repo: &'a Repository, path: &'a Path) -> Result<Self, Error> {
        // Validate that the path is relative for better feedback to API users
        if path.has_root() {
            fail!(
                ErrorKind::BadParam,
                "{} is not a relative path",
                path.display()
            );
        }
        let commit_id = repo.repo.refname_to_id("HEAD")?;
        let commit = repo.repo.find_commit(commit_id)?;
        commit.tree()?.get_path(path)?;
        Ok(GitPath { repo, path })
    }

    /// A path *relative to the root of the git repository* that is guaranteed to be tracked by Git
    pub fn path(&self) -> &'a Path {
        self.path
    }

    /// The git repository the path is tracked by
    pub fn repository(&self) -> &'a Repository {
        self.repo
    }
}