分类目录归档:Git

Git读取Jira issues的hook

为了方便填写git commit message,写了一个脚本用于读取jira上的issues,并列出选择作为commit message。
用到了git hook中的prepare-commit-msg
下面提供bash shell和python3两个版本
1.bash shell
此版本需要安装jq。

#!/bin/bash
USERNAME=`git config --get user.name`
if [ "$2" = "message" -o "$2" = "commit" ]
then
    exit 0
fi
exec 2> /dev/null
ISSUS=`curl -u jira:jira "http://localhost:8081/rest/api/2/search?jql=status+in+(Reopened,%20%22To%20Do%22,%20%22In%20Progress%22)%20AND%20assignee%20in%20($USERNAME)%20ORDER%20BY%20updated%20DESC" | jq '.issues[]|{summary: .fields.summary, key: .key}'`
KEYS=(`echo $ISSUS | jq '.key'`)
SUMMARYS_TMP=("`echo $ISSUS | jq '.summary'`")
IFS_old=$IFS
IFS=$'\n'
SUMMARYS=($SUMMARYS_TMP)
IFS='"'
number=${#KEYS[@]}
declare -a ISSUSES='()'
for((i=0;i<$number;i++))
do
    ISSUSES[$i]="${KEYS[$i]}${SUMMARYS[$i]}"
done
for((j=0;j<${#ISSUSES[@]};j++))
do
    let k=$j+1
    echo $k.${ISSUSES[$j]}
done
exec < /dev/tty
exec 2>&1
read -p "请输入编号:"
let answer=$REPLY-1
echo ${ISSUSES[$answer]} > $1
IFS=$IFS_old

2.python3

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys, os, re
from subprocess import check_output
import requests
import json

def query_report(user):
    s = requests.Session()
    s.post('http://localhost:8081/rest/auth/1/session', json={"username":"jira","password":"jira"})
    url = 'http://localhost:8081/rest/api/2/search?jql=status+in+(Reopened,%20%22To%20Do%22,%20%22In%20Progress%22)%20AND%20assignee%20in%20(' + user +')%20ORDER%20BY%20updated%20DESC'
    reports = s.get(url)
    return reports.json()

sys.stdin = open('/dev/tty')
# 收集参数
commit_msg_filepath = sys.argv[1]
if len(sys.argv) > 2:
    commit_type = sys.argv[2]
else:
    commit_type = ''
if len(sys.argv) > 3:
    commit_hash = sys.argv[3]
else:
    commit_hash = ''

if commit_type == "message":
    sys.exit(0)
# 检测我们所在的分支
branch = check_output(['git', 'symbolic-ref', '--short', 'HEAD']).strip()
branch = str(branch, 'utf-8')
print("On branch '%s'" % branch)
username = check_output(['git', 'config', '--get', 'user.name']).strip()
username = str(username, 'utf-8')
messages = []
if branch.startswith('master'):
    j = query_report(username)
    if j["total"] > 0:
        issues = j["issues"]
        for i in range(len(issues)):
            print(str(i+1) + ':' + issues[i]["key"] + " " + issues[i]["fields"]["summary"])
            messages.append(issues[i]["key"] + " " + issues[i]["fields"]["summary"]);
    number = input("请输入编号: ")
    message = messages[int(number) - 1]
    with open(commit_msg_filepath, 'w') as f:
        f.seek(0, 0)
        f.write(message)

怎样搭建本地AOSP Gerrit服务器

怎样搭建本地AOSP Gerrit服务器

在这个教程里,我会介绍怎样搭建一个本地Android源码Gerrit服务器。

完成这个教程后,你会拥有一个完全可运行的AOSP镜像和本地Gerrit服务器。

首先我们需要一个Linux服务器。这里用Ubuntu 14.04

Gerrit 需求:

1.Java JDK > 1.7

2.Git

3.SSH server

4.DB

我选择mysql作为数据库服务,当然你也可以用其他的数据库软件。可以参考Gerrit文档

这里有一句命令可以让你得到你所需要的一切。

$ sudo apt-get install git openjdk-8-jdk openssh-server mysql-server gitweb

下载并安装Android repo

$ sudo curl https://storage.googleapis.com/git-repo-downloads/repo > /usr/bin/repo
$ sudo chmod a+x /usr/bin

现在我们需要设置一个本地Android镜像,这需要一段时间,你可以先去喝杯咖啡。

$ mkdir -p /usr/local/aosp/mirror
$ cd /usr/local/aosp/mirror
$ repo init -u https://android.googlesource.com/mirror/manifest --mirror
$ repo sync

下载Gerrit

创建一个Gerrit用户

$ sudo adduser --system --shell /bin/bash --gecos 'Gerrit Code Review User' --group --disabled-password --home /home/gerrit2 gerrit2

Gerrit需要一个数据库来工作,这里用的是mysql。打开终端输入以下命令:

$ mysql -u root -p
CREATE USER 'gerrit2'@'localhost' IDENTIFIED BY 'secret';
CREATE DATABASE reviewdb;
ALTER DATABASE reviewdb charset=latin1;
GRANT ALL ON reviewdb.* TO 'gerrit2'@'localhost';
FLUSH PRIVILEGES;
quit

上面这个命令在mysql中创建了一个gerrit用户,一个数据库和gerrit用户需要的权限集合。

用新的系统用户登陆:

$ sudo su gerrit2

复制上面下载的文件gerrit-2.13.5.war到gerrit2的home下,然后执行下面的命令:

$ java -jar ./gerrit-2.11.war init -d review_site
Using secure store: com.google.gerrit.server.securestore.DefaultSecureStore

*** Gerrit Code Review 2.11
***

Create ‘/home/gerrit2/review_site’ [Y/n]?

*** Git Repositories
***

Location of Git repositories [git]:

*** SQL Database
***

Database server type [h2]: MySQL

Gerrit Code Review is not shipped with MySQL Connector/J 5.1.21
** This library is required for your configuration. **
Download and install it now [Y/n]?
Downloading http://repo2.maven.org/maven2/mysql/mysql-connector-java/5.1.21/mysql-connector-java-5.1.21.jar … OK
Checksum mysql-connector-java-5.1.21.jar OK
Server hostname [localhost]:
Server port [(mysql default)]:
Database name [reviewdb]:
Database username [gerrit2]: gerrit2
gerrit’s password :
confirm password :

*** Index
***

Type [LUCENE/?]:

*** User Authentication
***

Authentication method [OPENID/?]:

*** Review Labels
***

Install Verified label [y/N]?

*** Email Delivery
***

SMTP server hostname [localhost]:
SMTP server port [(default)]:
SMTP encryption [NONE/?]:
SMTP username :

*** Container Process
***

Run as [gerrit2]:
Java runtime [/usr/lib/jvm/java-8-openjdk-amd64/jre]:
Copy gerrit-2.11.war to /home/gerrit2/review_site/bin/gerrit.war [Y/n]?
Copying gerrit-2.11.war to /home/gerrit2/review_site/bin/gerrit.war
*** SSH Daemon
***

Listen on address [*]:
Listen on port [29418]:

Gerrit Code Review is not shipped with Bouncy Castle Crypto SSL v151
If available, Gerrit can take advantage of features
in the library, but will also function without it.
Download and install it now [Y/n]?
Downloading http://www.bouncycastle.org/download/bcpkix-jdk15on-151.jar … OK
Checksum bcpkix-jdk15on-151.jar OK
Generating SSH host key … rsa… dsa… done

*** HTTP Daemon
***

Behind reverse proxy [y/N]?
Use SSL (https://) [y/N]?
Listen on address [*]:
Listen on port [8080]:
Canonical URL [http://ubuntu:8080/]: http://10.0.0.9:8080

*** Plugins
***

Installing plugins.
Install plugin download-commands version v2.11 [y/N]?
Install plugin reviewnotes version v2.11 [y/N]?
Install plugin singleusergroup version v2.11 [y/N]?
Install plugin replication version v2.11 [y/N]?
Install plugin commit-message-length-validator version v2.11 [y/N]?
Initializing plugins.
No plugins found with init steps.

Initialized /home/gerrit2/review_site
Executing /home/gerrit2/review_site/bin/gerrit.sh start
Starting Gerrit Code Review: OK
Waiting for server on ubuntu:8080 … OK
Opening http://10.0.0.9:8080/#/admin/projects/ …FAILED
Open Gerrit with a JavaScript capable browser:
http://10.0.0.9:8080/#/admin/projects/

现在Gerrit已经开始运行了。

需要注意的是,第一个登陆到gerrit的用户将会成为管理员。

Gerrit能通过多种认证方式登陆。上面选择的OpenID。

现在让我们打开浏览器并打开Gerrit网站。

注册并登陆后需要设置SSH keys,这样我们就能够在命令行下工作。

下面我们在Ubuntu系统的另一个用户下执行以下命令:

$ ssh-keygen <ENTER>
Generating public/private rsa key pair.
Enter file in which to save the key (/home/serveradmin/.ssh/id_rsa):  <ENTER>
Enter passphrase (empty for no passphrase): <ENTER>
Enter same passphrase again: <ENTER>
Your identification has been saved in /home/serveradmin/.ssh/id_rsa.
Your public key has been saved in /home/serveradmin/.ssh/id_rsa.pub.
The key fingerprint is:
d5:7b:51:d8:22:0e:95:63:f9:0e:a2:22:1c:97:76:40 serveradmin@ubuntu
The key's randomart image is:
+---[RSA 2048]----+
| .E ..o o.|
| . ..* o..|
| o .+.+.. |
| . + ... o... |
| . + .S. ..o. |
| o . . .. |
| . . |
| |
| |
+-----------------+

$ cat ~/.ssh/id_rsa.pub  <ENTER>
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDobCUbb8ExP9ci48ZCzCUE3P+IjoH6zrv/l88+4NOTf9FJWDAl4SHCXI+mrZoFEeZo9
uieKTuvHqUFQNpnVA9vfNY6bhaTucAAt0fX9Q1M1ZtNj2IxaQd7u9PnxjSGEig0BUtjqDEu4CMhMShXGWsGAwL7ju/qu7G7RF
iK/Wtcye6wUSjziXseCusb1DUZZ6dsOpxrPYEM3kwXpItQtAq1oEIQWsxEgj2nrOqRXm1UWdqIQU1X45XHQtg5iqi44PfLVNU
3alu453MeWn5PrpSS5dFw/7AkBW4KMPwrOvVnu8gb9xLA0TWtPf+sQ9ROEQC5SBeO+4Q9XRNf5YaswpLj 

在网站的右上角点击你的用户名然后点击Settings,在左侧的菜单中选择“SSH Public Keys”,复制上面的内容到里面,然后点Add。

下面让我们测试一下通过SSH访问Gerrit:

ssh -p 29418 admin@localhost <ENTER> <Change "admin" to your username in gerrit>

**** Welcome to Gerrit Code Review ****

Hi Administrator, you have successfully connected over SSH.

Unfortunately, interactive shells are disabled.
To clone a hosted Git repository, use:

git clone ssh://ramon@10.0.0.9:29418/REPOSITORY_NAME.git
Connection to localhost closed.

出现以上信息说明登陆成功,下面创建两个group:“android-admin” and “android”

前者用来管理,包含review,merge,delete等权限,后者只有view和change-set的权限。

在网页中点击People -> Create New Group,输入名称“android-admin”。执行同样的操作创建“android”。

现在我们为AOSP代码树创建一个父项目。所有为这个项目的设置都会继承到子项目。如果跳过这步,AOSP项目会继承“All-Projects”项目,而这个项目太通用了。

在网站中点击Project -> Create New Project,填入以下内容:

Project Name: Android
Rights Inherit From: All-Projects

勾选 “Only Serve As Parent For Other Projects”,然后点击“Create Project”按钮。

项目已经创建完,下面来为这个项目设置权限控制:

点击Projects -> List,然后选择“Android”项目,点击“Access”然后点击“Edit”,修改成如下图。图中还包含了其他的群组,可自行设置。

Gerrit权限配置

现在我们可以把所有的Android项目推送到Gerrit了。

第一条命令会在Gerrit上创建所有的Android项目;第二条命令会将刚刚创建的项目的父项目修改为“Android”;第三条命令会将代码推送到Gerrit,时间会比较长。

进入下载Android镜像的目录执行以下命令:

$ repo forall -c 'echo $REPO_PATH; ssh -p 29418 admin@localhost gerrit create-project --name android/$REPO_PATH --owner android;'

accessories/manifest
device/asus/deb
device/asus/flo
device/asus/flo-kernel
device/asus/fugu
device/asus/fugu-kernel
…

$ repo forall -c 'echo $REPO_PATH; ssh -p 29418 admin@localhost gerrit set-project-parent --parent Android android/$REPO_PATH;'

accessories/manifest
device/asus/deb
device/asus/flo
device/asus/flo-kernel
…

$ repo forall -c 'echo $REPO_PATH; git push ssh://ramon@localhost:29418/android/$REPO_PATH +refs/heads/* +refs/tags/*;'

大功告成!

创建Gerrit服务自启:

$ sudo echo "GERRIT_SITE=/opt/gerrit-review" >> /etc/default/gerritcodereview
$ sudo ln -snf /opt/gerrit-review/bin/gerrit.sh /etc/init.d/gerrit
$ sudo update-rc.d gerrit defaults

下面贴一个gerrit配置文件(postgre数据库,ldap认证,提交信息增加jira链接)

[gerrit]
    basePath = git
    canonicalWebUrl = http://localhost:8080/
[database]
    type = postgresql
    hostname = localhost
    database = reviewdb
    username = gerrit2
[index]
    type = LUCENE
[auth]
    type = LDAP
[ldap]
    server = ldap://localhost:389
    username = cn=admin,dc=darkerthanblack,dc=org
    accountBase = ou=People,dc=darkerthanblack,dc=org
    groupBase = ou=group,dc=darkerthanblack,dc=org
[receive]
    enableSignedPush = false
[sendemail]
    smtpServer = smtp.exmail.qq.com
    smtpServerPort = 465
    smtpEncryption = SSL
    smtpUser = example@example.com
        smtpPass = your_password
        from = example@example.com
[container]
    user = gerrit2
    javaHome = /usr/lib/jvm/java-8-openjdk-amd64/jre
[sshd]
    listenAddress = *:29418
    maxConnectionsPerUser = 0
[httpd]
    listenUrl = http://*:8080/
[cache]
    directory = cache
[gitweb]
    cgi = /usr/lib/cgi-bin/gitweb.cgi
[commentlink "jira"]
    match = ([A-Z]+-[0-9]+)
    link = http://localhost:8081/browse/$1

参考:
http://openwares.net/linux/gerrit2_setup.html
https://nativeguru.wordpress.com/2015/08/18/how-to-set-local-aosp-gerrit-server-part-1

git-daemon的开启

安装脚本

  1. sudo apt-get install git-daemon-sysvinit

配置

  1. sudo vi /etc/default/git-daemon

配置信息如下

  1. # sourced by /etc/init.d/git-daemon
  2. # installed at /etc/default/git-daemon by the maintainer scripts
  3. #
  4. # This is a POSIX shell fragment
  5. #
  6. GIT_DAEMON_ENABLE=true
  7. GIT_DAEMON_USER=git
  8. GIT_DAEMON_DIRECTORY="/var/cache/git /home/git/repositories"
  9. # Additional options that are passed to the Daemon.
  10. GIT_DAEMON_OPTIONS="--export-all --enable=upload-pack --enable=upload-archive --enable=receive-pack --informative-errors"
  11. GIT_DAEMON_BASE_PATH=/home/git/repositories

控制

  1. sudo service git-daemon start|restart|stop

Ubuntu 12.04 – Installing Gitolite and Gitweb

Note this is only tested on Ubuntu 12.04 Server with apache2, I’m sure it would work on the desktop version also.

Installing Git:

  1. sudo apt-get install git-core

Optional, setup git global settings:

  1. git config --global user.name "Your Name"
  2. git config --global user.email your@email.com

SSH Key:
Generate ssh public/private key on the machine you would like to access git repo from and copy it to the server into the /tmp/ directory, for reference here is the command:

  1. ssh-keygen -t rsa -C "name@computer"

Installing Gitolite:

  1. sudo apt-get install gitolite

Create a user to access gitolite with, in this case I chose git since I don’t like to type:

  1. sudo adduser \
  2. --system \
  3. --shell /bin/bash \
  4. --gecos 'git version control' \
  5. --group \
  6. --disabled-password \
  7. --home /home/git \
  8. git

Now login to the newly created user, and set the path, and move to its home directory:

  1. sudo su git
  2. echo "PATH=$HOME/bin:$PATH" &gt; ~/.bashrc
  3. cd

Run the gitolite setup command with the public key you copied to the tmp directory to initialized the location for gitolite use. Then change the $REPO_UMASK to 0027 when it opens the .gitolite.rc for editing during the installation process. If it didn’t open the file for any reason just open it up in vim (Note this is only if you’d like to use gitweb):

  1. gl-setup /tmp/rachel.pub

# change $REPO_UMASK = 0077; to $REPO_UMASK = 0027; # gets you ‘rwxr-x—‘
Afterward, it has made the gitolite-admin.git, testing.git repo and all other necessary files. Check to see that everything works by cloning the repo on the machine with the public/private key.

  1. git clone git@:gitolite-admin.git

Here is a resource about the syntax for the config file and adding users.

Install Gitweb:
This is the tricky bit… Install gitweb and the highlight app. Gitweb is located at ‘/usr/share/gitweb’

  1. sudo apt-get install highlight gitweb

Edit the gitweb config to the locations of the project list and repos, and add the highlighting bit at the end of the file:

  1. sudo vim /etc/gitweb.conf
  2. # change $projectroot to /home/git/repositories
  3. # change $projects_list to /home/git/projects.list
  4. # Add Highlighting at the end
  5. $feature{'highlight'}{'default'} = [1];

Change gitolite instance to allow access for gitweb. First append www-data to git group so gitweb can access the repos, then change the permissions for git repos and the projects list, finally restart apache:

  1. sudo usermod -a -G git www-data
  2. sudo chmod g+r /home/git/projects.list
  3. sudo chmod -R g+rx /home/git/repositories
  4. sudo service apache2 restart

Finally you need to tell gitolite which repo you want to show up in gitweb. To do this edit the gitolite.conf file from the gitolite-admin.git repo:

  1. repo testing
  2. RW+ = @all
  3. R = gitweb

在apache2上架设gitweb

安装gitweb

  1. sudo apt-get install gitweb

运行命令后,会自动创建
/var/www/gitweb
/etc/gitweb.conf # 配置文件
目录/usr/share/gitweb下的3个文件 git-favicon.png, git-logo.png, gitweb.css

注意: 如果未生成/var/www/gitweb这个目录,则需要克隆 kernel.org 项目,再拷贝该项目下的 git/gitweb 文件夹到 /var/www/

  1. git clone git://git.kernel.org/pub/scm/git/git.git

在 /etc/gitweb.conf 里配置 你希望显示到gitweb里的项目路径

  1. $projectroot = "/home/yourname/projectpath";

再手动创建一个文件/etc/apach2/conf.d/gitweb,内容如下:

  1. Alias /gitweb /usr/share/gitweb
  2. <Directory /usr/share/gitweb>
  3. Options FollowSymLinks +ExecCGI
  4. AddHandler cgi-script .cgi
  5. </Directory>

重启下apache2

  1. sudo /etc/init.d/apache2 restart

更换gitweb.css和gitweb.js, 下载项目

  1. git clone https://github.com/kogakure/gitweb-theme.git

进入目录执行

  1. ./setup -vi --install

说明
-v, –verbose Verbose output
-i, –interactive Pauses for confirmation at each step

编辑配置文件

  1. sudo vim /etc/gitweb.conf
  2. # change $projectroot to /home/git/repositories
  3. # change $projects_list to /home/git/projects.list
  4. # 在末尾加入代码高亮特性
  5. $feature{'highlight'}{'default'} = [1];
  6. # 添加snapshot类型支持
  7. $feature {'snapshot'}{'default'} = ['zip', 'tgz'];  
  8. $feature {'snapshot'}{'override'} = 1;

Gitolite安装

系统需求

  • 类unix操作系统
  • sh
  • git 1.6.6+
  • perl 5.8.8+
  • openssh 5.0+
  • 一个git用户
  • 一个openssh公钥

安装

  • 登录到git用户。如果没有给git用户设置密码,可以从root用户通过su切换过去。
  • 确认 ~/.ssh/authorized_keys 不存在
  • 将公钥放在 ~/YourName.pub
  • 运行下面的命令:
# 获取版本库
git clone git://github.com/sitaramc/gitolite
# 创建bin目录,用于存放安装后的文件
mkdir -p ~/bin
# 将gitolite安装到bin目录
gitolite/install -to ~/bin
# 使用YourName.pub公钥初始化版本库
./bin/gitolite setup -pk YourName.pub

管理用户和版本库

不应该手动在服务器端加入新的用户或者版本库。
gitolite使用一个特殊的版本库 gitolite-admin 来管理员用户和版本库,只要在这个版本库中修改并 push,服务器就会自动根据配置作出修改。

首先在客户端迁出版本库:

git clone git@host:gitolite-admin

如果在迁出的过程中询问密码,那么说明配置出了问题。一般情况是密钥配置错误。可以检查客户端的 ~/.ssh 下有没有 YourName 私钥。如果需要使用不同的密钥连接多个ssh服务器,可以编辑 ~/.ssh/config 进行配置。

进入 gitolite-admin 目录,其中的 keydir 目录是用来放置用户公钥的,而 conf/gitolite.conf 则是用来配置用户和版本库。

编辑 conf/gitolite.conf如下:

repo foo
    RW+ = alice
    RW = bob
    R = carol

上面的配置的含义是:

有一个名为 foo 的版本库;
用户 alice 对它有读、写、删除权限;
用户 bob 对它有读写权限;
用户 carol 对它仅有只读权限。
另外,需要找 alice/bob/carol 用户索要他们的公钥,保存在 keydir 目录中,命名为 alice.pub/bob.pub/carol.pub,然后提交这些改动, push 到服务器。
服务器会自动将公钥加入到 ~/.ssh/authorized_keys 中,并创建 foo 版本库。

foo 版本库的访问地址为 git@host:foo。

如果希望把 foo 版本库放在 bar 目录下,可以这样编辑配置文件:

repo bar/foo
    RW+ = alice
    RW = bob
    R = carol

此时foo 版本库的访问地址为 git@host:bar/foo。

 

参考地址http://zengrong.net/post/1720.htm