Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Git hook automatically add files to the commit?

I'd like to add an automatically generated file to the same commit using a pre- or post-commit hook in Git, dependent on the files that were modified in that commit. How would I go about this?

I've tried this as a pre-commit hook, but no luck:

#!/bin/sh files=`git diff --cached --name-status` re="<files of importance>" if [[ $files =~ $re ]] then   echo "Creating files"   exec bundle exec create_my_files   exec git add my_files   exec git commit --amend -C HEAD fi 

This successfully adds them to the repository, but does not add them to the commit. I've also tried using the last two exec lines in a post-commit hook along with the pre-commit inspection, but no good either.

like image 491
Ian Terrell Avatar asked Jul 19 '10 19:07

Ian Terrell


People also ask

Does git commit add new files?

To add and commit files to a Git repositoryEnter git commit -m '<commit_message>' at the command line to commit new files/changes to the local repository.

Can you git commit without adding?

Without adding any files, the command git commit won't work. Git only looks to the staging area to find out what to commit. Staging, or adding, files, is possible through the command line, and also possible with most Git interfaces like GitHub Desktop by selecting the lines or files that you'd like to stage.

How do git hooks work?

Git hooks are scripts that run automatically every time a particular event occurs in a Git repository. They let you customize Git's internal behavior and trigger customizable actions at key points in the development life cycle.


2 Answers

Since git add was also not working for me in a pre commit, I followed mark's idea of using a .commit file and splitting the process into pre- and post-commit.

Here is some code that should be easy to understand

In the pre-commit:

  • Touch a file .commit or something. (be sure to add this to .gitignore)
#!/bin/sh  echo  touch .commit  exit 

In the post-commit:

if .commit exists you know a commit has just taken place but a post-commit hasn't run yet. So, you can do your code generation here. Additionally, test for .commit and if it exists:

  • add the files
  • commit --amend -C HEAD --no-verify (avoid looping)
  • delete .commit file
#!/bin/sh echo if [ -e .commit ]     then     rm .commit     git add yourfile     git commit --amend -C HEAD --no-verify fi exit 

Hope this makes it easier for people with few bash knowledge to follow mark's idea.

like image 168
bitluck Avatar answered Oct 12 '22 03:10

bitluck


It's possible to do what you want using pre-commit hooks. We do something similar for a heroku deployment (compiling coffeescript to javascript). The reason your script isn't working is because you used the exec command improperly.

From the man page:

The exec builtin is used to replace the currently running shells process image with a new command. On successful completion, exec never returns. exec can not be used inside a pipeline.

Only your first exec command is running. After that your script is basically terminated.

Give something like this a try (as a pre-commit hook):

#!/bin/sh files=`git diff --cached --name-status` re="<files of importance>" if [[ $files =~ $re ]] then   echo "Creating files"   bundle exec create_my_files   git add my_files fi 
like image 28
Jim Garvin Avatar answered Oct 12 '22 03:10

Jim Garvin