crontab automated git pull - python

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

Related

How to update personal cloned github?

I recently downloaded an open-source project from github into my server using
$ git clone www.github.com/project_url
Then I added comments and descriptions inside the multiple files here and there.
When I went to the github repository of the original project github page,
many of the files were updated. But I'm hesitating to clone again because I think it would just overwrite every local files to which I wrote many of the comments.
Is there anyway I can download up-to-date files in the github,
but leave the comments that I added not being removed at the same time?
There are multiple strategies to go around this problem. A strategy is already explained. Another could be
Commit your changes (git commit -am <message>)
Do a git pull (git pull -r)
Resolve conflicts if any
Also, you can keep your changes separate in a separate branch. Here is what else can be done:
Create a separate branch in your local repository (git checkout -b <branch-name>, eg. git checkout -b new-branch)
commit changes in that new branch (git commit -am <message>)
Either you can merge now from your new branch to the old branch
git checkout old-branch; git merge new-branch.
resolve conflicts if any
OR
Raise a Pull Request after resolving any conflicts if any
You can use git stash for this. This will store everything you changed locally. Then you pull the changes from the original repository and then with 'git stash pop' your local changes will come back.
git stash
git pull
git stash pop
Conflicts can come up and need to be resolved.

Using ac2git followed by bitbucket

I am using ac2git to convert my accurev depot to git repository. I am able to successfully make the conversion however when I follow the steps after creating the new repository I am unable to push the changes representing the accurev transactions that are now commits.
What I mean is I loose the history, I am only able to see the hist and the diff files when I check the commit options on bitbucket.
I followed the following steps:
python ac2git.py
cd existing-project
git add --all
git commit -m "Initial Commit"
git remote add origin http://****#bitbucket.******.git
git push -u origin master
I am new to bitbucket so I am not sure what the problem is? Has anyone tried this accurev->git->bitbucket before?
In other words, how do I move my git repository on my local created as a result of ac2git to a new repository on bitbucket ?
Your first step is to create a repository in BitBucket. Then you simply need to follow BitBucket's instructions:
cd /path/to/my/repo
git remote add origin http://****#bitbucket.******.git
git push -u origin --all # pushes up the repo and its refs for the first time
git push origin --tags # pushes up any tags
I'm not 100% certain that this is what has happened but it is worth noting that the ac2git script stores a lot of metadata under refs/ac2git/* refs, including a refs that will store a hist.xml, streams.xml and diff.xml in their commit history for each of your stream's transactions. See how_it_works.md which explains about refs/ac2git/depots/<depot_number>/streams/<stream_number>/info.
Though this ref acts as a branch, it shouldn't have been pushed up by invoking git push origin -u -all since the documentation claims that this only pushes things under refs/heads/. However, in case of errors the script may not correctly checkout a branch that was converted and it may leave your local repository in a detached HEAD state where it will have actually checked out one of the internal refs.
I don't know what a git push origin -u --all would do in this case for a brand new Bitbucket repo but if it had pushed your HEAD ref up to the repo then you would get this metadata on the remote.
However, for now this is just a theory and hopefully someone will be able to use this information to piece together a more clear solution for you.

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

How to use a pull request in my local copy of a Git branch?

I'm not familiar with Git, I only know how to download a master or dev branch of a python software project from Github and install into my system (I do not really use Git locally).
Now there is a "pull request" by a GitHub user which contains a feature I find useful but not in the main or dev branch. How can I use that in my local copy?
And if I manage to include it into my local copy, would it be lost if I later updated to the latest maaster/dev branch?
see: Git fetch remote branch
git fetch
git checkout <the name of the remote branch>
You can revert to any commit anytime you want, those changes are kept in the repository so you can visit different versions of it during the development on all branches. You need to do a merge command, which if, there's not any conflict with the patch contained in the pull request, will combine the code of the patch with the code of the repository. If there's conflicting code, like shared files for example, you will have to do the proper refactors and select which changes get inside or not.
Here you have basic information about merging different branches:
http://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging
Updating will only do a merge with the changes and your master. If you break something you can stash the changes and get to the original state.
Also I would suggest to checkout a fresh branch and apply that feature on the freshly created branch. If every thing works fine merge it to your master otherwise delete and forget it :P
if you dont know the branch name
use git fetch first to know the branch name
and then git checkout
Also maybe you can find this link useful

how to automate a push to repo using gitpython

I am trying to make a python script which would check for changes in my git local working folder, and automatically push them to the online repo. Currently only using git manually to do it. I want to know, what would a script require to do this without manual intervention.
The commands I'd type in my shell are:
#for checking the status, and determining if there are untracked files
git status
#if there are untracked files...add them
git add .
#add my commit message
git commit -m "7/8/2012 3:25am"
#push it to my online repo
git push origin master
#check if changes came on remote
git diff origin/master
#merge my repo with origin
git merge origin/master
When doing the git push, you'd always have to enter username/password. I know that git has a way around this which involves making ssh keys and all. But I am assuming there is some way GitPython is doing it. I mean we can pass username/password through code, or go with the former. So what are my options regarding authentication when I am using GitPython?
Edit: There are apps which actually generate the ssh keys, for e.g. github's windows application. How is the windows app doing this? My assumption is that there is surely some git api for it...
If you are authenticating using SSH keys, then use ssh-agent to load your key once and then you can keep using the key without having to provide the password all the time.
Alternatively, you could simply generate a key without a password, if you don’t care about your key security.
I have looked up the code to be sure, there is nothing that you can define a username/password combination for communication.
This has to be it because ssh does not give you the ability to provide password beforehand, it intentionally asks for user prompt. The only was is to use ssh keys for automation.
However, if you really want to bend your limits. There's open source app for non-interaction ssh communication without using ssh keys: http://sourceforge.net/projects/sshpass/
You compile & install this and direct a communication protocol like ssh:// to this app, it may work. However, I don't think that you should; just use keys, they're great =)

Categories

Resources