关于git钩子的自我记录


分别用root 和 git 用户配置了两个hook,可以实现两个用户都可以提交代码,分别在不同的仓库里,记录下关键信息,git安装 免密配置不记录了就。

一、git用户如下:

git用户下的仓库权限:

1
drwxr-xr-x 7 git git 4096 Dec  6 09:53 hexoBlog.git

git用户下的资源目录:

1
drwxr-xr-x 16 git        git        4096 Dec  6 09:53 Cyt_Blog

git用户下的hook配置:

1
2
3
4
5
6
[root@VM-16-4-centos ~]# cat /home/git/hexoBlog.git/hooks/post-update
#!/bin/bash
git --work-tree=/home/Cyt_Blog --git-dir=/home/git/hexoBlog.git checkout -f

[git@VM-16-4-centos hooks]$ ll post-update
-rwxr-xr-x 1 git git 89 Dec 5 17:29 post-update

推送后的资源:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@VM-16-4-centos Cyt_Blog]# ll
total 136
-rw-rw-r-- 1 git git 8303 Dec 6 09:50 003.png
drwxr-xr-x 7 git git 4096 Dec 6 09:50 2022
drwxr-xr-x 2 git git 4096 Dec 6 09:50 404
drwxr-xr-x 2 git git 4096 Dec 6 09:53 about
drwxr-xr-x 3 git git 4096 Dec 6 09:53 archives
drwxr-xr-x 2 git git 4096 Dec 6 09:50 categories
drwxr-xr-x 2 git git 4096 Dec 6 09:50 contact
drwxr-xr-x 2 git git 4096 Dec 6 09:50 css
-rw-rw-r-- 1 git git 5333 Dec 6 09:50 favicon.png
drwxr-xr-x 2 git git 4096 Dec 6 09:50 friends
-rw-rw-r-- 1 git git 40391 Dec 6 09:53 index.html
drwxr-xr-x 2 git git 4096 Dec 6 09:50 js
drwxr-xr-x 31 git git 4096 Oct 10 11:27 libs
drwxr-xr-x 4 git git 4096 Oct 10 11:27 live2dw
drwxr-xr-x 7 git git 4096 Dec 6 09:50 medias
-rw-rw-r-- 1 git git 21527 Dec 6 09:53 search.xml
drwxr-xr-x 11 git git 4096 Dec 6 09:53 tags

二、sudoers配置:

1
2
[root@VM-16-4-centos home]# ll /etc/sudoers
-r-------- 1 root root 4391 Dec 6 09:40 /etc/sudoers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin

## Next comes the main part: which users can run what software on
## which machines (the sudoers file can be shared between multiple
## systems).
## Syntax:
##
## user MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
git ALL=(ALL) ALL

三、root用户下:

root用户下的仓库权限:

1
drwxr-xr-x  7 root       root       4096 Dec  6 10:07 test_root.git

root用户下的资源目录:

1
drwxr-xr-x 15 root       root       4096 Dec  6 10:07 TEST_ROOT

root用户下的hook配置:

1
2
3
4
5
6
[root@VM-16-4-centos ~]# cat /home/test_root.git/hooks/post-receive 
#!/bin/bash
git --work-tree=/home/TEST_ROOT --git-dir=/home/test_root.git checkout -f

[root@VM-16-4-centos hooks]# ll post-receive
-rwxr-xr-x 1 root root 90 Dec 6 09:59 post-receive

推送后的资源:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
root@VM-16-4-centos TEST_ROOT]# ll
total 136
-rw-r--r-- 1 root root 8303 Dec 6 10:07 003.png
drwxr-xr-x 7 root root 4096 Dec 6 10:07 2022
drwxr-xr-x 2 root root 4096 Dec 6 10:07 404
drwxr-xr-x 2 root root 4096 Dec 6 10:07 about
drwxr-xr-x 3 root root 4096 Dec 6 10:07 archives
drwxr-xr-x 2 root root 4096 Dec 6 10:07 categories
drwxr-xr-x 2 root root 4096 Dec 6 10:07 contact
drwxr-xr-x 2 root root 4096 Dec 6 10:07 css
-rw-r--r-- 1 root root 5333 Dec 6 10:07 favicon.png
drwxr-xr-x 2 root root 4096 Dec 6 10:07 friends
-rw-r--r-- 1 root root 40391 Dec 6 10:07 index.html
drwxr-xr-x 2 root root 4096 Dec 6 10:07 js
drwxr-xr-x 31 root root 4096 Dec 6 10:07 libs
drwxr-xr-x 4 root root 4096 Dec 6 10:07 live2dw
drwxr-xr-x 7 root root 4096 Dec 6 10:07 medias
-rw-r--r-- 1 root root 21527 Dec 6 10:07 search.xml
drwxr-xr-x 11 root root 4096 Dec 6 10:07 tags

post-receive和post-update 的区别 目前就能搜到这么多:

post-receive和post-update钩子之间的区别在于参数:post-receive除了获得其名称之外,还将获得所有ref的旧值和新值。


文章作者: caoyutao
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 caoyutao !
评论
  目录