學科:Git/在Windows下安裝Gitolite

一些約定 編輯

安裝是在本地進行的(示例中域名均為localhost),示例中gitolite使用的賬戶為「git」,如果你要使用其他名字,請看清命令。粗體為用戶輸入,斜體部分是需要你根據實際情況進行修改的。


準備Cygwin環境 編輯

因為gitolite並不提供原生Windows支持,所以在Windows上搭建gitolite需要藉助Cygwin。Cygwin可以為Windows提供一個類似Linux的POSIX模擬系統。


安裝 編輯

  • 訪問 http://cygwin.com/install.html 獲取安裝文件
  • 按提示繼續,路徑建議不要包含中文或空格
  • 之後來到組件選擇 Select Packages 部分
推薦選擇:openssh(必選),vim(推薦,gl-setup安裝時如果未使用-q開關則會調用vi編輯配置文件),git(必選),git-completion,git-gui,git-svn(可選),gitk。
關於編輯器:如果你不熟悉vim或編輯器列表里其他編輯器,建議選擇nano
提示:建議使用搜索尋找需要的組件
 
  • 繼續按提示安裝

配置Cygwin 編輯

  • 將cygwin的bin目錄加入到Windows系統環境變量中,重啟生效
  • 打開Cygwin終端,執行
   $ /bin/cyglsa-config

將提示:

   Warning: Registering the Cygwin LSA authentication package requires administrator privileges!  You also have to reboot the machine to activate the change.
   Are you sure you want to continue? (yes/no)

yes確認:

   Cygwin LSA authentication package registered.
   Activating Cygwin's LSA authentication package requires to reboot.

重啟生效。


準備SSH 編輯

  • 以管理員身份啟動終端,執行
   $ ssh-host-config
  • 接下來要回答一系列問題(一次不成功沒關係,可以再次操作),以下是推薦的回答:
    • Should privilege separation be used? 選yes
    • Query: new local account 'sshd'? (yes/no) 選yes
    • Do you want to install sshd as a service? 選yes
    • Enter the value of CYGWIN for the daemon: [] 直接回車
    • Do you want to use a different name? (yes/no) 選no
    • Create new privileged user account 'cyg_server'? (yes/no) 選yes,接下來輸入密碼(如果組策略開啟了密碼強度要求,設置的密碼要符合該要求,否則會失敗)

如果成功最後幾條信息會是:

   *** Info: The sshd service has been installed under the 'cyg_server'
   *** Info: account.  To start the service now, call `net start sshd' or
   *** Info: `cygrunsrv -S sshd'.  Otherwise, it will start automatically
   *** Info: after the next reboot.
   
   *** Info: Host configuration finished. Have fun!

接下來執行

   $ sc start sshd

