@@ -3,22 +3,20 @@ use crate::tasks::spawn_blocking;
33use crate :: util:: diesel:: Conn ;
44use crate :: worker:: Environment ;
55use anyhow:: Context ;
6- use chrono:: Utc ;
7- use crates_io_env_vars:: var_parsed;
86use crates_io_index:: Repository ;
97use crates_io_worker:: BackgroundJob ;
108use diesel:: prelude:: * ;
119use diesel_async:: async_connection_wrapper:: AsyncConnectionWrapper ;
1210use sentry:: Level ;
1311use std:: fs:: { self , File } ;
1412use std:: io:: { ErrorKind , Write } ;
15- use std:: process:: Command ;
1613use std:: sync:: Arc ;
17- use url:: Url ;
1814
1915mod normalize;
16+ mod squash;
2017
2118pub use normalize:: NormalizeIndex ;
19+ pub use squash:: SquashIndex ;
2220
2321#[ derive( Serialize , Deserialize ) ]
2422pub struct SyncToGitIndex {
@@ -167,61 +165,3 @@ pub fn get_index_data(name: &str, conn: &mut impl Conn) -> anyhow::Result<Option
167165
168166 Ok ( Some ( str) )
169167}
170-
171- #[ derive( Serialize , Deserialize ) ]
172- pub struct SquashIndex ;
173-
174- impl BackgroundJob for SquashIndex {
175- const JOB_NAME : & ' static str = "squash_index" ;
176- const QUEUE : & ' static str = "repository" ;
177-
178- type Context = Arc < Environment > ;
179-
180- /// Collapse the index into a single commit, archiving the current history in a snapshot branch.
181- #[ instrument( skip_all) ]
182- async fn run ( & self , env : Self :: Context ) -> anyhow:: Result < ( ) > {
183- info ! ( "Squashing the index into a single commit" ) ;
184-
185- spawn_blocking ( move || {
186- let repo = env. lock_index ( ) ?;
187-
188- let now = Utc :: now ( ) . format ( "%Y-%m-%d" ) ;
189- let original_head = repo. head_oid ( ) ?. to_string ( ) ;
190- let msg = format ! ( "Collapse index into one commit\n \n \
191- Previous HEAD was {original_head}, now on the `snapshot-{now}` branch\n \n \
192- More information about this change can be found [online] and on [this issue].\n \n \
193- [online]: https://internals.rust-lang.org/t/cargos-crate-index-upcoming-squash-into-one-commit/8440\n \
194- [this issue]: https://github.com/rust-lang/crates-io-cargo-teams/issues/47") ;
195-
196- repo. squash_to_single_commit ( & msg) ?;
197-
198- // Shell out to git because libgit2 does not currently support push leases
199-
200- repo. run_command ( Command :: new ( "git" ) . args ( [
201- "push" ,
202- // Both updates should succeed or fail together
203- "--atomic" ,
204- "origin" ,
205- // Overwrite master, but only if it server matches the expected value
206- & format ! ( "--force-with-lease=refs/heads/master:{original_head}" ) ,
207- // The new squashed commit is pushed to master
208- "HEAD:refs/heads/master" ,
209- // The previous value of HEAD is pushed to a snapshot branch
210- & format ! ( "{original_head}:refs/heads/snapshot-{now}" ) ,
211- ] ) ) ?;
212-
213- if let Some ( archive_url) = var_parsed :: < Url > ( "GIT_ARCHIVE_REPO_URL" ) ? {
214- repo. run_command ( Command :: new ( "git" ) . args ( [
215- "push" ,
216- archive_url. as_str ( ) ,
217- & format ! ( "{original_head}:snapshot-{now}" ) ,
218- ] ) ) ?;
219- }
220-
221- info ! ( "The index has been successfully squashed." ) ;
222-
223- Ok ( ( ) )
224- } )
225- . await
226- }
227- }
0 commit comments