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
54 changes: 54 additions & 0 deletions .github/workflows/clippy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Clippy Test

on:
pull_request:


jobs:
git-diff:
runs-on: ubuntu-latest
name: 'Git Diff - Powered by Rust'
outputs:
DIFF_FILES: ${{ steps.git-diff.outputs.DIFF_FILES }}
DIFF_COUNT: ${{ steps.git-diff.outputs.DIFF_COUNT }}
steps:
- uses: actions/checkout@v4
- uses: LuisLiraC/[email protected]
id: git-diff
with:
patterns: '*.rs'

clippy-test:
runs-on: ubuntu-latest
needs: [git-diff]
if: ${{ needs.git-diff.outputs.DIFF_COUNT != 0 }}
name: Run Tests
steps:
- uses: actions/checkout@v4

- name: Cache cargo registry
uses: actions/cache@v4
with:
path: ~/.cargo/registry
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-registry-
- name: Cache cargo index
uses: actions/cache@v4
with:
path: ~/.cargo/git
key: ${{ runner.os }}-cargo-index-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-index-
- name: Cache cargo build
uses: actions/cache@v4
with:
path: target
key: ${{ runner.os }}-cargo-build-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-build-
- name: Run Tests
run: cargo clippy --color always -- -Dwarnings -W clippy::pedantic
2 changes: 1 addition & 1 deletion .github/workflows/prerelease.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
${{ runner.os }}-cargo-build-
- name: Build
run: cargo build --release
run: cargo build --release --color always

