Skip to content

Commit 77befd4

Browse files
committed
worker/jobs/index: Extract SquashIndex into dedicated module
1 parent 21b9122 commit 77befd4

File tree

2 files changed

+68
-62
lines changed

2 files changed

+68
-62
lines changed

src/worker/jobs/index/mod.rs

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,20 @@ use crate::tasks::spawn_blocking;
33
use crate::util::diesel::Conn;
44
use crate::worker::Environment;
55
use anyhow::Context;
6-
use chrono::Utc;
7-
use crates_io_env_vars::var_parsed;
86
use crates_io_index::Repository;
97
use crates_io_worker::BackgroundJob;
108
use diesel::prelude::*;
119
use diesel_async::async_connection_wrapper::AsyncConnectionWrapper;
1210
use sentry::Level;
1311
use std::fs::{self, File};
1412
use std::io::{ErrorKind, Write};
15-
use std::process::Command;
1613
use std::sync::Arc;
17-
use url::Url;
1814

1915
mod normalize;
16+
mod squash;
2017

2118
pub use normalize::NormalizeIndex;
19+
pub use squash::SquashIndex;
2220

2321
#[derive(Serialize, Deserialize)]
2422
pub 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-
}

src/worker/jobs/index/squash.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use crate::tasks::spawn_blocking;
2+
use crate::worker::Environment;
3+
use chrono::Utc;
4+
use crates_io_env_vars::var_parsed;
5+
use crates_io_worker::BackgroundJob;
6+
use std::process::Command;
7+
use std::sync::Arc;
8+
use url::Url;
9+
10+
#[derive(Serialize, Deserialize)]
11+
pub struct SquashIndex;
12+
13+
impl BackgroundJob for SquashIndex {
14+
const JOB_NAME: &'static str = "squash_index";
15+
const QUEUE: &'static str = "repository";
16+
17+
type Context = Arc<Environment>;
18+
19+
/// Collapse the index into a single commit, archiving the current history in a snapshot branch.
20+
#[instrument(skip_all)]
21+
async fn run(&self, env: Self::Context) -> anyhow::Result<()> {
22+
info!("Squashing the index into a single commit");
23+
24+
spawn_blocking(move || {
25+
let repo = env.lock_index()?;
26+
27+
let now = Utc::now().format("%Y-%m-%d");
28+
let original_head = repo.head_oid()?.to_string();
29+
let msg = format!("Collapse index into one commit\n\n\
30+
Previous HEAD was {original_head}, now on the `snapshot-{now}` branch\n\n\
31+
More information about this change can be found [online] and on [this issue].\n\n\
32+
[online]: https://internals.rust-lang.org/t/cargos-crate-index-upcoming-squash-into-one-commit/8440\n\
33+
[this issue]: https://github.com/rust-lang/crates-io-cargo-teams/issues/47");
34+
35+
repo.squash_to_single_commit(&msg)?;
36+
37+
// Shell out to git because libgit2 does not currently support push leases
38+
39+
repo.run_command(Command::new("git").args([
40+
"push",
41+
// Both updates should succeed or fail together
42+
"--atomic",
43+
"origin",
44+
// Overwrite master, but only if it server matches the expected value
45+
&format!("--force-with-lease=refs/heads/master:{original_head}"),
46+
// The new squashed commit is pushed to master
47+
"HEAD:refs/heads/master",
48+
// The previous value of HEAD is pushed to a snapshot branch
49+
&format!("{original_head}:refs/heads/snapshot-{now}"),
50+
]))?;
51+
52+
if let Some(archive_url) = var_parsed::<Url>("GIT_ARCHIVE_REPO_URL")? {
53+
repo.run_command(Command::new("git").args([
54+
"push",
55+
archive_url.as_str(),
56+
&format!("{original_head}:snapshot-{now}"),
57+
]))?;
58+
}
59+
60+
info!("The index has been successfully squashed.");
61+
62+
Ok(())
63+
})
64+
.await
65+
}
66+
}

0 commit comments

Comments
 (0)