[Android] System source code upload

foreword

In the previous article, I wrote how to download the system source code: [Android] system source code download and compile

This article is about uploading the system source code.

In order to control the quality of Android code, Google uses Gerrit for CodeView, and uses Jenkins for code static detection and automatic verification.

Gerrit is a set of free and open source code review system tailored by Google for Android system development. It allows team members to review, comment, and modify code, and it is audited and validated before the code is merged into the trunk. Gerrit provides a web interface and a set of command-line tools for managing the code review process. It also supports many popular code hosting platforms such as GitHub and GitLab.

Git、Gerrit、Repo

  • Gerrit is a Git-based code review tool that helps teams with code review and management.
  • Repo is Google's open source multi-repository management tool, which can help manage multiple Git repositories in one project.
  • Git is a distributed version control system that helps teams manage code versions, collaborate on development, and track code changes

Gerrit, repo, and Git are all tools related to the Git version control system. When using Gerrit for code review, you typically use Git to manage code versions and repo to manage multiple Git repositories.

Therefore, these three tools are often used together to help teams with efficient code development and management.

Next, let's talk about how we push the local Android system source code to Gerrit.

Configure gerrit public key

generate ssh keypair

ssh-keygen -t rsa

After entering the command, press the Enter key all the way until the ssh key pair is successfully generated. The key pair generated by default will exist in the ~/.ssh directory.

id_rsa.pubAmong them is the public key id_rsaand the private key. The private key must be guaranteed not to be leaked for security. The public key needs to be configured on the gerrit website for use.

Then we can view the public key with the command:

cat ~/.ssh/id_rsa.pub

insert image description here

After getting the public key, we need to set it to the Gerrit website, the specific steps are:

`Settings -> SSH Keys -> New SSH key -> 粘贴 ssh 公钥 -> 点击 ADD NEW SSH KEY` 

After the addition is complete, we can verify it in the terminal:

ssh -p 29418 [email protected]

If the verification is normal, the following information will be printed on the terminal:

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

  Hi username, you have successfully connected over SSH.

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

  git clone ssh://[email protected]:29418/REPOSITORY_NAME.git

Connection to gerrit.osc.com closed.

Push an empty project

After Gerrit is configured, we can try to push an empty project to it. In the terminal, execute the following command:

ssh -p 29418 用户名@gerrit-shengxue.youdao.com gerrit create-project --branch 分支名 --empty-commit 工程名/manifest 

Push Android source code

default.xml configuration file

This file is the default manifest file of the Repo tool in the Android source code, which defines all Git repositories contained in the Android source code tree and their corresponding branches and commit IDs.

When using the Repo tool to download the Android source code, default.xml the file , and all related code repositories will be downloaded and synchronized according to the Git repository information defined in the file. Therefore, the default.xml file is very important, which determines which Git repositories are included in the Android source code tree and their version information.

Developers can modify this file as needed to specify specific versions of Git repositories and branch information, or to add new Git repositories.

default.xmlThe files are generally located in the .repo/manifests/ directory under the source code directory, and the code is as follows:

insert image description here

In default.xmlthe file , the remote tag is used to define the remote server information where the Git warehouse is located, and the default tag is used to specify the default remote server and branch information.

  • remote tag: used to define a remote server, including server name, URL and fetch attributes
  • default tag: used to specify the default remote server and branch information, including remote and revision attributes. The remote attribute specifies the default remote server name, while the revision attribute specifies the default branch or commit ID

In fact, we can add multiple remotes in the manifest file, so that when operating the git warehouse, we can use different remote names to synchronize codes from different remote warehouses. like:

<remote fetch=".." name="remote1" review="http://gerrit-hw.xxxx1.com/"/>
<remote fetch=".." name="remote2" review="http://gerrit-hw.xxxx2.com/"/>

In addition, in default.xmlthe file , each project tag is used to define a Git warehouse, and specify its name, path and other related information. Specifically:

  • name attribute: specifies the name of the Git repository
  • path attribute: specifies the path of the Git repository in the local file system. When using the repo tool to download and synchronize the Android source code, this attribute specifies the local directory in which the repository will be stored
  • Other attributes: In addition to the name and path attributes, the project tag can also contain other attributes, such as the revision attribute, which is used to specify the commit ID or branch name of the Git repository, and the remote attribute, which is used to specify the remote server where the Git repository is located

By using the name and path attributes, the default.xml file can accurately specify the name and local path of each Git repository, which can ensure that the repo tool can correctly download and synchronize the Android source code.

