I am trying to execute a pre commit git hook in python to check if files have line lengths less than 80 chars. However i get a no such file/directory error. i am on fedora and have set the #!usr/bin/python.help would be appreciated
#!/usr/bin/env python
#-*- mode: python -*-
from subprocess import Popen, PIPE
import sys
def run(command):
p = Popen(command.split(), stdout=PIPE, stderr=PIPE)
p.wait()
return p.returncode, p.stdout.read().strip().split(), p.stderr.read()
def precommit():
_, files_modified, _= run("git diff-index --name-only HEAD")
i=1
for fname in files_modified:
file = open(fname)
while i==1:
line = file.readline()
if not line:
break
elif len(line)>80:
print("Commit failed: Line greater than 80 characters")
return 1
return 0
sys.exit(precommit())
#Solution 1:Delete the . git/hook folder and then do the npm install for reinstall husky. There are chances for conflicts with husky-generated files and . git/hook/ files.
The pre-commit hook is run first, before you even type in a commit message. It's used to inspect the snapshot that's about to be committed, to see if you've forgotten something, to make sure tests run, or to examine whatever you need to inspect in the code.
Use the --no-verify option to skip git commit hooks, e.g. git commit -m "commit message" --no-verify . When the --no-verify option is used, the pre-commit and commit-msg hooks are bypassed.
Open a terminal window by using option + T in GitKraken Client. Once the terminal windows is open, change directory to . git/hooks . Then use the command chmod +x pre-commit to make the pre-commit file executable.
Your pre-commit file has extraneous carriage returns in it. This can happen if you edit the file in Windows and copy the file to a Linux computer.
Try these commands:
cp .git/hooks/pre-commit /tmp/pre-commit
tr -d '\r' < /tmp/pre-commit > .git/hooks/pre-commit
And then rerun your git
command.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With