Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pip install -r requirements.txt on Git Pull?

Tags:

git

python

pip

I've got package management for Python going through pip right now, but I keep having people on my team forget to redo pip install -r requirements.txt when rerunning our code base and then getting confused about why stuff doesn't work. That said, is there any way I can tie that command into something else that they always have to do anyway, like git pull so that it's done for them and they don't need to think about it?

like image 852
Eli Avatar asked Oct 10 '12 18:10

Eli


2 Answers

You could use a shell script in the git post-merge hook, which will execute whenever a merge takes place. Just be aware that this hook will execute on every merge (all pulls that actually do something are a merge, but not all merges are pulls).

I don't know if its possible to automatically install a client side hook when a repository is cloned, but you should at least be able to get your post-merge hook installed once instead of getting your team to remember to do something with every pull.

like image 146
Joe Day Avatar answered Sep 29 '22 10:09

Joe Day


A post-merge hook should work fine here. Open the file .git/hooks/post-merge in your project's .git directory. Paste the following code snippet in the file.

#/usr/bin/env bash
changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"

check_and_run() {
       echo "$changed_files" | grep --quiet "$1" && eval "$2"
}

check_and_run requirements.txt  "/path/to/virtualenv/bin/pip install -r /path/to/requirements.txt"

Run chmod +x post-merge to make the file executable.

Thats it. Now every time when you do git merge and there is a change in requirements.txt file pip install will run automatically.

like image 35
avichalp Avatar answered Sep 29 '22 11:09

avichalp