大幅减小OH代码占用磁盘空间的几个小技巧

2024年 6月 4日 22.0k 0

大幅减小OH代码占用磁盘空间的几个小技巧-1

想了解更多关于开源的内容,请访问:

51CTO 鸿蒙开发者社区

https://ost.51cto.com

前言

随着版本的演进和更新,OpenHarmony的代码量越来越大,非常消耗磁盘空间。以 v4.1-Release
版本为例,参考官方文档提供的以下四条命令获取的OpenHarmony代码已经接近50G(包含//.repo/、//prebuilts/、checkout到工作区的代码和通过git
lfs pull下载的大文件):

$ repo init -u git@gitee.com:openharmony/manifest.git -b 
refs/tags/OpenHarmony-v4.1-Release --no-repo-verify
$ repo sync -c
$ repo forall -c 'git lfs pull'
$ ./build/prebuilts_download.sh
1.
2.
3.
4.

如果完成了三类系统(轻量、小型、标准)的全编译,则会产生几十个G的 .ccache 和几十个G的 //out/
,整体已经占用超过150G的磁盘空间了。

下面我们组合使用repo和git命令的一些参数,可以大幅减小OpenHarmony代码的磁盘占用空间。

repo sync -m 参数

在repo sync 命令增加 -m 参数,指定只同步(下载或clone)manifest仓库中的某个manifest文件。

例如,不使用 -m 参数的官方命令:

$ repo init -u git@gitee.com:openharmony/manifest.git -b 
refs/tags/OpenHarmony-v4.1-Release --no-repo-verify
1.

会在 //.repo/manifest.xml 中指定同步 //.repo/manifests/default.xml 文件,该文件中


  

会指定下载全量的OpenHarmony代码,包括了开源出来的所有chipsets仓库代码,这样会包含我们并不需要的非常多的仓库。

而通过增加 -m 参数则可以只下载我们需要的chipsets仓库代码,例如:

$ repo init -u git@gitee.com:openharmony/manifest.git -b 
refs/tags/OpenHarmony-v4.1-Release -m chipsets/hispark_taurus.xml 
--no-repo-verify
1.

上述命令增加 “-m chipsets/hispark_taurus.xml” 后,//.repo/manifest.xml 的描述则会指定同步
//.repo/manifests/chipsets/hispark_taurus.xml 文件:


  

这样可以节省不少磁盘空间。

如果我们还需要其它的chipsets的manifest,那我们可以直接手动修改 //.repo/manifest.xml
的描述,按规则增加对应的chipsets的manifest即可。

或者,不加 -m 参数,也可以直接修改 //.repo/manifests/default.xml 文件的描述,再去repo
sync,也可以达到同样的效果:


  
  
  

repo sync -g 参数

在repo sync 命令增加 -g 参数,可以对各仓库的groups字段进行过滤,匹配 -g
参数的仓库才会同步(下载或clone)到本地。例如:

$ repo init -u git@gitee.com:openharmony/manifest.git -b 
refs/tags/OpenHarmony-v4.1-Release -m chipsets/hispark_taurus.xml -g 
ohos:mini,ohos:small --no-repo-verify
$ repo init -u git@gitee.com:openharmony/manifest.git -b 
refs/tags/OpenHarmony-v4.1-Release -m chipsets/dayu200.xml -g ohos:standard 
--no-repo-verify

但是需要注意,这个 groups 标签,看上去维护得并不好,甚至有些混乱。

有些仓库只适用于标准系统,也添加了ohos:mini,ohos:small标签;有些仓库虽然没有ohos:mini,ohos:small标签,但是在执行
./build/prebuilts_download.sh
时,则是需要依赖到的;有些仓库则没有ohos:mini,ohos:small标签,但会在编译过程中或者镜像打包阶段依赖到而导致编译错误;这些都需要根据实际情况自行修改
ohos.xml 中对应仓库的 groups 标签然后再同步和编译代码。

repo sync --depth 参数

ohos.xml 中对Linux内核仓库的描述:

有一个 clone-depth=“1”
的字段,该字段表示在同步(下载或clone)远程仓库到本地时,只下载远程仓库默认分支的最新一次提交记录到本地,而不是将所有的历史记录都同步到本地,这样可以大幅减少仓库代码的磁盘占用空间。

如果只想对某些仓库(特别是历史记录特别长的仓库)做 clone-depth="num"的操作,可以像上面一样,在对应仓库的描述信息增加
clone-depth=“num” 字段就行了;如果想对所有仓库进行一次性的操作,那就给 repo sync 命令增加一个 --depth 参数。例如:

$ repo init -u git@gitee.com:openharmony/manifest.git -b 
refs/tags/OpenHarmony-v4.1-Release -m chipsets/hispark_taurus.xml -g 
ohos:mini,ohos:small --no-repo-verify --depth=1
$ repo init -u git@gitee.com:openharmony/manifest.git -b 
refs/tags/OpenHarmony-v4.1-Release -m chipsets/dayu200.xml -g ohos:standard 
--no-repo-verify --depth=1

git --depth 参数

通过 repo sync --depth=1
参数拉取的OpenHarmony代码,默认只获取远程仓库默认分支的最新一次提交记录到本地,并不包含更多的历史提交记录和其他费默认分支的记录。

对某个具体仓库,可以通过git命令和参数进行一些操作,获取更多的历史提交记录和其他费默认分支的记录到本地。

git 的 --depth参数:

--depth 
Create a shallow clone【浅克隆】 with a history truncated to the specified 
number【depth】 of commits.
Implies【隐含参数是】--single-branch【仅获取远程仓库默认分支的最新一次提交记录】unless【除非显式指定参数】--no-single-branch【通过这个参数指明获取远程仓库所有分支的最新一次提交记录】is 
given to fetch the histories near the tips of all branches.
If you want to clone submodules shallowly, also pass 
--shallow-submodules.

