Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

feat: changed files #4743

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions crates/rome_cli/src/cli_options.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::vcs::create_vcs_client;
use crate::CliDiagnostic;
use bpaf::Bpaf;
use rome_console::markup;
use rome_service::Configuration;
use std::str::FromStr;

/// Global options applied to all commands
Expand Down Expand Up @@ -44,6 +48,37 @@ pub struct CliOptions {
/// Reports information using the JSON format
#[bpaf(long("json"), switch, hide_usage)]
pub json: bool,

/// Only runs the operation on files that have changed. VCS support needs to be enabled. Doesn't work when running `rome ci`.
#[bpaf(long("changed"), switch)]
pub changed: bool,
}

impl CliOptions {
/// It validates the current CLI options against. If not errors are found, it calls the VCS client
/// via [std::process::Command] and returns a list of changed files
///
/// ## Errors
/// - `--changed` is passed and the VCS support is disabled
/// - errors are raised when calling the VCS client via [std::process::Command]
pub(crate) fn compute_changed_files(
&self,
configuration: &Configuration,
) -> Result<Vec<String>, CliDiagnostic> {
if let Some(vcs) = configuration.vcs.as_ref() {
if vcs.is_disabled() && self.changed {
return Err(CliDiagnostic::incompatible_end_configuration(markup! {
"You provided the "<Emphasis>"--changed"</Emphasis>" argument, but you haven't enabled VCS support. This is an error."
}));
}
if let Some(client_kind) = vcs.client_kind.as_ref() {
let vcs_client = create_vcs_client(client_kind);
return vcs_client.changed_files();
}
}

Ok(vec![])
}
}

#[derive(Debug, Clone)]
Expand Down
3 changes: 3 additions & 0 deletions crates/rome_cli/src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ pub(crate) fn check(

fs_configuration.merge_with(configuration);

let changed_files = cli_options.compute_changed_files(&fs_configuration)?;

// check if support of git ignore files is enabled
let vcs_base_path = configuration_path.or(session.app.fs.working_directory());
store_path_to_ignore_from_vcs(
Expand Down Expand Up @@ -122,5 +124,6 @@ pub(crate) fn check(
session,
&cli_options,
paths,
changed_files,
)
}
2 changes: 2 additions & 0 deletions crates/rome_cli/src/commands/ci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,7 @@ pub(crate) fn ci(mut session: CliSession, payload: CiCommandPayload) -> Result<(
session,
&payload.cli_options,
payload.paths,
// we never process changed files in CI
vec![],
)
}
4 changes: 3 additions & 1 deletion crates/rome_cli/src/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pub(crate) fn format(
configuration.merge_with(vcs_configuration);
configuration.merge_with(files_configuration);

let changed_files = cli_options.compute_changed_files(&configuration)?;

// check if support of git ignore files is enabled
let vcs_base_path = configuration_path.or(session.app.fs.working_directory());
store_path_to_ignore_from_vcs(
Expand Down Expand Up @@ -92,5 +94,5 @@ pub(crate) fn format(
})
};

