
??想了解更多關(guān)于開(kāi)源的內(nèi)容,請(qǐng)?jiān)L問(wèn):??
??51CTO 開(kāi)源基礎(chǔ)軟件社區(qū)??
??https://ost.51cto.com??
社區(qū)代碼度量方式總結(jié)
當(dāng)開(kāi)發(fā)者每提交一筆Pull Request/Commit時(shí),都會(huì)有代碼修改量,包含新增了多少行代碼、刪除了多少行代碼。這屬于過(guò)程動(dòng)態(tài)的統(tǒng)計(jì)方式。還可以統(tǒng)計(jì)倉(cāng)庫(kù)代碼量,不關(guān)注提交過(guò)程,只關(guān)注存留的代碼量。這屬于靜態(tài)的統(tǒng)計(jì)方式。每位開(kāi)發(fā)者都會(huì)關(guān)注自己貢獻(xiàn)的代碼量,此文就總結(jié)下,各個(gè)代碼量統(tǒng)計(jì)方式。
1、git log統(tǒng)計(jì)每筆提交的修改量
先看下git log相關(guān)的參數(shù)說(shuō)明。
git log 參數(shù)說(shuō)明
–numstat
Similar to --stat, but shows number of added and deleted lines in decimal notation and pathname without abbreviation, to make it more machine friendly. For binary files, outputs two ??-?
? instead of saying ??0 0?
?.
–shortstat
Output only the last line of the --stat format containing total number of modified files, as well as number of added and deleted lines.
–stat
使用–stat參數(shù)主要可以在git log 的基礎(chǔ)上輸出文件增刪改的統(tǒng)計(jì)數(shù)據(jù)。
$ git log --stat
commit fa71c098e2912b69a1c82348d403b3260f2dc64e (HEAD -> temp_temp)
Author: zz********g <z********g@gmail.com>
Date: Wed Aug 12 17:19:05 2020 +0800
add txt file and dir # commit信息
txt/a.txt | 1 + # 文件修改狀態(tài),添加或刪除了多少行
1 file changed, 1 insertion(+) # 統(tǒng)計(jì)變更文件數(shù)量?
執(zhí)行后獲得輸出如下:
commit d0411d5e8d26be3abde076e24f026b25cc2e7819 (HEAD -> master, origin/master, origin/HEAD)
Merge: ae99435 faf351b
Author: ******** <d********g@h****I.com>
Date: Tue Feb 21 12:54:28 2023 +0000
!1358 add communication_dsoftbus commiter
Merge pull request !1358 from michael4096/master
commit ae99435b2347d4b648c03f9dcf7d7e095bb150a4
Author: z********o <z********0@h****I.com>
Date: Tue Feb 21 12:42:59 2023 +0000
!1360 Add libabigail and elfutils to openharmony-sig
* Add libabigail and elfutils to openharmony-sig
2 0 sig/sig-basicsoftwareservice/sig-basicsoftwareservice.md
2 0 sig/sig-basicsoftwareservice/sig-basicsoftwareservice_cn.md
6 2 sig/sigs.json
3 files changed, 10 insertions(+), 2 deletions(-)
開(kāi)子進(jìn)程:
process = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding=_encoding, cwd=WORKING_DIR + project_name)
在檢出的代碼倉(cāng)目錄下,執(zhí)行 cmd=[‘git’, ‘log’, ‘–shortstat’, ‘–numstat’],處理輸出,就可以統(tǒng)計(jì)出來(lái)倉(cāng)庫(kù)每一筆提交的代碼增刪修改量。
2、統(tǒng)計(jì)倉(cāng)庫(kù)代碼量
使用cloc工具統(tǒng)計(jì)倉(cāng)庫(kù)代碼量,cloc相關(guān)的參數(shù)如下,其中force-lang-def指定支持的編程語(yǔ)言定義,by-file-by-lang 指定按編程語(yǔ)言按文件分別輸出。
--read-lang-def=E:\\WorkSpace\\lmk-bohan\\stat-data\\my_definitions.txt --by-file-by-lang
參數(shù)說(shuō)明
--force-lang-def=<file> Load language processing filters from <file>,
then use these filters instead of the built-in
filters. Note: languages which map to the same
file extension (for example:
MATLAB/Mathematica/Objective-C/MUMPS/Mercury;
Pascal/PHP; Lisp/OpenCL; Lisp/Julia; Perl/Prolog)
will be ignored as these require additional
processing that is not expressed in language
definition files. Use --read-lang-def to define
new language filters without replacing built-in
filters (see also --write-lang-def,
--write-lang-def-incl-dup).
--by-file-by-lang Report results for every source file encountered
in addition to reporting by language.?
在代碼倉(cāng)下,開(kāi)子進(jìn)程:
process = subprocess.Popen(cmd,stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
encoding='utf-8', cwd=WORKING_DIR, errors='ignore')
執(zhí)行:
cmd = ['stat-data\\cloc-1.92.exe', '--read-lang-def=' + _current_file + '\\stat-data\\my_definitions.txt',
'--by-file-by-lang', name]
即可以得到代碼倉(cāng)的每個(gè)程序的文件的代碼量詳情,包含blank空行、comment注釋行、code代碼行等,通常取其加和,都計(jì)算為代碼量。輸出示例內(nèi)容如下:
100 files
200 files
300 files
312 text files.
classified 254 files
Duplicate file check 254 files (245 known unique)
Unique: 100 files
Unique: 200 files
251 unique files.
Counting: 100
Counting: 200
131 files ignored.
github.com/AlDanial/cloc v 1.92 T=0.24 s (1034.9 files/s, 69044.2 lines/s)
-----------------------------------------------------------------------------------------
File blank comment code
-----------------------------------------------------------------------------------------
community\sig\sigs.json 0 0 1068
community\zh\committer.md 0 0 442
community\sig\sig_list.toml 49 1 283
community\sig\README.md 46 0 190
。
。
。
community\sig\sig-linkboy\oh\oh8.md 7 0 4
community\sig\sig-linkboy\sig_linkboy.md 4 0 4
community\sig\sig-linkboy\oh\oh3.md 6 0 3
-----------------------------------------------------------------------------------------
SUM: 3684 1 13061
-----------------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
Markdown 248 3635 0 11685
JSON 2 0 0 1093
TOML 1 49 1 283
-------------------------------------------------------------------------------
SUM: 251 3684 1 13061
-------------------------------------------------------------------------------
3、統(tǒng)計(jì)代碼倉(cāng)貢獻(xiàn)者的代碼量
在上一章節(jié),可以統(tǒng)計(jì)代碼倉(cāng)的代碼量,有時(shí)候還想知道每一個(gè)開(kāi)發(fā)者在這個(gè)代碼倉(cāng)中貢獻(xiàn)了多少代碼量,甚至還可以根據(jù)郵箱后綴,還知道每一家單位貢獻(xiàn)的代碼量。
對(duì)于上一章節(jié)cloc統(tǒng)計(jì)的每一個(gè)源代碼文件,可以對(duì)這些文件執(zhí)行g(shù)it blame命令:
- 遍歷cloc統(tǒng)計(jì)文件列表,執(zhí)行g(shù)it blame -e,獲取貢獻(xiàn)者及其貢獻(xiàn)的代碼量:
cmd = ['git', 'blame', '-e', _file_path]
示例輸出如下,可以看出這個(gè)文件,每一行最后是誰(shuí)貢獻(xiàn)的。如果一行文件,屬于A新增的,但是B修改后,那最終顯示這一行屬于B貢獻(xiàn)的,最后一次修改者最終貢獻(xiàn)了這一行代碼。
git blame -e default.xml
7d700a41 (<z**************n@h****I.com> 2022-05-07 21:01:53 +0800 1) <?xml version="1.0" encoding="UTF-8"?>
7d700a41 (<z**************n@h****I.com> 2022-05-07 21:01:53 +0800 2) <manifest>
7d700a41 (<z**************n@h****I.com> 2022-05-07 21:01:53 +0800 3) <remote fetch="."1 name="origin" review="https://openharmony.gitee.com/openharmony/"/>
7d700a41 (<z**************n@h****I.com> 2022-05-07 21:01:53 +0800 4) <default remote="origin" revision="master" sync-j="4" />
7d700a41 (<z**************n@h****I.com> 2022-05-07 21:01:53 +0800 5)
7d700a41 (<z**************n@h****I.com> 2022-05-07 21:01:53 +0800 6) <include name="ohos/ohos.xml" />
7d700a41 (<z**************n@h****I.com> 2022-05-07 21:01:53 +0800 7) <include name="chipsets/all.xml" />
7d700a41 (<z**************n@h****I.com> 2022-05-07 21:01:53 +0800 8) </manifest>
D:\codes\code-count\manifest>git blame -e devboard.xml
019f8d3f (<m***************1@h****I.com> 2021-04-20 15:11:42 +0800 1) <?xml version="1.0" encoding="UTF-8"?>
019f8d3f (<m***************1@h****I.com> 2021-04-20 15:11:42 +0800 2) <manifest>
9e17c922 (<m***************1@h****I.com> 2021-11-27 06:33:25 +0000 3) <remote fetch="https://gitee.com/openharmony-sig" name="sig" review="https://gitee.com/openharmony-sig/"/>
019f8d3f (<m***************1@h****I.com> 2021-04-20 15:11:42 +0800 4) <include name="default.xml" />
019f8d3f (<m***************1@h****I.com> 2021-04-20 15:11:42 +0800 5) <project name="device_st" path="device/st" revision="master" remote="sig"/>
c3deb066 (<l*************g@h****I.com> 2021-04-28 17:04:50 +0800 6) <project name="device_allwinner" path="device/allwinner" revision="master" remote="sig"/>
c3deb066 (<l*************g@h****I.com> 2021-04-28 17:04:50 +0800 7) <project name="vendor_h****I_ipcamera_v3s" path="vendor/h****I/ipcamera_v3s" revision="master" remote="sig"/>
019f8d3f (<m***************1@h****I.com> 2021-04-20 15:11:42 +0800 8) <project name="vendor_h****I_minidisplay_demo" path="vendor/h****I/minidisplay_demo" revision="master" remote="sig"/>
2bb5688e (<l*************g@h****I.com> 2021-06-07 10:45:13 +0800 9) <project name="device_mediatek" path="device/mediatek" revision="master" remote="sig"/>
2bb5688e (<l*************g@h****I.com> 2021-06-07 10:45:13 +0800 10) <project name="device_nordic" path="device/nordic" revision="master" remote="sig"/>
2bb5688e (<l*************g@h****I.com> 2021-06-07 10:45:13 +0800 11) <project name="device_nxp" path="device/nxp" revision="master" remote="sig"/>
2bb5688e (<l*************g@h****I.com> 2021-06-07 10:45:13 +0800 12) <project name="device_fudanmicro" path="device/fudanmicro" revision="master" remote="sig"/>
2bb5688e (<l*************g@h****I.com> 2021-06-07 10:45:13 +0800 13) <project name="device_bestechnic" path="device/bestechnic" revision="master" remote="sig"/>
2bb5688e (<l*************g@h****I.com> 2021-06-07 10:45:13 +0800 14) <project name="device_ingenic" path="device/ingenic" revision="master" remote="sig"/>
2bb5688e (<l*************g@h****I.com> 2021-06-07 10:45:13 +0800 15) <project name="device_espressif" path="device/espressif" revision="master" remote="sig"/>
2bb5688e (<l*************g@h****I.com> 2021-06-07 10:45:13 +0800 16) <project name="device_winnermicro" path="device/winnermicro" revision="master" remote="sig"/>
2bb5688e (<l*************g@h****I.com> 2021-06-07 10:45:13 +0800 17) <project name="device_unisoc" path="device/unisoc" revision="master" remote="sig"/>
2bb5688e (<l*************g@h****I.com> 2021-06-07 10:45:13 +0800 18) <project name="device_broadcom" path="device/broadcom" revision="master" remote="sig"/>
2bb5688e (<l*************g@h****I.com> 2021-06-07 10:45:13 +0800 19) <project name="device_realtek" path="device/realtek" revision="master" remote="sig"/>
2bb5688e (<l*************g@h****I.com> 2021-06-07 10:45:13 +0800 20) <project name="device_bouffalolab" path="device/bouffalolab" revision="master" remote="sig"/>
019f8d3f (<m***************1@h****I.com> 2021-04-20 15:11:42 +0800 21) </manifest>
2bb5688e (<l*************g@h****I.com> 2021-06-07 10:45:13 +0800 22)
小結(jié)
本文介紹了代碼量統(tǒng)計(jì)的各種方式。統(tǒng)計(jì)每一筆提交的增刪改修改量,偏重統(tǒng)計(jì)過(guò)程中的貢獻(xiàn),批量增刪時(shí),容易產(chǎn)生巨量的代碼量。cloc可以看出代碼倉(cāng)的最新最終規(guī)模,過(guò)程中的批量增刪,會(huì)對(duì)沖。而git blame統(tǒng)計(jì)會(huì)看重每一位貢獻(xiàn)者的代碼量。
??想了解更多關(guān)于開(kāi)源的內(nèi)容,請(qǐng)?jiān)L問(wèn):??
??51CTO 開(kāi)源基礎(chǔ)軟件社區(qū)??
??https://ost.51cto.com??