preface

Git-log refers to viewing git commit logs. This article will fully parse the use of git-log and put it into practice with a real SpringBoot project. If you’re a technical manager, you probably care about what your people are doing, how much code they’re putting out, and whether they’re blessed by The Buddha. Git-log can help you figure it out. It can also help you to code review, blame you. Do you want blame? Do you want to locate your code quickly? Let’s see what our Git log can do. Let’s Go~

The profile

$ git log<options>] [< Revision range>] [[--] <path>...Copy the code
  • This article will use the following two commands to do the demo, a normal log, a single line display log

    # ordinary log
    $ git log
    # single line display
    $ git log --oneline
    Copy the code

Options for the options

  • — Follow: History will be traced, including records before the rename

    $ git log -p gradle.properties
    Git commits a file named gradle.properties without following it
    $ git log -p --follow gradle.properties
    Git will find all file commits before and after the rename
    Copy the code
  • –no-decorate: Reference names for all submissions that are not displayed

    $ git log --no-decorate
    Copy the code
    commit hash
    Author: caining <[email protected]>
    Date:  Tue Apr 20 11:10:53 2021 +0800
    develop2
    Copy the code
  • – decorate [= short | full | auto | no] : displays all the reference name

    • Git Reference
      • Git Reference简写为refs
      • Local branch Reference format: refs/heads/

        For example, refs/heads/master can be abbreviated as master if it is unique.
      • Refs /remotes/

        Such as refs/remotes/origin/master, under the condition of the guarantee only can be abbreviated to origin/master;
      • Tag Reference format: refs/tags/

        : for example, refs/tags/v1.0.1 can be abbreviated to v1.0.1 if it is unique
      • Special refs-head, which points to the current commit status of the current local branch
      • The special refs-fetch_head points to the commit status obtained by the current local branch during the last fetch operation
      • The special refs-ORIG_head points to the commit state just checked out before any merge or rebase
    $ git log --decorate=full
    # the results: the HEAD - > refs/heads/develop, refs/remotes/origin/develo
    $ git log --decorate=short
    HEAD -> develop, origin/develop
    Copy the code
  • –source: all display commit branches

    Commit branches are displayed for each commit
    $ git log --oneline --source
    # 6256aa140 HEAD (HEAD -> develop, origin/develop) message
    Copy the code
  • –log-size: is the length of the commit message in bytes.

    $ git log --oneline --log-size
    
    # commit 6256aa140f910d81eb71a7651aad1d6bc5128da0 (HEAD -> develop, origin/develop)
    # log size 107
    # Author: caining <[email protected]>
    # Date: Thu Apr 22 09:59:29 2021 +0800
    # message
    Copy the code
  • -l <start>,<end>:<file>: view the modification records in certain lines of a file

    $ git log- L, 1, 1: the README. MdCopy the code
  • -l :<funcname>:<file>: Tracks the change history of a function

    • For this command to be effective, Git needs to know which lines are function declarations. Fatal: -l parameter ‘test’ starting at line 1: no match fatal: -l parameter ‘test’ starting at line 1: no match
    • Cause: In some languages, Git default regular expressions do not recognize the start and end identifiers of functions in the language, so function names cannot be matched
    • The solution
      • Project Creation.gitattributesfileAbout gitattributes
      • Configure the diFF for the corresponding language*.java diff=java
    $ git log -L :test:app/src/main/java/com/cnn/androidmavenpackage/MainActivity.java
    Copy the code

Focus – Screening

  • Note that these are applied before the submission order and formatting options (such as –reverse). Commit time a < b < c)

    # ordinary log
    $ git log
    # single line display
    $ git log --oneline
    Copy the code
    # list a to (draw a key: understand ^ Small in the front and big in the back
    $ git loga.. b $ gitlog b ^a
    Copy the code
    List all commits that can go from a to B without including C
    $ git log a b ^c
    Copy the code
  • Number of screening

    $ git log -3
    $ git log -n3
    $ git log --max-count=3
    # -<number> 
    # -n <number>
    # --max-count=<number>
    # --skip=<number>
    $ git log --skip=3 # Skip 3 records
    Copy the code
  • Time to screen

    $ git log --since='2021-04-02 00:00:00'
    # --since=<date>
    # --after=<date>
    $ git log --until='2021-04-02 00:00:00'
    # --until=<date>
    # --before=<date>
    # Submission within 2 weeks
    $ git log --since=2.weeks = $ git log --since="2 weeks ago"
    # Submission within 2 months
    $ git log --since=2.months = $ git log --since="2 months ago"
    # submission within 1 year
    $ git log --since=1.year = $ git log --since="1 year ago"
    Copy the code
  • The author screening

    $ git log --author='caining'
    # --author=<pattern>
    # --committer=<pattern>
    # Note that this is regular, fuzzy matching,
    # If it is multiple people check below
    $ git log -author="name1\|name2\|name3"
    Copy the code
  • Submit Description filtering

    $ git log --grep='bug fix'
    $ git log -i --grep="message1\|message2"
    # --grep=<pattern>
    SQL -and --grep commit with at least one matching author
    $ git log --author='caining' --all-match --grep='bug fix'
    --invert-grep -- invert-author = '-author'; -- invert-author = '-author'
    $ git log --author='caining' --invert-grep --grep='bug fix'
    A submission for caining that does not contain bug fixes
    Copy the code
  • Code content search -s, note that this search can be time-consuming.

    $ git log -S hello
    Copy the code

  • Multi – condition mixed screening

    $ git log --since=1.year --author='caining' --grep='get'- 3Copy the code

