Skip to content

Commit

Permalink
Categorize tags by major version
Browse files Browse the repository at this point in the history
  • Loading branch information
athre0z committed Aug 1, 2022
1 parent 708ec75 commit 9e22f8e
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub fn run() -> Result<()> {
.context("failed to parse regular expression")?;

let mut index = IndexContext::default();
for git_ref in repo.refs()? {
for git_ref in repo.refs()?.into_iter().rev() {
if !regexps.iter().any(|re| re.is_match(&git_ref)) {
continue;
}
Expand Down Expand Up @@ -106,7 +106,24 @@ pub fn run() -> Result<()> {

// Categorize and add to index.
let ref_vec = if git_ref.starts_with("refs/tags") {
&mut index.tags
// Split off the major part. For example `v4.1.2` -> `v4`.
let major = match short_ref.split_once('.') {
Some((major, _)) => major,
None => short_ref,
};

let bucket = match index.tags.iter_mut().find(|x| x.major == major) {
Some(bucket) => bucket,
None => {
index.tags.push(MajorVersion {
major: major.to_owned(),
subversions: Vec::new(),
});
index.tags.last_mut().unwrap()
}
};

&mut bucket.subversions
} else if git_ref.starts_with("refs/heads") {
&mut index.branches
} else {
Expand Down Expand Up @@ -142,9 +159,15 @@ struct IndexRef {
dir: String,
}

#[derive(Debug, Default, serde::Serialize)]
struct MajorVersion {
major: String,
subversions: Vec<IndexRef>,
}

#[derive(Debug, Default, serde::Serialize)]
struct IndexContext {
tags: Vec<IndexRef>,
tags: Vec<MajorVersion>,
branches: Vec<IndexRef>,
misc_refs: Vec<IndexRef>,
}
Expand Down

0 comments on commit 9e22f8e

Please sign in to comment.