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
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
4 changes: 2 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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;
});
Expand Down
11 changes: 7 additions & 4 deletions package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ./.;
Expand All @@ -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";
};
}
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
39 changes: 20 additions & 19 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -80,7 +81,7 @@ fn committed_changes(repo: &Repository, base: &str) -> Result<Vec<String>> {
.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<Commit<'repo>> {
Expand All @@ -95,34 +96,34 @@ fn find_tree(repo: &Repository, oid: Oid) -> Result<Tree<'_>> {
.with_context(|| format!("Cannot find the tree of {oid}"))
}

fn changed_path(delta: DiffDelta) -> Result<String> {
let path = delta
.new_file()
.path()
.context("A changed file has no path")?;
fn changed_path(delta: DiffDelta) -> Option<String> {
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<String> {
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<Vec<String>> {
let statuses = repo
.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 {
Expand Down
2 changes: 1 addition & 1 deletion tests/features/help.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
Loading