Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In a git post-commit hook how do I get a list of the files that were changed?

Tags:

git

githooks

Trying to figure out how long time was spent in a commit in a Git post-commit hook.

I've got a post-commit git hook that submits information over an API about the commit. What I want to do is figure out how long time was spent on the commit. Roughly.

My assumption is that a rough value can be figured out by finding the minimum of all the creation-time and modification-time of the files involved and compare with the maximum creation- and modification-time.

I can easily do this in a Python script. If someone tells me it was the files "foo.txt", "bar.txt" and "path/bla.txt" I can quickly do some arithmetic in a script based on these files.

So, In a git post-commit hook how do I get a list of the files that were changed?

like image 333
Peter Bengtsson Avatar asked Nov 17 '10 14:11

Peter Bengtsson


People also ask

What is post commit hook in Git?

The post-commit hook is called immediately after the commit-msg hook. It can't change the outcome of the git commit operation, so it's used primarily for notification purposes. The script takes no parameters and its exit status does not affect the commit in any way.

Where are Git commit hooks stored?

The hooks are all stored in the hooks subdirectory of the Git directory. In most projects, that's . git/hooks .


1 Answers

When writing scripts around git, you should try to stick to the plumbing commands — their format is less likely to change and easier to parse. Here is a command which outputs the names of the paths which changed in a commit:

git diff-tree -r --name-only --no-commit-id <tree-ish>

That aside, you may wish to examine the index, as it contains timestamps about when the files were staged, which might give you an extra edge; however, I don’t believe there is a way to access this information.

like image 161
Josh Lee Avatar answered Nov 11 '22 16:11

Josh Lee