Use git log to count the number of lines of code

1.Background

In the software development process, the number of lines of code is a very important indicator that can be used to measure the quality of the code and development efficiency. Git is a popular version control system that can record and track the history of code modifications. By using Git, we can easily count the number of lines of code. Below are some commonly used Git commands and explanations for counting lines of code.

2. Find the git commitId information to be counted

View the earliest commit of the current branch (that is, the earliest CommitId) : git log --reverse <branch-name> | head -1

View the latest commit of the current branch : git log <branch-name> | head -1

View the location where the current branch first masterforked from the branch : git log --reverse HEAD..$(git merge-base HEAD master)

3. Verify whether the submission corresponding to commitId is correct

View the specified commitIdcommit information and modifications : git show ${commitId}

View someone's first commit on a branch : git log --reverse <branch-name> --author="null <[email protected]>" | head -1

Find the commit id of the previous commit of a commit : git log --pretty=oneline 0b971fa90bfbbdcae0357a1d6efa4da432d7f0d0^

4. Count the number of lines of code

(1) Query the number of submitted rows in the time range command

git log [某个分支名] --author="[某个作者]" --since="[起始日期]" --until="[结束日期]" --pretty=tformat: --numstat --pretty="%H"  | awk 'NF==3 {plus+=$1; minus+=$2; total += $1 + $2;} END {printf("%d,%d,%d", plus, minus, total)}'

Command meaning

where git logthe command is used to get the commit history, --authorthe option is used to specify the committer, --sinceand --untilthe option is used to specify the time range. --pretty=tformat: --numstat --pretty="%H"Options are used to output the submitted commit ID and the number of added and deleted rows. Finally, awkthe command is used to count the number of lines.

Use demo

git log master --author="test <[email protected]>" --since="2022-12-31" --until="2023-03-31" --pretty=tformat: --numstat --pretty="%H"  | awk 'NF==3 {plus+=$1; minus+=$2; total += $1 + $2;} END {printf("%d,%d,%d", plus, minus, total)}'

(2) Query the number of submitted lines in the time range and commit range command

git log [某个分支名] --author="[某个作者]" --since="[起始日期]" --until="[结束日期]" --pretty=tformat: --numstat --pretty="%H" [startCommit]..[endCommit] | awk 'NF==3 {plus+=$1; minus+=$2; total += $1 + $2;} END {printf("%d,%d,%d", plus, minus, total)}'

Command meaning

${startCommit}..${endCommit}Represents the commit range, that is, the commit history from startCommitto . endCommitOther options and commands are similar to the previous command. Finally, awkthe command is also used to count the number of lines.

Use demo

git log master --author="test <[email protected]>" --since="2022-12-31" --until="2023-03-31" --pretty=tformat: --numstat --pretty="%H" ${startCommit}..${endCommit} | awk 'NF==3 {plus+=$1; minus+=$2; total += $1 + $2;} END {printf("%d,%d,%d", plus, minus, total)}'

Guess you like

Origin blog.csdn.net/Mint6/article/details/130173468