Push branches atomically when squashing the index
@@ -384,7 +384,7 @@ pub fn squash_index(env: &Environment) -> Result<(), PerformError> {
println!("Squashing the index into a single commit.");
let now = Utc::now().format("%Y-%m-%d");
- let original_head = repo.head_oid()?;
+ let original_head = repo.head_oid()?.to_string();
let msg = format!("Collapse index into one commit\n\n\
Previous HEAD was {}, now on the `snapshot-{}` branch\n\n\
@@ -394,9 +394,6 @@ pub fn squash_index(env: &Environment) -> Result<(), PerformError> {
[online]: https://internals.rust-lang.org/t/cargos-crate-index-upcoming-squash-into-one-commit/8440\n\
[this issue]: https://github.com/rust-lang/crates-io-cargo-teams/issues/47", original_head, now);
- // Create a snapshot branch of current `HEAD`.
- repo.push(&format!("HEAD:refs/heads/snapshot-{}", now))?;
-
repo.squash_to_single_commit(&msg)?;
// Shell out to git because libgit2 does not currently support push leases
@@ -431,9 +428,15 @@ pub fn squash_index(env: &Environment) -> Result<(), PerformError> {
)
.args(&[
"push",
+ // Both updates should succeed or fail together
+ "--atomic",
"origin",
- "HEAD:refs/heads/master",
+ // Overwrite master, but only if it server matches the expected value
&format!("--force-with-lease=refs/heads/master:{}", original_head),
+ // The new squashed commit is pushed to master
+ "HEAD:refs/heads/master",
+ // The previous value of HEAD is pushed to a snapshot branch
+ &format!("{}:refs/heads/snapshot-{}", original_head, now),
])
.output()?;
