本文已参与「新人创作礼」活动,一起开启掘金创作之路。
1 Tools introduction
We are using Git, Repo, Gerrit and Jenkins tools in our development system.
GIT | an open source version-control system designed to handle very large projects that are distributed over multiple repositories. In the context of Android, we use Git for local operations such as local branching, commits, diffs, and edits Git Tutorial for Beginners: Command-Line Fundamentals |
---|---|
Repo | Repo is a repository management tool that Google built on top of Git.Repo is a tool that makes it easier to work with Git in the context of Android. To install Repo:Download the Repo tool and ensure that it is executable:wget https://storage.googleapis.com/git-repo-downloads/repo chmod a+x reposudo mv repo /usr/local/bin/export PATH=$PATH:/usr/local/binRepo tool Command reference |
Gerrit | Gerrit is a web-based code review system for projects that use git. Gerrit encourages more centralized use of Git by allowing all authorized users to submit changes, which are automatically merged if they pass code review. In addition, Gerrit makes reviewing easier by displaying changes side by side in-browser and enabling inline comments |
2 Gerrit account setting
2.1 Generate SSH Keys
-
ssh-keygen
- Keep pressing ENTER for subsequent steps, which will generate public/private rsa key pair.
- Copy the contents of file "cat ~/.ssh/id_rsa.pub" and add it to ssh keys section on Gerrit server.
2.2 Setting up account with Gerrit
Gerrit supports LDAP authentication. Your domain account can be used as a gerrit account, Please log into Gerrit http://172.28.100.178:8091/ with your domain account first, then the administrator assigns project permissions for you.
2.3 Set up your .gitconfig
Replace your username and email here
.gitconfig
$ vim ~/.gitconfig [review "http://172.28.100.178:8091/``"] ``username = $``{GERRIT_USERNAME} [user] ``name = $``{USERNAME} ``email = $``{EMAIL} [color] ``ui = auto``[git] ``editor = vim |
---|
3 Work with Gerrit
3.1 Fetch code
The following is the command fetch 820A develop branch code
sync 820A code
repo init -u ssh://172.28.100.178:29418/YFIC/manifest -b 820a-dev -m 820a-dev.xml --repo-url=ssh://172.28.100.178:29418/YFIC/repo --no-repo-verify repo sync -c -j32 |
---|
3.2 Create local branch to work
Create local development/feature/bug-fix branch in specific git repository where you want to modify/add/delete source code :
| ``` cd to respective repos git checkout -b origin/820a-dev
Example git checkout -b integration origin/820a-dev
``` |
| ------------------------------------------------------------------------------------------------------------------------------------------- |
Re-sync
re sync before new work
**whenever you start working on new change you need to repo sync to latest target baseline, create new branch and then start making changes.** **Please make sure you are working on latest target branch not your local Branch. If you make changes on top of your other changes which is not yet** **in baseline then you will see merge conflict as parent commit for your change does not exist in the baseline.**
## 3.3 Work locally and push to remote server
1. **Make your code changes**
1. **git add . in repos you have might have added new files.**
1. **git commit your changes and add commit message.**
There are two ways to add commit message, one is add it in the commit message when git commit . The other way is to add it on Gerrit web interface.
Click Edit → Click "Commit Message"→Edit the commit message→Click Save→Click Close→Done Editing→Publish Editing. You will find the commit message is updated successfully , and there generate a new CommitID as a new patch set.
![]()
4. **Use topic**
Sometimes you may submit code in several repos for one issue, you may need add topic to mark them, there are plenty of benefits, such as convenient to find your review from Gerrit,
good for integration , easy to find the related changes by looking trough the Gerrit open reviews.
**git push with topic**
| `cd to your modified repo` `git push origin <Your local branch>:refs/``for``/820a-dev/<Topic Name>` |
| --------------------------------------------------------------------------------------------------- |
If you did not use git push to push your changes then you need to add "Topic" name manually into the gerrit "Topic" section as shown below.
If your change depends on other developer change then you edit your change "Topic" name with Other developer change "Topic" Name in your change "Topic" Name with comma separated.
![]()
5. **Squash your local commits before push to gerrit. **
git rebase -i HEAD~N
N is number of commits you would like to squash.
If you have more than one commit on your local work directory, please squash these commits ,otherwise when you push to gerrit, every commit will generate an open review, it may not what you want.
## 3.4 Gerrit Review
**add reviewers**
Form My—》Changes, Select the change needs to be reviewed,click the button "Add Reviewer", add developers/function owner/CCB members as reviewers.
![]()
**Reviewing changes.**
![]()
![]()
![]()
**Developer submit the code.**
When labels have reached to highest value, the submit button will show.
![]()
# 4 Other useful tips about Gerrit
## 4.1 Search changes in Gerrit
[http://172.28.100.178:8091/Documentation/user-search.html](http://172.29.169.30:8091/Documentation/user-search.html)
## 4.2 Watch and subscribe a repo
You can fill in specific info in the "Only if" block ,then you can get the notification as your will.
![]()
## 4.3 Cherry pick changes from Gerrit
Cherry-pick from Gerrit UI
![]()
## 4.4 Solve conflict when gerrit shows there is conflict
- Download the latest code
- git checkout -b <local_branch_name>
- Excute cherry-pick command get from following place
![]()
- Solve conlict
After excute the command you will get below conflict
![]()
Check git status, you need to resolve conflict for files marked as red
![]()
Go to conflict file and search for HEAD and resolve conflict
![]()
After resolving conflict follow below steps.
git add <file name>
git commit --amend
git push origin <local branch name>:refs/for/master (take master branch for example).