1
- use crate :: index:: { CloneOptions , LAST_SEEN_REFNAME } ;
1
+ use crate :: index:: { CloneOptions , CloneOptions2 , LAST_SEEN_REFNAME } ;
2
2
use crate :: Index ;
3
3
use git_repository as git;
4
4
use std:: path:: Path ;
5
+ use std:: sync:: atomic:: AtomicBool ;
5
6
6
7
/// The error returned by various initialization methods.
7
8
#[ derive( Debug , thiserror:: Error ) ]
@@ -13,6 +14,18 @@ pub enum Error {
13
14
Open ( #[ from] git:: open:: Error ) ,
14
15
}
15
16
17
+ /// The error returned by various initialization methods.
18
+ #[ derive( Debug , thiserror:: Error ) ]
19
+ #[ allow( missing_docs) ]
20
+ pub enum Error2 {
21
+ #[ error( transparent) ]
22
+ PrepareClone ( #[ from] git:: clone:: prepare:: Error ) ,
23
+ #[ error( transparent) ]
24
+ Fetch ( #[ from] git:: clone:: fetch:: Error ) ,
25
+ #[ error( transparent) ]
26
+ Open ( #[ from] git:: open:: Error ) ,
27
+ }
28
+
16
29
/// Initialization
17
30
impl Index {
18
31
/// Return a new `Index` instance from the given `path`, which should contain a bare or non-bare
@@ -94,6 +107,68 @@ impl Index {
94
107
} )
95
108
}
96
109
110
+ /// Return a new `Index` instance from the given `path`, which should contain a bare clone of the `crates.io` index.
111
+ /// If the directory does not contain the repository or does not exist, it will be cloned from
112
+ /// the official location automatically (with complete history).
113
+ ///
114
+ /// An error will occour if the repository exists and the remote URL does not match the given repository URL.
115
+ ///
116
+ /// # Examples
117
+ ///
118
+ /// ```no_run
119
+ /// use std::sync::atomic::AtomicBool;
120
+ /// use crates_index_diff::{Index, index, git};
121
+ ///
122
+ /// # let path = tempdir::TempDir::new("index").unwrap();
123
+ /// // Note that credentials are automatically picked up from the standard git configuration.
124
+ /// let mut options = index::CloneOptions2 {
125
+ /// url: "https://github.com/rust-lang/staging.crates.io-index".into(),
126
+ /// };
127
+ ///
128
+ ///
129
+ /// let index = Index::from_path_or_cloned_with_options2(path, git::progress::Discard, &AtomicBool::default(), options)?;
130
+ /// # Ok::<(), crates_index_diff::index::init::Error2>(())
131
+ /// ```
132
+ pub fn from_path_or_cloned_with_options2 (
133
+ path : impl AsRef < Path > ,
134
+ progress : impl git:: Progress ,
135
+ should_interrupt : & AtomicBool ,
136
+ CloneOptions2 { url } : CloneOptions2 ,
137
+ ) -> Result < Index , Error2 > {
138
+ let path = path. as_ref ( ) ;
139
+ let mut repo = match git:: open ( path) {
140
+ Ok ( repo) => repo,
141
+ Err ( git:: open:: Error :: NotARepository ( _) ) => {
142
+ let ( repo, _out) =
143
+ git:: prepare_clone_bare ( url, path) ?. fetch_only ( progress, should_interrupt) ?;
144
+ repo
145
+ }
146
+ Err ( err) => return Err ( err. into ( ) ) ,
147
+ }
148
+ . apply_environment ( ) ;
149
+
150
+ repo. object_cache_size_if_unset ( 4 * 1024 * 1024 ) ;
151
+ Ok ( Index {
152
+ repo,
153
+ remote_name : Some ( "origin" ) ,
154
+ branch_name : "master" ,
155
+ seen_ref_name : LAST_SEEN_REFNAME ,
156
+ } )
157
+ }
158
+
159
+ /// Return a new `Index` instance from the given `path`, which should contain a bare or non-bare
160
+ /// clone of the `crates.io` index.
161
+ /// If the directory does not contain the repository or does not exist, it will be cloned from
162
+ /// the official location automatically (with complete history).
163
+ pub fn from_path_or_cloned2 ( path : impl AsRef < Path > ) -> Result < Index , Error2 > {
164
+ Index :: from_path_or_cloned_with_options2 (
165
+ path,
166
+ git:: progress:: Discard ,
167
+ & AtomicBool :: default ( ) ,
168
+ CloneOptions2 :: default ( ) ,
169
+ )
170
+ }
171
+
97
172
/// Return a new `Index` instance from the given `path`, which should contain a bare or non-bare
98
173
/// clone of the `crates.io` index.
99
174
/// If the directory does not contain the repository or does not exist, it will be cloned from
0 commit comments