git hooks探索(3)

2015-12-9 sunsmile 专业

一、提交更新之后自动执行推送操作
在实际开发过程中,提交代码更新之后,通常都会将提交推送到服务端代码库,一般都是先执行“git commit”操作之后,执行“git push”操作,如果时间长了就发现太麻烦了,每次都要执行两个命令才能完成将更新提交到服务端代码库中,通过git hooks即可实现将两个步骤自动连接在一起执行。

修改post-commit脚本(如果原来hooks下面没有,那么直接创建一个文本文件,命名为“post-commit”,并使用“chmod u+x post-commit”命令添加可执行权限即可;如果是默认的post-commit.sample,那么按照前面说的方法,去掉其“.sample”后缀,并添加可执行权限),添加一行“git push”,如果需要指定远程代码库名称,那么直接“git push $remote_repo_name”即可。

test@git:~/git/cli/test$ git commit -am "run git push after commit automatically"
this is post-commit hook
run git push in post-commit hook
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 292 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To /home/sunsmile/git/cli/../repo/test.git/
   5f17861..338386c  master -> master
[master 338386c] run git push after commit automatically
 1 file changed, 1 insertion(+)

test@git:~/git/cli/test$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean


二、客户端推送更新时自动更新服务端代码
此处需要修改的是服务端代码库中名称post-receive的hooks脚本文件,这个文件一般默认为“post-receive.sample”,删除其“.sample”后缀,“chmod u+x post-receive”增加可执行权限。

添加post-receive内容如下:
#!/bin/bash                                                                                                      
unset GIT_DIR
proj_loc=/root/git/repo/proj/test
cd $proj_loc
pwd
git pull origin master
其中unset GIT_DIR为取消此环境变量的定义,否则默认的目录非当前实际目录。后面的代码就是切换到需要更新代码的项目目录,然后执行“git pull origin master”(这里都是默认目录,如果不是默认的分支和代码库名称,可以根据需要直接更新,所有命令都与手动执行的shell命令基本一样),即可更新代码。

客户端文件~/git/cli/test/aa在更新之前的内容如下所示
just for test the post-receive hook in server side repository
debug for the 1st time
debug for the 2nd time
debug for the 3rd time
debug for the 4th time


修改客户端文件~/git/cli/test/aa之后,内容如下所示
just for test the post-receive hook in server side repository
debug for the 1st time
debug for the 2nd time
debug for the 3rd time
debug for the 4th time, ok

this just for show in this time

保存修改之后,在客户端项目目录下“~/git/cli/test/”执行命令提交修改,并推送到服务端代码库中,操作过程以及日志输出信息如下所示
root@74ae6ad3f05b:~/git/cli/test# git commit -am "just for show"
[master 1e76367] just for show
 1 file changed, 3 insertions(+), 1 deletion(-)
root@74ae6ad3f05b:~/git/cli/test# git push origin master
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 298 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: /root/git/repo/proj/test
remote: From localhost:/root/git/repo/test
remote:  * branch            master     -> FETCH_HEAD
remote:    cacced2..1e76367  master     -> origin/master
remote: Updating cacced2..1e76367
remote: Fast-forward
remote:  aa | 4 +++-
remote:  1 file changed, 3 insertions(+), 1 deletion(-)
To localhost:/root/git/repo/test.git
   cacced2..1e76367  master -> master
root@74ae6ad3f05b:~/git/cli/test# 


客户端更新推送之前服务端文件git/repo/proj/test/aa内容如下所示
just for test the post-receive hook in server side repository
debug for the 1st time
debug for the 2nd time
debug for the 3rd time
debug for the 4th time

客户端更新推送之后服务端文件git/repo/proj/test/aa内容如下所示
just for test the post-receive hook in server side repository
debug for the 1st time
debug for the 2nd time
debug for the 3rd time
debug for the 4th time, ok

this just for show in this time

从输出日志中可以看到以“remote: ”开始的信息都是post-receive脚本在执行过程中的输出信息,可以看书更新了三行,并删除了一行,正好是本次修改的内容。
由此可以看出,只需要在git/repo/test/hooks/中post-receive脚本文件中添加适当内容即可实现,客户端的每次推送都能够实时更新到指定的服务端目录中。
在平时的WEB前端开发的过程中,这种实时同步的功能会使开发过程能够更加方便高效,开发者只需要在本地提交更新,并推送到代码库,即可自动更新/var/www目录下的web相关文件,直接打开浏览器测试就可以,而无需登陆到服务器手动更新相应目录的文件之后再去测试。
当然,通过post-receive的方式并不是唯一实现此功能的方法,网上也有通过post-update实现的,使用者可以发挥自己想象力。

发表评论:

Powered by emlog 京ICP备15044591号-1