啟動sshd服務,成功的輸出形如:

   SERVICE_NAME: sshd
       TYPE               : 10  WIN32_OWN_PROCESS
       STATE              : 2  START_PENDING
                               (STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
       WIN32_EXIT_CODE    : 0  (0x0)
       SERVICE_EXIT_CODE  : 0  (0x0)
       CHECKPOINT         : 0x2
       WAIT_HINT          : 0x1388
       PID                : 2644
       FLAGS              :
  • 提示:cygrunsrv用於管理服務,也可使用「cygrunsrv -S sshd」啟動服務,不同的是沒有信息輸出則表示成功啟動。使用「cygrunsrv -R sshd」可以移除sshd服務。
  • 可選:配置防火牆,允許ssh服務使用端口22(如果沒改配置文件中的端口設置的話)


準備git賬戶 編輯

雖然可以使用現有賬戶或ssh-host-config中創建的cyg_server賬戶,但還是推薦單獨創建一個名為「git」的賬戶。

賬戶創建 編輯

  • 轉到Windows控制面板,新建名為「git」賬戶(這步不區分大小寫)
  • 進入終端,執行:
   $ mkpasswd -l -u git >> /etc/passwd
這步是為了讓cygwin系統認得git賬戶對應Windows的哪個賬戶

登入git賬戶 編輯

  • 打開終端,通過ssh登入git賬戶,參考命令:
   $ ssh git@localhost

成功的參考輸出:

   The authenticity of host 'localhost (::1)' can't be established.
   ECDSA key fingerprint is 18:a6:df:7c:93:5f:c7:b6:20:ce:46:03:90:a9:f8:da.
   Are you sure you want to continue connecting (yes/no)? yes
   Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
   git@localhost's password:
   Could not chdir to home directory /home/git: No such file or directory
   Copying skeleton files.
   These files are for the users to personalise their cygwin experience.
   
   They will never be overwritten nor automatically updated.
   
   `./.bashrc' -> `/home/git//.bashrc'
   `./.bash_profile' -> `/home/git//.bash_profile'
   `./.inputrc' -> `/home/git//.inputrc'
   `./.profile' -> `/home/git//.profile'
   

並且命令的前導符變成:git@localhost。


安裝Gitolite 編輯

添加bash環境變量 編輯

編輯~/.bashrc,以nano作為示例:

   $ nano ~/.bashrc
  • nano的使用提示:nano和Windows筆記本很像,也可用方向鍵移動光標,底部會顯示一些如「^G」的命令(如果操作系統是中文的這些命令也應該有中文提示),「^」表示Ctrl,如按Ctrl+G獲取幫助。

在第一行添加:

  PATH="$PATH:$HOME/bin"

Ctrl+X,回車保存。為了使應用立即生效需要執行:

  $ source ~/.bashrc

確認添加結果可以執行:

   $ echo $PATH

剛才添加的路徑應該加到最後面了,示例為「/home/git/bin」。

Clone Gitolite文件 編輯

cd到/tmp/share(必須是這個路徑),命令:

   $ cd /tmp/share
  • 為什麼路徑必須是「/tmp/share」?
如果是Linux當然沒有這個限制,但cygwin下的gitolite在安裝中的路徑判斷存在問題,如果是其他路徑,在gl-setup時會提示找不到/tmp/share中的文件,所以目前就放到這個目錄了。後面給RC文件添加$ENV{PATH}也是cygwin路徑判斷的問題。 —— BB9z (talk) 10:44, 1 March 2012 (UTC)

然後從遠端clone gitolite倉庫:

   $ git clone git://github.com/sitaramc/gitolite.git

參考輸出:

   Cloning into 'gitolite'...
   remote: Counting objects: 5197, done.
   remote: Compressing objects: 100% (1775/1775), done.
   remote: Total 5197 (delta 3557), reused 4954 (delta 3366)
   Receiving objects: 100% (5197/5197), 1.74 MiB | 168 KiB/s, done.
   Resolving deltas: 100% (3557/3557), done.

gl-system-install 編輯

  • gl-system-install,參考命令及輸出:
   $ /tmp/share/gitolite/src/gl-system-install
   using default values for EUID=1015:
   /home/git/bin, /home/git/share/gitolite/conf, /home/git/share/gitolite/hooks

修正rc文件環境變量 編輯

註:這是cygwin獨有的問題,Linux無需此操作。

以nano為例,執行:

   $ nano ~/.gitolite.rc

在第一行添加如下內容,保存:

   $ENV{PATH}="/bin:/usr/bin";

準備管理員賬戶的公鑰 編輯

gitolite安裝初始需要提供一名管理員的公鑰,安裝好後只要有相應私鑰即可通過gitolite-admin庫進行管理,既可以在本地,也可以在遠端。 為了方便起見,下面以本機已有賬戶為例。

打開終端登入要作為初始管理員的賬戶(非git賬戶,默認打開終端使用的是當前登錄Windows的用戶,如果要把當前用戶作為管理員的話直接打開終端就可以了)。

將用戶公鑰從ssh用戶目錄中拷貝出來,參考命令:

   $ cp ~/.ssh/id_rsa.pub /name.pub

如果提示找不到文件則需要先生成一個,執行:

   $ ssh-keygen -t rsa

需要回答幾個問題,直接回車。參考的輸出:

   Generating public/private rsa key pair.
   Enter file in which to save the key (/home/git/.ssh/id_rsa):
   Enter passphrase (empty for no passphrase):
   Enter same passphrase again:
   Your identification has been saved in /home/git/.ssh/id_rsa.
   Your public key has been saved in /home/git/.ssh/id_rsa.pub.
   The key fingerprint is:
   95:bb:00:a0:04:2e:12:3c:21:9a:37:f6:8b:57:f0:e0 git@BBservice
   The key's randomart image is:
   +--[ RSA 2048]----+
   |=oo .            |
   |+* . .     .     |
   |=.* o .   o      |
   |oo + + . . .     |
   |    E o S .      |
   |   . o   . .     |
   |  . o     .      |
   |   .             |
   |                 |
   +-----------------+

然後再執行之前的拷貝命令。

gl-setup 編輯

  • 最後,執行gl-setup,參考命令:
  $ gl-setup -q path_to_admin_pub_file

成功的示例:

   creating gitolite-admin...
   Initialized empty Git repository in /home/git/repositories/gitolite-admin.git/
   creating testing...
   Initialized empty Git repository in /home/git/repositories/testing.git/
   [master (root-commit) 119d2f3] gl-setup -q /home/git.pub
    2 files changed, 8 insertions(+), 0 deletions(-)
    create mode 100644 conf/gitolite.conf
    create mode 100644 keydir/git.pub

至此Gitolite的安裝過程就全部完成了。

驗證安裝 編輯

首先,檢出gitolite-admin倉庫。

終端登入管理員身份,在合適的目錄執行:

   $ git clone git@localhost:gitolite-admin

成功的輸出參考:

   Cloning into 'gitolite-admin'...
   remote: Counting objects: 15, done.
   remote: Compressing objects: 100% (11/11), done.
   remote: Total 15 (delta 2), reused 0 (delta 0)
   Receiving objects: 100% (15/15), done.
   Resolving deltas: 100% (2/2), done.

之後可以修改配置文件,能順利提交的話就沒有問題了。

關於Gitolite的配置參見:Subject:Git/Gitolite配置


問題解答 編輯

  • 安裝Cygwin時少選或選錯組件了怎麼辦?
只需重新執行Setup.exe在原位安裝即可,選擇組件時,已有組件的狀態將顯示為keep。


  • 安裝gitolite的過程出問題了,如何重新安裝?
直接重新執行gl-system-install和gl-setup應該沒有問題,如果想全新的安裝,可以用rm命令刪除gitolite相關文件,詳見: http://sitaramc.github.com/gitolite/install.html#uninstall_
甚至你也可以用「rm -fr /home/git」將整個git用戶文件推倒重來,但要小心,「rm -fr」是極度危險的。

參考 編輯

  1. senawario - gitolite tutorial
  2. Google Group > gitolite - gitolite-admin does not appear... yes i failed now i'm stuck ><