如何简单计算patch代码变化量?

计算代码量从程序员的角度来说,真是个变态的想法。从系统集成的角度来看,模块代码量越大,显然出错的概率越高,可能需要更多的检视或者测试。

但是很多项目上,很多项目经理,或者管理人员比较喜欢算代码量,个人角度非常不赞成这种变态的绩效参考标准。但是统计下patch代码量从某个角度来看,可以看出代码变化的情况,也能说明很多设计上的问题。从敏捷的角度看,说明每次迭代导致的设计变更情况。

这里写个简单的脚本来回答这个问题:如何简单计算patch代码变化量?请注意,这里说patch代码变化量,刚开始我想用“如何简单计算patch代码工作量?”真是上了那些变态绩效的怪圈了。

好了,说了这么多,我们进入正题,如何简单实现patch代码变化量的统计?


首先,分析下patch里面哪些是与代码变化量相关的?这里我们截取u-boot-1.1.6_100ask24x0.patch中的一段

diff -urN -w u-boot-1.1.6/board/100ask24x0/config.mk u-boot-1.1.6_100ask24x0/board/100ask24x0/config.mk
--- u-boot-1.1.6/board/100ask24x0/config.mk	1970-01-01 08:00:00.000000000 +0800
+++ u-boot-1.1.6_100ask24x0/board/100ask24x0/config.mk	2007-09-08 19:14:43.000000000 +0800
@@ -0,0 +1,25 @@
+#
+# (C) Copyright 2002
+# Gary Jennejohn, DENX Software Engineering, <gj@denx.de>
+# David Mueller, ELSOFT AG, <d.mueller@elsoft.ch>
+#
+# SAMSUNG SMDK2410 board with S3C2410X (ARM920T) cpu
+#
+# see http://www.samsung.com/ for more information on SAMSUNG
+#
+
+#
+# SMDK2410 has 1 bank of 64 MB DRAM
+#
+# 3000'0000 to 3400'0000
+#
+# Linux-Kernel is expected to be at 3000'8000, entry 3000'8000
+# optionally with a ramdisk at 3080'0000
+#
+# we load ourself to 33F8'0000
+#
+# download area is 3300'0000
+#
+
+
+TEXT_BASE = 0x33F80000

从上面这段代码中可以看出真正与统计相关的只有一行,只要理解了这行的含义,一个patch文件里面所有相关统计行的数据分析整理清楚,就能回答我们的问题。

@@ -0,0 +1,25 @@

然后,我们看下这行统计数据究竟是什么意思?

"-" 代表需要删除的

"+" 代表需要增加的

-0,0 代表需要删除部分从源文件0行开始,一共0行

+1,25 代表需要增加部分从目标文件第一行开始,一共25行


到这里,我们应该已经基本上了解了这些统计数据的意思的。但是这里只是简单的统计数据,详细的数据需要从代码行的粒度来分析哪些行是真正需要添加,哪些是真正需要删除的。我们这里只是简单计算,对于详细的统计数据可以参考文献【2】


根据上面我们理解的统计信息,我们给出linux的脚本文件,对这些数据进行计算分析,这里对u-boot-1.1.6_100ask24x0.patch进行测试:



对于一些特殊格式的patch,统计的时候,脚本只是简单的跳过,以内核补丁3.2-rc4为例:(如果有朋友知道这个格式是什么意思,如何理解,请告知,本人将万分感谢 ^_^)

daniel@ubuntu:~/work/tmp$ cat patch-3.2-rc4 |grep "@@" |./patch_statistic.sh
Welcome to use ./patch_statistic.sh script. -- Daniel Li
Please issuing the following command to analyze your patch file.
 xxx.patch |grep @@ |./patch_statistic.sh [30] {debug}
