Ibolit
Ibolit

Reputation: 9730

How do I get added/changed files in a pre-commit hook?

I want to write a hook that checks certain coding conventions in files being committed, and if those are violated, rejects the commit/push. However, in my "rejection message" I want to tell the committer the line numbers, where the violations have been found.

The best I can think of so far is to get the whole file being committed, that will allow me to count the lines. That will also allow me to find the same coding convention violations in earlier code, so that the programmers will have to clean up the whole file they happened to modify.

Is it at all possible? How can I get the whole new file being committed.

Upvotes: 1

Views: 339

Answers (1)

Nevik Rehnel
Nevik Rehnel

Reputation: 52055

You can get the content of any Git object with git cat-file (you can also use it to find out the type of the object first, just to be sure).

As you have pointed out in the comments to your question, reading a file from the filesystem directly does not always work because it might not be fully staged, or it might have changed since being staged.

Upvotes: 1

Related Questions