Skip to content

File name extension requirement broke Git hooks #16480

Open
@anttiah

Description

@anttiah

Prerequisites

Description

Now that script files must have .ps1 file name extension in Windows, because of #15859, it's no longer possible to have Git hooks written in PowerShell. This is a breaking change!

From Git documentation at https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks (emphasis by me)

To enable a hook script, put a file in the hooks subdirectory of your .git directory that is named appropriately (without any extension) and is executable. From that point forward, it should be called. We’ll cover most of the major hook filenames here.

Git hooks in written in PowerShell have worked flawlessly since the file name exetension requirement was removed in PowerShell 6.x. After the 7.2 release, trying execute a git command that has hooks associated with it results in an error message and commit fails.

Steps to reproduce

Make sure Git is installed with full support for all commands. Meaning that paths

  • C:\Program Files\Git\cmd
  • C:\Program Files\Git\mingw64\bin
  • C:\Program Files\Git\usr\bin

are present in user or system PATH environment variable. Git for Windows installer will do this for you.

Then create a file named pre-commit with content:

#!/usr/bin/env pwsh
Write-Verbose 'Hello from pre-commit hook' -Verbose
exit 0

Note that shebang line is required here because of how Git was ported to Windows.

Make sure the file name does not have any extension and place the file into .git\hooks directory of a local git repository.

Make some changes in the repo and try to commit them. For example: git commit -m "Testing" or use VS Code's version control tab.

Expected behavior

pre-commit hook gets executed:

VERBOSE: Hello from pre-commit hook
[testing ca1faea] Testing
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt

Actual behavior

Commit fails with error message:

Git: Processing -File '.git/hooks/pre-commit' failed because the file does not have a '.ps1' extension. Specify a valid PowerShell script file name, and then try again.

Error details

No response

Environment data

Name                           Value
----                           -----
PSVersion                      7.2.0
PSEdition                      Core
GitCommitId                    7.2.0
OS                             Microsoft Windows 10.0.19043
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Metadata

Metadata

Assignees

Labels

KeepOpenThe bot will ignore these and not auto-closeNeeds-InvestigationThe behavior reported in the issue is unexpected and needs further investigation.WG-Enginecore PowerShell engine, interpreter, and runtimeWG-NeedsReviewNeeds a review by the labeled Working Group

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions