1、Android source code management,An introduction to Git, Repo, and Gerrit for Android developers.,Toolchain overview,Our Android source code is managed very differently from the OSE phones. Weve fully embraced the Google toolchain, which means a whole new set of tools to work with.,ClearCase,Git,CME,R
2、epo,DeliveryWeb,Gerrit,Version control,Repository mgmt,Delivery,Code review,Android 是谷歌(Google)开发的适合手持设备的操作系统,提供了当前最吸引眼球的开源的手持设备操作平台,大有超越苹果(A)专有的 iOS 系统的趋势。而 Android 的源代码就是使用 Git 进行维护的。Android 项目在使用 Git 进行源代码管理上有两个伟大的创造,一个 是用 Java 开发的名为 Gerrit 的代码审核服务器,另外一个就是本章要重点介绍的repo。,Introducing Repo,Repo is a
3、Google-developed tool for managing multiple Git repositories and submitting code for review to the Gerrit code review system. The Android platform source code consists of hundreds of repositories. Repo keeps track of the available repositories and helps us perform tasks on all of these usually with
4、one single command. Like CME, Repo wraps but does not replace the underlying version control system. In your daily work youll use both Git and Repo. The collection of git repositories is recorded in a manifest file. The manifest file is an XML file with a list of repositories and corresponding branc
5、hes, revisions, or tags. It is quite similar to the Modules section of a 1551 file.,The most common Repo commands,$ repo sync Downloads the latest changes in all repositories, rebasing your own branches as necessary. $ repo start Create a new branch for working on a topic. $ repo upload Upload one o
6、f more changes to the Gerrit code review system.,Introducing Gerrit,Gerrit is a Google-developed tool thats used by their Android open source project (AOSP). It is purely web based and relies on Git for storing the source code. Each Git commit becomes a change when uploaded to Gerrit. A change is su
7、bject to review and verification by one or more parties. The uploader of a change can at any time update the change. When the change is verified and approved, the uploader will submit the change to the designated branch. After the change has been submitted, it is no longer possible to update it. Ger
8、rit automatically tracks dependencies between changes in the same git.,Integrated change,The life of a source code change,Git,Hack hack hack,git commit,repo upload,Reviewable,Code reviewed,Verified,Code reviewed & verified,Git,Submit,Developer 1,Developer 2,Git,repo sync,Change,Change,Why Gerrit,Ger
9、rit is not only used by Google internally, it is used to maintain the OSS part of Android.Puts an extra, protective layer around the integration branch.With Gerrit, everyone can be a contributor, regardless of team affiliation.We dont want to let anyone check in anything at anytime, thats why we hav
10、e a review step.So, having clean commits helps the reviewer, and thus also yourself, in addition to making life easier in maintenance.,Basic workflow with repo and Gerrit,repo init u git:/ b ohd-cupcake-deckard repo sync cd myproject repo start mytopic057 # editeditedit git status git add git status
11、 git commit repo sync; repo uploadNow Gerrit takes over and you start over on a new branch $ repo start newtopic058 .,Repo init,repo init u -b Running repo init will download the manifest and repo repositories. Where repo can find the manifest repository is handled with the -u option. If a manifest
12、branch is specified with the -b option, this branch of the manifest git will be checked out. If no branch is specified, the master branch of the manifest will be used.Example:repo init -u git:/ b ohd-cupcake-deckard,Repo sync,repo sync .repo sync will iterate through the specified or all repositorie
13、s listed in the manifest and download the latest changes.After all changes are downloaded, your topic branch will be rebased on top of the latest version of the branch listed in the manifest for that repository.Normally, this version will be the latest approved version on the parent (release/integra
14、tion) branch.Only non-delivered topic branches will be rebased with repo sync.Never manually resolve any upload conflicts with git merge!,Repo start,repo start -all | .repo start creates a new branch starting from the revision (of the corresponding project) that is listed in the manifest.When using
15、Gerrit, this means that the branch will always be based on the latest approved software without any dependencies to changes that have not passed review.,Repo upload,repo upload . | -replace repo upload will push your changes to Gerrit for review. All commits not already delivered will be uploaded. E
16、ach commit will be a new change.If you have amended your commit, you can replace the change set for a Gerrit change by using the -replace option.,Repo download,repo download project /Downloads and checks out the specified change from GerritThis commit can be amended and uploaded again using repo upl
17、oad -replace,Repo abandon,repo abandon If you realize that what you were working on should be scrapped, you can abandon this topic. repo abandon only affects your local repository.You can also abandon a change in Gerrit. This will only affect that change in Gerrit and not affect any Git repository.,
18、Repo prune,repo prune . When you have uploaded your topics and they have been merged by Gerrit, you can run repo prune. repo prune will delete topic branches that are already merged to the integration branch. Only topic branches that have been merged to the integration branch will be deleted this is
19、 a safe command to run.,Repo forall,repo forall . -c .Runs a loop which will execute on every git repository.This is handy when there is no corresponding repo command for what you want to do.,Gerrit doesnt merge my commit,Your commit was approved and you submitted the change, but Gerrit still says “
20、Review in progress”Check the change comments. If Gerrit had problems merging the commit, it will be listed here.If a merge conflict was the problem, make sure you have the latest changes synced to your workspace. Checkout your topic branch with the change in question on it and rebase it against the
21、parent branch. The you can replace the old commit.,$ repo sync $ git checkout my-old-topic $ git rebase m/sw-integration $ git mergetool $ git rebase -continue $ repo upload -replace .,I forgot repo startwheres my commit?,If you forget to run repo start to create a topic branch, you will by default
22、work on a “detached head”. I.e. any commits you make after that will float around headless. You can recover. Run git reflog and find the commit you made. Start a new topic branch Then cherry-pick the commit using the first characters from the hash key as listed in the reflog and you are back on trac
23、k.,$ git reflog d230d0e. HEAD0: commit: Removed Camera/Album app. Its in the Camera apk. . $ repo start new-topic . $ git cherry-pick d230d0e,Changeset abuse Scenario One,You made a really cool change and committed it. Unfortunately, that last “harmless” edit you made turned out to break everythingI
24、f you worked on a topic branch, you have only harmed yourselfYou edit the file so that it works again and commit the fix.Now you want to share your nice new and working feature with othersYou upload your topic branch to Gerrit. Repo will upload ALL your commits on the topic branch including the brok
25、en oneSomeone discovers the error in the first commit during review. What happens?,Multiple commits in one change,Multiple commits multiple changes to review in Gerrit Which commit should be approved? How does the reviewer know what happened? If the broken commit is rejected, the second, containing
26、the fix, can never be merged as it depends on the rejected one.,Integration branch,Two commits,Upload,Two changes,Amending commits,If you discover that your previous commit was incomplete or broken you canAmend it!Check out the relevant topic branch Make your fix Add the changes to the index with gi
27、t add Now create a new commit, where you “fuse” your changes in the index with the previous commit:git commit -amendPresto changeo! You have a new commit with the fix in it, that replaces your previous one.,More than one “bad” commit?,Amending a commit only works with the previous one. If your funct
28、ionality are spread over more commits, you must use other means to put it in one single commitLuckily, this can be done, as you will see shortly,Checkpoint commits,You might commit when You go for lunch Try out some “experimental” code Try different solutions to the same problem Or many other reason
29、sThis is all fine butIf uploaded, each commit will be registered as a separate change in GerritBefore you share your changesmerge or “squash” related changesremove unrelated changes so that you only get a “clean” historyThen the integration branch will look nice and you will help the reviewer Or, you can temporarily put your changes in the stash ,