The format and style

  • –pretty[=<format>

  • – the date = < format > : –date=relative“–date=local“–date=default-local.–date=iso –date=iso8601“–date=rfc“–date=short“–date=rfc2822

    Date formatting

  • –parents

    Displays the submitted parent node

  • –children

    Displays the submitted child node

  • –graph

    –graph like this

  • Git log –pretty=format Existing styles include oneline,short, medium, full, Fuller, Reference, email, raw

    $ git log --pretty=oneline
    # style:
    # <hash> <title line>
    Copy the code
    $ git log --pretty=short
    # style:
    # commit <hash>
    # Author: <author>
    # <title line>
    Copy the code
    $ git log --pretty=medium
    # style:
    # commit <hash>
    # Author: <author>
    # Date: 
            
    # <title line>
    # <full commit message>
    Copy the code
    $ git log --pretty=full
    # style:
    # commit <hash>
    # Author: <author>
    # Commit: <committer>
    # <title line>
    # <full commit message>
    Copy the code
    $ git log --pretty=fuller
    # style:
    # commit <hash>
    # Author: 
            
    # AuthorDate: <author date>
    # Commit: 
            
    # CommitDate: <committer date>
    # <title line>
    # <full commit message>
    Copy the code
  • Git log –graph –pretty=format:

    #%Cred switches to red
    #%Cgreen switches to green
    #%Cblue switches to blue
    #%C(yellow) switches to yellow
    #%Creset resets color
    $ git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
    Copy the code

  • Format supports the following types, starting with % :

    # %H: full hash string
    # %h: short hash string
    # %T: the full hash string of the tree object
    # %t: short hash string for tree object
    # %P: the full hash string of the parent object
    # %p: a short hash string for the parent object
    # %an: the name of the author
    # %aN: The name of aN author (respecting. Mailmap, see [git-shortlog[1\]](https://git-scm.com/docs/git-shortlog) or [git-blame[1\]](https://git-scm.com/docs/git-blame))
    # % AE: author email
    # %aE:作者电子邮件 (respecting .mailmap, see [git-shortlog[1\]](https://git-scm.com/docs/git-shortlog) or [git-blame[1\]](https://git-scm.com/docs/git-blame))
    # %al: The local part of the author's email (the part before the @ sign)
    # %aL:author local-part (see *%al*) respecting .mailmap, see [git-shortlog[1\]](https://git-scm.com/docs/git-shortlog) or [git-blame[1\]](https://git-scm.com/docs/git-blame))
    # % AD: respects (format Respects --date= option)
    # %aD:author date, RFC2822 style
    # %ar: author date, relative
    # % AT: author date, UNIX timestamp
    # %ai:author date, ISO 8601-like format - author date, similar to IS0 8601 format
    # %aI:author date, strict ISO 8601 format - Author date, strict lS0 8601 format
    # % AS: author date, short format (YYYY-MM-DD)
    # %cn: submitter name
    # %cN:committer name (respecting .mailmap, see [git-shortlog[1\]](https://git-scm.com/docs/git-shortlog) or [git-blame[1\]](https://git-scm.com/docs/git-blame))
    # %ce: submitter email
    # %cE:committer email 
    # %cl:committer email local-part 
    # %cL:committer local-part 
    # %cd:committer date (format respects --date= option)
    # %cD:committer date, RFC2822 style
    # %cr:committer date, relative
    # %ct:committer date, UNIX timestamp
    # %ci:committer date, ISO 8601-like format
    # %cI:committer date, strict ISO 8601 format
    # %cs:committer date, short format (`YYYY-MM-DD`)
    # %s:message
    
    $ git log  --graph --pretty=format:Cred 'submission: - : % % h % Creset; % % % Cgreen date: - : ar Creset; % % % Cblue date at: - : at Creset; Ai % C (yellow) date: - : ai % % Creset; % % % Cred date AD: - : AD Creset; % % % Cblue an author: - : an Creset; % C (yellow) aN author: - : aN % % Creset; % % % Cgreen mailbox ce: - : ce Cresete: - : % e; sssss:-:%Cred%s%Creset; SSSS:-:%S; f-%f%n'
    Copy the code

Git shortlog – statistics

Output is grouped by contributors by default
$ git shortlog

# list submitter code contributions, print authors and contributions
$ git shortlog -sn==git shortlog -s -n
$ git log --pretty='%an' | sort | uniq -c | sort -k1 -n -r | head -n 10
The #-s argument omits the comment for each commit and simply returns a simple statistic.
The #-n parameter smoothly sorts users by the number of commits

# Sort by number of committed contributions and print message
$ git shortlog -n

Use the mailbox format to view the contribution degree
$ git shortlog -e
Copy the code
$ git shortlog -sn==git shortlog -s -n
# &&
$ git log --pretty='%an' | sort | uniq -c | sort -k1 -n -r | head -n 10
Copy the code

Git blame– Check commit

View the history of changes to the readme. md file, including the time, author, and content
git blame README.md

See who changed lines 11-12 of the readme. md fileGit blame -l 11 git blame -l 11 git blame -l 11 git git# look after line 11

Display the full hash value
git blame -l README.md

# display the number of lines changed
git blame -n README.md

# show author email
git blame -e README.md

Do a combined query on the parameters
git blame -enl -L 11 README.md
Copy the code

Practical project

  • First of all, we are going to do a “Git code annual report summary/annual code bill” statistical project. The idea is to refer to the “spending bill” of some treasure or the “listening bill” of some music cloud.

  • Export the required log content to a local file as required

    $ git log- date = iso - pretty = the format: '"%h"."%an"."%ad"."%s"."%ce"'>> ~/Desktop/commitlog- date = iso - pretty = the format: '"%h"."%ar"."%at"."%ai"."%ad"."%an"."%aN"."%ce"."%e"."%s"."%S"."%f"'>> ~/Desktop/commitlog- date = iso - pretty = the format: '"Project - demo"."%H"."%h"."%an"."%ae"."%at"."%ad"."%s"'>> ~/Desktop/ project -demo.csv# % H: complete hash % H: short an hash % : % ae: author email % at: date of the author, a UNIX timestamp "% AD", "% s"
    Git shortlog-sn > Yangkai.Shen > Yangkai.Shen > Yangkai
    $ git log --author="Yangkai.Shen\|yangkai.shen" --pretty=tformat: --numstat | awk '{ add += $1 ; subs += $2 ; Loc += $1 - $2} END {printf "add :%s \n",add,subs,loc}'>> ~/Desktop/ project -demo_code.csv# add rows :133582 Delete rows :77429 Total rows: 56153
    
    git log --author="Yangkai.Shen\|yangkai.shen" --pretty=tformat: --numstat | awk '{ add += $1 ; subs += $2 ; loc += $1 - $2 } END { printf "%s,%s,%s,%s\n",add,subs,loc,"Yangkai.Shen" }'>> ~/Desktop/ project -demo_code.csv#658 Yangkai.Shen
    # 57 yangkai.shen
    # 4 fxbin
    # 3 EchoCow
    # 2 chenqi
    # 1 yaodehuang
    # 1 76peter
    # 1 yidasanqian
    # 1 UndefinedDiary
    # 1 geek
    # 1 taffier
    # 1 yanshaoshuai
    Copy the code

  • Using the Python script to read the author cycle, you can obtain the number of codes of each author, assemble them into JSON, and export them. The key codes are as follows:
if __name__ == '__main__':
    p = subprocess.Popen("git log --pretty='%an' | sort | uniq -c | sort -k1 -n -r | head -n 10", shell=True,
                         cwd='/Users/caining/Documents/demo/spring-boot-demo', stdout=subprocess.PIPE)
    out, err = p.communicate()
    value = str(out, encoding="utf-8")
    arr = value.split("\n")
    subprocess.Popen('printf [>> ~/Desktop/ project -demo_code1.csv', shell=True,
                     cwd='/Users/caining/Documents/demo/spring-boot-demo')
    for a in arr:
        if len(a.split(""> =))2:
            name = a.split("") [len(a.split("")) - 1]
            author = name
            subprocess.Popen(
                'printf {\\"name\\":\\"' + author + '\ \ ", \ \ "with \ \" : \ \ "> > ~ / Desktop/project - demo_code1. CSV', shell=True,
                cwd='/Users/caining/Documents/demo/spring-boot-demo')
            cmdd = 'git log --author=\'' + author + '\' --pretty=tformat: --numstat | awk \'{ add += $1 ; subs += $2 ' \
                                                    '; loc += $1 - $2 } END ' \
                                                    '{printf "% s, % s, % s", add, subs, loc} \' > > ~ / Desktop/project - demo_code1. CSV '
            p = subprocess.Popen(cmdd, shell=True, cwd='/Users/caining/Documents/demo/spring-boot-demo')
            out, err = p.communicate()
            subprocess.Popen('printf \\"}, >> ~/Desktop/ project -demo_code1.csv', shell=True,
                                      cwd='/Users/caining/Documents/demo/spring-boot-demo')
    subprocess.Popen('printf {}] >> ~/Desktop/ project -demo_code1.csv', shell=True,
                     cwd='/Users/caining/Documents/demo/spring-boot-demo')
                      
Copy the code
  • The result is as follows:
[{"name":"Yangkai.Shen"."score":"124483769, 83475, 00"
    },
    {
        "name":"yangkai.shen"."score":"9099446865"
    },
    {
        "name":"fxbin"."score":"2902216268"},... ]Copy the code

  • Set up the springBoot project
  • Write the Upload interface and integrate Swagger2
  • Upload to mysql to ensure data integrity and correct after comparison

  • The key code for Spring to mysql is as follows
try {
            InputStream inputStream = file.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
            String str = null;
            while((str = br.readLine()) ! =null){
                String[] split = str.split("\n");
                int len = split.length;
                List<GitLog> gitLogList = new ArrayList<>();
                for (int j = 0; j < len; j++) {
                    String[] split1 = split[j].split(",");
                    try {
                        String commit_id_long = split1[1];
                        GitLog oneById = gitDao.findOneById(commit_id_long);
                        GitLog gitLog = new GitLog()
                            .setProject(split1[0].replace("’".""))
                            .setCommit_id_long(commit_id_long)
                            .setCommit_id(split1[2])
                            .setAuthor(split1[3])
                            .setEmail(split1[4])
                            .setTime_stamp(Long.valueOf(split1[5]))
                            .setTime(split1[6].replace("+ 0800".""))
                            .setMessage(split1[7].replace("’".""));
                        if(oneById ! =null) {
                            gitDao.update(gitLog);
                        } else{ gitLogList.add(gitLog); }}catch(Exception e) { e.printStackTrace(); } } gitDao.insertList(gitLogList); }}catch (IOException e) {
            e.printStackTrace();
        }
Copy the code
  • After storing in mysql, it is more convenient for statistics. In addition to the basic code quantity statistics, storing in mysql has the following advantages:
    • You can collect statistics on different authors of the same user
    • You can collect statistics of different emails with the same author
    • The latest time can be counted
    • You can count the maximum number of submissions that day
    • Duplicate author like this:
    • Other statistics (annual maximum number of submissions on a certain day, annual total number of submissions, annual ranking of submissions, annual latest submission on a certain day (latest submission for overtime work), annual overtime submission times, etc.)
SELECT COUNT(*) asTotal submissionsFROM git_log ORDER BY time_stamp ASC;
SELECT DATE_FORMAT(time,"%Y year %m month ")ASThis month,COUNT(*) asSubmit the numberFROM git_log GROUP BY DATE_FORMAT(time,"%Y year %m month ")SELECT DATE_FORMAT(time,"%Y年%m月%d日") ASThe date,COUNT(*) asSubmit the numberFROM git_log GROUP BY DATE_FORMAT(time,"%Y年%m月%d日") ORDER BYSubmit the numberDESC; 
SQL > select * from 'SQL'; The above SQL script is just an example, in progress, here does not affect this article.
Copy the code
  • In general, using SQL super statistics ability +git-log itself code statistics ability, we can do what we want to do project:

  • The author’s personal basic script and simple backend have been formed initially, and I plan to combine the front-end interface with the front-end interface. The front-end interface is responsible for the cool and finish the project. In the project planning, interested students are welcome to discuss with us. On baby!

  • To this project back-end and preparation stage script has been preliminarily formed, poor specific interface development and statistics

conclusion

  • Git log basic knowledge, format is very powerful, style is also very diverse

  • Git Shortlog – Git Blame

  • Project practice combined with mysql+ Spring boot, convenient statistics

  • Git command export scripts use Python to easily export the number of code written by the author

  • Mysql other scripts in progress

  • The construction of small program or Web interface is in progress

  • Discuss the @ QiShare

reference

  • Git-scm.com/docs/git-lo…
  • Git-scm.com/docs/git-bl…
  • Git-scm.com/docs/git-sh…
  • Git-scm.com/docs/gitatt…
  • This project back-end code && project statistics source reference: github.com/xkcoding/sp…