Skip to content

Git hooks strip ANSI sequences #4308

Open
@clemyan

Description

@clemyan
  • I was not able to find an open or closed issue matching what I'm seeing

Setup

  • Which version of Git for Windows are you using? Is it 32-bit or 64-bit?
# Tested with both released version on winget, and portable version of latest snapshot
$ git --version --build-options

git version 2.39.2.windows.1
cpu: x86_64
built from commit: a82fa99b36ddfd643e61ed45e52abe314687df67
sizeof-long: 4
sizeof-size_t: 8
shell-path: /bin/sh
feature: fsmonitor--daemon

$ git --version --build-options

git version 2.39.1.windows.1.536.g49a8ec9dac.20230214182258
cpu: x86_64
built from commit: 49a8ec9dac3cec6602f05fed1b3f80a549c8c05c
sizeof-long: 4
sizeof-size_t: 8
shell-path: /bin/sh
feature: fsmonitor--daemon
  • Which version of Windows are you running? Windows 10 Home 22H2 Build 19045.2604 (64-bit)
$ cmd.exe /c ver

Microsoft Windows [Version 10.0.19045.2604]
  • What options did you set as part of the installation? Or did you choose the
    defaults?
# One of the following:
> type "C:\Program Files\Git\etc\install-options.txt"
> type "C:\Program Files (x86)\Git\etc\install-options.txt"
> type "%USERPROFILE%\AppData\Local\Programs\Git\etc\install-options.txt"
> type "$env:USERPROFILE\AppData\Local\Programs\Git\etc\install-options.txt"
$ cat /etc/install-options.txt

Editor Option: VIM
Custom Editor Path:
Default Branch Option:
Path Option: Cmd
SSH Option: OpenSSH
Tortoise Option: false
CURL Option: OpenSSL
CRLF Option: CRLFAlways
Bash Terminal Option: MinTTY
Git Pull Behavior Option: Merge
Use Credential Manager: Enabled
Performance Tweaks FSCache: Enabled
Enable Symlinks: Disabled
Enable Pseudo Console Support: Disabled
Enable FSMonitor: Disabled

Details

  • Which terminal/shell are you running Git from? e.g Bash/CMD/PowerShell/other

Issue exists when using CMD (10.0.19045.2604) and PowerShell (7.3.2), but does not exist when using git-bash.

$ git init
$ nano .git/hooks/pre-commit # Create pre-commit hook
$ git commit --allow-empty

.git/hooks/pre-commit

#!/bin/sh
printf 'ANSI NOT \x1B[4DWORKING PROPERLY\n'
exit 1
  • What did you expect to occur after running these commands?

Prints ANSI WORKING PROPERLY

  • What actually happened instead?

Prints ANSI NOT WORKING PROPERLY

image


Investigation

ANSI sequences are not stripped if stdout is redirected to /dev/tty or if output is redirected to stderr

#!/bin/sh
exec > /dev/tty
printf 'ANSI NOT \x1B[4DWORKING PROPERLY\n'
exit 1
# ANSI WORKING PROPERLY
#!/bin/sh
printf 'ANSI NOT \x1B[4DWORKING PROPERLY\n' 1>&2
exit 1
# ANSI WORKING PROPERLY

So probably related to #2914?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions