alejandra icon indicating copy to clipboard operation
alejandra copied to clipboard

In a git hook the screen is cleared, but nothing printed

Open NobbZ opened this issue 2 years ago • 4 comments

I use alejandra --check in a git pre-commit hook.

This clears the screen and then prints a success message, that either all files are correctly formatted already or that some files have been formatted.

It should probably not format with --check, as already reported in #251.

When using --quiet nothing is printed at all, though for a pre-commit hook or a CI check it would be nice knowing which files actually blocked the check.

NobbZ avatar Mar 22 '22 06:03 NobbZ

Can you please share the code of the pre-commit hook? so that I can understand why is that happening

Guessing: seems something related to the stderr being suppressed by the hook

kamadorueda avatar Mar 23 '22 00:03 kamadorueda

I use lefthook to manage the hooks in the repository. The relevant lefthook description is this:

pre-commit:
  parallel: true
  commands:
    formatting:
      glob: "*.nix"
      run: nix shell --inputs-from . alejandra -c alejandra --check {all_files}

Lefthook does not provide a TTY AFAIR, so that might be related, though then I am wondering why the screen is cleared.

Still, a mode that does not use TUI (especially not clears the screen!), but still prints necessary information would be more than nice.

And even with 2>&1, there is no meaningfull output. There is no summary about which files have been formatted.

NobbZ avatar Mar 23 '22 06:03 NobbZ

Awesome, now I understand

Please try this:

# lefthook.yaml
pre-commit:
  parallel: true
  commands:
    formatting:
      glob: "*.nix"
      run: alejandra --check {all_files} </dev/null

output:

[kamadorueda@nixos:/data/alejandra]$ lefthook run pre-commit
  Lefthook v0.7.7
  RUNNING HOOKS GROUP: pre-commit
  
    EXECUTE > formatting
   Formatting: 4 files
  Changed: flake.nix
  
  Success! 1 file was changed
  
  
  SUMMARY: (done in 0.01 seconds)
  🥊  formatting

[kamadorueda@nixos:/data/alejandra]$ echo $?
  1

kamadorueda avatar Mar 23 '22 18:03 kamadorueda

Thanks, this helps!

Though a direct support through a flag would be nice, "cutting" stdin this way is a bit unintuitive.

NobbZ avatar Mar 23 '22 20:03 NobbZ

I believe this is no longer a problem in the release 3.0.0. We don't clear the screen anymore, and terminal output is text-based

kamadorueda avatar Aug 15 '22 19:08 kamadorueda