diff --git a/README.md b/README.md index 0bb4a99..af5e115 100644 --- a/README.md +++ b/README.md @@ -174,9 +174,3 @@ Compare with the `development` branch and compile each changed file: ```bash $ git-changed -0 -b development '**/*.rs' | xargs -0 -n 1 rustc ``` - -## Dependencies - -- [clap](https://docs.rs/clap/latest/clap/) -- [git2](https://docs.rs/git2/latest/git2/) -- [glob](https://docs.rs/glob/latest/glob/) diff --git a/flake.nix b/flake.nix index 8832ac6..974a44d 100644 --- a/flake.nix +++ b/flake.nix @@ -1,5 +1,5 @@ { - description = "Lists files modified since branching off"; + description = "List the files that changed in the current git branch"; inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; @@ -29,7 +29,7 @@ git-changed = { type = "app"; program = nixpkgs.lib.getExe self.packages.${system}.git-changed; - meta.description = "List the files that changed since the branch point"; + meta.description = self.packages.${system}.git-changed.meta.description; }; default = git-changed; }); diff --git a/package.nix b/package.nix index f3a344d..90c6fdd 100644 --- a/package.nix +++ b/package.nix @@ -6,9 +6,12 @@ git, }: +let + cargoPackage = (lib.importTOML ./Cargo.toml).package; +in rustPlatform.buildRustPackage { - pname = "git-changed"; - version = "0.0.2"; + pname = cargoPackage.name; + inherit (cargoPackage) version; src = lib.fileset.toSource { root = ./.; @@ -31,8 +34,8 @@ rustPlatform.buildRustPackage { nativeCheckInputs = [ git ]; meta = { - description = "Lists files modified since branching off"; - homepage = "https://github.com/function-artisans/git-changed"; + inherit (cargoPackage) description; + homepage = cargoPackage.repository; mainProgram = "git-changed"; }; } diff --git a/src/cli.rs b/src/cli.rs index f87ab70..f8527bf 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -18,7 +18,7 @@ pub fn get_options() -> Options { fn command() -> Command { Command::new("git-changed") - .about("Lists files modified since branching off") + .about(env!("CARGO_PKG_DESCRIPTION")) .args([ Arg::new("null") .short('0') diff --git a/src/git.rs b/src/git.rs index df5870e..665adc3 100644 --- a/src/git.rs +++ b/src/git.rs @@ -10,6 +10,7 @@ use git2::Tree; use std::collections::BTreeSet; use std::path::Path; use std::path::PathBuf; +use std::str; const CONFIG_PATH: &str = "changed.defaultBranch"; const DEFAULT_BRANCH: &str = "origin/main"; @@ -80,7 +81,7 @@ fn committed_changes(repo: &Repository, base: &str) -> Result> { .diff_tree_to_tree(Some(&old_tree), Some(&new_tree), None) .with_context(|| format!("Cannot compare {base} with HEAD"))?; - diff.deltas().map(changed_path).collect() + Ok(diff.deltas().filter_map(changed_path).collect()) } fn find_commit<'repo>(repo: &'repo Repository, revision: &str) -> Result> { @@ -95,17 +96,21 @@ fn find_tree(repo: &Repository, oid: Oid) -> Result> { .with_context(|| format!("Cannot find the tree of {oid}")) } -fn changed_path(delta: DiffDelta) -> Result { - let path = delta - .new_file() - .path() - .context("A changed file has no path")?; +fn changed_path(delta: DiffDelta) -> Option { + delta.new_file().path_bytes().and_then(utf8_path) +} - let text = path - .to_str() - .with_context(|| format!("The path {} is not UTF-8", path.display()))?; +// A path that is not UTF-8 gets a warning, so the other paths still go to the output. +fn utf8_path(bytes: &[u8]) -> Option { + match str::from_utf8(bytes) { + Ok(path) => Some(path.to_string()), + Err(_) => { + let path = String::from_utf8_lossy(bytes); + eprintln!("Skip the path {path}, because it is not UTF-8"); - Ok(text.to_string()) + None + } + } } fn uncommitted_changes(repo: &Repository) -> Result> { @@ -113,16 +118,12 @@ fn uncommitted_changes(repo: &Repository) -> Result> { .statuses(Some(&mut status_options())) .context("Cannot read the status of the working directory")?; - statuses + let paths = statuses .iter() - .map(|entry| { - let path = entry - .path() - .context("A changed file has a path that is not UTF-8")?; - - Ok(path.to_string()) - }) - .collect() + .filter_map(|entry| utf8_path(entry.path_bytes())) + .collect(); + + Ok(paths) } fn status_options() -> StatusOptions { diff --git a/tests/features/help.feature b/tests/features/help.feature index 5aeb962..2c57090 100644 --- a/tests/features/help.feature +++ b/tests/features/help.feature @@ -5,7 +5,7 @@ Feature: Help When the user executes the tool like: "git-changed -h" Then it succeeds with the following output """ - Lists files modified since branching off + List the files that changed in the current git branch Usage: git-changed [OPTIONS] [glob]