Skip to content

Commit 37d62d2

Browse files
committed
chore: use safe_command wrapper
1 parent 98e6756 commit 37d62d2

File tree

1 file changed

+66
-74
lines changed

1 file changed

+66
-74
lines changed

src/utils.rs

Lines changed: 66 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -93,57 +93,52 @@ pub fn git_update(repo_path: &Path, repo_url: &str, revision: Option<&str>) -> R
9393
//
9494
let tmp_remote_name = random_remote_name();
9595

96-
let command = format!("git remote add \"{}\" \"{}\"", tmp_remote_name, repo_url);
97-
let command_vec = shell_words::split(&command).map_err(anyhow::Error::new)?;
98-
9996
// Create a temporary remote
100-
Command::new(&command_vec[0])
101-
.args(&command_vec[1..])
102-
.current_dir(repo_path)
103-
.stdout(Stdio::null())
104-
.status()
105-
.with_context(|| {
106-
format!(
107-
"Error with adding {} as a remote named {} in {}",
108-
repo_url,
109-
tmp_remote_name,
110-
repo_path.display()
111-
)
112-
})?;
97+
safe_command(
98+
format!("git remote add \"{}\" \"{}\"", tmp_remote_name, repo_url),
99+
repo_path,
100+
)?
101+
.current_dir(repo_path)
102+
.stdout(Stdio::null())
103+
.status()
104+
.with_context(|| {
105+
format!(
106+
"Error with adding {} as a remote named {} in {}",
107+
repo_url,
108+
tmp_remote_name,
109+
repo_path.display()
110+
)
111+
})?;
113112

114113
let revision_str = revision.unwrap_or("main");
115114
let res = git_to_revision(repo_path, &tmp_remote_name, revision_str);
116115

117116
if let Err(e) = res {
118117
// Failed to switch to the desired revision. Cleanup!
119-
Command::new("git")
120-
.args(vec!["remote", "rm", &tmp_remote_name])
121-
.current_dir(repo_path)
118+
safe_command(format!("git remote rm \"{}\"", &tmp_remote_name), repo_path)?
122119
.stdout(Stdio::null())
123120
.status()
124-
.with_context(|| format!("Failed to execute process in {}", repo_path.display()))?;
121+
.with_context(|| {
122+
format!(
123+
"Failed to remove temporary remote {} in {}",
124+
tmp_remote_name,
125+
repo_path.display()
126+
)
127+
})?;
125128
return Err(e);
126129
}
127130

128-
let command = format!("git remote set-url origin \"{}\"", repo_url);
129-
let command_vec = shell_words::split(&command).map_err(anyhow::Error::new)?;
130-
131-
// Success! Cleanup: update the origin remote to remote URL & delete temporary remote.
132-
Command::new(&command_vec[0])
133-
.args(&command_vec[1..])
134-
.current_dir(repo_path)
135-
.stdout(Stdio::null())
136-
.status()
137-
.with_context(|| {
138-
format!(
139-
"Failed to set origin remote to {} in {}",
140-
repo_url,
141-
repo_path.display()
142-
)
143-
})?;
144-
Command::new("git")
145-
.args(vec!["remote", "rm", &tmp_remote_name])
146-
.current_dir(repo_path)
131+
safe_command(format!("git remote set-url origin \"{}\"", repo_url), repo_path)?
132+
.stdout(Stdio::null())
133+
.status()
134+
.with_context(|| {
135+
format!(
136+
"Failed to set origin remote to {} in {}",
137+
repo_url,
138+
repo_path.display()
139+
)
140+
})?;
141+
safe_command(format!("git remote rm \"{}\"", tmp_remote_name), repo_path)?
147142
.stdout(Stdio::null())
148143
.status()
149144
.with_context(|| {
@@ -282,7 +277,6 @@ fn git_resolve_revision(repo_path: &Path, remote_name: &str, revision: &str) ->
282277
})?;
283278
let stdout = child.stdout.take().expect("failed to capture stdout");
284279
let reader = BufReader::new(stdout);
285-
286280
for line in reader.lines() {
287281
match line {
288282
Ok(line) => {
@@ -317,43 +311,41 @@ fn safe_command(command: String, cwd: &Path) -> Result<Command, Error> {
317311
}
318312

319313
fn git_to_revision(repo_path: &Path, remote_name: &str, revision: &str) -> Result<()> {
320-
let command = format!("git fetch --quiet \"{}\" \"{}\"", remote_name, revision);
321-
let command_vec = shell_words::split(&command).map_err(anyhow::Error::new)?;
322-
323-
Command::new(&command_vec[0])
324-
.args(&command_vec[1..])
325-
.current_dir(repo_path)
326-
.stdout(Stdio::null())
327-
.status()
328-
.with_context(|| {
329-
format!(
330-
"Error with fetching revision {} in {}",
331-
revision,
332-
repo_path.display()
333-
)
334-
})?;
314+
// Download the object from the remote
315+
safe_command(
316+
format!("git fetch --quiet \"{}\" \"{}\"", remote_name, revision),
317+
repo_path,
318+
)?
319+
.status()
320+
.with_context(|| {
321+
format!(
322+
"Error with fetching revision {} in {}",
323+
revision,
324+
repo_path.display()
325+
)
326+
})?;
335327

336-
// Normalize the revision into the SHA. let command = format!("git rev-parse \"{}/{}\"", remote_name, revision);
328+
// Normalize the revision into the SHA.
329+
// let command = format!("git rev-parse \"{}/{}\"", remote_name, revision);
337330
let commit_sha = git_resolve_revision(repo_path, remote_name, revision)?;
338331

339-
let command = format!(
340-
"git -c advice.detachedHead=false checkout --quiet \"{}\"",
341-
commit_sha
342-
);
343-
let command_vec = shell_words::split(&command).map_err(anyhow::Error::new)?;
344-
345-
Command::new(&command_vec[0])
346-
.args(&command_vec[1..])
347-
.stdout(Stdio::null())
348-
.current_dir(repo_path)
349-
.status()
350-
.with_context(|| {
351-
format!(
352-
"Failed to checkout SHA {} in {}",
353-
commit_sha,
354-
repo_path.display()
355-
)
356-
})?;
332+
safe_command(
333+
format!(
334+
"git -c advice.detachedHead=false checkout --quiet \"{}\"",
335+
commit_sha
336+
),
337+
repo_path,
338+
)?
339+
.stdout(Stdio::null())
340+
.current_dir(repo_path)
341+
.status()
342+
.with_context(|| {
343+
format!(
344+
"Failed to checkout SHA {} in {}",
345+
commit_sha,
346+
repo_path.display()
347+
)
348+
})?;
357349

358350
Ok(())
359351
}

0 commit comments

Comments
 (0)