Skip to content

Commit 0a2d29e

Browse files
committed
Extract helper function for getting current HEAD SHA
1 parent f72ed91 commit 0a2d29e

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/sync.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::SyncContext;
22
use crate::josh::JoshProxy;
3-
use crate::utils::run_command_at;
43
use crate::utils::{ensure_clean_git_state, prompt};
4+
use crate::utils::{get_current_head_sha, run_command_at};
55
use crate::utils::{run_command, stream_command};
66
use anyhow::{Context, Error};
77
use std::path::{Path, PathBuf};
@@ -64,7 +64,7 @@ impl GitSync {
6464
&self.context.config.construct_josh_filter(),
6565
);
6666

67-
let orig_head = run_command(["git", "rev-parse", "HEAD"])?;
67+
let orig_head = get_current_head_sha()?;
6868
println!(
6969
"previous upstream base: {:?}",
7070
self.context.last_upstream_sha
@@ -137,8 +137,7 @@ This updates the rust-version file to {upstream_sha}."#,
137137
};
138138
let num_roots_before = num_roots()?;
139139

140-
let sha =
141-
run_command(&["git", "rev-parse", "HEAD"]).context("failed to get current commit")?;
140+
let sha = get_current_head_sha()?;
142141

143142
// The filtered SHA of upstream
144143
let incoming_ref = run_command(["git", "rev-parse", "FETCH_HEAD"])?;
@@ -170,8 +169,7 @@ This merge was created using https://github.com/rust-lang/josh-sync.
170169
])
171170
.context("FAILED to merge new commits, something went wrong")?;
172171

173-
let current_sha =
174-
run_command(&["git", "rev-parse", "HEAD"]).context("FAILED to get current commit")?;
172+
let current_sha = get_current_head_sha()?;
175173
if current_sha == sha {
176174
eprintln!(
177175
"No merge was performed, no changes to pull were found. Rolling back the preparation commit."
@@ -261,7 +259,7 @@ This merge was created using https://github.com/rust-lang/josh-sync.
261259
&["git", "fetch", &josh_url, &branch],
262260
&std::env::current_dir().unwrap(),
263261
)?;
264-
let head = run_command(&["git", "rev-parse", "HEAD"])?;
262+
let head = get_current_head_sha()?;
265263
let fetch_head = run_command(&["git", "rev-parse", "FETCH_HEAD"])?;
266264
if head != fetch_head {
267265
return Err(anyhow::anyhow!(

src/utils.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use anyhow::Context;
12
use std::path::Path;
23
use std::process::Command;
34

@@ -67,6 +68,10 @@ pub fn ensure_clean_git_state() {
6768
assert!(read.is_empty(), "working directory must be clean");
6869
}
6970

71+
pub fn get_current_head_sha() -> anyhow::Result<String> {
72+
run_command(&["git", "rev-parse", "HEAD"]).context("failed to get current commit")
73+
}
74+
7075
/// Ask a prompt to user and return true if they responded with `y`.
7176
/// Returns `default_response` on CI.
7277
pub fn prompt(prompt: &str, default_response: bool) -> bool {

0 commit comments

Comments
 (0)