Android repo magic

Source: Internet
Author: User
Tags git commands
Document directory
  • Shell script or Python?
  • Bootstrap and real Repo
  • The repo Bootstrap script calls init to complete the first-stage initialization only.
  • Second-stage repo init
  • Related posts

This article Reprinted from: http://www.worldhello.net/2010/08/31/1915.html

This article is an advanced description of repo. It describes the implementation principles of repo.

Android repo magic Android provides enterprises with a new market. Large enterprises and small enterprises are on the same starting line. To study the development of Android, especially the Android system's core or driver, you must first create an Android version library management mechanism by cloning it locally. Android uses Git as a code management tool and developed Gerrit for code review to better manage code in a centralized manner. It also developed the Repo command line tool to encapsulate some Git commands, organize over Git libraries effectively. It is not a simple task to clone and manage the hundreds of Git libraries. In research
During the Repo process, many documents are found on the Google Group, which is not "over the wall. Do not do anything illegal. Read the repo Code directly.

How to create a local Android library image

If you understand the implementation of Repo, refer to Using Repo and Git, it is not difficult to create a local android version library image:

  • Download the repo Bootstrap script

    $ curl http://android.git.kernel.org/repo >~/bin/repo$ chmod a+x ~/bin/repo$ export PATH=$PATH:~/bin
  • Provides the -- mirror parameter to call repo init to create a git version database clone.
    $ repo init -u git://android.git.kernel.org/platform/manifest.git --mirror
    • When -- morror is used, the next step and the source synchronization are organized locally according to the source version library. Otherwise, the Organization is reorganized and detected locally according to the method specified by manifest. xml.
  • Start and source Synchronization
    $ repo sync
  • Modify manifest, modify the GIT repository address, and point to the local git server.
    • Modify the existing xml file in the platform/manifest. git library, or create a new xml file.
    • Change git address to local address, submit and push
  • After the local repo image is created, you can use the manifest library after the local change when executing the repo init, and then execute the repo sync to synchronize it based on the local version library.
  • You can also modify the repo so that you do not have to initialize the repo tool or perform operations on the local network...
What does Repo init do?

In fact, after obtaining information about the customer's use of repo, first download the repo execution script to start the study.

curl http://android.git.kernel.org/repo >~/bin/repo

Is there only 600 lines of python code? It should be very simple. However, we can see that this is far from the case.

Shell script or python?

First, the repo script uses a magic: From the Perspective of shebang In the first line of the script, it should be a shell script, but it is full of Python syntax. Why?

 1 #!/bin/sh 2 3 ## repo default configuration 4 ## 5 REPO_URL='git://android.git.kernel.org/tools/repo.git' 6 REPO_REV='stable' 7 8 # Copyright (C) 2008 Google Inc.   ...22 magic='--calling-python-from-/bin/sh--'23 """exec" python -E "$0" "$@" """#$magic"24 if __name__ == '__main__':25   import sys26   if sys.argv[-1] == '#%s' % magic:27     del sys.argv[-1]28 del magic

Magic is in the second line. it cleverly writes a code that can be understood by Python and shell scripts through the three quotation marks of Python. In this field, the Code enters the world of Python through shell scripts.

Bootstrap and real repo

The repo downloaded through curl is not a complete repo script, but a bootstrap. When a repo is executed, it downloads the complete repo code and transfers control to the real repo. Through the main function, you can see the start of the repo operation, and try to find the local real complete repo code to hand over control:

544 def main(orig_args):545   main, dir = _FindRepo()586   try:587     os.execv(main, me)

The _ findrepo () of the first row will recursively look up ". repo/Main. py" in the current directory, and hand over the control (row 545) if found ).

The Repo bootstrap script calls init to complete the first-stage initialization only.

The bootstrap script of Repo only supports two Commands: Help and init, while init only clones the repo version Library (that is, the complete repo tool is installed), and then the control is transferred. Executing init in repo Bootstrap can provide many parameters, but in fact, only two parameters are used for the first-stage initialization (and both have default values)

  • Parameter: -- repo-url = git repository address of the URLrepo tool. Default Value: git: // android.git.kernel.org/tools/repo.git
  • Parameter: -- repo-branch = REVISION uses the repo version library, that is, the branch or milestone name of the repo git library. The default value is stable.
Second-stage repo init

Execute repo init in the second stage, and the control has been handed over to the script of the cloned repo git library. The repo git library is cloned/detected to the. repo/repo subdirectory in the current directory where the repo init command is executed. The main execution script is. repo/Main. py. Main. py then runs the repo init command. The repo code is well organized. Under the. repo/subcmds/subdirectory, It is the processing script of each repo command. The second-stage script of Repo init is composed of. repo/subcmds/init. py
Executed. The second stage is mainly completed:

  • Clone the manifest Git library provided by the-u parameter, such as when cloning the android Library:

    $ repo init -u git://android.git.kernel.org/platform/manifest.git
  • If the-B REVISION or -- manifest-branch = REVISION parameter is not provided, the master branch of the manifest Git library is checked out.
  • If the-m NAME. xml or -- manifest-name = NAME. xml parameter is not provided, the default value default. xml is used.
  • If the -- mirror parameter is provided, subsequent synchronization operations will be reflected accordingly.
What does Repo start do?

Android source code Network Station in the use of the repo model, there is a picture: http://source.android.com/images/git-repo-1.png, introduced the use of Repo process. "Repo start" is the first action after "Repo sync. So what is this action? Thanks to the repo encapsulation of git operations, the "Repo start" command only has 68 lines of code.

 37   def Execute(self, opt, args): 41     nb = args[0] 47     projects = [] 48     if not opt.all: 49       projects = args[1:] 54     all = self.GetProjects(projects) 57     for project in all: 59       if not project.StartBranch(nb): 60         err.append(project)

If you see row 59th, You can execute project. startbranch one by one for multiple git version libraries of the projects synchronized by repo. NB is the first parameter of Repo start, that is, the branch name. For startbranch code, in project. py:

 857   def StartBranch(self, name): 858     """Create a new branch off the manifest's revision. 859     """ 894     if GitCommand(self, 895                   ['checkout', '-b', branch.name, revid], 896                   capture_stdout = True, 897                   capture_stderr = True).Wait() == 0: 898       branch.Save() 899       return True

In this case, Repo start <branch_name> is to create a working branch for each version library one by one to facilitate work under this branch. Readers can find the implementation of various repo commands and crack their doubts.

Related Posts
Git Chinese localization 28 Feb 1, 2012 1 Comment and 5 Reactions
Gitolite administrator custom commands 2011 Nov 4 Comments and 0 Reactions
Gitolite wildcard version library custom authorization 2011 Nov 0 comments and 0 Reactions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.