Repo and git (2) repo source code analysis

Source: Internet
Author: User

Personal email: xiaokeweng@gmail.com

Next, let's take a look at the steps for using repo in the process of downloading the android source code provided on the official website:

Http://source.android.com/source/downloading.html

$ curl https://dl-ssl.google.com/dl/googlesource/git-repo/repo > ~/bin/repo$ chmod a+x ~/bin/repo$ repo init -u https://android.googlesource.com/platform/manifest

The first sentence is to download the repo from the official website ~ /Binfile. The path has been added to the environment variable in advance. In this way, you can run the repo in any path. After successful display:


After entering the file, we can see that the repo is there. If you want to run it, you need to change it to executable permissions. In fact, it is a shell script of hundreds of lines. In fact, this is not a real repo, but just a bootstrap. Although this shell script starts with shell syntax, the content is all python syntax, it is said that this is the magic of this script. We will analyze it briefly.

Ignore some comments:

magic='—calling-python-from-/bin/sh--' """exec" python -E "$0" "$@" """#$magic" if __name__ == '__main__':   import sys   if sys.argv[-1] == '#%s' % magic:     del sys.argv[-1] del magic

This part is very skillful in the repo script. It also implements the transition from shell to python. Because the execution mechanism of shell is somewhat different from that of python, first, an error occurs during shell execution and then stops. python ensures that the syntax is correct before execution. The above code is correct in both scripts.

(1) the code in shell is as follows:

Magic = '-calling-python-from-/bin/sh --'

"Exec" python-E "$0"
"$ @" # $ Magic"

2 "equals null, $0 equals the Script Name repo, $ @ is the parameter (such as init)

The syntax is equivalent:

Exec python-E
Repo init #-calling-python-from-/bin/sh --

(2) the code in python is as follows:

Magic = '-calling-python-from-/bin/sh --'

"Exec" python-E "$0" "$ @" # $ magic"

The three strings are equivalent to simple strings, # followed by comments.
Its syntax is equivalent:


Magic = '-calling-python-from-/bin/sh --'

Then exec enters python. shell exec creates a new process to replace the current process, and transfers all the data, heap, and stack. The PID remains unchanged, and then all of them are python active. As a result, I have a question: why does repo not directly use python, but overuses shell first? I don't know much about this problem.

On the Internet, I just found a foreign explanation as follows. In fact, this is not an explanation. It is just a method to execute Python on linux/unix. There is no reason for this method.

Http://effbot.org/pyfaq/how-do-i-make-a-python-script-executable-on-unix.htm

Next we will analyze in detail the repo script, the repo

Bootstrap is in ~ /Bin/path, this part of code will be typed into repo in different directories each time
Call it during init and load the complete repo file on the server through the network. The following is the complete tree structure of. repo after repo init. Therefore, the repo bootstrap should copy a copy and study it later.

. Repo
── Repo
── Color. py
── Color. pyc
── Command. py
── Command. pyc
── COPYING
── Docs
│ ── Manifest-format.txt.
── Editor. py
├ ── Editor. pyc
── Error. py
── Error. pyc
── Git_command.py
├ ── Git_command.pyc
── Git_config.py
├ ── Git_config.pyc
── Git_refs.py
├ ── Git_refs.pyc
├ ── Git_ssh
├ ── Hooks
│ ── Commit-msg
│ ── Pre-auto-gc
── Main. py
── Main. pyc
── Manifest_xml.py
── Manifest_xml.pyc
├ ── Pager. py
├ ── Pager. pyc
── Progress. py
├ ── Progress. pyc
── Project. py
── Project. pyc
── Repo
── Repoc
── Subcmds
│ ── Abandon. py
│ ── Abandon. pyc
│ ── Branches. py
│ ── Branches. pyc
│ ── Checkout. py
│ ── Checkout. pyc
│ ── Cherry_pick.py
│ ── Cherry_pick.pyc
│ ── Diff. py
│ ── Diff. pyc
│ ├ ── Download. py
│ ├ ── Download. pyc
│ ── Forall. py
│ ── Forall. pyc
│ ── Grep. py
│ ├ ── Grep. pyc
│ ── Help. py
│ ── Help. pyc
│ ── _ Init _. py
│ ── Init. py
│ ── _ Init _. pyc
│ ── Init. pyc
│ ── List. py
│ ── List. pyc
│ ── Manifest. py
│ ── Manifest. pyc
│ ── Overview. py
│ ── Overview. pyc
│ ├ ── Prune. py
│ ├ ── Prune. pyc
│ ── Rebase. py
│ ── Rebase. pyc
│ ── Selfupdate. py
│ ├ ── Selfupdate. pyc
│ ── Smartsync. py
│ ── Smartsync. pyc
│ ── Stage. py
│ ── Stage. pyc
│ ├ ── Start. py
│ ├ ── Start. pyc
│ ── Status. py
│ ── Status. pyc
│ ── Sync. py
│ ── Sync. pyc
│ ── Upload. py
│ ── Upload. pyc
│ ── Version. py
│ ── Version. pyc
── SUBMITTING_PATCHES
──
Tests
│ ── Fixtures
│ ── Test. gitconfig
│ ── Test_git_config.py
── Trace. py
── Trace. pyc

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.