2

Git status gives information on the modified files in a repository (be in un-tracked or staged files) I want to be able to access these list of files -- to preform some code quality checks on those files before a commit is performed via. hooks

Do let me know if I can access this list of files.

By the way, I am new to Git. So go easy on me if I made blunders in my assumptions

Naveen Dennis
  • 1,223
  • 3
  • 24
  • 39

1 Answers1

0

That would be a pre-commit hook, like the one coming with evey git installation: templates/hooks--pre-commit.sample.

A more complex example would be the project git-pylint-commit-hook for instance, for hecking Python code quality. The hook will check files ending with .py or that has a she bang (#!) containing python.

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.

Exiting non-zero from this hook aborts the commit, although you can bypass it with git commit --no-verify.

You can do things like check for code style (run lint or something equivalent), check for trailing whitespace (the default hook does exactly that), or check for appropriate documentation on new methods.

See more examples at "Customizing Git - An Example Git-Enforced Policy", in ruby:

files_modified = `git diff-index --cached --name-only HEAD`.split("\n")
  files_modified.each do |path|
    # do your checks
  end
end
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250