How to test pre/post commit scripts in git? - python

I've created a git post-commit hook in python and made a file executable and placed it into .git/post-commit.
It works when I run it via Python. Now I want test it locally in git. How can I do that? I can't make a commit to git locally from my computer in an easy way, right?
Should I copy the hook file to my server and make test commits to test it? Is there another way?

I can't make a commit to git locally from my computer in an easy way, right?
You need to differentiate between pushing a branch to a server, which may require some configuration, and simply creating a commit, which can be as easy as running a command:
$ git commit --allow-empty -m "Testing my hook"

pre-commit and post-commit hooks are run locally on commit (see https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks ), not on push (push is what transfers your local commits to the server).
To test a hook I would create a test branch, and make several test commits there with whatever content you want. After the hook is ready, you can delete that branch. Until you do a push, everything is local.
Also if you output something from the hook script, that will be visible in the terminal when you do a commit. This is useful for debugging.
For executing the code on the server you should use "Server-Side Hooks" (see at the bottom of this page, also documented here).

Related

hook script in git is not working properly even after reverted the changes

I used below pre-receive script for blocking the developers from committing the secrets insides the files.
followed the below steps to set up this in GitHub Enterprise Appliance.
1) created a repo in github, cloned to local desktop. saved the copied script (https://github.com/github/platform-samples/blob/master/pre-receive-hooks/block_confidentials.sh ) from the site as .sh file and committed and pushed.
2) in GitHub as site admin , created a hook from admincenter and pointed to previously created hookscriptone.
and after that i tested a scenario wit creating a new repo and adding a example.conf file to this.
1) When i did first commit with the simple content (without any secrets), its allowed me to commit .
2) in second scenario, i committed with a keyword "CONFIDENTIAL" in it, but it blocked as expected.
3) so for confirming the hooks functionality, i removed the "Confidential" word and tried to commit back.. but its still not allowing me to commit.
Any helps pls...

Git pull failed to my web server

Locally (Cloud9), I have commited my changes to my repo (https://github.com/edward408/my-first-blog), however when i perform a git pull on my deployment server (PythonAnywhere) i get the following error:
Updating aef1181..5d68bfa
error: Your local changes to the following files would be overwritten by merge:
db.sqlite3
Please, commit your changes or stash them before you can merge.
Aborting
I have pushed the changes from my local console (Cloud9) and made sure by veryfing git status afterwards. It seems that the best path long-term is to updatede git on PythonAnywhere while updating it from my local git at the same time. However, how would I implement that without starting a new repo again? Id prefer not to change anything that I already have on PythonAnywhere.
Anywho, what would the best feasible solution at this point in time?
EDITED: Its not completely the same as the suggested link. I already have my local dev environment pushing updates to the project repo. To use git stash on PythonAnywhere, i would have to push from there as well. Unless this means executing stash locally?
All that means is db.sqlite3 has been modified on your production server. Because of this, git isn't sure whether it is ok to overwrite it with your new code (because that file is present in your repo). Really, you should just add that file to your .gitignore, but you can get around it with (on your production server) running:
git stash
# do your pull
git stash pop
Basically, stash "saves" modified files that haven't been committed and allows you to later restore it by popping it off again, after the merge has overwritten it. https://git-scm.com/book/en/v1/Git-Tools-Stashing

crontab automated git pull

I have a server deployed. I am writing a crontab task. It uses a python script that need to check if there is new push into the repository. If it finds a new push, it will pull and update the server code and should restart the server.
My problem is how to make the python script know if there is new commit into the repository?
I know you can use
git rev-list deployment..origin/deployment
to check if there is any commit is available on the remote server.
But how to implement in the python script and make it decide to know that it need to pull?
Thanks
Nick
You will need to contact the server in any case, so you might as well pull.
If you are on the current branch when you pull, and want to detect whether the current branch has changed, you can always do (note: written in shell)
BEFORE=$(git rev-parse HEAD)
# git pull here
AFTER=$(git rev-parse HEAD)
# Changes if $BEFORE is different from $AFTER
fge is correct. You cannot see if someone else pushed without first either doing a git pull origin or a git fetch origin. Your command would have worked with a git fetch origin first.
git fetch origin
git rev-list deployment..origin/deployment

Continuous integration with python 2.7 / flask / mongoDB / git

How should I implement continuous integration on my new application? Currently, this is how we're pushing to production - please bear with me, I know this is far from sane:
From local, git push origin production (the production codebase is kept on the production branch, modifications are either written directly there and committed, or files are checked out individually from another branch. Origin is the remote production server)
On the remote box, sudo stop gunicorn (application is running as a process)
cp ~/flaskgit/application.py ~/flask/applicaion.py (the git push origin from local pushes to an init -bare repo with a post-update hook that populates the files in ~/flaskgit. ~/flask is where the gunicorn service runs the application under a virtualenv)
sudo start gunicorn
we do our testing with the ~/flaskgit code running on a different port. once it looks good we do the CP
I would love to have something more fluid. I have used jenkins in the past, and loved the experience - but didn't set it up.
What resources / utilities should I look up in order to do this well?
Thank you!
buildbot, jenkins/hudson but these give you continuous integration in the sense you can run a "make" equivalent with every code base change through a commit hook. You could also look at vagrant if there is something there for you for creating repeatable vm's wrt to config/setup. Could tie it with a commit hook.

mercurial: running remote regression tests automatically on every commit

I commit every time I make some changes that I think might work: I don't do extensive testing before a commit. Also, my commits will soon be automatically pushed to a remote repository. (I'm the only developer, and I have to add features or rewrite parts of the code many times a day.)
I'd like to set up a remote computer to run regression tests automatically whenever I commit anything; and then email me back the differences report.
What's the easiest way to set this up?
All my code is in Python 3. My own system is Windows 7, ActiveState Python, TortoiseHG, and Wing IDE. I can set up the remote computer as either Linux or Windows. The application is all command-line, with text input and output.
Use a continious integration server such as Buildbot or Jenkins and configure it to monitor the repository. Then run the tests using that. Buildbot is written in Python so you should feel right at home with it.
If you feel it's wasteful to make Buildbot or Jenkins poll the repository (even though hg pull uses very few resources when there are no new changesets), then you can configure a changegroup hook in the repository to trigger a build in the CI server.
I would recommend setting up Buildbot. You can have it watch a remote repository (Mercurial is supported) and automatically kick off a build when the repository changes. In your case, a build would just be running your test suite.
Its waterfall display allows you to see which builds failed and when, in relation to commits from the repository. It can even notify you, with the offending commit, when something breaks.
Jenkins is another option, supporting most of the same features. There are even cloud hosting options, like ShiningPanda that can host it for you, and they offer free licensing for open-source projects.

Categories

Resources