|
1 | | -use crate::error::Result; |
2 | | -use crate::utils::report_error; |
| 1 | +use crate::{ |
| 2 | + error::Result, |
| 3 | + utils::{report_error, spawn_blocking}, |
| 4 | +}; |
3 | 5 | 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; |
| 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 | +/// async-friendly wrapper around `crates_index_diff::Index` |
9 | 14 | pub struct Index { |
10 | 15 | path: PathBuf, |
11 | 16 | repository_url: Option<String>, |
| 17 | + // NOTE: we can use a sync mutex here, because we're only locking it |
| 18 | + // inside spawn_blocking calls, so the mutex lock won't ever be held |
| 19 | + // across await-points. |
| 20 | + #[allow(clippy::disallowed_types)] |
| 21 | + index: Arc<Mutex<crates_index_diff::Index>>, |
12 | 22 | } |
13 | 23 |
|
14 | 24 | 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")?; |
| 25 | + pub async fn from_url( |
| 26 | + path: impl AsRef<Path>, |
| 27 | + repository_url: Option<impl AsRef<str>>, |
| 28 | + ) -> Result<Self> { |
| 29 | + let path = path.as_ref().to_path_buf(); |
| 30 | + let repository_url = repository_url.map(|url| url.as_ref().to_owned()); |
24 | 31 |
|
25 | | - Ok(Self { |
26 | | - path, |
27 | | - repository_url: Some(url), |
| 32 | + let clone_options = repository_url |
| 33 | + .as_ref() |
| 34 | + .map(|url| crates_index_diff::index::CloneOptions { url: url.clone() }) |
| 35 | + .unwrap_or_default(); |
| 36 | + |
| 37 | + let index = spawn_blocking({ |
| 38 | + let path = path.clone(); |
| 39 | + move || { |
| 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 | + ))) |
| 50 | + } |
28 | 51 | }) |
29 | | - } |
| 52 | + .await?; |
30 | 53 |
|
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 | 54 | Ok(Self { |
| 55 | + index, |
38 | 56 | path, |
39 | | - repository_url: None, |
| 57 | + repository_url, |
40 | 58 | }) |
41 | 59 | } |
42 | 60 |
|
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) |
| 61 | + pub async fn new(path: impl AsRef<Path>) -> Result<Self> { |
| 62 | + Self::from_url(path, None::<&str>).await |
57 | 63 | } |
58 | 64 |
|
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) |
69 | | - } |
70 | | - |
71 | | - pub fn run_git_gc(&self) { |
| 65 | + pub async fn run_git_gc(&self) { |
72 | 66 | let gc = Command::new("git") |
73 | 67 | .arg("-C") |
74 | 68 | .arg(&self.path) |
75 | 69 | .args(["gc", "--auto"]) |
76 | 70 | .output() |
| 71 | + .await |
77 | 72 | .with_context(|| format!("failed to run `git gc --auto`\npath: {:#?}", &self.path)); |
78 | 73 |
|
79 | 74 | if let Err(err) = gc { |
80 | 75 | report_error(&err); |
81 | 76 | } |
82 | 77 | } |
83 | 78 |
|
| 79 | + async fn peek_changes_with_order( |
| 80 | + &self, |
| 81 | + order: Order, |
| 82 | + ) -> Result<(Vec<Change>, gix::hash::ObjectId)> { |
| 83 | + spawn_blocking({ |
| 84 | + let index = self.index.clone(); |
| 85 | + move || { |
| 86 | + let index = index.lock().unwrap(); |
| 87 | + index |
| 88 | + .peek_changes_with_options( |
| 89 | + gix::progress::Discard, |
| 90 | + &AtomicBool::default(), |
| 91 | + order, |
| 92 | + ) |
| 93 | + .map_err(Into::into) |
| 94 | + } |
| 95 | + }) |
| 96 | + .await |
| 97 | + } |
| 98 | + |
| 99 | + pub async fn peek_changes(&self) -> Result<(Vec<Change>, gix::hash::ObjectId)> { |
| 100 | + self.peek_changes_with_order(Order::ImplementationDefined) |
| 101 | + .await |
| 102 | + } |
| 103 | + |
| 104 | + pub async fn peek_changes_ordered(&self) -> Result<(Vec<Change>, gix::hash::ObjectId)> { |
| 105 | + self.peek_changes_with_order(Order::AsInCratesIndex).await |
| 106 | + } |
| 107 | + |
| 108 | + pub async fn set_last_seen_reference(&self, to: gix::hash::ObjectId) -> Result<()> { |
| 109 | + spawn_blocking({ |
| 110 | + let index = self.index.clone(); |
| 111 | + move || { |
| 112 | + let index = index.lock().unwrap(); |
| 113 | + index.set_last_seen_reference(to).map_err(Into::into) |
| 114 | + } |
| 115 | + }) |
| 116 | + .await |
| 117 | + } |
| 118 | + |
| 119 | + pub async fn latest_commit_reference(&self) -> Result<gix::ObjectId> { |
| 120 | + let (_, oid) = self.peek_changes().await?; |
| 121 | + Ok(oid) |
| 122 | + } |
| 123 | + |
84 | 124 | pub fn repository_url(&self) -> Option<&str> { |
85 | 125 | self.repository_url.as_deref() |
86 | 126 | } |
87 | 127 | } |
| 128 | + |
| 129 | +#[cfg(test)] |
| 130 | +mod tests { |
| 131 | + use super::*; |
| 132 | + |
| 133 | + // Compile-time check that Index implements Send + Sync. |
| 134 | + fn assert_send_sync<T: Send + Sync>() {} |
| 135 | + |
| 136 | + #[test] |
| 137 | + fn index_is_send_and_sync() { |
| 138 | + // This will fail to compile if `Index` is not `Send` and `Sync`. |
| 139 | + assert_send_sync::<Index>(); |
| 140 | + } |
| 141 | +} |
0 commit comments