Skip to content

Commit

Permalink
Use arguments for to pass staged filenames to pre-commit task
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbode committed Dec 12, 2024
1 parent 59b6788 commit 1d61aa4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/cli/generate/git-pre-commit.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
This command generates a git pre-commit hook that runs a mise task like `mise run pre-commit`
when you commit changes to your repository.

Staged files are passed to the task as `STAGED`.
Staged files are passed to the task via appended arguments

## Flags

Expand Down
2 changes: 1 addition & 1 deletion mise.usage.kdl
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ cmd "generate" subcommand_required=true help="[experimental] Generate files for
This command generates a git pre-commit hook that runs a mise task like `mise run pre-commit`
when you commit changes to your repository.

Staged files are passed to the task as `STAGED`."
Staged files are passed to the task via appended arguments."
after_long_help r#"Examples:

$ mise generate git-pre-commit --write --task=pre-commit
Expand Down
23 changes: 18 additions & 5 deletions src/cli/generate/git_pre_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::git::Git;
/// This command generates a git pre-commit hook that runs a mise task like `mise run pre-commit`
/// when you commit changes to your repository.
///
/// Staged files are passed to the task as `STAGED`.
/// Staged files are passed to the task via appended arguments
#[derive(Debug, clap::Args)]
#[clap(verbatim_doc_comment, visible_alias = "pre-commit", after_long_help = AFTER_LONG_HELP)]
pub struct GitPreCommit {
Expand Down Expand Up @@ -51,10 +51,23 @@ impl GitPreCommit {
fn generate(&self) -> String {
let task = &self.task;
format!(
r#"#!/bin/sh
STAGED="$(git diff-index --cached --name-only HEAD | tr ' ' '\ ' | tr '\n' ' ' | xargs)"
export STAGED
exec mise run {task}
r#"#! /bin/sh
set -eu
PIPE=$(mktemp -u "mise.{task}.XXXXXXXX")
mkfifo -m 600 "${{PIPE}}"
cleanup() {{
rm -f "${{PIPE}}"
}}
trap 'cleanup' EXIT INT TERM
git diff-index --cached --name-only HEAD > "${{PIPE}}" &
while read -r ARG; do
set -- "$@" "${{ARG}}"
done < "${{PIPE}}"
exec mise run "{task}" "$@"
"#
)
}
Expand Down

0 comments on commit 1d61aa4

Please sign in to comment.