@@ -55,13 +55,18 @@ impl<'cfg> RemoteRegistry<'cfg> {
55
55
self . repo . get_or_try_init ( || {
56
56
let path = self . index_path . clone ( ) . into_path_unlocked ( ) ;
57
57
58
+ // Fast path without a lock
59
+ if let Ok ( repo) = git2:: Repository :: open ( & path) {
60
+ return Ok ( repo)
61
+ }
62
+
63
+ // Ok, now we need to lock and try the whole thing over again.
64
+ let lock = self . index_path . open_rw ( Path :: new ( INDEX_LOCK ) ,
65
+ self . config ,
66
+ "the registry index" ) ?;
58
67
match git2:: Repository :: open ( & path) {
59
68
Ok ( repo) => Ok ( repo) ,
60
69
Err ( _) => {
61
- self . index_path . create_dir ( ) ?;
62
- let lock = self . index_path . open_rw ( Path :: new ( INDEX_LOCK ) ,
63
- self . config ,
64
- "the registry index" ) ?;
65
70
let _ = lock. remove_siblings ( ) ;
66
71
Ok ( git2:: Repository :: init_bare ( & path) ?)
67
72
}
@@ -153,25 +158,29 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
153
158
"the registry index" ) ?;
154
159
self . config . shell ( ) . status ( "Updating" ,
155
160
format ! ( "registry `{}`" , self . source_id. url( ) ) ) ?;
161
+ let mut needs_fetch = true ;
156
162
157
163
if self . source_id . url ( ) . host_str ( ) == Some ( "github.com" ) {
158
164
if let Ok ( oid) = self . head ( ) {
159
165
let mut handle = self . easy ( ) ?. borrow_mut ( ) ;
160
166
debug ! ( "attempting github fast path for {}" ,
161
167
self . source_id. url( ) ) ;
162
168
if github_up_to_date ( & mut handle, self . source_id . url ( ) , & oid) {
163
- return Ok ( ( ) )
169
+ needs_fetch = false ;
170
+ } else {
171
+ debug ! ( "fast path failed, falling back to a git fetch" ) ;
164
172
}
165
- debug ! ( "fast path failed, falling back to a git fetch" ) ;
166
173
}
167
174
}
168
175
169
- // git fetch origin master
170
- let url = self . source_id . url ( ) . to_string ( ) ;
171
- let refspec = "refs/heads/master:refs/remotes/origin/master" ;
172
- git:: fetch ( & repo, & url, refspec, self . config ) . chain_error ( || {
173
- human ( format ! ( "failed to fetch `{}`" , url) )
174
- } ) ?;
176
+ if needs_fetch {
177
+ // git fetch origin master
178
+ let url = self . source_id . url ( ) . to_string ( ) ;
179
+ let refspec = "refs/heads/master:refs/remotes/origin/master" ;
180
+ git:: fetch ( & repo, & url, refspec, self . config ) . chain_error ( || {
181
+ human ( format ! ( "failed to fetch `{}`" , url) )
182
+ } ) ?;
183
+ }
175
184
self . head . set ( None ) ;
176
185
* self . tree . borrow_mut ( ) = None ;
177
186
Ok ( ( ) )
0 commit comments