Skip to content

Commit 6956c04

Browse files
committed
Auto merge of #12192 - weihanglo:source-doc, r=epage
docs: add doc comments for git source and friends
2 parents 068e549 + 9a29b8b commit 6956c04

File tree

6 files changed

+199
-47
lines changed

6 files changed

+199
-47
lines changed

Diff for: src/cargo/sources/git/known_hosts.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
//! SSH host key validation support.
22
//!
3+
//! The only public item in this module is [`certificate_check`],
4+
//! which provides a callback to [`git2::RemoteCallbacks::certificate_check`].
5+
//!
36
//! A primary goal with this implementation is to provide user-friendly error
47
//! messages, guiding them to understand the issue and how to resolve it.
58
//!

Diff for: src/cargo/sources/git/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1+
//! Home of the [`GitSource`].
2+
//!
3+
//! Apparently, the most important type in this module is [`GitSource`].
4+
//! [`utils`] provides libgit2 utilities like fetch and checkout, whereas
5+
//! [`oxide`] is the couterpart for gitoxide integration. [`known_hosts`]
6+
//! is the mitigation of [CVE-2022-46176].
7+
//!
8+
//! [CVE-2022-46176]: https://blog.rust-lang.org/2023/01/10/cve-2022-46176.html
9+
110
pub use self::source::GitSource;
211
pub use self::utils::{fetch, GitCheckout, GitDatabase, GitRemote};
312
mod known_hosts;
413
mod oxide;
514
mod source;
615
mod utils;
716

17+
/// For `-Zgitoxide` integration.
818
pub mod fetch {
919
use crate::core::features::GitoxideFeatures;
1020
use crate::Config;

Diff for: src/cargo/sources/git/oxide.rs

+2
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,8 @@ pub fn cargo_config_to_gitoxide_overrides(config: &Config) -> CargoResult<Vec<BS
353353
Ok(values)
354354
}
355355

356+
/// Reinitializes a given Git repository. This is useful when a Git repoistory
357+
/// seems corrupted and we want to start over.
356358
pub fn reinitialize(git_dir: &Path) -> CargoResult<()> {
357359
fn init(path: &Path, bare: bool) -> CargoResult<()> {
358360
let mut opts = git2::RepositoryInitOptions::new();

Diff for: src/cargo/sources/git/source.rs

+66-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//! See [GitSource].
2+
13
use crate::core::source::{MaybePackage, QueryKind, Source, SourceId};
24
use crate::core::GitReference;
35
use crate::core::{Dependency, Package, PackageId, Summary};
@@ -13,18 +15,71 @@ use std::fmt::{self, Debug, Formatter};
1315
use std::task::Poll;
1416
use 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
1661
pub 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

2781
impl<'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>`.
76136
fn 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.
93155
fn ident_shallow(id: &SourceId, is_shallow: bool) -> String {
94156
let mut ident = ident(id);
95157
if is_shallow {

0 commit comments

Comments
 (0)