1
1
use super :: { Change , CrateVersion } ;
2
2
use std:: path:: Path ;
3
3
4
+ use crate :: Index ;
4
5
use git2:: {
5
6
build:: RepoBuilder , Delta , Error as GitError , ErrorClass , Object , ObjectType , Oid , Reference ,
6
7
Repository , Tree ,
7
8
} ;
8
9
use std:: str;
9
- use crate :: Index ;
10
10
11
11
static INDEX_GIT_URL : & str = "https://github.com/rust-lang/crates.io-index" ;
12
12
static LAST_SEEN_REFNAME : & str = "refs/heads/crates-index-diff_last-seen" ;
@@ -30,6 +30,7 @@ impl<'a> Default for CloneOptions<'a> {
30
30
}
31
31
}
32
32
33
+ /// Access
33
34
impl Index {
34
35
/// Return the crates.io repository.
35
36
pub fn repository ( & self ) -> & Repository {
@@ -40,7 +41,10 @@ impl Index {
40
41
pub fn last_seen_reference ( & self ) -> Result < Reference < ' _ > , GitError > {
41
42
self . repo . find_reference ( self . seen_ref_name )
42
43
}
44
+ }
43
45
46
+ /// Initialization
47
+ impl Index {
44
48
/// Return a new `Index` instance from the given `path`, which should contain a bare or non-bare
45
49
/// clone of the `crates.io` index.
46
50
/// If the directory does not contain the repository or does not exist, it will be cloned from
@@ -138,7 +142,10 @@ impl Index {
138
142
pub fn from_path_or_cloned ( path : impl AsRef < Path > ) -> Result < Index , GitError > {
139
143
Index :: from_path_or_cloned_with_options ( path, CloneOptions :: default ( ) )
140
144
}
145
+ }
141
146
147
+ /// Find changes without modifying the underling repository
148
+ impl Index {
142
149
/// As `peek_changes_with_options`, but without the options.
143
150
pub fn peek_changes ( & self ) -> Result < ( Vec < Change > , git2:: Oid ) , GitError > {
144
151
self . peek_changes_with_options ( None )
@@ -191,66 +198,6 @@ impl Index {
191
198
) )
192
199
}
193
200
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
-
254
201
/// Similar to `changes()`, but requires `from` and `to` objects to be provided. They may point
255
202
/// to either `Commit`s or `Tree`s.
256
203
pub fn changes_from_objects (
@@ -316,3 +263,66 @@ impl Index {
316
263
Ok ( changes)
317
264
}
318
265
}
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