FreeBSD install Git and GitWeb


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# 先從 devel/git 的 ports 中安裝 git
# 同時間選取 gitweb 以提供網頁界面
# 之後遇到的設定頁照原來的 OK OK 就可以
cd /usr/ports/devel/git
make install clean

# 增加 git 的使用者,並指定 git 的家用目錄為 /home/git
# 同時間使用 git-shell 作為 shell 指令, uid, gid 為 9418
pw groupadd -n git -g 9418
pw useradd -n git -u 9418 -g git -c git -d /home/git -s /usr/local/libexec/git-core/git-shell -h -

# 新增 repo (倉庫 work),並更改屬性
chown git:git /home/git
chmod 755 /home/git

mkdir /home/git/work/
chmod 775 /home/git/work/
chown git:git /home/git/work/

# 設定除了 commit 外還可以使用額外指令的用戶
# 如果使用者只需要 commit,就可以不加進來
vi /etc/group
git:*:9418:zeuxis,neo

# 設定 SSH 認證金鑰,因為 git 要用到這東西
# 每個使用者的 SSH Key 也要加進 authorized_keys.
# 每個 key 為一行
mkdir /home/git/.ssh/
chmod 700 /home/git/.ssh/

touch /home/git/.ssh/authorized_keys
chmod 600 /home/git/.ssh/authorized_keys
chown -R git:git /home/git/.ssh/

# 生成指定用戶的 SSH Key
# 在安裝情況下是使用 ssh-keygen -d 生成 dsa
# 另還可用 ssh-keygen -t rsa 生成 rsa
# 生成時 Enter passphrase 無視按下 enter 即可
su username
ssh-keygen -d
su root
cat /home/username/.ssh/id_dsa.pub > /home/git/.ssh/anthorized_keys

# 接著設定 git 的 repo
mkdir /home/git/work/test.git
cd /home/git/work/test.git && git init --bare --shared

# 接著進行一個簡單的測試
mkdir /tmp/test && cd /tmp/test && git init
echo "This is a test" > index.html
git add
git commit -m 'added index.html'

# 之後 commit 到遠端的 repo
git remote add origin [email protected]:work/test.git
git push origin master

# 之後再設定 gitweb
mkdir /home/git/public_html
chmod 755 /home/git/public_html
chown git:git /home/git/public_html
cp /usr/local/share/examples/git/gitweb/git* /home/git/public_html

# 再設定 apache
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/home/git/public_html"
ServerName git.example.com

<Directory "/home/git/public_html">
Options ExecCGI
Order allow,deny
Allow from all

DirectoryIndex gitweb.cgi
AddHandler cgi-script .cgi
</Directory>
</VirtualHost>

# 之後再編輯 gitweb.cgi
our $projectroot = "/home/git/work";
our $home_link_str = "work";
our $site_name = "git.example.com"
our $projects_list_description_width = 40; # 調整 description 空間

# 修改 test.git 的描述
echo "test.git" > /home/git/work/test.git/description

# 修改 test.git 的 owner
vim /home/git/work/test.git/config

# 在其後面加入
[gitweb]
owner = zeuxis
url = git://git.example.com/work/test.git
url = [email protected]:work/test.git

# 最後就是設定 Git protocol
# 在 /etc/rc.conf 最後加入這段
git_daemon_enable="YES"
git_daemon_directory="/home/git"
git_daemon_flags="--syslog --base-path=/git --export-all"

# 之後啟動 Git Daemon
/usr/local/etc/rc.d/git_daemon start

# 如果以上面這句子會出現停在 starting 的字眼可以改為以下句式
# 送出後接兩下 Enter 結束即可
/usr/local/etc/rc.d/git_daemon start &

# 檢查是否在執行
ps -eaf | grep -v grep | grep git

# 如有東西就可以再測試可否使用了
cd /tmp/test
rm -Rf *
git clone git://git.example.com/work/test.git
1
2
3
4
5
6
參考:
http://dennylin93.wordpress.com/2010/02/04/how-to-setup-git/
http://vanix.blogspot.com/2010/01/setup-git-server-on-freebsd.html
http://plog.longwin.com.tw/my_note-unix/2009/05/08/build-git-env-over-ssh-2009
http://plog.longwin.com.tw/my_note/2005/12/28/ssh_keygen_no_passwd
http://hi.baidu.com/d_life/blog/item/c52d75013566a5dd267fb54f.html