即在默认的 --single-branch 情况下,只获取默认分支到本地;如果要查看其他分支的代码和提交记录,可以按如下一些操作进行处理。

# 例如本地只有OpenHarmony-v4.1-Release的记录,想要使用远程的OpenHarmony-v3.2-Release分支,
# 这样操作就可以把远程的OpenHarmony-v3.2-Release分支拉取到本地进行切换和使用
$ git remote set-branches origin OpenHarmony-v3.2-Release
$ git fetch --depth=1 origin 
OpenHarmony-v3.2-Release:OpenHarmony-v3.2-Release
$ git checkout OpenHarmony-v3.2-Release

git-sparse-checkout 参数

对于特定的仓库,git 还有一个稀疏检出的操作可以稍微减少checkout的代码量,更重要的是这个参数可以让工作区的代码目录更清爽。

例如,对于 //vendor/hisilicon/ 仓库,默认是:

ohos@ohos:~/Lite/A41Rel/vendor/hisilicon$ ls -l
drwxrwxr-x 13 ohos ohos 4096 5月 30 15:26 ./
drwxrwxr-x 7 ohos ohos 4096 4月 29 17:48 ../
drwxrwxr-x 3 ohos ohos 4096 5月 30 15:26 .git/
drwxrwxr-x 2 ohos ohos 4096 5月 30 15:26 .gitee/
-rw-rw-r-- 1 ohos ohos 84 5月 30 15:26 .gitignore
drwxrwxr-x 6 ohos ohos 4096 5月 30 15:26 hispark_aries/
drwxrwxr-x 4 ohos ohos 4096 4月 14 11:50 hispark_pegasus/
drwxrwxr-x 3 ohos ohos 4096 5月 30 15:26 hispark_pegasus_mini_system/
drwxrwxr-x 7 ohos ohos 4096 5月 30 15:26 hispark_phoenix/
drwxrwxr-x 6 ohos ohos 4096 4月 14 00:21 hispark_taurus/
drwxrwxr-x 5 ohos ohos 4096 5月 10 09:18 hispark_taurus_linux/
drwxrwxr-x 6 ohos ohos 4096 5月 30 15:26 hispark_taurus_mini_system/
drwxrwxr-x 7 ohos ohos 4096 5月 30 15:26 hispark_taurus_standard/
-rw-rw-r-- 1 ohos ohos 10347 5月 30 15:26 LICENSE
-rw-rw-r-- 1 ohos ohos 6854 5月 30 15:26 OAT.xml
-rw-rw-r-- 1 ohos ohos 1345 5月 30 15:26 README_zh.md
drwxrwxr-x 6 ohos ohos 4096 5月 30 15:26 watchos/

这里面有很多项目是我们平常基本上用不到也改不到的,放在这里很碍眼,通过hb
set选择项目时,也会出现太多的选项,因此,可以使用git-sparse-checkout的配置来只checkout我们想要的文件夹(项目)。

可以在这个仓库目录下执行:

git config core.sparsecheckout true
# true 或 1,enable sparsecheckout
# false 或 0,disable sparsecheckout

该命令会在 //vendor/hisilicon/.git/config 文件的 [core] 段新增一个 sparsecheckout = true
的配置,enable 了sparsecheckout 功能,然后再执行:

git sparse-checkout set hispark_pegasus hispark_taurus 
hispark_taurus_linux
或者
echo "hispark_pegasus hispark_taurus hispark_taurus_linux" > 
.git/info/sparse-checkout

作用都是将需要checkout的目录和文件列表写入到 //vendor/hisilicon/.git/info/sparse-checkout
文件中去,而不在该文件中的目录和文件则不会checkout出来(但这些文件的objects对象,还是在本地仓库中的,只是没有解压到工作区而已),而我们的修改和提交,也不会影响到未checkout的目录和文件。

ohos@ohos:~/Lite/A41Rel/vendor/hisilicon$ ll
drwxrwxr-x 6 ohos ohos 4096 5月 30 15:46 ./
drwxrwxr-x 7 ohos ohos 4096 4月 29 17:48 ../
drwxrwxr-x 3 ohos ohos 4096 5月 30 15:46 .git/
drwxrwxr-x 4 ohos ohos 4096 4月 14 11:50 hispark_pegasus/
drwxrwxr-x 6 ohos ohos 4096 4月 14 00:21 hispark_taurus/
drwxrwxr-x 5 ohos ohos 4096 5月 10 09:18 hispark_taurus_linux/

当我们需要把全部的目录和文件列表全部checkout出来的时候,可以直接执行:

git sparse-checkout set *
或者
echo "*" > .git/info/sparse-checkout

然后重新checkout一下当前的分支即可。

注意:

实测发现,是否执行 “git config core.sparsecheckout” 命令来enable或disable
sparsecheckout都没关系(不知道是git版本问题还是bug),只要有 .git/info/sparse-checkout
文件,都可以通过改写该文件达到稀疏检出的目的。

补充

经过上述命令和参数的组合使用,可以大幅减少OpenHarmony仓库和代码所占用的磁盘空间,但是三大巨头(//prebuilts/、//out/、.ccache)仍然是占用着非常大的磁盘空间。

想了解更多关于开源的内容,请访问:

51CTO 鸿蒙开发者社区

https://ost.51cto.com

相关文章

猎豹浏览器怎么更改下载路径
Apache的URL缩短功能如何实现
Apache的点击劫持保护如何设置
Apache的HSTS功能是什么如何启用
Apache的X-Frame-Options如何配置以防止点击劫持
Apache的Content Security Policy如何设置

发布评论