Skip to content

Commit

Permalink
feat(cli): add --staged flag (#2300)
Browse files Browse the repository at this point in the history
Signed-off-by: Andres Correa Casablanca <[email protected]>
  • Loading branch information
castarco authored Apr 12, 2024
1 parent 60671ec commit af121e3
Show file tree
Hide file tree
Showing 19 changed files with 441 additions and 28 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b
```
</details>

- Added new `--staged` flag to the `check`, `format` and `lint` subcommands.

This new option allows users to apply the command _only_ to the files that are staged (the
ones that will be committed), which can be very useful to simplify writing git hook scripts
such as `pre-commit`. Contributed by @castarco

#### Enhancements

- Improve support of `.prettierignore` when migrating from Prettier
Expand Down
10 changes: 10 additions & 0 deletions crates/biome_cli/src/changed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,13 @@ pub(crate) fn get_changed_files(

Ok(filtered_changed_files)
}

pub(crate) fn get_staged_files(
fs: &DynRef<'_, dyn FileSystem>,
) -> Result<Vec<OsString>, CliDiagnostic> {
let staged_files = fs.get_staged_files()?;

let filtered_staged_files = staged_files.iter().map(OsString::from).collect::<Vec<_>>();

Ok(filtered_staged_files)
}
16 changes: 9 additions & 7 deletions crates/biome_cli/src/commands/check.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::changed::get_changed_files;
use crate::cli_options::CliOptions;
use crate::commands::{get_stdin, resolve_manifest, validate_configuration_diagnostics};
use crate::commands::{
get_files_to_process, get_stdin, resolve_manifest, validate_configuration_diagnostics,
};
use crate::{
execute_mode, setup_cli_subscriber, CliDiagnostic, CliSession, Execution, TraversalMode,
};
Expand All @@ -26,6 +27,7 @@ pub(crate) struct CheckCommandPayload {
pub(crate) formatter_enabled: Option<bool>,
pub(crate) linter_enabled: Option<bool>,
pub(crate) organize_imports_enabled: Option<bool>,
pub(crate) staged: bool,
pub(crate) changed: bool,
pub(crate) since: Option<String>,
}
Expand All @@ -46,6 +48,7 @@ pub(crate) fn check(
organize_imports_enabled,
formatter_enabled,
since,
staged,
changed,
} = payload;
setup_cli_subscriber(cli_options.log_level, cli_options.log_kind);
Expand Down Expand Up @@ -120,13 +123,12 @@ pub(crate) fn check(

let stdin = get_stdin(stdin_file_path, &mut *session.app.console, "check")?;

if since.is_some() && !changed {
return Err(CliDiagnostic::incompatible_arguments("since", "changed"));
if let Some(_paths) =
get_files_to_process(since, changed, staged, &session.app.fs, &fs_configuration)?
{
paths = _paths;
}

if changed {
paths = get_changed_files(&session.app.fs, &fs_configuration, since)?;
}
session
.app
.workspace
Expand Down
17 changes: 9 additions & 8 deletions crates/biome_cli/src/commands/format.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::changed::get_changed_files;
use crate::cli_options::CliOptions;
use crate::commands::{get_stdin, resolve_manifest, validate_configuration_diagnostics};
use crate::commands::{
get_files_to_process, get_stdin, resolve_manifest, validate_configuration_diagnostics,
};
use crate::diagnostics::DeprecatedArgument;
use crate::{
execute_mode, setup_cli_subscriber, CliDiagnostic, CliSession, Execution, TraversalMode,
Expand Down Expand Up @@ -30,6 +31,7 @@ pub(crate) struct FormatCommandPayload {
pub(crate) write: bool,
pub(crate) cli_options: CliOptions,
pub(crate) paths: Vec<OsString>,
pub(crate) staged: bool,
pub(crate) changed: bool,
pub(crate) since: Option<String>,
}
Expand All @@ -51,6 +53,7 @@ pub(crate) fn format(
mut json_formatter,
mut css_formatter,
since,
staged,
changed,
} = payload;
setup_cli_subscriber(cli_options.log_level, cli_options.log_kind);
Expand Down Expand Up @@ -156,12 +159,10 @@ pub(crate) fn format(
let (vcs_base_path, gitignore_matches) =
configuration.retrieve_gitignore_matches(&session.app.fs, vcs_base_path.as_deref())?;

if since.is_some() && !changed {
return Err(CliDiagnostic::incompatible_arguments("since", "changed"));
}

if changed {
paths = get_changed_files(&session.app.fs, &configuration, since)?;
if let Some(_paths) =
get_files_to_process(since, changed, staged, &session.app.fs, &configuration)?
{
paths = _paths;
}

session
Expand Down
17 changes: 9 additions & 8 deletions crates/biome_cli/src/commands/lint.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::changed::get_changed_files;
use crate::cli_options::CliOptions;
use crate::commands::{get_stdin, resolve_manifest, validate_configuration_diagnostics};
use crate::commands::{
get_files_to_process, get_stdin, resolve_manifest, validate_configuration_diagnostics,
};
use crate::{
execute_mode, setup_cli_subscriber, CliDiagnostic, CliSession, Execution, TraversalMode,
};
Expand All @@ -24,6 +25,7 @@ pub(crate) struct LintCommandPayload {
pub(crate) files_configuration: Option<PartialFilesConfiguration>,
pub(crate) paths: Vec<OsString>,
pub(crate) stdin_file_path: Option<String>,
pub(crate) staged: bool,
pub(crate) changed: bool,
pub(crate) since: Option<String>,
}
Expand All @@ -39,6 +41,7 @@ pub(crate) fn lint(session: CliSession, payload: LintCommandPayload) -> Result<(
stdin_file_path,
vcs_configuration,
files_configuration,
staged,
changed,
since,
} = payload;
Expand Down Expand Up @@ -95,12 +98,10 @@ pub(crate) fn lint(session: CliSession, payload: LintCommandPayload) -> Result<(
let (vcs_base_path, gitignore_matches) =
fs_configuration.retrieve_gitignore_matches(&session.app.fs, vcs_base_path.as_deref())?;

if since.is_some() && !changed {
return Err(CliDiagnostic::incompatible_arguments("since", "changed"));
}

if changed {
paths = get_changed_files(&session.app.fs, &fs_configuration, since)?;
if let Some(_paths) =
get_files_to_process(since, changed, staged, &session.app.fs, &fs_configuration)?
{
paths = _paths;
}

let stdin = get_stdin(stdin_file_path, &mut *session.app.console, "lint")?;
Expand Down
47 changes: 45 additions & 2 deletions crates/biome_cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::changed::{get_changed_files, get_staged_files};
use crate::cli_options::{cli_options, CliOptions, ColorsArg};
use crate::diagnostics::DeprecatedConfigurationFile;
use crate::execute::Stdin;
Expand All @@ -14,11 +15,11 @@ use biome_configuration::{
use biome_configuration::{ConfigurationDiagnostic, PartialConfiguration};
use biome_console::{markup, Console, ConsoleExt};
use biome_diagnostics::{Diagnostic, PrintDiagnostic};
use biome_fs::BiomePath;
use biome_fs::{BiomePath, FileSystem};
use biome_service::configuration::LoadedConfiguration;
use biome_service::documentation::Doc;
use biome_service::workspace::{OpenProjectParams, UpdateProjectParams};
use biome_service::WorkspaceError;
use biome_service::{DynRef, WorkspaceError};
use bpaf::Bpaf;
use std::ffi::OsString;
use std::path::PathBuf;
Expand Down Expand Up @@ -109,6 +110,11 @@ pub enum BiomeCommand {
#[bpaf(long("stdin-file-path"), argument("PATH"), hide_usage)]
stdin_file_path: Option<String>,

/// When set to true, only the files that have been staged (the ones prepared to be committed)
/// will be linted.
#[bpaf(long("staged"), switch)]
staged: bool,

/// When set to true, only the files that have been changed compared to your `defaultBranch`
/// configuration will be linted.
#[bpaf(long("changed"), switch)]
Expand Down Expand Up @@ -150,6 +156,10 @@ pub enum BiomeCommand {
/// Example: `echo 'let a;' | biome lint --stdin-file-path=file.js`
#[bpaf(long("stdin-file-path"), argument("PATH"), hide_usage)]
stdin_file_path: Option<String>,
/// When set to true, only the files that have been staged (the ones prepared to be committed)
/// will be linted.
#[bpaf(long("staged"), switch)]
staged: bool,
/// When set to true, only the files that have been changed compared to your `defaultBranch`
/// configuration will be linted.
#[bpaf(long("changed"), switch)]
Expand Down Expand Up @@ -197,6 +207,11 @@ pub enum BiomeCommand {
#[bpaf(switch)]
write: bool,

/// When set to true, only the files that have been staged (the ones prepared to be committed)
/// will be linted.
#[bpaf(long("staged"), switch)]
staged: bool,

/// When set to true, only the files that have been changed compared to your `defaultBranch`
/// configuration will be linted.
#[bpaf(long("changed"), switch)]
Expand Down Expand Up @@ -510,6 +525,34 @@ pub(crate) fn get_stdin(
Ok(stdin)
}

fn get_files_to_process(
since: Option<String>,
changed: bool,
staged: bool,
fs: &DynRef<'_, dyn FileSystem>,
configuration: &PartialConfiguration,
) -> Result<Option<Vec<OsString>>, CliDiagnostic> {
if since.is_some() {
if !changed {
return Err(CliDiagnostic::incompatible_arguments("since", "changed"));
}
if staged {
return Err(CliDiagnostic::incompatible_arguments("since", "staged"));
}
}

if changed {
if staged {
return Err(CliDiagnostic::incompatible_arguments("changed", "staged"));
}
Ok(Some(get_changed_files(fs, configuration, since)?))
} else if staged {
Ok(Some(get_staged_files(fs)?))
} else {
Ok(None)
}
}

/// Tests that all CLI options adhere to the invariants expected by `bpaf`.
#[test]
fn check_options() {
Expand Down
6 changes: 6 additions & 0 deletions crates/biome_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ impl<'app> CliSession<'app> {
linter_enabled,
organize_imports_enabled,
formatter_enabled,
staged,
changed,
since,
} => commands::check::check(
Expand All @@ -102,6 +103,7 @@ impl<'app> CliSession<'app> {
linter_enabled,
organize_imports_enabled,
formatter_enabled,
staged,
changed,
since,
},
Expand All @@ -115,6 +117,7 @@ impl<'app> CliSession<'app> {
stdin_file_path,
vcs_configuration,
files_configuration,
staged,
changed,
since,
} => commands::lint::lint(
Expand All @@ -128,6 +131,7 @@ impl<'app> CliSession<'app> {
stdin_file_path,
vcs_configuration,
files_configuration,
staged,
changed,
since,
},
Expand Down Expand Up @@ -165,6 +169,7 @@ impl<'app> CliSession<'app> {
files_configuration,
json_formatter,
css_formatter,
staged,
changed,
since,
} => commands::format::format(
Expand All @@ -180,6 +185,7 @@ impl<'app> CliSession<'app> {
files_configuration,
json_formatter,
css_formatter,
staged,
changed,
since,
},
Expand Down
Loading

0 comments on commit af121e3

Please sign in to comment.