Skip to content

Commit 0df91df

Browse files
committed
Allow tagged refs
1 parent 94a4b8e commit 0df91df

File tree

1 file changed

+32
-27
lines changed

1 file changed

+32
-27
lines changed

src/main.rs

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,9 @@ impl App {
211211
Ok(())
212212
}
213213

214-
fn clone(&self, full_name: &str, branch: &str, depth: usize) -> Result<(), Box<dyn Error>> {
214+
fn clone(&self, full_name: &str, r#ref: &str, depth: usize) -> Result<(), Box<dyn Error>> {
215+
let (ref_type, ref_name) = ref_to_branch(r#ref).ok_or("Invalid reference")?;
216+
215217
assert!(cmd(
216218
"git",
217219
&[
@@ -230,10 +232,18 @@ impl App {
230232
.wait()?
231233
.success());
232234

235+
if ref_type == "tag" {
236+
// Checkout the commit associated with the tag
237+
assert!(cmd("git", &["checkout", ref_name])?.wait()?.success());
238+
} else {
239+
// Checkout the branch
240+
assert!(cmd("git", &["checkout", ref_name])?.wait()?.success());
241+
}
242+
233243
Ok(())
234244
}
235245

236-
fn list_cpp_files<'a>(&'a self) -> impl Iterator<Item = String> + 'a {
246+
fn list_files<'a>(&'a self, includes: &'a [Pattern], excludes: &'a [Pattern]) -> impl Iterator<Item = String> + 'a {
237247
BufReader::new(
238248
Command::new("git")
239249
.args(&["ls-tree", "-r", "HEAD", "--name-only", "--full-tree"])
@@ -243,26 +253,18 @@ impl App {
243253
.stdout
244254
.unwrap(),
245255
)
246-
.lines()
247-
.map(|s| s.unwrap())
248-
.filter(move |s| self.includes.iter().any(|p| p.matches(&s)))
249-
.filter(move |s| !self.excludes.iter().any(|p| p.matches(&s)))
256+
.lines()
257+
.map(|s| s.unwrap())
258+
.filter(move |s| includes.iter().any(|p| p.matches(&s)))
259+
.filter(move |s| !excludes.iter().any(|p| p.matches(&s)))
260+
}
261+
262+
fn list_cpp_files<'a>(&'a self) -> impl Iterator<Item = String> + 'a {
263+
self.list_files(&self.includes, &self.excludes)
250264
}
251265

252266
fn list_py_files<'a>(&'a self) -> impl Iterator<Item = String> + 'a {
253-
BufReader::new(
254-
Command::new("git")
255-
.args(&["ls-tree", "-r", "HEAD", "--name-only", "--full-tree"])
256-
.stdout(Stdio::piped())
257-
.spawn()
258-
.unwrap()
259-
.stdout
260-
.unwrap(),
261-
)
262-
.lines()
263-
.map(|s| s.unwrap())
264-
.filter(move |s| self.py_includes.iter().any(|p| p.matches(&s)))
265-
.filter(move |s| !self.excludes.iter().any(|p| p.matches(&s)))
267+
self.list_files(&self.py_includes, &self.excludes)
266268
}
267269

268270
fn format_all(&self) {
@@ -447,14 +449,17 @@ fn load_payload<T: DeserializeOwned>() -> Result<T, Box<dyn Error>> {
447449
Ok(serde_json::from_str(&github_event)?)
448450
}
449451

450-
fn ref_to_branch(r#ref: &str) -> &str {
451-
let expected_prefix = "refs/heads/";
452-
assert!(
453-
r#ref.starts_with(expected_prefix),
454-
"Unexpected push ref: {}",
455-
r#ref
456-
);
457-
&r#ref[expected_prefix.len()..]
452+
fn ref_to_branch(r#ref: &str) -> Option<(&str, &str)> {
453+
let branch_prefix = "refs/heads/";
454+
let tag_prefix = "refs/tags/";
455+
456+
if r#ref.starts_with(branch_prefix) {
457+
Some(("branch", &r#ref[branch_prefix.len()..]))
458+
} else if r#ref.starts_with(tag_prefix) {
459+
Some(("tag", &r#ref[tag_prefix.len()..]))
460+
} else {
461+
None
462+
}
458463
}
459464

460465
fn cmd<S: AsRef<OsStr>>(program: S, args: &[&str]) -> Result<Child, Box<dyn Error>> {

0 commit comments

Comments
 (0)