read_url/git/
git_url.rs

1use super::super::{context::*, errors::*, url::*, util::*};
2
3use {
4    relative_path::*,
5    std::{fmt, sync::*},
6};
7
8//
9// GitUrl
10//
11
12/// A URL for an entry in a git repository.
13///
14/// The URL scheme is "git:", followed by repository URL, a `!`, and then the entry path
15/// within the repository. The fragment of the repository URL is used to select a git tag,
16/// a commit hash in hex, or a branch name (in which case the tip of the branch will be used).
17/// The default is to use the tip of the default branch.
18///
19/// Note that the fragment cannot be used with local repositories, which will be accessed in
20/// their current state.
21#[derive(Clone, Debug)]
22pub struct GitUrl {
23    /// The repository [URL].
24    pub repository_url: Arc<UrlRef>,
25
26    /// The repository [gix Url](gix::Url).
27    pub repository_gix_url: gix::Url,
28
29    /// The entry path.
30    pub path: RelativePathBuf,
31
32    pub(crate) context: UrlContextRef,
33}
34
35impl GitUrl {
36    /// Parse.
37    pub fn parse(url_representation: &str) -> Result<(String, String), UrlError> {
38        parse_archive_entry_url_representation(url_representation, "git")
39    }
40
41    /// Constructor.
42    pub fn new(
43        context: &UrlContextRef,
44        repository_url: Arc<UrlRef>,
45        repository_gix_url: gix::Url,
46        path: RelativePathBuf,
47    ) -> Self {
48        Self { repository_url, repository_gix_url, path, context: context.clone() }
49    }
50
51    /// Constructor.
52    pub fn new_with(&self, path: RelativePathBuf) -> Self {
53        Self::new(&self.context, self.repository_url.clone(), self.repository_gix_url.clone(), path)
54    }
55}
56
57impl fmt::Display for GitUrl {
58    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
59        write!(formatter, "git:{}!{}", self.repository_url, self.path)
60    }
61}
62
63// Conversions
64
65impl Into<UrlRef> for GitUrl {
66    fn into(self) -> UrlRef {
67        Box::new(self)
68    }
69}