Skip to content

Commit 78e05bd

Browse files
committed
refactor (#16)
1 parent 946ca4c commit 78e05bd

File tree

4 files changed

+335
-317
lines changed

4 files changed

+335
-317
lines changed

src/index.rs

Lines changed: 71 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use super::{Change, CrateVersion};
22
use std::path::Path;
33

4+
use crate::Index;
45
use git2::{
56
build::RepoBuilder, Delta, Error as GitError, ErrorClass, Object, ObjectType, Oid, Reference,
67
Repository, Tree,
78
};
89
use std::str;
9-
use crate::Index;
1010

1111
static INDEX_GIT_URL: &str = "https://github.com/rust-lang/crates.io-index";
1212
static LAST_SEEN_REFNAME: &str = "refs/heads/crates-index-diff_last-seen";
@@ -30,6 +30,7 @@ impl<'a> Default for CloneOptions<'a> {
3030
}
3131
}
3232

33+
/// Access
3334
impl Index {
3435
/// Return the crates.io repository.
3536
pub fn repository(&self) -> &Repository {
@@ -40,7 +41,10 @@ impl Index {
4041
pub fn last_seen_reference(&self) -> Result<Reference<'_>, GitError> {
4142
self.repo.find_reference(self.seen_ref_name)
4243
}
44+
}
4345

46+
/// Initialization
47+
impl Index {
4448
/// Return a new `Index` instance from the given `path`, which should contain a bare or non-bare
4549
/// clone of the `crates.io` index.
4650
/// If the directory does not contain the repository or does not exist, it will be cloned from
@@ -138,7 +142,10 @@ impl Index {
138142
pub fn from_path_or_cloned(path: impl AsRef<Path>) -> Result<Index, GitError> {
139143
Index::from_path_or_cloned_with_options(path, CloneOptions::default())
140144
}
145+
}
141146

147+
/// Find changes without modifying the underling repository
148+
impl Index {
142149
/// As `peek_changes_with_options`, but without the options.
143150
pub fn peek_changes(&self) -> Result<(Vec<Change>, git2::Oid), GitError> {
144151
self.peek_changes_with_options(None)
@@ -191,66 +198,6 @@ impl Index {
191198
))
192199
}
193200

194-
/// As `fetch_changes_with_options`, but without the options.
195-
pub fn fetch_changes(&self) -> Result<Vec<Change>, GitError> {
196-
self.fetch_changes_with_options(None)
197-
}
198-
199-
/// Return all `Change`s that are observed between the last time this method was called
200-
/// and the latest state of the `crates.io` index repository, which is obtained by fetching
201-
/// the remote called `origin`.
202-
/// The `last_seen_reference()` will be created or adjusted to point to the latest fetched
203-
/// state, which causes this method to have a different result each time it is called.
204-
///
205-
/// # Resource Usage
206-
///
207-
/// As this method fetches the git repository, loose objects or small packs may be created. Over time,
208-
/// these will accumulate and either slow down subsequent operations, or cause them to fail due to exhaustion
209-
/// of the maximum number of open file handles as configured with `ulimit`.
210-
///
211-
/// Thus it is advised for the caller to run `git gc` occasionally based on their own requirements and usage patterns.
212-
pub fn fetch_changes_with_options(
213-
&self,
214-
options: Option<&mut git2::FetchOptions<'_>>,
215-
) -> Result<Vec<Change>, GitError> {
216-
let (changes, to) = self.peek_changes_with_options(options)?;
217-
self.set_last_seen_reference(to)?;
218-
Ok(changes)
219-
}
220-
221-
/// Set the last seen reference to the given Oid. It will be created if it does not yet exists.
222-
pub fn set_last_seen_reference(&self, to: Oid) -> Result<(), GitError> {
223-
self.last_seen_reference()
224-
.and_then(|mut seen_ref| {
225-
seen_ref.set_target(to, "updating seen-ref head to latest fetched commit")
226-
})
227-
.or_else(|_err| {
228-
self.repo.reference(
229-
self.seen_ref_name,
230-
to,
231-
true,
232-
"creating seen-ref at latest fetched commit",
233-
)
234-
})?;
235-
Ok(())
236-
}
237-
238-
/// Return all `CreateVersion`s observed between `from` and `to`. Both parameter are ref-specs
239-
/// pointing to either a commit or a tree.
240-
/// Learn more about specifying revisions
241-
/// in the
242-
/// [official documentation](https://www.kernel.org/pub/software/scm/git/docs/gitrevisions.html)
243-
pub fn changes(
244-
&self,
245-
from: impl AsRef<str>,
246-
to: impl AsRef<str>,
247-
) -> Result<Vec<Change>, GitError> {
248-
self.changes_from_objects(
249-
&self.repo.revparse_single(from.as_ref())?,
250-
&self.repo.revparse_single(to.as_ref())?,
251-
)
252-
}
253-
254201
/// Similar to `changes()`, but requires `from` and `to` objects to be provided. They may point
255202
/// to either `Commit`s or `Tree`s.
256203
pub fn changes_from_objects(
@@ -316,3 +263,66 @@ impl Index {
316263
Ok(changes)
317264
}
318265
}
266+
267+
/// Find changes while changing the underlying repository in one way or another.
268+
impl Index {
269+
/// As `fetch_changes_with_options`, but without the options.
270+
pub fn fetch_changes(&self) -> Result<Vec<Change>, GitError> {
271+
self.fetch_changes_with_options(None)
272+
}
273+
274+
/// Return all `Change`s that are observed between the last time this method was called
275+
/// and the latest state of the `crates.io` index repository, which is obtained by fetching
276+
/// the remote called `origin`.
277+
/// The `last_seen_reference()` will be created or adjusted to point to the latest fetched
278+
/// state, which causes this method to have a different result each time it is called.
279+
///
280+
/// # Resource Usage
281+
///
282+
/// As this method fetches the git repository, loose objects or small packs may be created. Over time,
283+
/// these will accumulate and either slow down subsequent operations, or cause them to fail due to exhaustion
284+
/// of the maximum number of open file handles as configured with `ulimit`.
285+
///
286+
/// Thus it is advised for the caller to run `git gc` occasionally based on their own requirements and usage patterns.
287+
pub fn fetch_changes_with_options(
288+
&self,
289+
options: Option<&mut git2::FetchOptions<'_>>,
290+
) -> Result<Vec<Change>, GitError> {
291+
let (changes, to) = self.peek_changes_with_options(options)?;
292+
self.set_last_seen_reference(to)?;
293+
Ok(changes)
294+
}
295+
296+
/// Set the last seen reference to the given Oid. It will be created if it does not yet exists.
297+
pub fn set_last_seen_reference(&self, to: Oid) -> Result<(), GitError> {
298+
self.last_seen_reference()
299+
.and_then(|mut seen_ref| {
300+
seen_ref.set_target(to, "updating seen-ref head to latest fetched commit")
301+
})
302+
.or_else(|_err| {
303+
self.repo.reference(
304+
self.seen_ref_name,
305+
to,
306+
true,
307+
"creating seen-ref at latest fetched commit",
308+
)
309+
})?;
310+
Ok(())
311+
}
312+
313+
/// Return all `CreateVersion`s observed between `from` and `to`. Both parameter are ref-specs
314+
/// pointing to either a commit or a tree.
315+
/// Learn more about specifying revisions
316+
/// in the
317+
/// [official documentation](https://www.kernel.org/pub/software/scm/git/docs/gitrevisions.html)
318+
pub fn changes(
319+
&self,
320+
from: impl AsRef<str>,
321+
to: impl AsRef<str>,
322+
) -> Result<Vec<Change>, GitError> {
323+
self.changes_from_objects(
324+
&self.repo.revparse_single(from.as_ref())?,
325+
&self.repo.revparse_single(to.as_ref())?,
326+
)
327+
}
328+
}

0 commit comments

Comments
 (0)