Create and upload manifest repository

After understanding default.xmlthe file , we know it is very important. Generally speaking, Android will upload it in a separate manifest library, and then we need to create and upload the manifest library. Specific steps are as follows:

First, you need to create a manifest library on Gerrit. It can be created manually on Gerrit, or by the following command:

ssh -p 29418 用户名@gerrit-xxx.com gerrit create-project --branch 分支名 --empty-commit 工程名/manifest 

After creating the manifest library, we clone the manifest locally and execute the command:

git clone -b 分支名 "http://[email protected]/工程名/manifest"

Then default.xmladd , and finally push the modification to the server:

git push origin -f

Batch upload Git repository using Bash script

After creating and uploading the manifest library, the next goal is to create other libraries in the Android source code.

First of all, we need to know that the Android source code is composed of hundreds of git repositories. For example, the Android 8.1 source code contains more than 1,000 Git repositories.

All these repositories can be downloaded, managed and synchronized through the Repo tool. Some of these repositories are required, including the Android platform frameworks and core applications, while others are optional, such as third-party libraries and drivers. Therefore, if you want to upload the Android system source code to Gerrit, you must first determine the names of all Git repositories.

In the repo tool, project.listfiles are used to list all Git repositories in the current source code. project.listThe file is generally located in the .repo/ directory under the source code directory, and its structure is as follows:

insert image description here

Note: The manifests repository is not included in the project.list file, because the manifests repository is a repository for storing the manifest files of the repo tool, and it is not a Git repository for ordinary Android applications or system components. In fact, the information of the manifests warehouse is directly written in the code of the repo tool, rather than managed through the project.list file. If you want to view or modify the information of the manifests warehouse, you need to directly edit the code of the repo tool instead of modifying the project.list file.

Since there are too many libraries, we use Bash scripts here to help us upload.

First, create a create.shfile ( create.shthe file and project.listthe file are placed in the same directory), and write the code as follows:

#!/bin/bash

# 验证传入的参数是否至少有一个参数
if [ $# -lt 1 ] ; then
    echo "usage : ./create.sh project.list";
    exit 1;
fi

# 读取传入的参数作为项目列表
project_list=$1

# 提示输入项目名称
echo -n 'input project name:'
read project_name

# 如果项目名称为空,则退出脚本
if [ -z "$project_name" ]; then
    echo "project name can not be empty"
    exit
fi

# 提示输入分支名称
echo -n "input branch name:"
read branch_name

# 如果分支名称为空,则使用项目名称后加上 _master 作为默认分支名称
if [ -z "$branch_name" ]; then
    branch_name="${project_name}_master"
    echo "use default branch name:$branch_name"
else
    echo "branch name:$branch_name"
fi

# 提示输入帐户名称
echo -n "input account name:"
read user

# 如果帐户名称为空,则退出脚本
if [ -z "$user" ];then
    echo "account can not be empty"
    exit
fi

# 遍历每个项目名称并创建 Gerrit 项目
for i in  `cat ${
     
     project_list}`;
do
    echo $i
    # 使用 Gerrit 命令行工具通过 SSH 创建每个项目,并设置指定的分支名称和空提交
    echo "ssh -p 29418 ${user}@gerrit-shengxue.youdao.com gerrit create-project --branch $branch_name  --empty-commit ${project_name}/$i"
    ssh -p 29418 ${user}@gerrit-shengxue.youdao.com gerrit create-project --branch $branch_name  --empty-commit ${project_name}/$i
done

When ready, execute the script in the terminal:

./create_projects.sh project.list

The script will ask for three names,

  • Project name— consistent with the project name when the manifest was created earlier
  • Default branch name— consistent with the branch name when creating the manifest earlier
  • Username — the user used to create the repository

Wait for the execution to complete, and you can go to Gerrit to confirm whether the creation is successful.

code upload

After creating all the Git repositories, the next step is to upload the source code to the repositories.

Through repo forallthe command , we can traverse all warehouses and execute the corresponding instructions, namely:

repo forall -c "对应指令"

First, we enter the root directory of the source code and switch to the branch corresponding to the warehouse we created on Gerrit:

repo forall -c "git checkout test_branch"

Merge the source code into the test_branch branch, use the rebase or merge command

repo forall -c "git rebase aosp_branch"

Push all source code to Gerrit's test_branch branch

repo forall -c "git push origin test_branch -f"

Guess you like

Origin blog.csdn.net/yang553566463/article/details/129959660