Skip to content

Instantly share code, notes, and snippets.

@Cynnexis
Last active August 4, 2021 14:25
Show Gist options
  • Select an option

  • Save Cynnexis/b79ea3a883073711b342273b646acf3c to your computer and use it in GitHub Desktop.

Select an option

Save Cynnexis/b79ea3a883073711b342273b646acf3c to your computer and use it in GitHub Desktop.
Git post-commit hook for making Shell script in project executable. This is for Windows users using git.

post-commit dos2unix

Put the post-commit file under .git/hooks/ of your project, and make sure it is well-formatted (LF) and it has the correct permissions.

If you want to automate its creation from a Makefile, use the following rule:

.PHONY: help configure-git configure

# [...]

help:
	# [...]
	@echo "  configure         - Configure the project folder."
	@echo "  configure-git     - Configure the project folder for git usage. Use 'configure' for global configuration of the project."
	# [...]

.git/hooks/post-commit:
	@if command -v "dos2unix" > /dev/null 2>&1; then \
		curl -fsSL "https://gist.githubusercontent.com/Cynnexis/b79ea3a883073711b342273b646acf3c/raw/post-commit" -o ".git/hooks/post-commit"; \
		dos2unix ".git/hooks/post-commit"; \
	else \
		touch ".git/hooks/post-commit"; \
	fi

configure-git: .git/hooks/post-commit

configure: configure-git

# [...]
#!/bin/bash
# This post-commit hook is made for Windows users using Git-for-Windows. Git on
# Windows may reformat your files to be MS-DOS-like, depending on your git
# configuration, in order to be properly committed. However, this approach
# as the inconvenience to reformat Shell files, making them unusable because
# of their CRLF end-of-lines and the loss of their executable flag.
# This post-commit will reformat the Shell scripts back to their original state
# in order to be executable again.
# Note that the program 'dos2unix' needs to be installed on your machine in
# order to work (in your WSL for instance, or Cygwin), and bear in mind that
# ALL your Shell scripts in your project will be executable.
find . -type f \( -iname '*.sh' -o -iname '*.bash' -o -name 'Makefile' \) -exec bash -c 'dos2unix --keepdate {} && chmod a+x {}' \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment