1use 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::cache_digest;
13use uv_git_types::{GitOid, GitUrl};
14use uv_redacted::SafeUrl;
15
16use crate::credentials::GIT_STORE;
17use crate::git::{GitDatabase, GitRemote};
18
19pub(crate) struct GitSource {
21 git: GitUrl,
23 disable_ssl: bool,
25 offline: bool,
27 cache: PathBuf,
29 reporter: Option<Arc<dyn Reporter>>,
31}
32
33impl GitSource {
34 pub(crate) 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 #[must_use]
47 pub(crate) fn dangerous(self) -> Self {
48 Self {
49 disable_ssl: true,
50 ..self
51 }
52 }
53
54 #[must_use]
56 pub(crate) fn with_reporter(self, reporter: Arc<dyn Reporter>) -> Self {
57 Self {
58 reporter: Some(reporter),
59 ..self
60 }
61 }
62
63 #[instrument(skip(self), fields(repository = %self.git.url(), rev = ?self.git.precise()))]
65 pub(crate) fn fetch(self) -> Result<Fetch> {
66 let lfs_requested = self.git.lfs().enabled();
67
68 let repository_digest = cache_digest(self.git.repository());
70 let db_path = self.cache.join("db").join(repository_digest);
71
72 let remote = if let Some(credentials) = GIT_STORE.get(self.git.repository()) {
74 Cow::Owned(credentials.apply(self.git.url().clone()))
75 } else {
76 Cow::Borrowed(self.git.url())
77 };
78
79 let (db, actual_rev, maybe_task) = || -> Result<(GitDatabase, GitOid, Option<usize>)> {
82 let git_remote = GitRemote::new(remote.clone().into_owned());
83 let maybe_db = git_remote.db_at(&db_path).ok();
84
85 if let (Some(rev), Some(db)) = (self.git.precise(), &maybe_db) {
89 if db.contains(rev) && (!lfs_requested || db.contains_lfs_artifacts(rev)) {
90 debug!("Using existing Git source `{}`", self.git.url());
91 return Ok((
92 maybe_db
93 .unwrap()
94 .with_lfs_ready(lfs_requested.then_some(true)),
95 rev,
96 None,
97 ));
98 }
99 }
100
101 debug!("Updating Git source `{}`", self.git.url());
105
106 let task = self.reporter.as_ref().map(|reporter| {
108 reporter.on_checkout_start(git_remote.url(), self.git.reference().as_rev())
109 });
110
111 let (db, actual_rev) = git_remote.checkout(
112 &db_path,
113 maybe_db,
114 self.git.reference(),
115 self.git.precise(),
116 self.disable_ssl,
117 self.offline,
118 lfs_requested,
119 )?;
120
121 Ok((db, actual_rev, task))
122 }()?;
123
124 let git = self.git.clone().with_precise(actual_rev)?;
126
127 let short_id = db.to_short_id(actual_rev)?;
130
131 let checkout_path = self
132 .cache
133 .join("checkouts")
134 .join(git.repository().checkout_digest(lfs_requested))
135 .join(short_id.as_str());
136
137 let checkout = db.copy_to(actual_rev, &checkout_path)?;
141
142 if let Some(task) = maybe_task {
144 if let Some(reporter) = self.reporter.as_ref() {
145 reporter.on_checkout_complete(remote.as_ref(), actual_rev.as_str(), task);
146 }
147 }
148
149 Ok(Fetch {
150 git,
151 path: checkout_path,
152 lfs_ready: checkout.lfs_ready().unwrap_or(false),
153 })
154 }
155}
156
157pub struct Fetch {
158 git: GitUrl,
160 path: PathBuf,
162 lfs_ready: bool,
164}
165
166impl Fetch {
167 pub fn git(&self) -> &GitUrl {
168 &self.git
169 }
170
171 pub fn path(&self) -> &Path {
172 &self.path
173 }
174
175 pub fn lfs_ready(&self) -> &bool {
176 &self.lfs_ready
177 }
178}
179
180pub trait Reporter: Send + Sync {
181 fn on_checkout_start(&self, url: &SafeUrl, rev: &str) -> usize;
183
184 fn on_checkout_complete(&self, url: &SafeUrl, rev: &str, index: usize);
186}