Implement listing of current patches

This commit is contained in:
Quentin Boyer 2024-02-08 23:29:15 +01:00
parent 29dfad3809
commit 5d1d7c5042

View file

@ -34,6 +34,14 @@ enum Command {
/// Format a patch. alias "p" /// Format a patch. alias "p"
#[command(alias = "p")] #[command(alias = "p")]
FormatPatch(FormatPatch), FormatPatch(FormatPatch),
#[command(alias = "ls")]
List(List),
}
#[derive(Args, Debug)]
struct List {
#[arg(short, long)]
verbose: bool,
} }
#[derive(Args, Debug)] #[derive(Args, Debug)]
@ -74,6 +82,41 @@ struct GsmConfig {
interdiff_base: Option<String>, interdiff_base: Option<String>,
} }
fn latest_version(branch_dir: &Path) -> Result<Option<u64>> {
let mut dir_content = branch_dir
.read_dir()
.into_diagnostic()
.wrap_err("could not read branch dir")?
.peekable();
match dir_content.peek() {
Some(_) => Ok(Some(
dir_content
.filter_map(|e| {
let entry = match e.into_diagnostic().wrap_err("Could not read entry") {
Ok(e) => e,
Err(e) => return Some(Err(e)),
};
let name = entry.file_name();
let name = name.to_str().expect("patch set entry is not utf8");
if name == "cover-letter" {
None
} else {
Some(Ok(name.parse().expect("version is not an int")))
}
})
.try_fold(0, |cur, version| -> Result<_> {
let version = version?;
Ok(std::cmp::max(version, cur))
})?,
)),
None => Ok(None),
}
}
fn git_bare(args: Vec<&str>) -> Result<String> { fn git_bare(args: Vec<&str>) -> Result<String> {
let out = duct::cmd("git", args) let out = duct::cmd("git", args)
.stderr_to_stdout() .stderr_to_stdout()
@ -182,45 +225,9 @@ fn main() -> Result<()> {
let version = match args.version { let version = match args.version {
Some(v) => Some(v), Some(v) => Some(v),
None => { None => latest_version(&branch_dir)
let mut dir_content = branch_dir .wrap_err("could not get version")?
.read_dir() .map(|v| v + 1),
.into_diagnostic()
.wrap_err("could not read branch dir")?
.peekable();
match dir_content.peek() {
Some(_) => Some(
dir_content
.into_iter()
.filter_map(|e| {
let entry = match e
.into_diagnostic()
.wrap_err("Could not read entry")
{
Ok(e) => e,
Err(e) => return Some(Err(e)),
};
let name = entry.file_name();
let name = name.to_str().expect("patch set entry is not utf8");
if name == "cover-letter" {
None
} else {
Some(Ok(name.parse().expect("version is not an int")))
}
})
.try_fold(0, |cur, version| -> Result<_> {
let version = version?;
Ok(std::cmp::max(version, cur))
})?
+ 1,
),
None => None,
}
}
}; };
let version_dir = branch_dir.join(version.unwrap_or(1).to_string()); let version_dir = branch_dir.join(version.unwrap_or(1).to_string());
@ -423,6 +430,51 @@ fn main() -> Result<()> {
.into_diagnostic() .into_diagnostic()
.wrap_err("Could not save cover letter")?; .wrap_err("Could not save cover letter")?;
Ok(())
}
Command::List(list) => {
for entry in patch_dir
.read_dir()
.into_diagnostic()
.wrap_err("Could not read patch dir")?
{
let entry = entry
.into_diagnostic()
.wrap_err("Could not read patch dir entry")?;
if entry.file_name() == "config.toml" {
continue;
}
let branch_dir = patch_dir.join(entry.file_name());
let Some(branch_version) =
latest_version(&branch_dir).wrap_err("Could not fetch latest version")?
else {
continue;
};
println!(
" - {}: v{branch_version}",
entry.file_name().to_string_lossy()
);
if list.verbose {
println!(" Patches:");
for entry in branch_dir
.join(branch_version.to_string())
.read_dir()
.into_diagnostic()
.wrap_err("Could not read patchset dir")?
{
let entry = entry
.into_diagnostic()
.wrap_err("Could not read patchset entry")?;
println!(" - {}", entry.file_name().to_string_lossy());
}
}
}
Ok(()) Ok(())
} }
} }