From 6cdabd1b614eb27a9f9483e3333ae27c193cf981 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sun, 1 Dec 2024 20:45:49 +0100 Subject: [PATCH] feat: remove tracking file --- README.md | 4 +-- src/main.rs | 85 ++++++++++++++++++++--------------------------------- 2 files changed, 34 insertions(+), 55 deletions(-) diff --git a/README.md b/README.md index 178e66f..924231d 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,8 @@ Using `nixp-meta track ` on can see which nixpkgs branches a pull requests h If you cannot wait for a PR to be merged, or just want to test it while it's still being worked on, this module is for you. -`nixp-meta add-pr ` Downloads the diff for the specified PR to a folder (default `./patches/PR`) and updates a tracking file(default `pr.txt`). -`nixp-meta update-prs` then uses that tracking file to update all prs contained. +`nixp-meta add-pr ` Downloads the diff for the specified PR to a folder (default `./patches/PR`) +`nixp-meta update-prs` updates all prs. Then using below module one can alter nixpkgs to use these patches diff --git a/src/main.rs b/src/main.rs index a7befa2..5724e65 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,7 @@ use std::{ env, - fmt::Write, - fs::{self, File, OpenOptions}, - io::{self, BufRead, BufReader}, + fs::{self, read_dir, File, OpenOptions}, + io::BufReader, }; use clap::{Args, Parser, Subcommand}; @@ -22,8 +21,6 @@ struct Cli { enum CliCommands { /// Track how far a PR has made it in the nix pipeline Track(Track), - /// Download a diff without touching the tracking file - GetDiff(GetDiff), /// Update all PR, removing them if they are contained in your local nixpkgs UpdatePrs(UpdatePRs), /// Unconditionally add a new PR @@ -32,23 +29,14 @@ enum CliCommands { #[derive(Args, Debug, Default)] struct UpdatePRs { - #[arg(long, default_value_t = ("./pr.txt".to_string()))] - pr_file: String, #[arg(long, default_value_t = ("./patches/PR".to_string()))] path: String, + #[arg(long)] + pr: Option, } #[derive(Args, Debug, Default)] struct AddPR { - pr: String, - #[arg(long, default_value_t = ("./pr.txt".to_string()))] - pr_file: String, - #[arg(long, default_value_t = ("./patches/PR".to_string()))] - path: String, -} - -#[derive(Args, Debug, Default)] -struct GetDiff { pr: String, #[arg(long, default_value_t = ("./patches/PR".to_string()))] path: String, @@ -176,51 +164,42 @@ async fn main() -> Result<()> { println!("{}: {}", i, contains(i, &pr, &client).await?); } } - CliCommands::GetDiff(opts) => { - let pr = parse_pr(&opts.pr)?; - get_diff(pr, &opts.path, &client).await?; - } CliCommands::UpdatePrs(opts) => { let mut trackable: Vec<(u32, String)> = Vec::new(); - { - let file = File::open(opts.pr_file.clone())?; - for l in io::BufReader::new(file).lines() { - let l = l?; - let l = if let Some((pr, _)) = l.split_once("#") { - let pr = pr.trim(); - pr.parse::()? - } else { - let l = l.trim(); - l.parse::()? - }; - let pr = get_pr(l, &client).await?; - println!("Fetching diff for PR #{}: {}", l, pr.title); - let branch = get_local_nixp_rev()?; - if contains(&branch, &pr, &client).await? { - println!("PR is contained in your local nixpkgs, removing diff"); - fs::remove_file(format!("{}/{}.diff", opts.path, l))?; - } else { - get_diff(l, &opts.path, &client).await?; - trackable.push((l, pr.title)); - } + let re = Regex::new(r"^[0-9]*.diff$")?; + let prs = read_dir(&opts.path)?.filter(|x| { + x.as_ref().is_ok_and(|x| { + x.metadata().is_ok_and(|x| x.file_type().is_file()) + && x.file_name().to_str().is_some_and(|x| re.is_match(x)) + }) + }); + for l in prs { + let l = l?.file_name(); + let l = l + .to_str() + .to_owned() + .and_then(|x| x.strip_suffix(".diff").to_owned()); + let l = if let Some(pr) = l { + let pr = pr.trim(); + pr.parse::()? + } else { + bail!("This shouldn't happen") + }; + let pr = get_pr(l, &client).await?; + println!("Fetching diff for PR #{}: {}", l, pr.title); + let branch = get_local_nixp_rev()?; + if contains(&branch, &pr, &client).await? { + println!("PR is contained in your local nixpkgs, removing diff"); + fs::remove_file(format!("{}/{}.diff", opts.path, l))?; + } else { + get_diff(l, &opts.path, &client).await?; + trackable.push((l, pr.title)); } } - let mut str = String::new(); - for (pr, title) in trackable { - let title = title.replace("\n", "//"); - writeln!(&mut str, "{} # {}", pr, title)?; - } - - fs::write(&opts.pr_file, str)?; } CliCommands::AddPr(opts) => { let pr = parse_pr(&opts.pr)?; get_diff(pr, &opts.path, &client).await?; - let pr = get_pr(pr, &client).await?; - let title = pr.title.replace("\n", "//"); - let mut file = OpenOptions::new().append(true).open(&opts.pr_file)?; - use std::io::Write; - writeln!(file, "{} # {}", pr.number, title)?; } }; Ok(())