大幅減小OH代碼占用磁盤(pán)空間的幾個(gè)小技巧
想了解更多關(guān)于開(kāi)源的內(nèi)容,請(qǐng)?jiān)L問(wèn):
前言
隨著版本的演進(jìn)和更新,OpenHarmony的代碼量越來(lái)越大,非常消耗磁盤(pán)空間。以 v4.1-Release 版本為例,參考官方文檔提供的以下四條命令獲取的OpenHarmony代碼已經(jīng)接近50G(包含//.repo/、//prebuilts/、checkout到工作區(qū)的代碼和通過(guò)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.
如果完成了三類系統(tǒng)(輕量、小型、標(biāo)準(zhǔn))的全編譯,則會(huì)產(chǎn)生幾十個(gè)G的 .ccache 和幾十個(gè)G的 //out/ ,整體已經(jīng)占用超過(guò)150G的磁盤(pán)空間了。
下面我們組合使用repo和git命令的一些參數(shù),可以大幅減小OpenHarmony代碼的磁盤(pán)占用空間。
repo sync -m 參數(shù)
在repo sync 命令增加 -m 參數(shù),指定只同步(下載或clone)manifest倉(cāng)庫(kù)中的某個(gè)manifest文件。
例如,不使用 -m 參數(shù)的官方命令:
$ repo init -u git@gitee.com:openharmony/manifest.git -b
refs/tags/OpenHarmony-v4.1-Release --no-repo-verify
1.
會(huì)在 //.repo/manifest.xml 中指定同步 //.repo/manifests/default.xml 文件,該文件中
<include name="ohos/ohos.xml" />
<include name="chipsets/all.xml" />
會(huì)指定下載全量的OpenHarmony代碼,包括了開(kāi)源出來(lái)的所有chipsets倉(cāng)庫(kù)代碼,這樣會(huì)包含我們并不需要的非常多的倉(cāng)庫(kù)。
而通過(guò)增加 -m 參數(shù)則可以只下載我們需要的chipsets倉(cāng)庫(kù)代碼,例如:
$ 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 的描述則會(huì)指定同步 //.repo/manifests/chipsets/hispark_taurus.xml 文件:
<include name="ohos/ohos.xml" />
<include name="chipsets/hispark/hispark.xml" />
這樣可以節(jié)省不少磁盤(pán)空間。
如果我們還需要其它的chipsets的manifest,那我們可以直接手動(dòng)修改 //.repo/manifest.xml 的描述,按規(guī)則增加對(duì)應(yīng)的chipsets的manifest即可。
或者,不加 -m 參數(shù),也可以直接修改 //.repo/manifests/default.xml 文件的描述,再去repo sync,也可以達(dá)到同樣的效果:
<include name="ohos/ohos.xml" />
<!-- include name="chipsets/all.xml" / -->
<include name="chipsets/hispark/hispark.xml" />
<include name="chipsets/dayu200/dayu200.xml" />
repo sync -g 參數(shù)
在repo sync 命令增加 -g 參數(shù),可以對(duì)各倉(cāng)庫(kù)的groups字段進(jìn)行過(guò)濾,匹配 -g 參數(shù)的倉(cāng)庫(kù)才會(huì)同步(下載或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
但是需要注意,這個(gè) groups 標(biāo)簽,看上去維護(hù)得并不好,甚至有些混亂。
有些倉(cāng)庫(kù)只適用于標(biāo)準(zhǔn)系統(tǒng),也添加了ohos:mini,ohos:small標(biāo)簽;有些倉(cāng)庫(kù)雖然沒(méi)有ohos:mini,ohos:small標(biāo)簽,但是在執(zhí)行 ./build/prebuilts_download.sh 時(shí),則是需要依賴到的;有些倉(cāng)庫(kù)則沒(méi)有ohos:mini,ohos:small標(biāo)簽,但會(huì)在編譯過(guò)程中或者鏡像打包階段依賴到而導(dǎo)致編譯錯(cuò)誤;這些都需要根據(jù)實(shí)際情況自行修改 ohos.xml 中對(duì)應(yīng)倉(cāng)庫(kù)的 groups 標(biāo)簽然后再同步和編譯代碼。
repo sync --depth 參數(shù)
ohos.xml 中對(duì)Linux內(nèi)核倉(cāng)庫(kù)的描述:
<project name="kernel_linux_5.10" path="kernel/linux/linux-5.10" clone-depth="1" groups="..."/>
有一個(gè) clone-depth=“1” 的字段,該字段表示在同步(下載或clone)遠(yuǎn)程倉(cāng)庫(kù)到本地時(shí),只下載遠(yuǎn)程倉(cāng)庫(kù)默認(rèn)分支的最新一次提交記錄到本地,而不是將所有的歷史記錄都同步到本地,這樣可以大幅減少倉(cāng)庫(kù)代碼的磁盤(pán)占用空間。
如果只想對(duì)某些倉(cāng)庫(kù)(特別是歷史記錄特別長(zhǎng)的倉(cāng)庫(kù))做 clone-depth="num"的操作,可以像上面一樣,在對(duì)應(yīng)倉(cāng)庫(kù)的描述信息增加 clone-depth=“num” 字段就行了;如果想對(duì)所有倉(cāng)庫(kù)進(jìn)行一次性的操作,那就給 repo sync 命令增加一個(gè) --depth 參數(shù)。例如:
$ 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 參數(shù)
通過(guò) repo sync --depth=1 參數(shù)拉取的OpenHarmony代碼,默認(rèn)只獲取遠(yuǎn)程倉(cāng)庫(kù)默認(rèn)分支的最新一次提交記錄到本地,并不包含更多的歷史提交記錄和其他費(fèi)默認(rèn)分支的記錄。
對(duì)某個(gè)具體倉(cāng)庫(kù),可以通過(guò)git命令和參數(shù)進(jìn)行一些操作,獲取更多的歷史提交記錄和其他費(fèi)默認(rèn)分支的記錄到本地。
git 的 --depth參數(shù):
--depth
Create a shallow clone【淺克隆】 with a history truncated to the specified
number【depth】 of commits.
Implies【隱含參數(shù)是】--single-branch【僅獲取遠(yuǎn)程倉(cāng)庫(kù)默認(rèn)分支的最新一次提交記錄】unless【除非顯式指定參數(shù)】--no-single-branch【通過(guò)這個(gè)參數(shù)指明獲取遠(yuǎn)程倉(cāng)庫(kù)所有分支的最新一次提交記錄】is
given to fetch the histories near the tips of all branches.
If you want to clone submodules shallowly, also pass
--shallow-submodules.
即在默認(rèn)的 --single-branch 情況下,只獲取默認(rèn)分支到本地;如果要查看其他分支的代碼和提交記錄,可以按如下一些操作進(jìn)行處理。
# 例如本地只有OpenHarmony-v4.1-Release的記錄,想要使用遠(yuǎn)程的OpenHarmony-v3.2-Release分支,
# 這樣操作就可以把遠(yuǎn)程的OpenHarmony-v3.2-Release分支拉取到本地進(jìn)行切換和使用
$ 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 參數(shù)
對(duì)于特定的倉(cāng)庫(kù),git 還有一個(gè)稀疏檢出的操作可以稍微減少checkout的代碼量,更重要的是這個(gè)參數(shù)可以讓工作區(qū)的代碼目錄更清爽。
例如,對(duì)于 //vendor/hisilicon/ 倉(cāng)庫(kù),默認(rèn)是:
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/
這里面有很多項(xiàng)目是我們平常基本上用不到也改不到的,放在這里很礙眼,通過(guò)hb set選擇項(xiàng)目時(shí),也會(huì)出現(xiàn)太多的選項(xiàng),因此,可以使用git-sparse-checkout的配置來(lái)只checkout我們想要的文件夾(項(xiàng)目)。
可以在這個(gè)倉(cāng)庫(kù)目錄下執(zhí)行:
git config core.sparsecheckout true
# true 或 1,enable sparsecheckout
# false 或 0,disable sparsecheckout
該命令會(huì)在 //vendor/hisilicon/.git/config 文件的 [core] 段新增一個(gè) sparsecheckout = true 的配置,enable 了sparsecheckout 功能,然后再執(zhí)行:
git sparse-checkout set hispark_pegasus hispark_taurus
hispark_taurus_linux
或者
echo "hispark_pegasus hispark_taurus hispark_taurus_linux" >
.git/info/sparse-checkout
作用都是將需要checkout的目錄和文件列表寫(xiě)入到 //vendor/hisilicon/.git/info/sparse-checkout 文件中去,而不在該文件中的目錄和文件則不會(huì)checkout出來(lái)(但這些文件的objects對(duì)象,還是在本地倉(cāng)庫(kù)中的,只是沒(méi)有解壓到工作區(qū)而已),而我們的修改和提交,也不會(huì)影響到未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/
當(dāng)我們需要把全部的目錄和文件列表全部checkout出來(lái)的時(shí)候,可以直接執(zhí)行:
git sparse-checkout set *
或者
echo "*" > .git/info/sparse-checkout
然后重新checkout一下當(dāng)前的分支即可。
注意:
實(shí)測(cè)發(fā)現(xiàn),是否執(zhí)行 “git config core.sparsecheckout” 命令來(lái)enable或disable sparsecheckout都沒(méi)關(guān)系(不知道是git版本問(wèn)題還是bug),只要有 .git/info/sparse-checkout 文件,都可以通過(guò)改寫(xiě)該文件達(dá)到稀疏檢出的目的。
補(bǔ)充
經(jīng)過(guò)上述命令和參數(shù)的組合使用,可以大幅減少OpenHarmony倉(cāng)庫(kù)和代碼所占用的磁盤(pán)空間,但是三大巨頭(//prebuilts/、//out/、.ccache)仍然是占用著非常大的磁盤(pán)空間。