Render latest git date commit in django

Create file on server:

nano pull_repo.sh

#!/bin/sh

git –work-tree=/home/…web/ –git-dir=/home/…web/.git pull origin dev

git –work-tree=/home/…web/ –git-dir=/home/…web/.git log -1 –format=%cd > /home/…web/last.commit_date

chmod +x pull_repo.sh

Edit crontab:

crontab -e

Add line:

*/5 * * * * /home/…/pull_web.sh

In Django

decorator:

def prepare_commit_date(function):
    def wrap(request, *args, **kwargs):
        try:
            with open(os.path.join(BASE_DIR, "last.commit_date"), "r") as last_commit_date:
                request.commit_date = last_commit_date.readlines()[0].replace('\n', '')
        except Exception as ex:
            request.commit_date = str(ex.message)
        return function(request, *args, **kwargs)
    return wrap

view:

@prepare_commit_date
def index(request):
 return render(request, 'index.html', {})

template:

{{ request.commit_date }}

Advertisements

git hook (post receive)

I have app on my virtual server: app. Also i have git repository on the same VPS.

Simple way to init git repository:

mkdir app.git && cd app.git

git init –bare

nano hooks/post-receive

paste code, for example, pull app from local repository:

#!/bin/sh

git –work-tree=/home/user/app/ –git-dir=/home/user/app/.git pull origin master

After that make file executable:

sudo chmod +x hooks/post-receive

If you are misunderstand  how to clone repository you created on server, it can help(setting from local repository – path to remote origin):

url = ssh://my_user@my_ip:ssh_port/home/my_user/app.git

for example if your ip is 7.7.7.7, and ssh port is 22(default) url must be:

url = ssh://my_user@7.7.7.7:22/home/my_user/app.git

deploy django by git push

The local repository

It doesn’t really matter how the local repository is set up, but for the sake of argument, let’s suppose you’re starting one from scratch.

$ mkdir website && cd website
$ git init
Initialized empty Git repository in /home/ams/website/.git/
$ echo 'Hello, world!' > index.html
$ git add index.html
$ git commit -q -m "The humble beginnings of my web site."

Anyway, however you got there, you have a repository whose contents you want to turn into a web site.

The remote repository

I assume that the web site will live on a server to which you have ssh access, and that things are set up so that you can ssh to it without having to type a password (i.e., that your public key is in~/.ssh/authorized_keys and you are running ssh-agent locally).

On the server, we create a new repository to mirror the local one.

$ mkdir website.git && cd website.git
$ git init --bare
Initialized empty Git repository in /home/ams/website.git/

Then we define (and enable) a post-receive hook that checks out the latest tree into the web server’s DocumentRoot (this directory must exist; Git will not create it for you):

$ mkdir /var/www/www.example.org
$ cat > hooks/post-receive
#!/bin/sh
GIT_WORK_TREE=/var/www/www.example.org git checkout -f
$ chmod +x hooks/post-receive

Note: earlier versions of this howto depended on setting the git config variables core.worktree to the target directory, core.bare to false, and receive.denycurrentbranch to ignore. But these changes are not needed if you use GIT_WORK_TREE (which didn’t work when I first wrote the howto), and the remote repository can remain bare.

Back on the workstation, we define a name for the remote mirror, and then mirror to it, creating a new “master” branch there.

$ git remote add web ssh://server.example.org/home/ams/website.git
$ git push web +master:refs/heads/master

On the server, /var/www/www.example.org should now contain a copy of your files, independent of any .git metadata.

The update process

Nothing could be simpler. In the local repository, just run

$ git push web

This will transfer any new commits to the remote repository, where the post-receive hook will immediately update the DocumentRoot for you.

(This is more convenient than defining your workstation as a remote on the server, and running “git pull” by hand or from a cron job, and it doesn’t require your workstation to be accessible by ssh.)

source: http://toroid.org/ams/git-website-howto

hotfix to nice model in git

git checkout master
git pull
git checkout -b hotfix/${version}
//тут меняем версию
git commit -a -m ‘version changed’
//тут меняем применяем горячий фикс
git commit -a -m ‘hotfix applied’
git checkout develop
git pull
git merge hotfix/${version}
git checkout master
git merge hotfix/${version}
git tag -a ${version}
git push
git push –tags
git branch -d hotfix/{version}
git checkout develop

gitflow in windows

Скачиваем файлы (вибираем бинарники – ищем указаный файл):
getopt.exe – http://gnuwin32.sourceforge.net/packages/util-linux-ng.htm
libintl – http://gnuwin32.sourceforge.net/packages/libintl.htm
libiconv – http://gnuwin32.sourceforge.net/packages/libiconv.htm
Копируем эти файлы(getopt.exe, libintl3.dll, libiconv2.dll) в корень папки гита (напр. C:\Program Files (x86)\Git)

запускаем cmd с правами администратора, идем в папку /gitflow/contrib , исполняем
msysgit-install.cmd “GIT_HOME”(например: C:\Program Files (x86)\Git) )