- name: Create Archive Folder
run: mkdir ${{ runner.os }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
${{ runner.os }}-cargo-build-
- name: Build
run: cargo build --release
run: cargo build --release --color always

- name: Create Archive Folder
run: mkdir ${{ runner.os }}
Expand Down
17 changes: 16 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,23 @@ on:


jobs:
git-diff:
runs-on: ubuntu-latest
name: 'Git Diff - Powered by Rust'
outputs:
DIFF_FILES: ${{ steps.git-diff.outputs.DIFF_FILES }}
DIFF_COUNT: ${{ steps.git-diff.outputs.DIFF_COUNT }}
steps:
- uses: actions/checkout@v4
- uses: LuisLiraC/[email protected]
id: git-diff
with:
patterns: '*.rs'

test:
runs-on: ubuntu-latest
needs: [git-diff]
if: ${{ needs.git-diff.outputs.DIFF_COUNT != 0 }}
name: Run Tests
steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -36,4 +51,4 @@ jobs:
${{ runner.os }}-cargo-build-
- name: Run Tests
run: cargo test
run: cargo test --color always
2 changes: 1 addition & 1 deletion action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ runs:
- name: Get Binary
shell: bash
run: |
BIN_URL=$(curl https://api.github.com/repos/LuisLiraC/git-diff/releases/tags/v1.0.2 | jq -r '.assets[0].browser_download_url')
BIN_URL=$(curl https://api.github.com/repos/LuisLiraC/git-diff/releases/tags/v1.0.3 | jq -r '.assets[0].browser_download_url')
curl -s -L $BIN_URL -o rust-binary.tgz
tar -xzvf rust-binary.tgz
mv ./Linux/git-diff ./git-diff
Expand Down
58 changes: 29 additions & 29 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ fn main() {
let args: Vec<String> = env::args().collect();
let mut patterns_filters: Vec<PatternFilter> = Vec::new();

for arg in args.iter() {
for arg in &args {
if arg.starts_with("--patterns=") {
patterns_filters = create_patterns_filters(&arg);
patterns_filters = create_patterns_filters(arg);
}
}

Expand All @@ -26,34 +26,34 @@ fn main() {
return;
}

let (include_patterns_filters, exclude_patterns_filters) = categorize_filters(patterns_filters);
let (include_patterns_filters, exclude_patterns_filters) = categorize_filters(&patterns_filters);

let start = Instant::now();
let changed_files = get_changed_files();
let duration = start.elapsed();
println!("Getting changed files done in: {:?}", duration);
println!("Getting changed files done in: {duration:?}");

println!("Changed files: {:?}", changed_files);
println!("Changed files: {changed_files:?}");

let start = Instant::now();
let filtered_files = filter_files(changed_files, include_patterns_filters, exclude_patterns_filters);
let filtered_files = filter_files(&changed_files, &include_patterns_filters, &exclude_patterns_filters);
let duration = start.elapsed();
println!("Filtering files done in: {:?}", duration);
println!("Filtering files done in: {duration:?}");

let count = get_count(filtered_files.clone());
let count = get_count(&filtered_files);

println!("Filtered files: {:?}", filtered_files);
println!("Count: {}", count);
println!("Filtered files: {filtered_files:?}");
println!("Count: {count}");

Command::new("sh")
.arg("-c")
.arg(format!("echo \"DIFF_FILES={:?}\" >> $GITHUB_OUTPUT", filtered_files))
.arg(format!("echo \"DIFF_FILES={filtered_files:?}\" >> $GITHUB_OUTPUT"))
.output()
.expect("Failed to execute DIFF_FILES command");

Command::new("sh")
.arg("-c")
.arg(format!("echo \"DIFF_COUNT={}\" >> $GITHUB_OUTPUT", count))
.arg(format!("echo \"DIFF_COUNT={count}\" >> $GITHUB_OUTPUT"))
.output()
.expect("Failed to execute DIFF_COUNT command");
}
Expand All @@ -63,9 +63,9 @@ fn create_patterns_filters(arg: &str) -> Vec<PatternFilter> {
.split('=')
.last()
.expect("Failed to get patterns")
.replace(" ", "")
.replace("\n", ",")
.replace("\r", "")
.replace(' ', "")
.replace('\n', ",")
.replace('\r', "")
.replace(",,", ",")
.trim_end_matches(',')
.to_string();
Expand All @@ -76,12 +76,12 @@ fn create_patterns_filters(arg: &str) -> Vec<PatternFilter> {

let mut patterns_filters: Vec<PatternFilter> = Vec::new();

for pattern in patterns.iter() {
for pattern in &patterns {
let exclude = pattern.starts_with('!');
let pattern = if exclude {
pattern[1..].to_string()
} else {
pattern.to_string()
(*pattern).to_string()
};

patterns_filters.push(PatternFilter {
Expand All @@ -104,11 +104,11 @@ fn get_changed_files() -> Vec<String> {

Command::new("sh")
.arg("-c")
.arg(format!("git fetch origin {}", base_ref_env))
.arg(format!("git fetch origin {base_ref_env}"))
.output()
.expect("Failed to execute fetch branch command");

let base_ref_string = format!("refs/remotes/origin/{}", base_ref_env);
let base_ref_string = format!("refs/remotes/origin/{base_ref_env}");
let base_ref = repository.find_reference(&base_ref_string).expect("Failed to find default branch");
let base_commit = base_ref.peel_to_commit().expect("Failed to peel default branch to commit");

Expand All @@ -134,41 +134,41 @@ fn get_changed_files() -> Vec<String> {
changed_files
}

fn filter_files(changed_files: Vec<String>, include_patterns_filters: HashSet<String>, exclude_patterns_filters: HashSet<String>) -> HashSet<String> {
fn filter_files(changed_files: &Vec<String>, include_patterns_filters: &HashSet<String>, exclude_patterns_filters: &HashSet<String>) -> HashSet<String> {
let mut hash_set_filtered_files = HashSet::new();

for changed_file in changed_files.iter() {
include_patterns_filters.iter().for_each(|pattern| {
for changed_file in changed_files {
for pattern in include_patterns_filters {
if Pattern::new(pattern).expect("Failed to create pattern").matches(changed_file) {
hash_set_filtered_files.insert(changed_file.to_string());
}

exclude_patterns_filters.iter().for_each(|pattern| {
for pattern in exclude_patterns_filters {
if Pattern::new(pattern).expect("Failed to create pattern").matches(changed_file) {
hash_set_filtered_files.remove(changed_file);
}
});
});
}
}
}

hash_set_filtered_files
}

fn get_count(filtered_files: HashSet<String>) -> usize {
fn get_count(filtered_files: &HashSet<String>) -> usize {
filtered_files.len()
}

fn categorize_filters(filters: Vec<PatternFilter>) -> (HashSet<String>, HashSet<String>) {
fn categorize_filters(filters: &Vec<PatternFilter>) -> (HashSet<String>, HashSet<String>) {
let mut exclude_patterns_filters: HashSet<String> = HashSet::new();
let mut include_patterns_filters: HashSet<String> = HashSet::new();

filters.iter().for_each(|pattern_filter| {
for pattern_filter in filters {
if pattern_filter.exclude {
exclude_patterns_filters.insert(pattern_filter.clone().pattern);
} else {
include_patterns_filters.insert(pattern_filter.clone().pattern);
}
});
}

(include_patterns_filters, exclude_patterns_filters)
}
Expand Down
39 changes: 18 additions & 21 deletions src/tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
use crate::*;

#[cfg(test)]
mod integration {
use super::*;

#[test]
fn test_filter() {
let arg = "--patterns=*.rs,!*..txt";
let files = vec![
String::from("src/main.rs"),
String::from("lib.rs"),
String::from("test.txt"),
];
#[test]
fn test_filter() {
let arg = "--patterns=*.rs,!*..txt";
let files = vec![
String::from("src/main.rs"),
String::from("lib.rs"),
String::from("test.txt"),
];

let filters = create_patterns_filters(arg);
let filters = create_patterns_filters(arg);

let (include_patterns_filters, exclude_patterns_filters) = categorize_filters(filters);
let (include_patterns_filters, exclude_patterns_filters) = categorize_filters(&filters);

let filtered_files = filter_files(files, include_patterns_filters, exclude_patterns_filters);
let filtered_files = filter_files(&files, &include_patterns_filters, &exclude_patterns_filters);

let count = get_count(filtered_files.clone());
let count = get_count(&filtered_files);

let expected_filtered_files = HashSet::from([
String::from("src/main.rs"),
String::from("lib.rs"),
]);
let expected_filtered_files = HashSet::from([
String::from("src/main.rs"),
String::from("lib.rs"),
]);

assert_eq!(filtered_files, expected_filtered_files);
assert_eq!(count, 2);
}
assert_eq!(filtered_files, expected_filtered_files);
assert_eq!(count, 2);
}
Loading