Skip to content

Commit 68bf2e7

Browse files
committed
Push branches atomically when squashing the index
1 parent 38857cd commit 68bf2e7

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/git.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ pub fn squash_index(env: &Environment) -> Result<(), PerformError> {
384384
println!("Squashing the index into a single commit.");
385385

386386
let now = Utc::now().format("%Y-%m-%d");
387-
let original_head = repo.head_oid()?;
387+
let original_head = repo.head_oid()?.to_string();
388388
let msg = format!("Collapse index into one commit\n\n\
389389
390390
Previous HEAD was {}, now on the `snapshot-{}` branch\n\n\
@@ -394,9 +394,6 @@ pub fn squash_index(env: &Environment) -> Result<(), PerformError> {
394394
[online]: https://internals.rust-lang.org/t/cargos-crate-index-upcoming-squash-into-one-commit/8440\n\
395395
[this issue]: https://github.com/rust-lang/crates-io-cargo-teams/issues/47", original_head, now);
396396

397-
// Create a snapshot branch of current `HEAD`.
398-
repo.push(&format!("HEAD:refs/heads/snapshot-{}", now))?;
399-
400397
repo.squash_to_single_commit(&msg)?;
401398

402399
// Shell out to git because libgit2 does not currently support push leases
@@ -431,9 +428,15 @@ pub fn squash_index(env: &Environment) -> Result<(), PerformError> {
431428
)
432429
.args(&[
433430
"push",
431+
// Both updates should succeed or fail together
432+
"--atomic",
434433
"origin",
435-
"HEAD:refs/heads/master",
434+
// Overwrite master, but only if it server matches the expected value
436435
&format!("--force-with-lease=refs/heads/master:{}", original_head),
436+
// The new squashed commit is pushed to master
437+
"HEAD:refs/heads/master",
438+
// The previous value of HEAD is pushed to a snapshot branch
439+
&format!("{}:refs/heads/snapshot-{}", original_head, now),
437440
])
438441
.output()?;
439442

0 commit comments

Comments
 (0)