|
1 | | -use crate::error::Result; |
2 | | -use crate::utils::report_error; |
3 | | -use anyhow::Context; |
4 | | -use crates_index_diff::gix; |
5 | | -use std::path::PathBuf; |
6 | | -use std::process::Command; |
7 | | -use std::sync::atomic::AtomicBool; |
| 1 | +use crate::{ |
| 2 | + error::Result, |
| 3 | + utils::{report_error, run_blocking_scoped}, |
| 4 | +}; |
| 5 | +use anyhow::Context as _; |
| 6 | +use crates_index_diff::{Change, gix, index::diff::Order}; |
| 7 | +use std::{ |
| 8 | + path::{Path, PathBuf}, |
| 9 | + sync::{Arc, Mutex, atomic::AtomicBool}, |
| 10 | +}; |
| 11 | +use tokio::process::Command; |
8 | 12 |
|
| 13 | +const THREAD_NAME: &str = "crates-index-diff"; |
| 14 | + |
| 15 | +/// async-friendly wrapper around `crates_index_diff::Index` |
9 | 16 | pub struct Index { |
10 | 17 | path: PathBuf, |
11 | 18 | repository_url: Option<String>, |
| 19 | + // NOTE: we can use a sync mutex here, because we're only locking it |
| 20 | + // inside handle_in_thread calls, so the mutex lock won't ever be held |
| 21 | + // across await-points. |
| 22 | + #[allow(clippy::disallowed_types)] |
| 23 | + index: Arc<Mutex<crates_index_diff::Index>>, |
12 | 24 | } |
13 | 25 |
|
14 | 26 | impl Index { |
15 | | - pub fn from_url(path: PathBuf, url: String) -> Result<Self> { |
16 | | - crates_index_diff::Index::from_path_or_cloned_with_options( |
17 | | - &path, |
18 | | - gix::progress::Discard, |
19 | | - &AtomicBool::default(), |
20 | | - crates_index_diff::index::CloneOptions { url: url.clone() }, |
21 | | - ) |
22 | | - .map(|_| ()) |
23 | | - .context("initialising registry index repository")?; |
| 27 | + pub async fn from_url( |
| 28 | + path: impl AsRef<Path>, |
| 29 | + repository_url: Option<impl AsRef<str>>, |
| 30 | + ) -> Result<Self> { |
| 31 | + let path = path.as_ref().to_path_buf(); |
| 32 | + let repository_url = repository_url.map(|url| url.as_ref().to_owned()); |
24 | 33 |
|
25 | | - Ok(Self { |
26 | | - path, |
27 | | - repository_url: Some(url), |
| 34 | + let clone_options = repository_url |
| 35 | + .as_ref() |
| 36 | + .map(|url| crates_index_diff::index::CloneOptions { url: url.clone() }) |
| 37 | + .unwrap_or_default(); |
| 38 | + |
| 39 | + let index = run_blocking_scoped(THREAD_NAME, || { |
| 40 | + Ok(Arc::new(Mutex::new( |
| 41 | + #[allow(clippy::disallowed_types)] |
| 42 | + crates_index_diff::Index::from_path_or_cloned_with_options( |
| 43 | + &path, |
| 44 | + gix::progress::Discard, |
| 45 | + &AtomicBool::default(), |
| 46 | + clone_options, |
| 47 | + ) |
| 48 | + .context("initialising registry index repository")?, |
| 49 | + ))) |
28 | 50 | }) |
29 | | - } |
| 51 | + .await?; |
30 | 52 |
|
31 | | - pub fn new(path: PathBuf) -> Result<Self> { |
32 | | - // This initializes the repository, then closes it afterwards to avoid leaking file descriptors. |
33 | | - // See https://github.com/rust-lang/docs.rs/pull/847 |
34 | | - crates_index_diff::Index::from_path_or_cloned(&path) |
35 | | - .map(|_| ()) |
36 | | - .context("initialising registry index repository")?; |
37 | 53 | Ok(Self { |
| 54 | + index, |
38 | 55 | path, |
39 | | - repository_url: None, |
| 56 | + repository_url, |
40 | 57 | }) |
41 | 58 | } |
42 | 59 |
|
43 | | - pub fn diff(&self) -> Result<crates_index_diff::Index> { |
44 | | - let options = self |
45 | | - .repository_url |
46 | | - .clone() |
47 | | - .map(|url| crates_index_diff::index::CloneOptions { url }) |
48 | | - .unwrap_or_default(); |
49 | | - let diff = crates_index_diff::Index::from_path_or_cloned_with_options( |
50 | | - &self.path, |
51 | | - gix::progress::Discard, |
52 | | - &AtomicBool::default(), |
53 | | - options, |
54 | | - ) |
55 | | - .context("re-opening registry index for diff")?; |
56 | | - Ok(diff) |
57 | | - } |
58 | | - |
59 | | - pub(crate) fn crates(&self) -> Result<crates_index::GitIndex> { |
60 | | - tracing::debug!("Opening with `crates_index`"); |
61 | | - // crates_index requires the repo url to match the existing origin or it tries to reinitialize the repo |
62 | | - let repo_url = self |
63 | | - .repository_url |
64 | | - .as_deref() |
65 | | - .unwrap_or("https://github.com/rust-lang/crates.io-index"); |
66 | | - let mut index = crates_index::GitIndex::with_path(&self.path, repo_url)?; |
67 | | - index.update()?; |
68 | | - Ok(index) |
| 60 | + pub async fn new(path: impl AsRef<Path>) -> Result<Self> { |
| 61 | + Self::from_url(path, None::<&str>).await |
69 | 62 | } |
70 | 63 |
|
71 | | - pub fn run_git_gc(&self) { |
| 64 | + pub async fn run_git_gc(&self) { |
72 | 65 | let gc = Command::new("git") |
73 | 66 | .arg("-C") |
74 | 67 | .arg(&self.path) |
75 | 68 | .args(["gc", "--auto"]) |
76 | 69 | .output() |
| 70 | + .await |
77 | 71 | .with_context(|| format!("failed to run `git gc --auto`\npath: {:#?}", &self.path)); |
78 | 72 |
|
79 | 73 | if let Err(err) = gc { |
80 | 74 | report_error(&err); |
81 | 75 | } |
82 | 76 | } |
83 | 77 |
|
| 78 | + async fn peek_changes_with_order( |
| 79 | + &self, |
| 80 | + order: Order, |
| 81 | + ) -> Result<(Vec<Change>, gix::hash::ObjectId)> { |
| 82 | + run_blocking_scoped(THREAD_NAME, || { |
| 83 | + let index = self.index.lock().unwrap(); |
| 84 | + index |
| 85 | + .peek_changes_with_options(gix::progress::Discard, &AtomicBool::default(), order) |
| 86 | + .map_err(Into::into) |
| 87 | + }) |
| 88 | + .await |
| 89 | + } |
| 90 | + |
| 91 | + pub async fn peek_changes(&self) -> Result<(Vec<Change>, gix::hash::ObjectId)> { |
| 92 | + self.peek_changes_with_order(Order::ImplementationDefined) |
| 93 | + .await |
| 94 | + } |
| 95 | + |
| 96 | + pub async fn peek_changes_ordered(&self) -> Result<(Vec<Change>, gix::hash::ObjectId)> { |
| 97 | + self.peek_changes_with_order(Order::AsInCratesIndex).await |
| 98 | + } |
| 99 | + |
| 100 | + pub async fn set_last_seen_reference(&self, to: gix::hash::ObjectId) -> Result<()> { |
| 101 | + run_blocking_scoped(THREAD_NAME, || { |
| 102 | + let index = self.index.lock().unwrap(); |
| 103 | + index.set_last_seen_reference(to).map_err(Into::into) |
| 104 | + }) |
| 105 | + .await |
| 106 | + } |
| 107 | + |
| 108 | + pub async fn latest_commit_reference(&self) -> Result<gix::ObjectId> { |
| 109 | + let (_, oid) = self.peek_changes().await?; |
| 110 | + Ok(oid) |
| 111 | + } |
| 112 | + |
84 | 113 | pub fn repository_url(&self) -> Option<&str> { |
85 | 114 | self.repository_url.as_deref() |
86 | 115 | } |
|
0 commit comments