execute_mode(execution, session, &cli_options, paths)
execute_mode(execution, session, &cli_options, paths, changed_files)
}
3 changes: 3 additions & 0 deletions crates/rome_cli/src/commands/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ pub(crate) fn lint(

fs_configuration.merge_with(configuration);

let changed_files = cli_options.compute_changed_files(&fs_configuration)?;

// check if support of git ignore files is enabled
let vcs_base_path = configuration_path.or(session.app.fs.working_directory());
store_path_to_ignore_from_vcs(
Expand Down Expand Up @@ -90,5 +92,6 @@ pub(crate) fn lint(
session,
&cli_options,
paths,
changed_files,
)
}
1 change: 1 addition & 0 deletions crates/rome_cli/src/commands/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub(crate) fn migrate(
session,
&cli_options,
vec![],
vec![],
)
} else {
Err(CliDiagnostic::MigrateError(MigrationDiagnostic {
Expand Down
8 changes: 4 additions & 4 deletions crates/rome_cli/src/diagnostics.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rome_console::fmt::Formatter;
use rome_console::fmt::{Display, Formatter};
use rome_console::markup;
use rome_diagnostics::adapters::{BpafError, IoError};
use rome_diagnostics::{
Expand Down Expand Up @@ -219,7 +219,7 @@ pub struct ServerNotRunning;
)
)]
pub struct IncompatibleEndConfiguration {
reason: String,
reason: MessageAndDescription,
}

#[derive(Debug, Diagnostic)]
Expand Down Expand Up @@ -430,9 +430,9 @@ impl CliDiagnostic {
/// results in a combination of options that doesn't allow to run the command correctly.
///
/// A reason needs to be provided
pub fn incompatible_end_configuration(reason: impl Into<String>) -> Self {
pub fn incompatible_end_configuration(reason: impl Display) -> Self {
Self::IncompatibleEndConfiguration(IncompatibleEndConfiguration {
reason: reason.into(),
reason: MessageAndDescription::from(markup! {{reason}}.to_owned()),
})
}

Expand Down
14 changes: 13 additions & 1 deletion crates/rome_cli/src/execute/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ pub(crate) fn execute_mode(
session: CliSession,
cli_options: &CliOptions,
paths: Vec<OsString>,
changed_files: Vec<String>,
) -> Result<(), CliDiagnostic> {
if cli_options.max_diagnostics > MAXIMUM_DISPLAYABLE_DIAGNOSTICS {
return Err(CliDiagnostic::overflown_argument(
Expand All @@ -229,6 +230,17 @@ pub(crate) fn execute_mode(

mode.max_diagnostics = cli_options.max_diagnostics;

// At this point, we should already have checked if the configuration has already enabled the VCS support
// let changed_files = if cli_options.changed && !mode.is_ci() {
// if let Some(vcs) = vcs {
// vcs.changed_files()?
// } else {
// vec![]
// }
// } else {
// vec![]
// };

// don't do any traversal if there's some content coming from stdin
if let Some((path, content)) = mode.as_stdin_file() {
let rome_path = RomePath::new(path);
Expand All @@ -240,6 +252,6 @@ pub(crate) fn execute_mode(
{
migrate::run(session, write, configuration_path, cli_options.verbose)
} else {
traverse(mode, session, cli_options, paths)
traverse(mode, session, cli_options, paths, changed_files)
}
}
13 changes: 10 additions & 3 deletions crates/rome_cli/src/execute/traverse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub(crate) fn traverse(
session: CliSession,
cli_options: &CliOptions,
inputs: Vec<OsString>,
changed_files: Vec<String>,
) -> Result<(), CliDiagnostic> {
init_thread_pool();
if inputs.is_empty() && execution.as_stdin_file().is_none() {
Expand Down Expand Up @@ -112,6 +113,7 @@ pub(crate) fn traverse(
traverse_inputs(
fs,
inputs,
changed_files,
&TraversalOptions {
fs,
workspace,
Expand Down Expand Up @@ -239,12 +241,17 @@ fn init_thread_pool() {

/// Initiate the filesystem traversal tasks with the provided input paths and
/// run it to completion, returning the duration of the process
fn traverse_inputs(fs: &dyn FileSystem, inputs: Vec<OsString>, ctx: &TraversalOptions) -> Duration {
fn traverse_inputs(
fs: &dyn FileSystem,
inputs: Vec<OsString>,
changed_files: Vec<String>,
ctx: &TraversalOptions,
) -> Duration {
let start = Instant::now();

let changed_files = changed_files.as_slice();
fs.traversal(Box::new(move |scope: &dyn TraversalScope| {
for input in inputs {
scope.spawn(ctx, PathBuf::from(input));
scope.spawn(ctx, PathBuf::from(input), changed_files);
}
}));

Expand Down
12 changes: 12 additions & 0 deletions crates/rome_cli/src/vcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use rome_service::configuration::FilesConfiguration;
use rome_service::{Configuration, WorkspaceError};
use std::path::PathBuf;

mod git;

/// This function will check if the configuration is set to use the VCS integration and try to
/// read the ignored files.
pub(crate) fn store_path_to_ignore_from_vcs(
Expand Down Expand Up @@ -98,3 +100,13 @@ pub(crate) fn read_vcs_ignore_file(

Ok(vec![])
}

pub(crate) trait VcsClient {
fn changed_files(&self) -> Result<Vec<String>, CliDiagnostic>;
}

pub(crate) fn create_vcs_client(client_kind: &VcsClientKind) -> Box<dyn VcsClient> {
Box::new(match client_kind {
VcsClientKind::Git => git::Git,
})
}
28 changes: 28 additions & 0 deletions crates/rome_cli/src/vcs/git.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use crate::vcs::VcsClient;
use crate::CliDiagnostic;
use std::io::Read;
use std::process::{Command, Stdio};

pub(crate) struct Git;

impl Git {
const COMMAND: &'static str = "git";
}

impl VcsClient for Git {
fn changed_files(&self) -> Result<Vec<String>, CliDiagnostic> {
let process = Command::new(Git::COMMAND)
.arg("diff")
.arg("--name-only")
.stdout(Stdio::piped())
.spawn()?;

if let Some(mut output) = process.stdout {
let mut buffer = String::new();
output.read_to_string(&mut buffer)?;
Ok(buffer.lines().map(String::from).collect::<Vec<_>>())
} else {
Ok(Vec::new())
}
}
}
28 changes: 28 additions & 0 deletions crates/rome_cli/tests/cases/changed_files.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use crate::run_cli;
use crate::snap_test::{assert_cli_snapshot, SnapshotPayload};
use bpaf::Args;
use rome_console::BufferConsole;
use rome_fs::MemoryFileSystem;
use rome_service::DynRef;

#[test]
fn emits_an_error_if_vcs_is_not_enabled() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let result = run_cli(
DynRef::Borrowed(&mut fs),
&mut console,
Args::from([("check"), "--changed", "./file.js"].as_slice()),
);

assert!(result.is_err(), "run_cli returned {result:?}");

assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"emits_an_error_if_vcs_is_not_enabled",
fs,
console,
result,
));
}
1 change: 1 addition & 0 deletions crates/rome_cli/tests/cases/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Add here test cases that are not related directly to a command, but to specific
//! case that affects many commands

mod changed_files;
mod config_extends;
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: crates/rome_cli/tests/snap_test.rs
expression: content
---
# Termination Message

```block
internalError/io ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

× The combination of configuration and arguments is invalid:
You provided the --changed argument, but you haven't enabled VCS support. This is an error.



```


Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Global options applied to all commands
during the execution of the command.
--error-on-warnings Tell Rome to exit with an error code if some diagnostics emit warnings.
--json Reports information using the JSON format
--changed Only runs the operation on files that have changed. VCS support needs to
be enabled. Doesn't work when running `rome ci`.

Available positional items:
PATH Single file, single path or list of paths
Expand Down
2 changes: 2 additions & 0 deletions crates/rome_cli/tests/snapshots/main_commands_ci/ci_help.snap
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ Global options applied to all commands
during the execution of the command.
--error-on-warnings Tell Rome to exit with an error code if some diagnostics emit warnings.
--json Reports information using the JSON format
--changed Only runs the operation on files that have changed. VCS support needs to
be enabled. Doesn't work when running `rome ci`.

Available positional items:
PATH Single file, single path or list of paths
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Global options applied to all commands
during the execution of the command.
--error-on-warnings Tell Rome to exit with an error code if some diagnostics emit warnings.
--json Reports information using the JSON format
--changed Only runs the operation on files that have changed. VCS support needs to
be enabled. Doesn't work when running `rome ci`.

Available positional items:
PATH Single file, single path or list of paths.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Global options applied to all commands
during the execution of the command.
--error-on-warnings Tell Rome to exit with an error code if some diagnostics emit warnings.
--json Reports information using the JSON format
--changed Only runs the operation on files that have changed. VCS support needs to
be enabled. Doesn't work when running `rome ci`.

Available positional items:
PATH Single file, single path or list of paths
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ expression: content
Acts as a server for the Language Server Protocol over stdin/stdout

Usage: lsp-proxy [--colors=<off|force>] [--use-server] [--verbose] [--config-path=PATH] [--max-diagnostics
=NUMBER] [--skip-errors] [--no-errors-on-unmatched] [--error-on-warnings]
=NUMBER] [--skip-errors] [--no-errors-on-unmatched] [--error-on-warnings] [--changed]

Global options applied to all commands
--colors=<off|force> Set the formatting mode for markup: "off" prints everything as plain text,
Expand All @@ -25,6 +25,8 @@ Global options applied to all commands
during the execution of the command.
--error-on-warnings Tell Rome to exit with an error code if some diagnostics emit warnings.
--json Reports information using the JSON format
--changed Only runs the operation on files that have changed. VCS support needs to
be enabled. Doesn't work when running `rome ci`.

Available options:
-h, --help Prints help information
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Global options applied to all commands
during the execution of the command.
--error-on-warnings Tell Rome to exit with an error code if some diagnostics emit warnings.
--json Reports information using the JSON format
--changed Only runs the operation on files that have changed. VCS support needs to
be enabled. Doesn't work when running `rome ci`.

Available options:
--write Writes the new configuration file to disk
Expand Down
Loading
Loading