Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Want to setup a hook that copies committed files to a particular folder

Background: Developing a Facebook app using PHP Laravel framework & MySQL for database.

I have setup Gitlab on our development server and created a repository on it where the team has been added and is committing & pushing code.

What I would like to do is, when I push code to a particular branch on GitLab (for example Master) I would like it to be available at /var/www/productname so that I can test it within the Facebook canvas as well (certain things happen there that can't be tested on your local machine).

However, I don't want the hook to copy all the files over every time a push happens on master, just the files that were modified.

Can anyone help out with such a hook?

Thank you.

like image 899
Kopty Avatar asked Apr 18 '13 07:04

Kopty


People also ask

How do you make a commit hook?

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.

What is a commit hook?

The commit-msg hook is much like the prepare-commit-msg hook, but it's called after the user enters a commit message. This is an appropriate place to warn developers that their message doesn't adhere to your team's standards. The only argument passed to this hook is the name of the file that contains the message.

What is a pre-commit hook?

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.


1 Answers

You would need to add to the bare repo (managed by GitLab) a post-receive hook which would:

  • maintain a working tree (git checkout -f master)
  • copy the files you want from that working

That would be:

cd ~git/repositories/yourRepo.git/hooks
touch post-receive
chmod +x post-receive

You can make sure that hook will only be active if someone pushes on branch master:

#!/bin/bash
while read oldrev newrev refname
do
    branch=$(git rev-parse --symbolic --abbrev-ref $refname)
    if [ "master" == "$branch" ]; then
        # Do something
    fi
done

For the checkout done by that hook, see "GIT post-receive checkout without root folder", that is:
make sure you specify --git-dir and --git-work-tree:

git --git-dir=/path/to/project_root.git --work-tree=/path/to/your/workingtree checkout -f

Again, /path/to/your/workingtree can be:

  • only an intermediate working tree for you to extract from it the relevant files you want to copy.
  • or the path to your app, if you want all the files from master to be updated in the destination directory of your wab app.
like image 52
VonC Avatar answered Oct 21 '22 10:10

VonC