Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use arguments for to pass staged filenames to pre-commit task #3492

Merged
merged 2 commits into from
Dec 12, 2024
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
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}" "$@"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I already merged this but realized this should be exec mise run "{task}" -- "$@" though the only time that would matter is if you added a flag to the pre-commit task that had the exact string as a filename

Copy link
Contributor Author

@joshbode joshbode Dec 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah - good point, but yes... pretty unlikely - and hopefully people avoid filenames with leading dashes :)

"#
)
}
Expand Down
Loading