Skip to content

Commit 4cedf27

Browse files
committed
change!: remove git2 in favor of gitoxide.
`gitoxide` is now used for cloning and fetching as well. NOTE: advanced git2 based credential configuration isn't supported anymore until `gitoxide` catches up. It generally implements all configuration options that are relevant for `git` and fully implements HTTP based authentication, but is probably lacking with supporting non-default ssh configuration.
1 parent 6975d67 commit 4cedf27

File tree

6 files changed

+25
-230
lines changed

6 files changed

+25
-230
lines changed

Cargo.toml

-5
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ serde_json = "1"
2222
bstr = "1.0.1"
2323
thiserror = "1.0.32"
2424

25-
[dependencies.git2]
26-
version = "0.14.0"
27-
default-features = false
28-
features = ["https"]
29-
3025
[dev-dependencies]
3126
git-testtools = "0.8.0"
3227
tempdir = "0.3.5"

src/index/diff/mod.rs

+4-89
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::{Change, Index};
22
use git_repository as git;
33
use git_repository::prelude::ObjectIdExt;
44
use git_repository::refs::transaction::PreviousValue;
5-
use std::convert::TryFrom;
65
use std::sync::atomic::AtomicBool;
76

87
mod delegate;
@@ -12,8 +11,6 @@ use delegate::Delegate;
1211
#[derive(Debug, thiserror::Error)]
1312
#[allow(missing_docs)]
1413
pub enum Error {
15-
#[error("Failed to fetch crates.io index repository")]
16-
FetchGit2(#[from] git2::Error),
1714
#[error("Couldn't update marker reference")]
1815
ReferenceEdit(#[from] git::reference::edit::Error),
1916
#[error("Failed to parse rev-spec to determine which revisions to diff")]
@@ -53,62 +50,7 @@ pub enum Error {
5350
impl Index {
5451
/// As `peek_changes_with_options`, but without the options.
5552
pub fn peek_changes(&self) -> Result<(Vec<Change>, git::hash::ObjectId), Error> {
56-
self.peek_changes_with_options(None)
57-
}
58-
59-
/// Return all `Change`s that are observed between the last time `fetch_changes(…)` was called
60-
/// and the latest state of the `crates.io` index repository, which is obtained by fetching
61-
/// the remote called `origin`.
62-
/// The `last_seen_reference()` will not be created or updated.
63-
/// The second field in the returned tuple is the commit object to which the changes were provided.
64-
/// If one would set the `last_seen_reference()` to that object, the effect is exactly the same
65-
/// as if `fetch_changes(…)` had been called.
66-
///
67-
/// # Resource Usage
68-
///
69-
/// As this method fetches the git repository, loose objects or small packs may be created. Over time,
70-
/// these will accumulate and either slow down subsequent operations, or cause them to fail due to exhaustion
71-
/// of the maximum number of open file handles as configured with `ulimit`.
72-
///
73-
/// Thus it is advised for the caller to run `git gc` occasionally based on their own requirements and usage patterns.
74-
pub fn peek_changes_with_options(
75-
&self,
76-
options: Option<&mut git2::FetchOptions<'_>>,
77-
) -> Result<(Vec<Change>, git::hash::ObjectId), Error> {
78-
let repo = &self.repo;
79-
let from = repo
80-
.find_reference(self.seen_ref_name)
81-
.ok()
82-
.and_then(|r| r.try_id().map(|id| id.detach()))
83-
.unwrap_or_else(|| git::hash::ObjectId::empty_tree(repo.object_hash()));
84-
let remote_name = self
85-
.remote_name
86-
.as_deref()
87-
.expect("always set for this old portion of the code");
88-
let to = {
89-
let repo = git2::Repository::open(repo.git_dir())?;
90-
repo.find_remote(remote_name).and_then(|mut r| {
91-
r.fetch(
92-
&[format!(
93-
"+refs/heads/{branch}:refs/remotes/{remote}/{branch}",
94-
remote = remote_name,
95-
branch = self.branch_name,
96-
)],
97-
options,
98-
None,
99-
)
100-
})?;
101-
git::hash::ObjectId::try_from(
102-
repo.refname_to_id(&format!(
103-
"refs/remotes/{}/{}",
104-
remote_name, self.branch_name
105-
))?
106-
.as_bytes(),
107-
)
108-
.expect("valid oid")
109-
};
110-
111-
Ok((self.changes_between_commits(from, to)?, to))
53+
self.peek_changes_with_options(git::progress::Discard, &AtomicBool::default())
11254
}
11355

11456
/// Return all `Change`s that are observed between the last time `peek_changes*(…)` was called
@@ -128,7 +70,7 @@ impl Index {
12870
///
12971
/// Thus it is advised for the caller to run `git gc` occasionally based on their own requirements and usage patterns.
13072
// TODO: update this once it's clear how auto-gc works in `gitoxide`.
131-
pub fn peek_changes_with_options2(
73+
pub fn peek_changes_with_options(
13274
&self,
13375
progress: impl git::Progress,
13476
should_interrupt: &AtomicBool,
@@ -243,14 +185,9 @@ impl Index {
243185

244186
/// Find changes while changing the underlying repository in one way or another.
245187
impl Index {
246-
/// As `fetch_changes_with_options`, but without the options.
247-
pub fn fetch_changes2(&self) -> Result<Vec<Change>, Error> {
248-
self.fetch_changes_with_options2(git::progress::Discard, &AtomicBool::default())
249-
}
250-
251188
/// As `fetch_changes_with_options`, but without the options.
252189
pub fn fetch_changes(&self) -> Result<Vec<Change>, Error> {
253-
self.fetch_changes_with_options(None)
190+
self.fetch_changes_with_options(git::progress::Discard, &AtomicBool::default())
254191
}
255192

256193
/// Return all `Change`s that are observed between the last time this method was called
@@ -267,33 +204,11 @@ impl Index {
267204
///
268205
/// Thus it is advised for the caller to run `git gc` occasionally based on their own requirements and usage patterns.
269206
pub fn fetch_changes_with_options(
270-
&self,
271-
options: Option<&mut git2::FetchOptions<'_>>,
272-
) -> Result<Vec<Change>, Error> {
273-
let (changes, to) = self.peek_changes_with_options(options)?;
274-
self.set_last_seen_reference(to)?;
275-
Ok(changes)
276-
}
277-
278-
/// Return all `Change`s that are observed between the last time this method was called
279-
/// and the latest state of the `crates.io` index repository, which is obtained by fetching
280-
/// the remote called `origin`.
281-
/// The `last_seen_reference()` will be created or adjusted to point to the latest fetched
282-
/// state, which causes this method to have a different result each time it is called.
283-
///
284-
/// # Resource Usage
285-
///
286-
/// As this method fetches the git repository, loose objects or small packs may be created. Over time,
287-
/// these will accumulate and either slow down subsequent operations, or cause them to fail due to exhaustion
288-
/// of the maximum number of open file handles as configured with `ulimit`.
289-
///
290-
/// Thus it is advised for the caller to run `git gc` occasionally based on their own requirements and usage patterns.
291-
pub fn fetch_changes_with_options2(
292207
&self,
293208
progress: impl git::Progress,
294209
should_interrupt: &AtomicBool,
295210
) -> Result<Vec<Change>, Error> {
296-
let (changes, to) = self.peek_changes_with_options2(progress, should_interrupt)?;
211+
let (changes, to) = self.peek_changes_with_options(progress, should_interrupt)?;
297212
self.set_last_seen_reference(to)?;
298213
Ok(changes)
299214
}

src/index/init.rs

+8-105
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
1-
use crate::index::{CloneOptions, CloneOptions2, LAST_SEEN_REFNAME};
1+
use crate::index::{CloneOptions, LAST_SEEN_REFNAME};
22
use crate::Index;
33
use git_repository as git;
44
use std::path::Path;
55
use std::sync::atomic::AtomicBool;
66

7-
/// The error returned by various initialization methods.
8-
#[derive(Debug, thiserror::Error)]
9-
#[allow(missing_docs)]
10-
pub enum Error {
11-
#[error(transparent)]
12-
Clone(#[from] git2::Error),
13-
#[error(transparent)]
14-
Open(#[from] git::open::Error),
15-
}
16-
177
/// The error returned by various initialization methods.
188
#[derive(Debug, thiserror::Error)]
199
#[allow(missing_docs)]
@@ -28,85 +18,6 @@ pub enum Error2 {
2818

2919
/// Initialization
3020
impl Index {
31-
/// Return a new `Index` instance from the given `path`, which should contain a bare or non-bare
32-
/// clone of the `crates.io` index.
33-
/// If the directory does not contain the repository or does not exist, it will be cloned from
34-
/// the official location automatically (with complete history).
35-
///
36-
/// An error will occour if the repository exists and the remote URL does not match the given repository URL.
37-
///
38-
/// # Examples
39-
///
40-
/// ```no_run
41-
/// use crates_index_diff::{Index, index};
42-
///
43-
/// # let path = tempdir::TempDir::new("index").unwrap();
44-
/// let mut options = index::CloneOptions {
45-
/// repository_url: "https://github.com/rust-lang/staging.crates.io-index".into(),
46-
/// ..Default::default()
47-
/// };
48-
///
49-
///
50-
/// let index = Index::from_path_or_cloned_with_options(path, options)?;
51-
/// # Ok::<(), crates_index_diff::index::init::Error>(())
52-
/// ```
53-
/// Or to access a private repository, use fetch options.
54-
///
55-
/// ```no_run
56-
/// use crates_index_diff::{index, Index};
57-
/// let fo = {
58-
/// let mut fo = git2::FetchOptions::new();
59-
/// fo.remote_callbacks({
60-
/// let mut callbacks = git2::RemoteCallbacks::new();
61-
/// callbacks.credentials(|_url, username_from_url, _allowed_types| {
62-
/// git2::Cred::ssh_key_from_memory(
63-
/// username_from_url.unwrap(),
64-
/// None,
65-
/// &std::env::var("PRIVATE_KEY").unwrap(),
66-
/// None,
67-
/// )
68-
/// });
69-
/// callbacks
70-
/// });
71-
/// fo
72-
/// };
73-
/// Index::from_path_or_cloned_with_options(
74-
/// "index",
75-
/// index::CloneOptions {
76-
/// repository_url: "[email protected]:private-index/goes-here.git".into(),
77-
/// fetch_options: Some(fo),
78-
/// },
79-
/// ).unwrap();
80-
/// ```
81-
pub fn from_path_or_cloned_with_options(
82-
path: impl AsRef<Path>,
83-
CloneOptions {
84-
repository_url,
85-
fetch_options,
86-
}: CloneOptions<'_>,
87-
) -> Result<Index, Error> {
88-
let repo = git2::Repository::open(path.as_ref()).or_else(|err| {
89-
if err.class() == git2::ErrorClass::Repository {
90-
let mut builder = git2::build::RepoBuilder::new();
91-
if let Some(fo) = fetch_options {
92-
builder.fetch_options(fo);
93-
}
94-
builder.bare(true).clone(&repository_url, path.as_ref())
95-
} else {
96-
Err(err)
97-
}
98-
})?;
99-
100-
let mut repo = git::open(repo.path())?.apply_environment();
101-
repo.object_cache_size_if_unset(4 * 1024 * 1024);
102-
Ok(Index {
103-
repo,
104-
remote_name: Some("origin".into()),
105-
branch_name: "master",
106-
seen_ref_name: LAST_SEEN_REFNAME,
107-
})
108-
}
109-
11021
/// Return a new `Index` instance from the given `path`, which should contain a bare clone of the `crates.io` index.
11122
/// If the directory does not contain the repository or does not exist, it will be cloned from
11223
/// the official location automatically (with complete history).
@@ -121,19 +32,19 @@ impl Index {
12132
///
12233
/// # let path = tempdir::TempDir::new("index").unwrap();
12334
/// // Note that credentials are automatically picked up from the standard git configuration.
124-
/// let mut options = index::CloneOptions2 {
35+
/// let mut options = index::CloneOptions {
12536
/// url: "https://github.com/rust-lang/staging.crates.io-index".into(),
12637
/// };
12738
///
12839
///
129-
/// let index = Index::from_path_or_cloned_with_options2(path, git::progress::Discard, &AtomicBool::default(), options)?;
40+
/// let index = Index::from_path_or_cloned_with_options(path, git::progress::Discard, &AtomicBool::default(), options)?;
13041
/// # Ok::<(), crates_index_diff::index::init::Error2>(())
13142
/// ```
132-
pub fn from_path_or_cloned_with_options2(
43+
pub fn from_path_or_cloned_with_options(
13344
path: impl AsRef<Path>,
13445
progress: impl git::Progress,
13546
should_interrupt: &AtomicBool,
136-
CloneOptions2 { url }: CloneOptions2,
47+
CloneOptions { url }: CloneOptions,
13748
) -> Result<Index, Error2> {
13849
let path = path.as_ref();
13950
let mut repo = match git::open(path) {
@@ -165,20 +76,12 @@ impl Index {
16576
/// clone of the `crates.io` index.
16677
/// If the directory does not contain the repository or does not exist, it will be cloned from
16778
/// the official location automatically (with complete history).
168-
pub fn from_path_or_cloned2(path: impl AsRef<Path>) -> Result<Index, Error2> {
169-
Index::from_path_or_cloned_with_options2(
79+
pub fn from_path_or_cloned(path: impl AsRef<Path>) -> Result<Index, Error2> {
80+
Index::from_path_or_cloned_with_options(
17081
path,
17182
git::progress::Discard,
17283
&AtomicBool::default(),
173-
CloneOptions2::default(),
84+
CloneOptions::default(),
17485
)
17586
}
176-
177-
/// Return a new `Index` instance from the given `path`, which should contain a bare or non-bare
178-
/// clone of the `crates.io` index.
179-
/// If the directory does not contain the repository or does not exist, it will be cloned from
180-
/// the official location automatically (with complete history).
181-
pub fn from_path_or_cloned(path: impl AsRef<Path>) -> Result<Index, Error> {
182-
Index::from_path_or_cloned_with_options(path, CloneOptions::default())
183-
}
18487
}

src/index/mod.rs

+3-20
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,15 @@ use std::str;
55
static INDEX_GIT_URL: &str = "https://github.com/rust-lang/crates.io-index";
66
static LAST_SEEN_REFNAME: &str = "refs/heads/crates-index-diff_last-seen";
77

8-
/// Options for use in `Index::from_path_or_cloned_with_options`
9-
pub struct CloneOptions<'a> {
10-
/// The url from which the repository should be cloned.
11-
pub repository_url: String,
12-
/// Git2 fetch options to control exactly how to clone.
13-
pub fetch_options: Option<git2::FetchOptions<'a>>,
14-
}
15-
16-
impl<'a> Default for CloneOptions<'a> {
17-
fn default() -> Self {
18-
CloneOptions {
19-
repository_url: INDEX_GIT_URL.into(),
20-
fetch_options: None,
21-
}
22-
}
23-
}
24-
258
/// Options for cloning the crates-io index.
26-
pub struct CloneOptions2 {
9+
pub struct CloneOptions {
2710
/// The url to clone the crates-index repository from.
2811
pub url: String,
2912
}
3013

31-
impl Default for CloneOptions2 {
14+
impl Default for CloneOptions {
3215
fn default() -> Self {
33-
CloneOptions2 {
16+
CloneOptions {
3417
url: INDEX_GIT_URL.into(),
3518
}
3619
}

src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@
88
pub mod index;
99
mod types;
1010

11-
pub use git2;
1211
pub use git_repository as git;
1312
pub use types::{Change, CrateVersion, Dependency, Index};

0 commit comments

Comments
 (0)