Skip to content

Commit 6635def

Browse files
committed
Repository: Extract run_command() function
1 parent abdaa20 commit 6635def

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;
@@ -387,4 +388,32 @@ impl Repository {
387388

388389
Ok(())
389390
}
391+
392+
/// Runs the specified `git` command in the working directory of the local
393+
/// crate index repository.
394+
///
395+
/// This function also temporarily sets the `GIT_SSH_COMMAND` environment
396+
/// variable to ensure that `git push` commands are able to succeed.
397+
pub fn run_command(&self, command: &mut Command) -> Result<(), PerformError> {
398+
let checkout_path = self.checkout_path.path();
399+
command.current_dir(checkout_path);
400+
401+
let temp_key_path = self.credentials.write_temporary_ssh_key()?;
402+
command.env(
403+
"GIT_SSH_COMMAND",
404+
format!(
405+
"ssh -o StrictHostKeyChecking=accept-new -i {}",
406+
temp_key_path.display()
407+
),
408+
);
409+
410+
let output = command.output()?;
411+
if !output.status.success() {
412+
let stderr = String::from_utf8_lossy(&output.stderr);
413+
let message = format!("Running git command failed with: {}", stderr);
414+
return Err(message.into());
415+
}
416+
417+
Ok(())
418+
}
390419
}

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)