Skip to content

Commit

Permalink
Abort fix with changes in working directory
Browse files Browse the repository at this point in the history
  • Loading branch information
chriscerie committed Oct 14, 2023
1 parent 679cd8c commit 0560a86
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/src/cli/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ USAGE:
FLAGS:
--allow-warnings Pass when only warnings occur
--no-exclude Ignore excludes defined in config
--fix Automatically applies some lint suggestions
--fix Automatically fix applicable lint warnings
-h, --help Prints help information
-n, --no-summary Suppress summary information
-q, --quiet Display only the necessary information. Equivalent to --display-style="quiet"
Expand Down
36 changes: 36 additions & 0 deletions selene/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{
fmt, fs,
io::{self, Read, Write},
path::{Path, PathBuf},
process::Command,
sync::{
atomic::{AtomicUsize, Ordering},
Arc, RwLock,
Expand Down Expand Up @@ -553,6 +554,17 @@ fn start(mut options: opts::Options) {
None => {}
}

if options.fix && !options.allow_dirty {
if has_unstaged_changes() || (!options.allow_staged && has_staged_changes()) {
error(
"the working directory of this package has uncommitted changes, and `selene --fix` can potentially \
perform destructive changes; if you'd like to suppress this error pass `--allow-dirty`, `--allow-staged`, \
or commit the changes"
);
std::process::exit(1);
}
}

let (config, config_directory): (CheckerConfig<toml::value::Value>, Option<PathBuf>) =
match options.config {
Some(config_file) => {
Expand Down Expand Up @@ -830,6 +842,30 @@ fn get_opts_safe(mut args: Vec<OsString>, luacheck: bool) -> Result<opts::Option
}
}

fn has_unstaged_changes() -> bool {
let output = Command::new("git")
.arg("status")
.arg("--porcelain")
.output()
.expect("Failed to execute git");

let stdout = String::from_utf8(output.stdout).expect("Failed to convert git output to string");

stdout.lines().any(|line| line.chars().nth(1) != Some(' '))
}

fn has_staged_changes() -> bool {
let output = Command::new("git")
.arg("status")
.arg("--porcelain")
.output()
.expect("Failed to execute git");

let stdout = String::from_utf8(output.stdout).expect("Failed to convert git output to string");

stdout.lines().any(|line| line.chars().nth(0) != Some(' '))
}

#[cfg(feature = "roblox")]
fn generate_roblox_std() -> color_eyre::Result<StandardLibrary> {
let (contents, std) = roblox::RobloxGenerator::generate()?;
Expand Down
10 changes: 10 additions & 0 deletions selene/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ pub struct Options {

#[structopt(long)]
pub fix: bool,

// Fix code even if the working directory has changes
// Only used with `fix`
#[structopt(long, hidden(true))]
pub allow_dirty: bool,

// Fix code even if the working directory has staged changes
// Only used with `fix`
#[structopt(long, hidden(true))]
pub allow_staged: bool,
}

impl Options {
Expand Down

0 comments on commit 0560a86

Please sign in to comment.