Skip to content

Commit e366c06

Browse files
committed
Repository: Extract run_command() function
1 parent 9161e60 commit e366c06

File tree

2 files changed

+42
-31
lines changed

2 files changed

+42
-31
lines changed

src/git.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use anyhow::{anyhow, Context};
22
use std::collections::HashMap;
33
use std::io::Write;
44
use std::path::{Path, PathBuf};
5+
use std::process::Command;
56

67
use swirl::PerformError;
78
use tempfile::TempDir;
@@ -344,4 +345,32 @@ impl Repository {
344345

345346
Ok(())
346347
}
348+
349+
/// Runs the specified `git` command in the working directory of the local
350+
/// crate index repository.
351+
///
352+
/// This function also temporarily sets the `GIT_SSH_COMMAND` environment
353+
/// variable to ensure that `git push` commands are able to succeed.
354+
pub fn run_command(&self, command: &mut Command) -> Result<(), PerformError> {
355+
let checkout_path = self.checkout_path.path();
356+
command.current_dir(checkout_path);
357+
358+
let temp_key_path = self.credentials.write_temporary_ssh_key()?;
359+
command.env(
360+
"GIT_SSH_COMMAND",
361+
format!(
362+
"ssh -o StrictHostKeyChecking=accept-new -i {}",
363+
temp_key_path.display()
364+
),
365+
);
366+
367+
let output = command.output()?;
368+
if !output.status.success() {
369+
let stderr = String::from_utf8_lossy(&output.stderr);
370+
let message = format!("Running git command failed with: {}", stderr);
371+
return Err(message.into());
372+
}
373+
374+
Ok(())
375+
}
347376
}

src/worker/git.rs

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use anyhow::Context;
55
use chrono::Utc;
66
use diesel::prelude::*;
77
use std::fs::{self, OpenOptions};
8+
use std::process::Command;
89
use swirl::PerformError;
910

1011
#[swirl::background_job]
@@ -98,37 +99,18 @@ pub fn squash_index(env: &Environment) -> Result<(), PerformError> {
9899

99100
// Shell out to git because libgit2 does not currently support push leases
100101

101-
let temp_key_path = repo.credentials.write_temporary_ssh_key()?;
102-
103-
let checkout_path = repo.checkout_path.path();
104-
let output = std::process::Command::new("git")
105-
.current_dir(checkout_path)
106-
.env(
107-
"GIT_SSH_COMMAND",
108-
format!(
109-
"ssh -o StrictHostKeyChecking=accept-new -i {}",
110-
temp_key_path.display()
111-
),
112-
)
113-
.args(&[
114-
"push",
115-
// Both updates should succeed or fail together
116-
"--atomic",
117-
"origin",
118-
// Overwrite master, but only if it server matches the expected value
119-
&format!("--force-with-lease=refs/heads/master:{}", original_head),
120-
// The new squashed commit is pushed to master
121-
"HEAD:refs/heads/master",
122-
// The previous value of HEAD is pushed to a snapshot branch
123-
&format!("{}:refs/heads/snapshot-{}", original_head, now),
124-
])
125-
.output()?;
126-
127-
if !output.status.success() {
128-
let stderr = String::from_utf8_lossy(&output.stderr);
129-
let message = format!("Running git command failed with: {}", stderr);
130-
return Err(message.into());
131-
}
102+
repo.run_command(Command::new("git").args(&[
103+
"push",
104+
// Both updates should succeed or fail together
105+
"--atomic",
106+
"origin",
107+
// Overwrite master, but only if it server matches the expected value
108+
&format!("--force-with-lease=refs/heads/master:{}", original_head),
109+
// The new squashed commit is pushed to master
110+
"HEAD:refs/heads/master",
111+
// The previous value of HEAD is pushed to a snapshot branch
112+
&format!("{}:refs/heads/snapshot-{}", original_head, now),
113+
]))?;
132114

133115
println!("The index has been successfully squashed.");
134116

0 commit comments

Comments
 (0)