Compile the git version number into the program

Source: Internet
Author: User
Tags iso 8601 iso 8601 format version control system hex code

The question is raised

Regardless of the version management tool, each commit record will have a corresponding version number, which is generally an integer, and Git is a hash string. However, this version number is unique, and sometimes we will print out the program version number in the log when the program is running, or output the current program version number in the console when the command line is running. In general, if our program output version number, and version control system source code corresponding to the version of the relationship is the best, so when the running program problems, you can use the program version number, to source version control system to find the corresponding source code for analysis, In other words, we know that the current running program corresponds to the source code in the version control system.

Get version number with git command

The use of the environment here is Linux, our source client is git, through the git command we can get the current version of the source version of the repository, using the format output of the git log command, you can get the various parts of each commit result, such as the version number, commit time, commit the log. The git log command prints out the details of all commit records by default, and by using the--pretty option it provides, we can specify the part of the Git log output that we need, such as the hash string that represents the version number.

git log --pretty=format:"%H" 

Output:

082472d159a9ccd72fe241319d120b1a3dd87283
59ab0468389b511d0949aaef4e5324277e1899ce
134cb39bbb64b203b146626776a56037bccb469f
395db26c60e2f3544ec85d62e6caef911e9b16df
a29c1f4b7d72bb636ea844fc2d2e70c6f49eb046

Of course, we can also only output short hash, for example:

git log --pretty=format:"%h"

Output:

082472d
59ab046
134cb39
395db26
A29c1f4

Also, the time of our code submission can be obtained in the following format, simply by modifying the format parameter:

git log --format="%ct" 

Output:

1499330142
1499245162
1499244031
1499237075
1498813631

Here the output is the UNIX timestamp of all committed records, we want to get the latest one, only need to add the parameter-n where n is greater than 0 integer, representing the output log of the first n times the commit record, for example:

git log -1 --format="%ct"

Output:
1499330142

Represents the submission timestamp of the most recent commit of the output. Now that we have this information, we can get the current source of the latest parts of the information, in fact, the most important is the time of submission, as well as the version number, with the previous command to use, we can write the following shell code:

#!/bin/shcommit_ts= ' git log-1--format= "%ct" ' commit_time= ' date [email protected] $commit _ts + "%y-%m-%d%h:%m:%s" Current_time= ' date + '%y-%m-%d%h:%m:%s ' ' git_version= ' git log-1--format= '%H ' sed  s/myversion/' version: $git _ Version commit: $commit _time Build: $current _time "/g version.h.tmp > Version.hmake cleanmake

Save the script content as build.sh each time you submit the source code, run the build.sh script directly will generate the latest header file, the header file is compiled into the program, we see the version information contains the latest version number, commit time, compile time. Where we have a template file, Version.h.tmp, whose contents are as follows:

#ifndef _version_#define _version_ "Myversion" #endif

The Version.h file generated after running build.sh is similar to the following:

#ifndef _version_#define _version_ "version:082472d commit:2017-07-06 16:35:42 build:2017-07-11 21:01:31" #endif

We can use the _version_ macro directly in our source file, we should add the Version.h.tmp template file to the version control system when Git commits the version, and the script-generated version.h will be ignored because each build will change.

About the format description of the git log command

I'm here to list a more comprehensive format for git log for your reference:

%h:commit hash%h: Shortened commit hash%t:tree hash%t: Shortened tree hash%p:parent hashes%p: Shortened parent Hashes%an: Author name%an:mailmap The author name (. Mailmap corresponds, details refer to Git-shortlog (1) or git-blame (1))%ae: Author Mailbox%ae: Author mailbox (. Mailmap correspondence, details refer to Git-shortlog (1) or git-blame ( 1)%ad: Date (--date= established format)%ad: date, RFC2822 format%ar: date, relative format (1 day ago)%at: Date, UNIX timestamp%ai: date, ISO 8601 format%cn: Submitter Name% CN: Submitter name (. Mailmap correspondence, details refer to Git-shortlog (1) or git-blame (1))%ce: Submitter Email%ce: Submitter Email (. Mailmap correspondence, details refer to Git-shortlog (1) or Git-blame (1))%CD: submission Date (--date= format established)%CD: submission date, RFC2822 format%CR: submission date, relative format (1 day ago)%ct: submission date, UNIX TIMESTAMP%CI: Date Submitted, ISO 8601 Format%d:ref name%e:encoding%s:commit information title%f:sanitized subject line, suitable for a filename%b:commit information content%n:commit Notes%gd:reflog selector, e.g, refs/[email protected]{1}%gd:shortened reflog selector, e.g., [email  Protected]{1}%gs:reflog subject%cred: Switch to Red%cgreen: Switch to Green%cblue: Switch to blue%creset: Reset Color%c (...): Make color, as described in color. branch.* config option%m:left, right or BoundarY mark%n: NewLine percent: a raw%%x00:print a byte from a hex code%w ([[[, [,]]): Switch lines wrapping, like the-w option of Git-sho Rtlog (1).

Finally, share a better git log format output:

git log--graph--pretty=format: '%cred%h%creset-%c (yellow)%d%creset%cn%s%cgreen (%CR)%creset '--abbrev-commit--date =relative

The output effect is as follows:

Compile the git version number into the program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.