uv_git/source.rs
1//! Git support is derived from Cargo's implementation.
2//! Cargo is dual-licensed under either Apache 2.0 or MIT, at the user's choice.
3//! Source: <https://github.com/rust-lang/cargo/blob/23eb492cf920ce051abfc56bbaf838514dc8365c/src/cargo/sources/git/source.rs>
4
5use std::borrow::Cow;
6use std::path::{Path, PathBuf};
7use std::sync::Arc;
8
9use anyhow::Result;
10use tracing::{debug, instrument};
11
12use uv_cache_key::{RepositoryUrl, cache_digest};
13use uv_git_types::{GitOid, GitReference, GitUrl};
14use uv_redacted::DisplaySafeUrl;
15
16use crate::GIT_STORE;
17use crate::git::{GitDatabase, GitRemote};
18
19/// A remote Git source that can be checked out locally.
20pub struct GitSource {
21 /// The Git reference from the manifest file.
22 git: GitUrl,
23 /// Whether to disable SSL verification.
24 disable_ssl: bool,
25 /// Whether to operate without network connectivity.
26 offline: bool,
27 /// The path to the Git source database.
28 cache: PathBuf,
29 /// The reporter to use for this source.
30 reporter: Option<Arc<dyn Reporter>>,
31}
32
33impl GitSource {
34 /// Initialize a [`GitSource`] with the given Git URL, HTTP client, and cache path.
35 pub fn new(git: GitUrl, cache: impl Into<PathBuf>, offline: bool) -> Self {
36 Self {
37 git,
38 disable_ssl: false,
39 offline,
40 cache: cache.into(),
41 reporter: None,
42 }
43 }
44
45 /// Disable SSL verification for this [`GitSource`].
46 #[must_use]
47 pub fn dangerous(self) -> Self {
48 Self {
49 disable_ssl: true,
50 ..self
51 }
52 }
53
54 /// Set the [`Reporter`] to use for the [`GitSource`].
55 #[must_use]
56 pub fn with_reporter(self, reporter: Arc<dyn Reporter>) -> Self {
57 Self {
58 reporter: Some(reporter),
59 ..self
60 }
61 }
62
63 /// Fetch the underlying Git repository at the given revision.
64 #[instrument(skip(self), fields(repository = %self.git.repository(), rev = ?self.git.precise()))]
65 pub fn fetch(self) -> Result<Fetch> {
66 // Compute the canonical URL for the repository.
67 let canonical = RepositoryUrl::new(self.git.repository());
68
69 // The path to the repo, within the Git database.
70 let ident = cache_digest(&canonical);
71 let db_path = self.cache.join("db").join(&ident);
72
73 // Authenticate the URL, if necessary.
74 let remote = if let Some(credentials) = GIT_STORE.get(&canonical) {
75 Cow::Owned(credentials.apply(self.git.repository().clone()))
76 } else {
77 Cow::Borrowed(self.git.repository())
78 };
79
80 // Fetch the commit, if we don't already have it. Wrapping this section in a closure makes
81 // it easier to short-circuit this in the cases where we do have the commit.
82 let (db, actual_rev, maybe_task) = || -> Result<(GitDatabase, GitOid, Option<usize>)> {
83 let git_remote = GitRemote::new(&remote);
84 let maybe_db = git_remote.db_at(&db_path).ok();
85
86 // If we have a locked revision, and we have a pre-existing database which has that
87 // revision, then no update needs to happen.
88 if let (Some(rev), Some(db)) = (self.git.precise(), &maybe_db) {
89 if db.contains(rev) {
90 debug!("Using existing Git source `{}`", self.git.repository());
91 return Ok((maybe_db.unwrap(), rev, None));
92 }
93 }
94
95 // If the revision isn't locked, but it looks like it might be an exact commit hash,
96 // and we do have a pre-existing database, then check whether it is, in fact, a commit
97 // hash. If so, treat it like it's locked.
98 if let Some(db) = &maybe_db {
99 if let GitReference::BranchOrTagOrCommit(maybe_commit) = self.git.reference() {
100 if let Ok(oid) = maybe_commit.parse::<GitOid>() {
101 if db.contains(oid) {
102 // This reference is an exact commit. Treat it like it's
103 // locked.
104 debug!("Using existing Git source `{}`", self.git.repository());
105 return Ok((maybe_db.unwrap(), oid, None));
106 }
107 }
108 }
109 }
110
111 // ... otherwise, we use this state to update the Git database. Note that we still check
112 // for being offline here, for example in the situation that we have a locked revision
113 // but the database doesn't have it.
114 debug!("Updating Git source `{}`", self.git.repository());
115
116 // Report the checkout operation to the reporter.
117 let task = self.reporter.as_ref().map(|reporter| {
118 reporter.on_checkout_start(git_remote.url(), self.git.reference().as_rev())
119 });
120
121 let (db, actual_rev) = git_remote.checkout(
122 &db_path,
123 maybe_db,
124 self.git.reference(),
125 self.git.precise(),
126 self.disable_ssl,
127 self.offline,
128 )?;
129
130 Ok((db, actual_rev, task))
131 }()?;
132
133 // Don’t use the full hash, in order to contribute less to reaching the
134 // path length limit on Windows.
135 let short_id = db.to_short_id(actual_rev)?;
136
137 // Check out `actual_rev` from the database to a scoped location on the
138 // filesystem. This will use hard links and such to ideally make the
139 // checkout operation here pretty fast.
140 let checkout_path = self
141 .cache
142 .join("checkouts")
143 .join(&ident)
144 .join(short_id.as_str());
145
146 db.copy_to(actual_rev, &checkout_path)?;
147
148 // Report the checkout operation to the reporter.
149 if let Some(task) = maybe_task {
150 if let Some(reporter) = self.reporter.as_ref() {
151 reporter.on_checkout_complete(remote.as_ref(), actual_rev.as_str(), task);
152 }
153 }
154
155 Ok(Fetch {
156 git: self.git.with_precise(actual_rev),
157 path: checkout_path,
158 })
159 }
160}
161
162pub struct Fetch {
163 /// The [`GitUrl`] reference that was fetched.
164 git: GitUrl,
165 /// The path to the checked out repository.
166 path: PathBuf,
167}
168
169impl Fetch {
170 pub fn git(&self) -> &GitUrl {
171 &self.git
172 }
173
174 pub fn path(&self) -> &Path {
175 &self.path
176 }
177
178 pub fn into_git(self) -> GitUrl {
179 self.git
180 }
181
182 pub fn into_path(self) -> PathBuf {
183 self.path
184 }
185}
186
187pub trait Reporter: Send + Sync {
188 /// Callback to invoke when a repository checkout begins.
189 fn on_checkout_start(&self, url: &DisplaySafeUrl, rev: &str) -> usize;
190
191 /// Callback to invoke when a repository checkout completes.
192 fn on_checkout_complete(&self, url: &DisplaySafeUrl, rev: &str, index: usize);
193}