Skip to content

Commit b89dbc3

Browse files
authored
Merge pull request #9 from raszi/fix/audit-findings
Fix the glob match and document a remote base branch
2 parents 6e81394 + 9476936 commit b89dbc3

4 files changed

Lines changed: 28 additions & 11 deletions

File tree

‎README.org‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ Or only for the current repository:
4343
$ git config changed.defaultBranch development
4444
#+end_src
4545

46+
The value can be any revision. If your local ~main~ is older than the remote one, the tool also lists the files that changed on ~main~ after your local copy. To compare with the remote branch, set:
47+
48+
#+begin_src bash
49+
$ git config changed.defaultBranch origin/main
50+
#+end_src
51+
4652
*** Filtering files with glob pattern
4753

4854
#+begin_src bash

‎src/git.rs‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use anyhow::Context;
22
use anyhow::Result;
3+
use git2::Commit;
34
use git2::DiffDelta;
45
use git2::ErrorCode;
56
use git2::Oid;
@@ -70,15 +71,15 @@ fn default_branch(repo: &Repository) -> Result<String> {
7071
}
7172

7273
fn committed_changes(repo: &Repository, base: &str) -> Result<Vec<String>> {
73-
let base_oid = find_commit_oid(repo, base)?;
74-
let head_oid = find_commit_oid(repo, "HEAD")?;
74+
let base_commit = find_commit(repo, base)?;
75+
let head_commit = find_commit(repo, "HEAD")?;
7576

7677
let merge_base_oid = repo
77-
.merge_base(base_oid, head_oid)
78+
.merge_base(base_commit.id(), head_commit.id())
7879
.with_context(|| format!("Cannot find a merge base of {base} and HEAD"))?;
7980

8081
let old_tree = find_tree(repo, merge_base_oid)?;
81-
let new_tree = find_tree(repo, head_oid)?;
82+
let new_tree = head_commit.tree().context("Cannot find the tree of HEAD")?;
8283

8384
let diff = repo
8485
.diff_tree_to_tree(Some(&old_tree), Some(&new_tree), None)
@@ -87,13 +88,10 @@ fn committed_changes(repo: &Repository, base: &str) -> Result<Vec<String>> {
8788
diff.deltas().map(changed_path).collect()
8889
}
8990

90-
fn find_commit_oid(repo: &Repository, revision: &str) -> Result<Oid> {
91-
let commit = repo
92-
.revparse_single(revision)
91+
fn find_commit<'repo>(repo: &'repo Repository, revision: &str) -> Result<Commit<'repo>> {
92+
repo.revparse_single(revision)
9393
.and_then(|object| object.peel_to_commit())
94-
.with_context(|| format!("Cannot find the commit of {revision}"))?;
95-
96-
Ok(commit.id())
94+
.with_context(|| format!("Cannot find the commit of {revision}"))
9795
}
9896

9997
fn find_tree(repo: &Repository, oid: Oid) -> Result<Tree<'_>> {

‎src/main.rs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use self::cli::Options;
22
use anyhow::Context;
33
use anyhow::Result;
4+
use glob::MatchOptions;
45
use std::env;
56
use std::io;
67
use std::io::Write;
@@ -12,6 +13,12 @@ use std::process;
1213
mod cli;
1314
mod git;
1415

16+
const SHELL_MATCH_OPTIONS: MatchOptions = MatchOptions {
17+
case_sensitive: true,
18+
require_literal_separator: true,
19+
require_literal_leading_dot: false,
20+
};
21+
1522
fn main() {
1623
let options = cli::get_options();
1724

@@ -62,7 +69,7 @@ fn relative_path(file: &Path, directory: &Path) -> PathBuf {
6269
}
6370

6471
fn is_selected(file: &Path, options: &Options) -> bool {
65-
if !options.glob.matches_path(file) {
72+
if !options.glob.matches_path_with(file, SHELL_MATCH_OPTIONS) {
6673
return false;
6774
}
6875

‎tests/features/subdirectory.feature‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ Feature: Run the tool in a subdirectory
2525
main.rs
2626
"""
2727

28+
Scenario: It does not match a file in another directory with a star
29+
When the user executes the tool in "src" like: "git-changed *.md"
30+
Then it succeeds with the following output
31+
"""
32+
"""
33+
2834
Scenario: It finds the existing files from the subdirectory
2935
When the user executes the tool in "src" like: "git-changed -e ../*.md"
3036
Then it succeeds with the following output

0 commit comments

Comments
 (0)