Skip to main content

cargo/sources/git/
oxide.rs

1//! This module contains all code sporting `gitoxide` for operations on `git` repositories and it mirrors
2//! `utils` closely for now. One day it can be renamed into `utils` once `git2` isn't required anymore.
3
4use crate::util::HumanBytes;
5use crate::util::network::http::HttpTimeout;
6use crate::util::{MetricsCounter, Progress, network};
7use crate::{CargoResult, GlobalContext};
8use cargo_util::paths;
9use gix::bstr::{BString, ByteSlice};
10use std::cell::RefCell;
11use std::path::Path;
12use std::sync::atomic::{AtomicBool, Ordering};
13use std::sync::{Arc, Weak};
14use std::time::{Duration, Instant};
15use tracing::debug;
16
17/// For the time being, `repo_path` makes it easy to instantiate a gitoxide repo just for fetching.
18/// In future this may change to be the gitoxide repository itself.
19pub fn with_retry_and_progress(
20    repo_path: &std::path::Path,
21    gctx: &GlobalContext,
22    repo_remote_url: &str,
23    cb: &(
24         dyn Fn(
25        &std::path::Path,
26        &AtomicBool,
27        &mut gix::progress::tree::Item,
28        &mut dyn FnMut(&gix::bstr::BStr),
29    ) -> Result<(), crate::sources::git::fetch::Error>
30             + Send
31             + Sync
32     ),
33) -> CargoResult<()> {
34    std::thread::scope(|s| {
35        let mut progress_bar = Progress::new("Fetch", gctx);
36        let is_shallow = gctx.cli_unstable().git.map_or(false, |features| {
37            features.shallow_deps || features.shallow_index
38        });
39        network::retry::with_retry(gctx, || {
40            let progress_root: Arc<gix::progress::tree::Root> =
41                gix::progress::tree::root::Options {
42                    initial_capacity: 10,
43                    message_buffer_capacity: 10,
44                }
45                .into();
46            let root = Arc::downgrade(&progress_root);
47            let thread = s.spawn(move || {
48                let mut progress = progress_root.add_child("operation");
49                let mut urls = RefCell::new(Default::default());
50                let res = cb(
51                    &repo_path,
52                    &AtomicBool::default(),
53                    &mut progress,
54                    &mut |url| {
55                        *urls.borrow_mut() = Some(url.to_owned());
56                    },
57                );
58                amend_authentication_hints(res, repo_remote_url, urls.get_mut().take())
59            });
60            translate_progress_to_bar(&mut progress_bar, root, is_shallow)?;
61            thread.join().expect("no panic in scoped thread")
62        })
63    })
64}
65
66fn translate_progress_to_bar(
67    progress_bar: &mut Progress<'_>,
68    root: Weak<gix::progress::tree::Root>,
69    is_shallow: bool,
70) -> CargoResult<()> {
71    let remote_progress: gix::progress::Id = gix::remote::fetch::ProgressId::RemoteProgress.into();
72    let read_pack_bytes: gix::progress::Id =
73        gix::odb::pack::bundle::write::ProgressId::ReadPackBytes.into();
74    let delta_index_objects: gix::progress::Id =
75        gix::odb::pack::index::write::ProgressId::IndexObjects.into();
76    let resolve_objects: gix::progress::Id =
77        gix::odb::pack::index::write::ProgressId::ResolveObjects.into();
78
79    // We choose `N=10` here to make a `300ms * 10slots ~= 3000ms`
80    // sliding window for tracking the data transfer rate (in bytes/s).
81    let mut last_percentage_update = Instant::now();
82    let mut last_fast_update = Instant::now();
83    let mut counter = MetricsCounter::<10>::new(0, last_percentage_update);
84
85    let mut tasks = Vec::with_capacity(10);
86    let slow_check_interval = std::time::Duration::from_millis(300);
87    let fast_check_interval = Duration::from_millis(50);
88    let sleep_interval = Duration::from_millis(10);
89    debug_assert_eq!(
90        slow_check_interval.as_millis() % fast_check_interval.as_millis(),
91        0,
92        "progress should be smoother by keeping these as multiples of each other"
93    );
94    debug_assert_eq!(
95        fast_check_interval.as_millis() % sleep_interval.as_millis(),
96        0,
97        "progress should be smoother by keeping these as multiples of each other"
98    );
99
100    let num_phases = if is_shallow { 3 } else { 2 }; // indexing + delta-resolution, both with same amount of objects to handle
101    while let Some(root) = root.upgrade() {
102        std::thread::sleep(sleep_interval);
103        let needs_update = last_fast_update.elapsed() >= fast_check_interval;
104        if !needs_update {
105            continue;
106        }
107        let now = Instant::now();
108        last_fast_update = now;
109
110        root.sorted_snapshot(&mut tasks);
111
112        fn progress_by_id(
113            id: gix::progress::Id,
114            task: &gix::progress::Task,
115        ) -> Option<(&str, &gix::progress::Value)> {
116            (task.id == id)
117                .then(|| task.progress.as_ref())
118                .flatten()
119                .map(|value| (task.name.as_str(), value))
120        }
121        fn find_in<K>(
122            tasks: &[(K, gix::progress::Task)],
123            cb: impl Fn(&gix::progress::Task) -> Option<(&str, &gix::progress::Value)>,
124        ) -> Option<(&str, &gix::progress::Value)> {
125            tasks.iter().find_map(|(_, t)| cb(t))
126        }
127
128        if let Some((_, objs)) = find_in(&tasks, |t| progress_by_id(resolve_objects, t)) {
129            // Phase 3: Resolving deltas.
130            let objects = objs.step.load(Ordering::Relaxed);
131            let total_objects = objs.done_at.expect("known amount of objects");
132            let msg = format!(", ({objects}/{total_objects}) resolving deltas");
133
134            progress_bar.tick(
135                (total_objects * (num_phases - 1)) + objects,
136                total_objects * num_phases,
137                &msg,
138            )?;
139        } else if let Some((objs, read_pack)) =
140            find_in(&tasks, |t| progress_by_id(read_pack_bytes, t)).and_then(|read| {
141                find_in(&tasks, |t| progress_by_id(delta_index_objects, t))
142                    .map(|delta| (delta.1, read.1))
143            })
144        {
145            // Phase 2: Receiving objects.
146            let objects = objs.step.load(Ordering::Relaxed);
147            let total_objects = objs.done_at.expect("known amount of objects");
148            let received_bytes = read_pack.step.load(Ordering::Relaxed);
149
150            let needs_percentage_update = last_percentage_update.elapsed() >= slow_check_interval;
151            if needs_percentage_update {
152                counter.add(received_bytes, now);
153                last_percentage_update = now;
154            }
155            let rate = HumanBytes(counter.rate() as u64);
156            let msg = format!(", {rate:.2}/s");
157
158            progress_bar.tick(
159                (total_objects * (num_phases - 2)) + objects,
160                total_objects * num_phases,
161                &msg,
162            )?;
163        } else if let Some((action, remote)) =
164            find_in(&tasks, |t| progress_by_id(remote_progress, t))
165        {
166            if !is_shallow {
167                continue;
168            }
169            // phase 1: work on the remote side
170
171            // Resolving deltas.
172            let objects = remote.step.load(Ordering::Relaxed);
173            if let Some(total_objects) = remote.done_at {
174                let msg = format!(", ({objects}/{total_objects}) {action}");
175                progress_bar.tick(objects, total_objects * num_phases, &msg)?;
176            }
177        }
178    }
179    Ok(())
180}
181
182#[derive(Debug, thiserror::Error)]
183#[error("{0}")]
184pub(super) struct CredentialError(#[source] pub gix::Error);
185
186fn amend_authentication_hints(
187    res: Result<(), crate::sources::git::fetch::Error>,
188    remote_url: &str,
189    last_url_for_authentication: Option<gix::bstr::BString>,
190) -> CargoResult<()> {
191    let Err(err) = res else { return Ok(()) };
192    let auth_message = if err.downcast_any_ref::<CredentialError>().is_some()
193        || err.is_unauthenticated()
194    {
195        if err
196            .downcast_any_ref::<gix::protocol::transport::client::AuthenticationRequired>()
197            .is_none()
198        {
199            "\n* attempted to find username/password via \
200                     git's `credential.helper` support, but failed"
201                .into()
202        } else {
203            "\n* attempted to find username/password via \
204                     `credential.helper`, but maybe the found \
205                     credentials were incorrect"
206                .into()
207        }
208    } else {
209        if err
210            .downcast_any_ref::<gix::protocol::transport::client::Error>()
211            .is_some()
212        {
213            let msg = format!(
214                concat!(
215                    "network failure seems to have happened\n",
216                    "if a proxy or similar is necessary `net.git-fetch-with-cli` may help here\n",
217                    "https://doc.rust-lang.org/cargo/reference/config.html#netgit-fetch-with-cli",
218                    "{}"
219                ),
220                super::utils::note_github_pull_request(remote_url).unwrap_or_default()
221            );
222            return Err(anyhow::Error::from(err).context(msg));
223        }
224        None
225    };
226    if let Some(auth_message) = auth_message {
227        let mut msg = "failed to authenticate when downloading \
228                       repository"
229            .to_string();
230        if let Some(url) = last_url_for_authentication {
231            msg.push_str(": ");
232            msg.push_str(url.to_str_lossy().as_ref());
233        }
234        msg.push('\n');
235        msg.push_str(auth_message);
236        msg.push_str("\n\n");
237        msg.push_str("if the git CLI succeeds then `net.git-fetch-with-cli` may help here\n");
238        msg.push_str("https://doc.rust-lang.org/cargo/reference/config.html#netgit-fetch-with-cli");
239        return Err(anyhow::Error::from(err).context(msg));
240    }
241    Err(err.into())
242}
243
244/// The reason we are opening a git repository.
245///
246/// This can affect the way we open it and the cost associated with it.
247pub enum OpenMode {
248    /// We need `git_binary` configuration as well for being able to see credential helpers
249    /// that are configured with the `git` installation itself.
250    /// However, this is slow on windows (~150ms) and most people won't need it as they use the
251    /// standard index which won't ever need authentication, so we only enable this when needed.
252    ForFetch,
253}
254
255impl OpenMode {
256    /// Sometimes we don't need to pay for figuring out the system's git installation, and this tells
257    /// us if that is the case.
258    pub fn needs_git_binary_config(&self) -> bool {
259        match self {
260            OpenMode::ForFetch => true,
261        }
262    }
263}
264
265/// Produce a repository with everything pre-configured according to `config`. Most notably this includes
266/// transport configuration. Knowing its `purpose` helps to optimize the way we open the repository.
267/// Use `config_overrides` to configure the new repository.
268pub fn open_repo(
269    repo_path: &std::path::Path,
270    config_overrides: Vec<BString>,
271    purpose: OpenMode,
272) -> gix::Result<gix::Repository> {
273    gix::open_opts(repo_path, {
274        let mut opts = gix::open::Options::default();
275        opts.permissions.config = gix::open::permissions::Config::all();
276        opts.permissions.config.git_binary = purpose.needs_git_binary_config();
277        opts.with(gix::sec::Trust::Full)
278            .config_overrides(config_overrides)
279    })
280}
281
282/// Convert `git` related cargo configuration into the respective `git` configuration which can be
283/// used when opening new repositories.
284pub fn cargo_config_to_gitoxide_overrides(gctx: &GlobalContext) -> CargoResult<Vec<BString>> {
285    use gix::config::tree::{Core, Http, Key, gitoxide};
286    let timeout = HttpTimeout::new(gctx)?;
287    let http = gctx.http_config()?;
288
289    let mut values = vec![
290        gitoxide::Http::CONNECT_TIMEOUT.validated_assignment_fmt(&timeout.dur.as_millis())?,
291        Http::LOW_SPEED_LIMIT.validated_assignment_fmt(&timeout.low_speed_limit)?,
292        Http::LOW_SPEED_TIME.validated_assignment_fmt(&timeout.dur.as_secs())?,
293        // Assure we are not depending on committer information when updating refs after cloning.
294        Core::LOG_ALL_REF_UPDATES.validated_assignment_fmt(&false)?,
295    ];
296    if let Some(proxy) = &http.proxy {
297        values.push(Http::PROXY.validated_assignment_fmt(proxy)?);
298    }
299    if let Some(check_revoke) = http.check_revoke {
300        values.push(Http::SCHANNEL_CHECK_REVOKE.validated_assignment_fmt(&check_revoke)?);
301    }
302    if let Some(cainfo) = &http.cainfo {
303        values.push(
304            Http::SSL_CA_INFO.validated_assignment_fmt(&cainfo.resolve_path(gctx).display())?,
305        );
306    }
307
308    values.push(if let Some(user_agent) = &http.user_agent {
309        Http::USER_AGENT.validated_assignment_fmt(user_agent)
310    } else {
311        Http::USER_AGENT.validated_assignment_fmt(&format!("cargo {}", crate::version()))
312    }?);
313    if let Some(ssl_version) = &http.ssl_version {
314        use crate::context::SslVersionConfig;
315        match ssl_version {
316            SslVersionConfig::Single(version) => {
317                values.push(Http::SSL_VERSION.validated_assignment_fmt(&version)?);
318            }
319            SslVersionConfig::Range(range) => {
320                values.push(
321                    gitoxide::Http::SSL_VERSION_MIN
322                        .validated_assignment_fmt(&range.min.as_deref().unwrap_or("default"))?,
323                );
324                values.push(
325                    gitoxide::Http::SSL_VERSION_MAX
326                        .validated_assignment_fmt(&range.max.as_deref().unwrap_or("default"))?,
327                );
328            }
329        }
330    } else if cfg!(windows) {
331        // This text is copied from https://github.com/rust-lang/cargo/blob/39c13e67a5962466cc7253d41bc1099bbcb224c3/src/ops/registry.rs#L658-L674 .
332        // This is a temporary workaround for some bugs with libcurl and
333        // schannel and TLS 1.3.
334        //
335        // Our libcurl on Windows is usually built with schannel.
336        // On Windows 11 (or Windows Server 2022), libcurl recently (late
337        // 2022) gained support for TLS 1.3 with schannel, and it now defaults
338        // to 1.3. Unfortunately there have been some bugs with this.
339        // https://github.com/curl/curl/issues/9431 is the most recent. Once
340        // that has been fixed, and some time has passed where we can be more
341        // confident that the 1.3 support won't cause issues, this can be
342        // removed.
343        //
344        // Windows 10 is unaffected. libcurl does not support TLS 1.3 on
345        // Windows 10. (Windows 10 sorta had support, but it required enabling
346        // an advanced option in the registry which was buggy, and libcurl
347        // does runtime checks to prevent it.)
348        values.push(gitoxide::Http::SSL_VERSION_MIN.validated_assignment_fmt(&"default")?);
349        values.push(gitoxide::Http::SSL_VERSION_MAX.validated_assignment_fmt(&"tlsv1.2")?);
350    }
351    if let Some(debug) = http.debug {
352        values.push(gitoxide::Http::VERBOSE.validated_assignment_fmt(&debug)?);
353    }
354    if let Some(multiplexing) = http.multiplexing {
355        let http_version = multiplexing.then(|| "HTTP/2").unwrap_or("HTTP/1.1");
356        // Note that failing to set the HTTP version in `gix-transport` isn't fatal,
357        // which is why we don't have to try to figure out if HTTP V2 is supported in the
358        // currently linked version (see `try_old_curl!()`)
359        values.push(Http::VERSION.validated_assignment_fmt(&http_version)?);
360    }
361
362    Ok(values)
363}
364
365/// Reinitializes a given Git repository. This is useful when a Git repository
366/// seems corrupted, and we want to start over.
367pub fn reinitialize(git_dir: &Path) -> CargoResult<()> {
368    fn init(path: &Path, bare: bool) -> CargoResult<()> {
369        let mut opts = git2::RepositoryInitOptions::new();
370        // Skip anything related to templates, they just call all sorts of issues as
371        // we really don't want to use them yet they insist on being used. See #6240
372        // for an example issue that comes up.
373        opts.external_template(false);
374        opts.bare(bare);
375        git2::Repository::init_opts(&path, &opts)?;
376        Ok(())
377    }
378    // Here we want to drop the current repository object pointed to by `repo`,
379    // so we initialize temporary repository in a sub-folder, blow away the
380    // existing git folder, and then recreate the git repo. Finally we blow away
381    // the `tmp` folder we allocated.
382    debug!("reinitializing git repo at {:?}", git_dir);
383    let tmp = git_dir.join("tmp");
384    let bare = !git_dir.ends_with(".git");
385    init(&tmp, false)?;
386    for entry in git_dir.read_dir()? {
387        let entry = entry?;
388        if entry.file_name().to_str() == Some("tmp") {
389            continue;
390        }
391        let path = entry.path();
392        drop(paths::remove_file(&path).or_else(|_| paths::remove_dir_all(&path)));
393    }
394    init(git_dir, bare)?;
395    paths::remove_dir_all(&tmp)?;
396    Ok(())
397}
398
399#[cfg(test)]
400mod tests {
401    use super::{CredentialError, amend_authentication_hints};
402    use gix::error::ErrorExt;
403    use gix::protocol::transport::client::{AuthenticationRequired, Error};
404
405    #[test]
406    fn authentication_hints_preserve_causes_and_distinguish_network_errors() {
407        let missing = gix::error::unauthenticated("missing identity").raise();
408        let helper =
409            CredentialError(gix::error::message("Terminal prompts are disabled").raise()).raise();
410        let rejected = std::io::Error::new(
411            std::io::ErrorKind::PermissionDenied,
412            AuthenticationRequired::default(),
413        )
414        .raise();
415        let network = Error::Io(std::io::Error::from(std::io::ErrorKind::ConnectionReset)).raise();
416        for (err, hint) in [
417            (missing, "git's `credential.helper` support, but failed"),
418            (helper, "git's `credential.helper` support, but failed"),
419            (rejected, "credentials were incorrect"),
420            (network, "network failure seems to have happened"),
421        ] {
422            let err = amend_authentication_hints(
423                Err(err),
424                "https://example.com/repo",
425                Some("https://example.com/auth".into()),
426            )
427            .unwrap_err();
428            assert!(err.to_string().contains(hint));
429            assert!(err.downcast_ref::<gix::Error>().is_some());
430        }
431        let cancelled = CredentialError(
432            std::io::Error::from(std::io::ErrorKind::ConnectionReset)
433                .and_raise(gix::error::cancelled("cancelled")),
434        )
435        .raise();
436        assert!(cancelled.is_cancelled());
437        assert!(!cancelled.can_retry_lenient());
438    }
439}