@@ -5,17 +5,21 @@ use crate::worker::Environment;
55use anyhow:: Context ;
66use chrono:: Utc ;
77use crates_io_env_vars:: var_parsed;
8- use crates_io_index:: { Crate , Repository } ;
8+ use crates_io_index:: Repository ;
99use crates_io_worker:: BackgroundJob ;
1010use diesel:: prelude:: * ;
1111use diesel_async:: async_connection_wrapper:: AsyncConnectionWrapper ;
1212use sentry:: Level ;
1313use std:: fs:: { self , File } ;
14- use std:: io:: { BufRead , BufReader , ErrorKind , Write } ;
14+ use std:: io:: { ErrorKind , Write } ;
1515use std:: process:: Command ;
1616use std:: sync:: Arc ;
1717use url:: Url ;
1818
19+ mod normalize;
20+
21+ pub use normalize:: NormalizeIndex ;
22+
1923#[ derive( Serialize , Deserialize ) ]
2024pub struct SyncToGitIndex {
2125 krate : String ,
@@ -221,94 +225,3 @@ impl BackgroundJob for SquashIndex {
221225 . await
222226 }
223227}
224-
225- #[ derive( Serialize , Deserialize ) ]
226- pub struct NormalizeIndex {
227- dry_run : bool ,
228- }
229-
230- impl NormalizeIndex {
231- pub fn new ( dry_run : bool ) -> Self {
232- Self { dry_run }
233- }
234- }
235-
236- impl BackgroundJob for NormalizeIndex {
237- const JOB_NAME : & ' static str = "normalize_index" ;
238- const QUEUE : & ' static str = "repository" ;
239-
240- type Context = Arc < Environment > ;
241-
242- async fn run ( & self , env : Self :: Context ) -> anyhow:: Result < ( ) > {
243- info ! ( "Normalizing the index" ) ;
244-
245- let dry_run = self . dry_run ;
246- spawn_blocking ( move || {
247- let repo = env. lock_index ( ) ?;
248-
249- let files = repo. get_files_modified_since ( None ) ?;
250- let num_files = files. len ( ) ;
251-
252- for ( i, file) in files. iter ( ) . enumerate ( ) {
253- if i % 50 == 0 {
254- info ! ( num_files, i, ?file) ;
255- }
256-
257- let crate_name = file. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) ;
258- let path = repo. index_file ( crate_name) ;
259- if !path. exists ( ) {
260- continue ;
261- }
262-
263- let mut body: Vec < u8 > = Vec :: new ( ) ;
264- let file = fs:: File :: open ( & path) ?;
265- let reader = BufReader :: new ( file) ;
266- let mut versions = Vec :: new ( ) ;
267- for line in reader. lines ( ) {
268- let line = line?;
269- if line. is_empty ( ) {
270- continue ;
271- }
272-
273- let mut krate: Crate = serde_json:: from_str ( & line) ?;
274- for dep in & mut krate. deps {
275- // Remove deps with empty features
276- dep. features . retain ( |d| !d. is_empty ( ) ) ;
277- // Set null DependencyKind to Normal
278- dep. kind =
279- Some ( dep. kind . unwrap_or ( crates_io_index:: DependencyKind :: Normal ) ) ;
280- }
281- krate. deps . sort ( ) ;
282- versions. push ( krate) ;
283- }
284- for version in versions {
285- serde_json:: to_writer ( & mut body, & version) . unwrap ( ) ;
286- body. push ( b'\n' ) ;
287- }
288- fs:: write ( path, body) ?;
289- }
290-
291- info ! ( "Committing normalization" ) ;
292- let msg = "Normalize index format\n \n \
293- More information can be found at https://github.com/rust-lang/crates.io/pull/5066";
294- repo. run_command ( Command :: new ( "git" ) . args ( [ "commit" , "-am" , msg] ) ) ?;
295-
296- let branch = match dry_run {
297- false => "master" ,
298- true => "normalization-dry-run" ,
299- } ;
300-
301- info ! ( ?branch, "Pushing to upstream repository" ) ;
302- repo. run_command ( Command :: new ( "git" ) . args ( [
303- "push" ,
304- "origin" ,
305- & format ! ( "HEAD:{branch}" ) ,
306- ] ) ) ?;
307-
308- info ! ( "Index normalization completed" ) ;
309-
310- Ok ( ( ) )
311- } )
312- . await
313- }
314- }
0 commit comments