WARNING: Default comment ratios is 30%
WARNING: [skip] @@ -1 +0,0 @@
WARNING: [skip] @@ -1 +0,0 @@
WARNING: [skip] @@ -1 +0,0 @@
WARNING: [skip] @@ -1 +0,0 @@
WARNING: [skip] @@ -1 +0,0 @@
WARNING: [skip] @@ -1 +0,0 @@
WARNING: [skip] @@ -1 +0,0 @@
WARNING: [skip] @@ -1 +0,0 @@
WARNING: [skip] @@ debug using ARM EmbeddedICE DCC channel
WARNING: [skip] @@ -1,120 +1 @@
WARNING: [skip] @@ -0,0 +1 @@
WARNING: [skip] @@ -0,0 +1 @@
WARNING: [skip] @@ -0,0 +1 @@
WARNING: [skip] @@ -0,0 +1 @@
WARNING: [skip] @@ -1,74 +1 @@
WARNING: [skip] @@ -1 +1 @@
WARNING: [skip] @@ -1,26 +1 @@
WARNING: [skip] @@ -1 +1 @@
WARNING: [skip] @@ -1,35 +1 @@
WARNING: [skip] @@ -0,0 +1 @@
WARNING: [skip] @@ -0,0 +1 @@
WARNING: [skip] @@ -0,0 +1 @@
WARNING: [skip] @@ -1,6 +1 @@
WARNING: [skip] @@ -1,74 +1 @@
WARNING: [skip] @@ -1,98 +1 @@
WARNING: [skip] @@ -1,60 +1 @@
WARNING: [skip] @@ -1,294 +1 @@
WARNING: [skip] @@ -1,6 +1 @@
WARNING: [skip] @@ -0,0 +1 @@
WARNING: [skip] @@ -1,6 +1 @@
WARNING: [skip] @@ -1,39 +1 @@
WARNING: [skip] @@ -1,24 +1 @@
WARNING: [skip] @@ -1 +0,0 @@
WARNING: [skip] @@ -0,0 +1 @@
WARNING: [skip] @@ -1 +0,0 @@
WARNING: [skip] @@ -1 +0,0 @@
WARNING: [skip] @@ -1 +0,0 @@
WARNING: [skip] @@ -1 +0,0 @@
WARNING: [skip] @@ -1 +0,0 @@
WARNING: [skip] @@ -0,0 +1 @@
WARNING: [skip] @@ -0,0 +1 @@
WARNING: [skip] -       /* @@@ Force down.  */
WARNING: [skip] -//@@@0xff00, 0x0600, OP_EQ, 18, S1_TCPSQ, 0, S1_ESP6,  LD_LEN, 0x03f,  1, 0x0,                                      0xffff,
WARNING: [skip] -//@@@0x00ff, 0x0032, OP_EQ,  0, S1_CLNP2, 0, S1_AH6, IM_CTL, 0x021, 1,  0x0, 0x                                     ffff,
WARNING: [skip] -//@@@0x00ff, 0x0033, OP_EQ,  0, S1_CLNP2, 0, S1_CLNP, IM_CTL, 0x021, 1,  0x0, 0                                     xffff,
WARNING: [skip] +       /* @@@ Force down.  */
WARNING: [skip] +//@@@0xff00, 0x0600, OP_EQ, 18, S1_TCPSQ, 0, S1_ESP6,  LD_LEN, 0x03f,  1, 0x0,                                      0xffff,
WARNING: [skip] +//@@@0x00ff, 0x0032, OP_EQ,  0, S1_CLNP2, 0, S1_AH6, IM_CTL, 0x021, 1,  0x0, 0x                                     ffff,
WARNING: [skip] +//@@@0x00ff, 0x0033, OP_EQ,  0, S1_CLNP2, 0, S1_CLNP, IM_CTL, 0x021, 1,  0x0, 0                                     xffff,
WARNING: [skip] +/* @@@ Poll the SFP moddef0 line too.  */
WARNING: [skip] +/* @@@ Try to use the pm3386 DOOL interrupt as well.  */
WARNING: [skip] +                       /* @@@ Should report autonegotiation status.  */
WARNING: [skip] +               /* @@@ Flush out pending packets.  */
WARNING: [skip] +               /* @@@ Count drops.  */
WARNING: [skip] +               /* @@@ Make FCS stripping configurable.  */
WARNING: [skip] +               /* @@@ Check whether entries come back in order.  */
WARNING: [skip] +       /* @@@ Maybe we shouldn't be preallocating TX buffers.  */
WARNING: [skip] +       /* @@@ Flush out pending packets.  */
WARNING: [skip] +        * for more information.  (@@@ We also need to issue a reset
WARNING: [skip] +       /* @@@ Should set 0x337/0x437 (RX forwarding threshold.)  */
WARNING: [skip] +       /* @@@ Add other stats.  */
WARNING: [skip] -/* @@@ Poll the SFP moddef0 line too.  */
WARNING: [skip] -/* @@@ Try to use the pm3386 DOOL interrupt as well.  */
WARNING: [skip] -                       /* @@@ Should report autonegotiation status.  */
WARNING: [skip] -               /* @@@ Flush out pending packets.  */
WARNING: [skip] -               /* @@@ Count drops.  */
WARNING: [skip] -               /* @@@ Make FCS stripping configurable.  */
WARNING: [skip] -               /* @@@ Check whether entries come back in order.  */
WARNING: [skip] -       /* @@@ Maybe we shouldn't be preallocating TX buffers.  */
WARNING: [skip] -       /* @@@ Flush out pending packets.  */
WARNING: [skip] -        * for more information.  (@@@ We also need to issue a reset
WARNING: [skip] -       /* @@@ Should set 0x337/0x437 (RX forwarding threshold.)  */
WARNING: [skip] -       /* @@@ Add other stats.  */
WARNING: [skip] @@ -1 +0,0 @@
WARNING: [skip] @@ -1 +0,0 @@
WARNING: [skip] @@ -0,0 +1 @@
WARNING: [skip] @@ -0,0 +1 @@
WARNING: [skip] -       BH_JWrite,              /* Being written to log (@@@ DEBUGGING) */
WARNING: [skip] -       BH_JWrite,              /* Being written to log (@@@ DEBUGGING) */
WARNING: [skip] +       BH_JWrite,              /* Being written to log (@@@ DEBUGGING) */
WARNING: [skip] $realfile =~ s@^([^/]*)/@@;
WARNING: [skip] $realfile =~ s@^([^/]*)/@@;


RESULT:
ratio of actual code is .7
total 224684 added, 1688741 modified.
actual line 157278.8 added, 1688741 modified, 67405.2 comments.



【1】Unix Patch

【2】用Diff和Patch工具维护源码

【3】脚本下载链接


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值