Skip to content

Commit

Permalink
Implement release subcommand in clippy_dev
Browse files Browse the repository at this point in the history
This is a QoL improvement for doing releases. The `release bump_version`
subcommand increments the version in all relevant `Cargo.toml` files. The
`release commit` command will only work with the new Josh syncs. Until then the
old way, using `git log` has to be used.
  • Loading branch information
flip1995 committed May 4, 2024
1 parent 71033c5 commit b916a64
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[package]
name = "clippy"
# begin autogenerated version
version = "0.1.80"
# end autogenerated version
description = "A bunch of helpful lints to avoid common pitfalls in Rust"
repository = "https://github.com/rust-lang/rust-clippy"
readme = "README.md"
Expand Down
2 changes: 2 additions & 0 deletions clippy_config/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[package]
name = "clippy_config"
# begin autogenerated version
version = "0.1.80"
# end autogenerated version
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
1 change: 1 addition & 0 deletions clippy_dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub mod dogfood;
pub mod fmt;
pub mod lint;
pub mod new_lint;
pub mod release;
pub mod serve;
pub mod setup;
pub mod sync;
Expand Down
28 changes: 27 additions & 1 deletion clippy_dev/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#![warn(rust_2018_idioms, unused_lifetimes)]

use clap::{Args, Parser, Subcommand};
use clippy_dev::{dogfood, fmt, lint, new_lint, serve, setup, sync, update_lints, utils};
use clippy_dev::{dogfood, fmt, lint, new_lint, release, serve, setup, sync, update_lints, utils};
use std::convert::Infallible;

fn main() {
Expand Down Expand Up @@ -84,6 +84,10 @@ fn main() {
force,
} => sync::rustc_push(repo_path, &user, branch, force),
},
DevCommand::Release(ReleaseCommand { subcommand }) => match subcommand {
ReleaseSubcommand::BumpVersion => release::bump_version(),
ReleaseSubcommand::Commit { repo_path, branch } => release::rustc_clippy_commit(repo_path, branch),
},
}
}

Expand Down Expand Up @@ -236,6 +240,8 @@ enum DevCommand {
},
/// Sync between the rust repo and the Clippy repo
Sync(SyncCommand),
/// Manage Clippy releases
Release(ReleaseCommand),
}

#[derive(Args)]
Expand Down Expand Up @@ -330,3 +336,23 @@ enum SyncSubcommand {
force: bool,
},
}

#[derive(Args)]
struct ReleaseCommand {
#[command(subcommand)]
subcommand: ReleaseSubcommand,
}

#[derive(Subcommand)]
enum ReleaseSubcommand {
#[command(name = "bump_version")]
/// Bump the version in the Cargo.toml files
BumpVersion,
/// Print the Clippy commit in the rustc repo for the specified branch
Commit {
/// The path to a rustc repo to look for the commit
repo_path: String,
/// For which branch to print the commit
branch: release::Branch,
},
}
76 changes: 76 additions & 0 deletions clippy_dev/src/release.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use std::fmt::{Display, Write};
use std::path::Path;

use crate::sync::PUSH_PR_DESCRIPTION;
use crate::utils::{clippy_version, replace_region_in_file, UpdateMode};

use clap::ValueEnum;
use xshell::{cmd, Shell};

const CARGO_TOML_FILES: [&str; 5] = [
"clippy_config/Cargo.toml",
"clippy_lints/Cargo.toml",
"clippy_utils/Cargo.toml",
"declare_clippy_lint/Cargo.toml",
"Cargo.toml",
];

pub fn bump_version() {
let (minor, mut patch) = clippy_version();
patch += 1;
for file in &CARGO_TOML_FILES {
replace_region_in_file(
UpdateMode::Change,
Path::new(file),
"# begin autogenerated version\n",
"# end autogenerated version",
|res| {
writeln!(res, "version = \"0.{minor}.{patch}\"").unwrap();
},
);
}
}

#[derive(ValueEnum, Copy, Clone)]
pub enum Branch {
Stable,
Beta,
Master,
}

impl Display for Branch {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Branch::Stable => write!(f, "stable"),
Branch::Beta => write!(f, "beta"),
Branch::Master => write!(f, "master"),
}
}
}

pub fn rustc_clippy_commit(rustc_path: String, branch: Branch) {
let sh = Shell::new().expect("failed to create shell");
sh.change_dir(rustc_path);

let base = branch.to_string();
cmd!(sh, "git fetch https://github.com/rust-lang/rust {base}")
.run()
.expect("failed to fetch base commit");
let last_rustup_commit = cmd!(
sh,
"git log -1 --merges --grep=\"{PUSH_PR_DESCRIPTION}\" FETCH_HEAD -- src/tools/clippy"
)
.read()
.expect("failed to run git log");

let commit = last_rustup_commit
.lines()
.find(|c| c.contains("Sync from Clippy commit:"))
.expect("no commit found")
.trim()
.rsplit_once('@')
.expect("no commit hash found")
.1;

println!("{commit}");
}
2 changes: 2 additions & 0 deletions clippy_lints/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[package]
name = "clippy_lints"
# begin autogenerated version
version = "0.1.80"
# end autogenerated version
description = "A bunch of helpful lints to avoid common pitfalls in Rust"
repository = "https://github.com/rust-lang/rust-clippy"
readme = "README.md"
Expand Down
2 changes: 2 additions & 0 deletions clippy_utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[package]
name = "clippy_utils"
# begin autogenerated version
version = "0.1.80"
# end autogenerated version
edition = "2021"
publish = false

Expand Down
2 changes: 2 additions & 0 deletions declare_clippy_lint/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[package]
name = "declare_clippy_lint"
# begin autogenerated version
version = "0.1.80"
# end autogenerated version
edition = "2021"
publish = false

Expand Down
2 changes: 2 additions & 0 deletions rust-toolchain
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[toolchain]
# begin autogenerated version
channel = "nightly-2024-05-02"
# end autogenerated version
components = ["cargo", "llvm-tools", "rust-src", "rust-std", "rustc", "rustc-dev", "rustfmt"]

0 comments on commit b916a64

Please sign in to comment.