前言
簡單清晰的程式碼結構有利於專案程式碼的管理。
OneOS-Lite
使用
git submodule
的方式來進行程式碼的管理。
其中,
OneOS-Lite
倉庫為主儲存庫。而核心
kernel
,以及元件
components
和驅動
drivers
都將作為子模組的形式和
OneOS-Lite
主倉庫關聯管理。
檢索子模組
該命令
git submodule
可以檢視主倉庫中,已有的子模組或子倉庫。
$ git submodule-ba2c4170016ff14db298c5e87134704996c5aff5 thirdparty/cJSON-e99c7b71349f74804eb215c59fa426bf5a357d8f thirdparty/telnetd
拿以上舉例:
-
代表該子模組倉庫原始碼檔案還未檢入(空資料夾),沒有
-
代表已檢入;
e99c7b71349f74804eb215c59fa426bf5a357d8f
代表該子模組元件和
OneOS-Lite
所關聯的版本id;
thirdparty/telnetd
代表該子模組倉庫對應的目錄和其元件名稱。
獲取子模組
使用
git clone
是不會把子模組倉庫原始碼檔案檢入的,只會把拉取下來對應子模組元件的空資料夾。這時,如果需要使用某個子模組元件,可以使用以下命令:
git submodule initgit submodule update xxx(子模組元件名,可透過git submodule檢視已有的貢獻元件)
舉個栗子:
$ git submodule initSubmodule ‘thirdparty/cJSON’ (git@10。12。3。198:luoshunyuan/cJSON。git) registered for path ‘thirdparty/cJSON’Submodule ‘thirdparty/telnetd’ (git@10。12。3。198:luoshunyuan/telnetd。git) registered for path ‘thirdparty/telnetd’
$ git submodule update thirdparty/telnetdCloning into ‘D:/xiaoneng/oneos-2。0-test/thirdparty/telnetd’。。。Authorized users only。 All activity may be monitored and reported。Submodule path ‘thirdparty/telnetd’: checked out ‘e99c7b71349f74804eb215c59fa426bf5a357d8f’
如果需要獲取所有子模組倉庫,可以直接使用
git submodule update
。
關聯子模組
透過專案根目錄下的一個
。gitmodules
檔案,可以發現已經關聯的子倉庫,每新增一個子模組倉庫就會新增一條記錄。內容如下:
[submodule “thirdparty/telnetd”] path = thirdparty/telnetd url = git@10。12。3。198:luoshunyuan/telnetd。git[submodule “thirdparty/cJSON”] path = cJSON url = git@10。12。3。198:luoshunyuan/cJSON。git
繼續關聯新的子模組可以使用如下
git
命令,這裡以
telnetd
為例:
git submodule add git@10。12。3。198:luoshunyuan/telnetd。git thirdparty/telnetd
如果需要更改子倉庫,可先在對應的子倉庫目錄中,開啟
git
命令列,進入對應貢獻元件的git倉庫管理,並執行程式碼提交操作:
因為修改了子模組倉庫程式碼並提交了,但是主倉庫的指標依舊指向那個老的子倉庫的
commit id
,如果不提交這個修改的話,別人拉取主專案並且使用
git submodule update
更新子模組還是會拉取到修改前的程式碼。
因此,這時候需要把主專案進行提交更新。
刪除廢棄元件
當某些子倉庫不需要了,需要廢棄,刪除的時候,
git
沒有直接刪除子模組的命令,所以只能逐步刪除相關檔案:
在版本控制中刪除子模組:
git rm -r thirdparty/cJSON
在編輯器中刪除
。gitmodules
相關內容:
[submodule
“thirdparty/cJSON”
] path = thirdparty/cJSON url = git@10。12。3。198
:luoshunyuan/cJSON
。git
在編輯器中刪除
。git/config
相關內容:
[submodule
“thirdparty/cJSON”
] url = git@10。12。3。198
:luoshunyuan/cJSON
。git active =
true
刪除
。git
下的快取模組:
rm
-rf 。git/modules/thirdparty/cJSON
提交修改:
git
commit -am “delete submodule”
git
push
關注&&聯絡
gitee: https://gitee。com/cmcc-oneos/OneOS-Lite
docs: https://oneos-lite。com/
公眾號:CMCC-OneOS