1+ //! See [GitSource].
2+
13use crate :: core:: source:: { MaybePackage , QueryKind , Source , SourceId } ;
24use crate :: core:: GitReference ;
35use crate :: core:: { Dependency , Package , PackageId , Summary } ;
@@ -13,18 +15,71 @@ use std::fmt::{self, Debug, Formatter};
1315use std:: task:: Poll ;
1416use url:: Url ;
1517
18+ /// `GitSource` contains one or more packages gathering from a Git repository.
19+ /// Under the hood it uses [`PathSource`] to discover packages inside the
20+ /// repository.
21+ ///
22+ /// ## Filesystem layout
23+ ///
24+ /// During a successful `GitSource` download, at least two Git repositories are
25+ /// created: one is the shared Git database of this remote, and the other is the
26+ /// Git checkout to a specific revision, which contains the actual files to be
27+ /// compiled. Multiple checkouts can be cloned from a single Git database.
28+ ///
29+ /// Those repositories are located at Cargo's Git cache directory
30+ /// `$CARGO_HOME/git`. The file tree of the cache directory roughly looks like:
31+ ///
32+ /// ```text
33+ /// $CARGO_HOME/git/
34+ /// ├── checkouts/
35+ /// │ ├── gimli-a0d193bd15a5ed96/
36+ /// │ │ ├── 8e73ef0/ # Git short ID for a certain revision
37+ /// │ │ ├── a2a4b78/
38+ /// │ │ └── e33d1ac/
39+ /// │ ├── log-c58e1db3de7c154d-shallow/
40+ /// │ │ └── 11eda98/
41+ /// └── db/
42+ /// ├── gimli-a0d193bd15a5ed96/
43+ /// └── log-c58e1db3de7c154d-shallow/
44+ /// ```
45+ ///
46+ /// For more on Git cache directory, see ["Cargo Home"] in The Cargo Book.
47+ ///
48+ /// For more on the directory format `<pkg>-<hash>[-shallow]`, see [`ident`]
49+ /// and [`ident_shallow`].
50+ ///
51+ /// ## Locked to a revision
52+ ///
53+ /// Once a `GitSource` is fetched, it will resolve to a specific commit revision.
54+ /// This is often mentioned as "locked revision" (`locked_rev`) throughout the
55+ /// codebase. The revision is written into `Cargo.lock`. This is essential since
56+ /// we want to ensure a package can compiles with the same set of files when
57+ /// a `Cargo.lock` is present. With the `locked_rev` provided, `GitSource` can
58+ /// precisely fetch the same revision from the Git repository.
59+ ///
60+ /// ["Cargo Home"]: https://doc.rust-lang.org/nightly/cargo/guide/cargo-home.html#directories
1661pub struct GitSource < ' cfg > {
62+ /// The git remote which we're going to fetch from.
1763 remote : GitRemote ,
64+ /// The Git reference from the manifest file.
1865 manifest_reference : GitReference ,
66+ /// The revision which a git source is locked to.
67+ /// This is expected to be set after the Git repository is fetched.
1968 locked_rev : Option < git2:: Oid > ,
69+ /// The unique identifier of this source.
2070 source_id : SourceId ,
71+ /// The underlying path source to discover packages inside the Git repository.
2172 path_source : Option < PathSource < ' cfg > > ,
73+ /// The identifer of this source for Cargo's Git cache directory.
74+ /// See [`ident`] for more.
2275 ident : String ,
2376 config : & ' cfg Config ,
77+ /// Disables status messages.
2478 quiet : bool ,
2579}
2680
2781impl < ' cfg > GitSource < ' cfg > {
82+ /// Creates a git source for the given [`SourceId`].
2883 pub fn new ( source_id : SourceId , config : & ' cfg Config ) -> CargoResult < GitSource < ' cfg > > {
2984 assert ! ( source_id. is_git( ) , "id is not git, id={}" , source_id) ;
3085
@@ -59,10 +114,14 @@ impl<'cfg> GitSource<'cfg> {
59114 Ok ( source)
60115 }
61116
117+ /// Gets the remote repository URL.
62118 pub fn url ( & self ) -> & Url {
63119 self . remote . url ( )
64120 }
65121
122+ /// Returns the packages discovered by this source. It may fetch the Git
123+ /// repository as well as walk the filesystem if package informations
124+ /// haven't yet updated.
66125 pub fn read_packages ( & mut self ) -> CargoResult < Vec < Package > > {
67126 if self . path_source . is_none ( ) {
68127 self . invalidate_cache ( ) ;
@@ -72,7 +131,8 @@ impl<'cfg> GitSource<'cfg> {
72131 }
73132}
74133
75- /// Create an identifier from a URL, essentially turning `proto://host/path/repo` into `repo-<hash-of-url>`.
134+ /// Create an identifier from a URL,
135+ /// essentially turning `proto://host/path/repo` into `repo-<hash-of-url>`.
76136fn ident ( id : & SourceId ) -> String {
77137 let ident = id
78138 . canonical_url ( )
@@ -86,10 +146,12 @@ fn ident(id: &SourceId) -> String {
86146 format ! ( "{}-{}" , ident, short_hash( id. canonical_url( ) ) )
87147}
88148
89- /// Like `ident()`, but appends `-shallow` to it, turning `proto://host/path/repo` into `repo-<hash-of-url>-shallow`.
149+ /// Like [`ident()`], but appends `-shallow` to it, turning
150+ /// `proto://host/path/repo` into `repo-<hash-of-url>-shallow`.
90151///
91- /// It's important to separate shallow from non-shallow clones for reasons of backwards compatibility - older
92- /// cargo's aren't necessarily handling shallow clones correctly.
152+ /// It's important to separate shallow from non-shallow clones for reasons of
153+ /// backwards compatibility --- older cargo's aren't necessarily handling
154+ /// shallow clones correctly.
93155fn ident_shallow ( id : & SourceId , is_shallow : bool ) -> String {
94156 let mut ident = ident ( id) ;
95157 if is_shallow {
0 commit comments