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 dae8f5f
Show file tree
Hide file tree
Showing 3 changed files with 46 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
35 changes: 35 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 @@ -664,6 +665,16 @@ fn start(mut options: opts::Options) {

let pool = ThreadPool::new(options.num_threads);

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"
);
}
}

for filename in &options.files {
if filename == "-" {
let checker = Arc::clone(&checker);
Expand Down Expand Up @@ -830,6 +841,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 dae8f5f

Please sign in to comment.