From de80a197ed3b897ae8716bdc6ec6b4db0d2d3925 Mon Sep 17 00:00:00 2001 From: Timothy Andrew Date: Sat, 6 Jun 2020 01:03:30 +0530 Subject: [PATCH] cargo fix + fmt + clippy --- src/api/search.rs | 6 +++--- src/git.rs | 40 ++++++++++++++++++++-------------------- src/graph.rs | 6 +++--- src/lib.rs | 2 +- src/main.rs | 23 +++++++++++++---------- src/markdown.rs | 2 +- src/persist.rs | 2 +- 7 files changed, 42 insertions(+), 39 deletions(-) diff --git a/src/api/search.rs b/src/api/search.rs index 4599948..e666772 100644 --- a/src/api/search.rs +++ b/src/api/search.rs @@ -22,7 +22,7 @@ pub enum PullRequestStatus { #[serde(rename = "open")] Open, #[serde(rename = "closed")] - Closed + Closed, } #[derive(Deserialize, Debug, Clone)] @@ -34,7 +34,7 @@ pub struct PullRequest { title: String, url: String, body: String, - state: PullRequestStatus + state: PullRequestStatus, } impl PullRequest { @@ -57,7 +57,7 @@ impl PullRequest { pub fn title(&self) -> String { match &self.state { PullRequestStatus::Open => self.title.to_owned(), - PullRequestStatus::Closed => format!("~~{}~~", &self.title.trim()) + PullRequestStatus::Closed => format!("~~{}~~", &self.title.trim()), } } diff --git a/src/git.rs b/src/git.rs index b0dc70e..561d3d5 100644 --- a/src/git.rs +++ b/src/git.rs @@ -1,53 +1,53 @@ -use regex::Regex; -use crate::graph::FlatDep; use crate::api::search::PullRequestStatus; +use crate::graph::FlatDep; fn process_ref(git_ref: &str) -> String { - let re = Regex::new("heap:").unwrap(); - re.replace_all(git_ref, "").into_owned() + git_ref.replace("heap:", "") } -/// For all open pull requests in the graph, generate a series of commands +/// For all open pull requests in the graph, generate a series of commands /// (force-pushes) that will rebase the entire stack. The "PREBASE" variable /// is a base for the first branch in the stack (essentially a "stop cherry-picking /// here" marker), and is required because of our squash-merge workflow. /// TODO: Move this directly into Rust. pub fn generate_rebase_script(deps: FlatDep) -> String { - - let deps = deps.iter().filter(|(dep, _)| { - *dep.state() == PullRequestStatus::Open - }).collect::>(); - + let deps = deps + .iter() + .filter(|(dep, _)| *dep.state() == PullRequestStatus::Open) + .collect::>(); + let mut out = String::new(); - + out.push_str("#!/usr/bin/env bash\n\n"); out.push_str("set -euo pipefail\n"); out.push_str("set -o xtrace\n\n"); - + out.push_str("# ------ THIS SCRIPT ASSUMES YOUR PR STACK IS A SINGLE CHAIN WITHOUT BRANCHING ----- #\n\n"); out.push_str("# It starts at the base of the stack, cherry-picking onto the new base and force-pushing as it goes.\n"); out.push_str("# We can't tell where the initial cherry-pick should stop (mainly because of our squash merge workflow),\n"); - out.push_str("# so that initial stopping point for the first PR needs to be specified manually.\n\n"); - + out.push_str( + "# so that initial stopping point for the first PR needs to be specified manually.\n\n", + ); + out.push_str("export PREBASE=\"\"\n"); - + for (from, to) in deps { let to = if let Some(pr) = to { pr.head().to_string() } else { String::from("") }; - + out.push_str("\n# -------------- #\n\n"); - + out.push_str(&format!("export TO=\"{}\"\n", process_ref(&to))); out.push_str(&format!("export FROM=\"{}\"\n\n", process_ref(from.head()))); - + out.push_str("git checkout heap/\"$TO\"\n"); out.push_str("git cherry-pick \"$PREBASE\"..heap/\"$FROM\"\n"); out.push_str("export PREBASE=\"$(git rev-parse --verify heap/$FROM)\"\n"); out.push_str("git push -f heap HEAD:refs/heads/\"$FROM\"\n"); } - + out -} \ No newline at end of file +} diff --git a/src/graph.rs b/src/graph.rs index 02ee08a..b205c4e 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -1,14 +1,14 @@ use petgraph::visit::Bfs; use petgraph::visit::EdgeRef; use petgraph::{Direction, Graph}; -use std::rc::Rc; use std::collections::HashMap; +use std::rc::Rc; use crate::api::search::PullRequest; pub type FlatDep = Vec<(Rc, Option>)>; -pub fn build(prs: &Vec>) -> Graph, usize> { +pub fn build(prs: &[Rc]) -> Graph, usize> { let mut tree = Graph::, usize>::new(); let heads = prs.iter().map(|pr| pr.head()); let handles: Vec<_> = prs.iter().map(|pr| tree.add_node(pr.clone())).collect(); @@ -25,7 +25,7 @@ pub fn build(prs: &Vec>) -> Graph, usize> { } /// Return a flattened list of graph nodes as tuples; each tuple is `(node, node's parent [if exists])`. -pub fn log(graph: &Graph, usize>) -> FlatDep { +pub fn log(graph: &Graph, usize>) -> FlatDep { let roots: Vec<_> = graph.externals(Direction::Incoming).collect(); let mut out = Vec::new(); diff --git a/src/lib.rs b/src/lib.rs index 312f289..eaf48bd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,8 @@ pub mod api; +pub mod git; pub mod graph; pub mod markdown; pub mod persist; -pub mod git; pub struct Credentials { // Personal access token diff --git a/src/main.rs b/src/main.rs index 7138e7f..a8837cc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,14 @@ use std::collections::HashMap; use std::env; use std::error::Error; -use std::process; -use std::io::{self, Write}; -use std::rc::Rc; use std::fs; +use std::io::{self, Write}; +use std::process; +use std::rc::Rc; +use gh_stack::api::search::PullRequest; use gh_stack::Credentials; -use gh_stack::{api, graph, markdown, persist, git}; +use gh_stack::{api, git, graph, markdown, persist}; pub fn read_cli_input(message: &str) -> String { print!("{}", message); @@ -51,7 +52,10 @@ async fn main() -> Result<(), Box> { let credentials = Credentials::new(token); let prs = api::search::fetch_pull_requests_matching(&pattern, &credentials).await?; - let prs = prs.into_iter().map(|pr| Rc::new(pr)).collect(); + let prs = prs + .into_iter() + .map(Rc::new) + .collect::>>(); let tree = graph::build(&prs); match command { @@ -59,15 +63,14 @@ async fn main() -> Result<(), Box> { let table = markdown::build_table(tree, pattern); let output = match prelude { - Some(prelude) => build_final_output(prelude, &table), - None => table + Some(prelude) => build_final_output(prelude, &table), + None => table, }; for pr in prs.iter() { println!("{}: {}", pr.number(), pr.title()); } - let response = read_cli_input("Going to update these PRs ☝️ (y/n): "); match &response[..] { "y" => persist::persist(&prs, &output, &credentials).await?, @@ -88,12 +91,12 @@ async fn main() -> Result<(), Box> { for (pr, maybe_parent) in log { match maybe_parent { Some(parent) => println!("{} → {}", pr.head(), parent.head()), - None => println!("{} → N/A", pr.head()) + None => println!("{} → N/A", pr.head()), } } } - _ => { panic!("Invalid command!") } + _ => panic!("Invalid command!"), }; Ok(()) diff --git a/src/markdown.rs b/src/markdown.rs index c54096a..f9717f1 100644 --- a/src/markdown.rs +++ b/src/markdown.rs @@ -32,7 +32,7 @@ pub fn build_table(graph: Graph, usize>, title: &str) -> String "|#{}|{}|#{}|\n", node.number(), node.title(), - graph[parent.source().clone()].number() + graph[parent.source()].number() ), None => format!("|#{}|{}|**N/A**|\n", node.number(), node.title()), }; diff --git a/src/persist.rs b/src/persist.rs index e3662a2..e055f74 100644 --- a/src/persist.rs +++ b/src/persist.rs @@ -29,7 +29,7 @@ fn safe_replace(body: &str, table: &str) -> String { } pub async fn persist( - prs: &Vec>, + prs: &[Rc], table: &str, c: &Credentials, ) -> Result<(), Box> {