前言
某天,產(chǎn)品叫我算下某個項(xiàng)目的代碼行數(shù),我一愣,這怎么統(tǒng)計?總不可能一個文件一個文件算吧?后面我找了下,git是可以統(tǒng)計提交到倉庫的所有的代碼的,不過有個問題,就是假如有些文件我不想算進(jìn)去怎么辦?之后我再查了下,可以只統(tǒng)計指定文件類型的代碼的。
Git統(tǒng)計代碼行數(shù)
在指定項(xiàng)目文件夾里,打開git命令窗口,執(zhí)行下面的命令(直接復(fù)制)
1、統(tǒng)計所有行數(shù)
git log --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END {
printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'
或者
git ls-files | xargs cat | wc -l
上面兩個命令只統(tǒng)計行數(shù),沒有細(xì)分到文件,下面這個命令是會把每個文件都列出來,并統(tǒng)計每個文件的行數(shù)。
git ls-files | xargs wc -l
2、統(tǒng)計指定文件類型的行數(shù)
find . "(" -name "*.java" -or -name "*.xml" -or -name "*.yml" -or -name "*.properties" ")" -print | xargs wc -l
這個命令會打印出文件和對應(yīng)的行數(shù)。
3、統(tǒng)計某個用戶的代碼量
git log --author="username" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END {
printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'
4、統(tǒng)計某個時間段內(nèi)的代碼量
git log --since=2022-12-06 --until==2022-12-07 --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END {
printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'
--since
:起始時間,--until
:終止時間
5、統(tǒng)計某個用戶某個時間段內(nèi)的代碼量
git log --since =2022-12-06 --until==2022-12-07 --author="username" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END {
printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'
還有其他命令,可以參考下 這篇文章 ,比較全。
Java實(shí)現(xiàn)統(tǒng)計代碼行數(shù)
Git可以統(tǒng)計代碼行數(shù),但是如果我不想要空行和注釋行也算進(jìn)去怎么辦?我找了下,好像沒有命令可以忽略空行或注釋行,既然Git不行,那我們就自己用代碼實(shí)現(xiàn)統(tǒng)計。
要統(tǒng)計總代碼量,得遍歷這個項(xiàng)目下的全部文件,然后讀取文件內(nèi)容,遍歷每一行的內(nèi)容,如果是空行或者注釋行就忽略。文章來源:http://www.zghlxwxcb.cn/news/detail-784978.html
實(shí)現(xiàn)
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class 統(tǒng)計代碼行數(shù) {
private static long 空行數(shù) = 0;
private static long 注釋行數(shù) = 0;
private static long 代碼行數(shù) = 0;
private static long 總行數(shù) = 0;
public static void main(String[] args) {
// 這里只統(tǒng)計 \src\main 文件夾里的文件,其他文件像 .idea、test 這些文件夾里的就不算進(jìn)去了
循環(huán)文件夾("D:\\Java\\workspace\\AssetsJava","\\src\\main\\");
System.out.println("空行:" + 空行數(shù));
System.out.println("注釋行:" + 注釋行數(shù));
System.out.println("代碼行:" + 代碼行數(shù));
System.out.println("總行:" + 總行數(shù));
}
private static void 循環(huán)文件夾(String 項(xiàng)目路徑,String 只統(tǒng)計指定文件夾) {
File f = new File(項(xiàng)目路徑);
if (!f.exists()){
System.out.println("項(xiàng)目路徑不存在!");
return;
}
File[] childs = f.listFiles(); // 獲取這個項(xiàng)目下的文件、文件夾
for (int i = 0; i < childs.length; i++) {
File child = childs[i];
if (!child.isDirectory()) { // 當(dāng)前文件不是文件夾,就讀取
if (child.getParent().contains(只統(tǒng)計指定文件夾)){ // java文件、xml文件、配置文件 只統(tǒng)計main文件夾下的
if (child.getName().matches(".*\\.java$") || child.getName().endsWith(".yml") ||
child.getName().endsWith(".properties") || child.getName().endsWith(".xml")) {
long 單個文件代碼行數(shù) = 統(tǒng)計代碼行數(shù)(child);
System.out.println(單個文件代碼行數(shù)+"\t\t"+child.getName());
}
}else if ("pom.xml".equals(child.getName())){
long 單個文件代碼行數(shù) = 統(tǒng)計代碼行數(shù)(child);
System.out.println(單個文件代碼行數(shù)+"\t\t"+child.getName());
}
}else { // 當(dāng)前文件是文件夾,繼續(xù)遞歸
循環(huán)文件夾(child.getPath(),只統(tǒng)計指定文件夾);
}
}
}
private static long 統(tǒng)計代碼行數(shù)(File file){
long 單個文件代碼行數(shù) = 0;
BufferedReader br = null;
try{
br = new BufferedReader(new FileReader(file));
String line = "";
boolean flag = false; // 用于標(biāo)記xml多行注釋,為true表示當(dāng)前行在多行注釋中,一直到最后一行注釋
while ((line = br.readLine()) != null){
總行數(shù)++;
String 內(nèi)容 = line.trim(); // 每一行的內(nèi)容,去掉空格
if (flag){
注釋行數(shù)++;
// 當(dāng)多行注釋結(jié)尾是 --> 說明多行注釋結(jié)束,重新標(biāo)記為false,表示當(dāng)前不在統(tǒng)計多行注釋
if (內(nèi)容.endsWith("-->")) flag = false;
}else {
if (內(nèi)容.length() == 0){
空行數(shù)++;
}else if (內(nèi)容.startsWith("http://") || 內(nèi)容.startsWith("/**") || 內(nèi)容.startsWith("*") ||
內(nèi)容.startsWith("*/") || 內(nèi)容.startsWith("/*") || 內(nèi)容.startsWith("#")){
注釋行數(shù)++;
}else if(內(nèi)容.startsWith("<!--")){
注釋行數(shù)++;
// 當(dāng)前行屬于xml的注釋,且結(jié)尾不是 --> 時,表示是多行注釋,設(shè)置標(biāo)記為true
if (!內(nèi)容.endsWith("-->")) flag = true;
}else {
代碼行數(shù)++;
單個文件代碼行數(shù)++;
}
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return 單個文件代碼行數(shù);
}
}
統(tǒng)計結(jié)果
最后的總行數(shù),是和用Git統(tǒng)計的總行數(shù)對得上的。文章來源地址http://www.zghlxwxcb.cn/news/detail-784978.html
到了這里,關(guān)于Git統(tǒng)計代碼行數(shù);Java實(shí)現(xiàn)統(tǒng)計代碼行數(shù),忽略空行、注釋行的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!