Skip to content
Merged
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
1,131 changes: 560 additions & 571 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 3 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ edition = "2021"
[dependencies]
anyhow = "1.0.70"
clap = "4.0.18"
git2 = "0.15"
git2 = "0.21"
glob = "0.3.0"

[dev-dependencies]
cucumber = "0.19"
cucumber = "0.23"
futures = "0.3"
tempfile = "3.5.0"
shlex = "1.1.0"
# fake = "2.5.0"
# rand = "0.8.5"
shlex = "2.0.1"

[[test]]
name = "features"
Expand Down
3 changes: 0 additions & 3 deletions devenv.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
{
packages = [
pkgs.cargo-outdated
pkgs.pkg-config
pkgs.openssl
pkgs.zlib
];

languages.rust = {
Expand Down
51 changes: 13 additions & 38 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,17 @@ use clap::builder::ArgAction;
use clap::builder::Command;
use clap::ArgMatches;
use glob::Pattern;
use std::fmt;

#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct Options {
pub pattern: Pattern,
pub glob: Pattern,
pub base: Option<String>,
pub null: bool,
pub exist: bool,
}

impl fmt::Display for Options {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"pattern: {}, base: {:?}, null: {}, exist: {}",
self.pattern, self.base, self.null, self.exist
)
}
pub fn get_options() -> Options {
matches_to_options(command().get_matches())
}

fn command() -> Command {
Expand All @@ -39,42 +32,24 @@ fn command() -> Command {
Arg::new("exist")
.short('e')
.long("exist")
.help("Filter files that still exist")
.help("Show only the files that still exist")
.action(ArgAction::SetTrue),
Arg::new("glob")
.default_value("**/*")
.value_parser(Pattern::new)
.index(1)
.help("Use this glob to filter files"),
])
}

fn matches_to_options(matches: ArgMatches) -> Options {
let glob = matches
.get_one::<String>("glob")
.expect("Default value is missing for glob");

let pattern = Pattern::new(glob).expect("Valid glob required");

let base = matches.get_one::<String>("base").cloned();

let null = match matches.get_one::<bool>("null") {
Some(value) => value.to_owned(),
None => false,
};

let exist = match matches.get_one::<bool>("exist") {
Some(value) => value.to_owned(),
None => false,
};

Options {
pattern,
base,
null,
exist,
glob: matches
.get_one::<Pattern>("glob")
.cloned()
.expect("The glob has a default value"),
base: matches.get_one::<String>("base").cloned(),
null: matches.get_flag("null"),
exist: matches.get_flag("exist"),
}
}

pub fn get_options() -> Options {
matches_to_options(command().get_matches())
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn relative_path(file: &Path, directory: &Path) -> PathBuf {
}

fn is_selected(file: &Path, options: &Options) -> bool {
if !options.pattern.matches_path(file) {
if !options.glob.matches_path(file) {
return false;
}

Expand Down
6 changes: 5 additions & 1 deletion tests/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ use std::process::Stdio;
use tempfile::TempDir;

fn main() {
futures::executor::block_on(GitWorld::cucumber().run_and_exit("tests/features"));
futures::executor::block_on(
GitWorld::cucumber()
.fail_on_skipped()
.run_and_exit("tests/features"),
);
}

#[derive(Debug, World)]
Expand Down
11 changes: 11 additions & 0 deletions tests/features/glob.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Feature: Glob

Scenario: It rejects an invalid glob
Given an empty directory
When the user executes the tool like: "git-changed ["
Then it fails with the following error
"""
error: invalid value '[' for '[glob]': Pattern syntax error near position 0: invalid range pattern

For more information, try '--help'.
"""
2 changes: 1 addition & 1 deletion tests/features/help.feature
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ Feature: Help
Options:
-0, --print0 Use NUL characters as separators (character code 0)
-b, --base <base> Use this commit instead of the default branch
-e, --exist Filter files that still exist
-e, --exist Show only the files that still exist
-h, --help Print help
"""
Loading