Skip to content

Commit 9a15db6

Browse files
committed
Add git_command helper
1 parent 4074569 commit 9a15db6

File tree

2 files changed

+26
-31
lines changed

2 files changed

+26
-31
lines changed

build_system/prepare.rs

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::build_system::rustc_info::get_default_sysroot;
88
use super::build_sysroot::{BUILD_SYSROOT, ORIG_BUILD_SYSROOT, SYSROOT_RUSTC_VERSION, SYSROOT_SRC};
99
use super::path::{Dirs, RelPath};
1010
use super::rustc_info::get_rustc_version;
11-
use super::utils::{copy_dir_recursively, retry_spawn_and_wait, spawn_and_wait};
11+
use super::utils::{copy_dir_recursively, git_command, retry_spawn_and_wait, spawn_and_wait};
1212

1313
pub(crate) fn prepare(dirs: &Dirs) {
1414
if RelPath::DOWNLOAD.to_path(dirs).exists() {
@@ -96,14 +96,14 @@ impl GitRepo {
9696
fn clone_repo(download_dir: &Path, repo: &str, rev: &str) {
9797
eprintln!("[CLONE] {}", repo);
9898
// Ignore exit code as the repo may already have been checked out
99-
Command::new("git").arg("clone").arg(repo).arg(&download_dir).spawn().unwrap().wait().unwrap();
99+
git_command(None, "clone").arg(repo).arg(download_dir).spawn().unwrap().wait().unwrap();
100100

101-
let mut clean_cmd = Command::new("git");
102-
clean_cmd.arg("checkout").arg("--").arg(".").current_dir(&download_dir);
101+
let mut clean_cmd = git_command(download_dir, "checkout");
102+
clean_cmd.arg("--").arg(".");
103103
spawn_and_wait(clean_cmd);
104104

105-
let mut checkout_cmd = Command::new("git");
106-
checkout_cmd.arg("checkout").arg("-q").arg(rev).current_dir(download_dir);
105+
let mut checkout_cmd = git_command(download_dir, "checkout");
106+
checkout_cmd.arg("-q").arg(rev);
107107
spawn_and_wait(checkout_cmd);
108108
}
109109

@@ -159,25 +159,16 @@ fn clone_repo_shallow_github(dirs: &Dirs, download_dir: &Path, user: &str, repo:
159159
}
160160

161161
fn init_git_repo(repo_dir: &Path) {
162-
let mut git_init_cmd = Command::new("git");
163-
git_init_cmd.arg("init").arg("-q").current_dir(repo_dir);
162+
let mut git_init_cmd = git_command(repo_dir, "init");
163+
git_init_cmd.arg("-q");
164164
spawn_and_wait(git_init_cmd);
165165

166-
let mut git_add_cmd = Command::new("git");
167-
git_add_cmd.arg("add").arg(".").current_dir(repo_dir);
166+
let mut git_add_cmd = git_command(repo_dir, "add");
167+
git_add_cmd.arg(".");
168168
spawn_and_wait(git_add_cmd);
169169

170-
let mut git_commit_cmd = Command::new("git");
171-
git_commit_cmd
172-
.arg("-c")
173-
.arg("user.name=Dummy")
174-
.arg("-c")
175-
.arg("user.email=dummy@example.com")
176-
.arg("commit")
177-
.arg("-m")
178-
.arg("Initial commit")
179-
.arg("-q")
180-
.current_dir(repo_dir);
170+
let mut git_commit_cmd = git_command(repo_dir, "commit");
171+
git_commit_cmd.arg("-m").arg("Initial commit").arg("-q");
181172
spawn_and_wait(git_commit_cmd);
182173
}
183174

@@ -212,16 +203,8 @@ fn apply_patches(dirs: &Dirs, crate_name: &str, target_dir: &Path) {
212203
target_dir.file_name().unwrap(),
213204
patch.file_name().unwrap()
214205
);
215-
let mut apply_patch_cmd = Command::new("git");
216-
apply_patch_cmd
217-
.arg("-c")
218-
.arg("user.name=Dummy")
219-
.arg("-c")
220-
.arg("user.email=dummy@example.com")
221-
.arg("am")
222-
.arg(patch)
223-
.arg("-q")
224-
.current_dir(target_dir);
206+
let mut apply_patch_cmd = git_command(target_dir, "am");
207+
apply_patch_cmd.arg(patch).arg("-q");
225208
spawn_and_wait(apply_patch_cmd);
226209
}
227210
}

build_system/utils.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ impl CargoProject {
103103
RelPath::BUILD.join(self.target).to_path(dirs)
104104
}
105105

106+
#[must_use]
106107
fn base_cmd(&self, command: &str, cargo: &Path, dirs: &Dirs) -> Command {
107108
let mut cmd = Command::new(cargo);
108109

@@ -115,6 +116,7 @@ impl CargoProject {
115116
cmd
116117
}
117118

119+
#[must_use]
118120
fn build_cmd(&self, command: &str, compiler: &Compiler, dirs: &Dirs) -> Command {
119121
let mut cmd = self.base_cmd(command, &compiler.cargo, dirs);
120122

@@ -191,6 +193,16 @@ pub(crate) fn hyperfine_command(
191193
bench
192194
}
193195

196+
#[must_use]
197+
pub(crate) fn git_command<'a>(repo_dir: impl Into<Option<&'a Path>>, cmd: &str) -> Command {
198+
let mut git_cmd = Command::new("git");
199+
git_cmd.arg("-c").arg("user.name=Dummy").arg("-c").arg("user.email=dummy@example.com").arg(cmd);
200+
if let Some(repo_dir) = repo_dir.into() {
201+
git_cmd.current_dir(repo_dir);
202+
}
203+
git_cmd
204+
}
205+
194206
#[track_caller]
195207
pub(crate) fn try_hard_link(src: impl AsRef<Path>, dst: impl AsRef<Path>) {
196208
let src = src.as_ref();

0 commit comments

Comments
 (0)