#gitpython# #版本控制# 利用gitpython获取git&编译信息

开发嵌入式系统下的项目,升级一个版本要改代码,还要提交,还要打标签,比较麻烦。
考虑到后续项目版本管理的重要性,也希望c代码中能、能自动将项目的版本编译进行自动化处理。

于是自己写了一个python脚本,完成对项目git信息,编译信息进行自动化处理。

  • gitpython:是一个与Git库交互的Python库,可以实现绝大部分的Git读写操作。主要是取代shell的命令操作,通过这个进行本地仓库的一些拉取代码、创建分支、push代码等操作。

安装Gitpython:

仍然要先安装python专门用来操作git的库,使用pip即可

pip install gitpython

 

按提示更新gitpyhton库即可

python -m pip install --upgrade pip

或者安装3以上的python版本 

 

 获取git&编译信息

# -*- coding: utf-8 -*-
from git import Repo
import tkinter
import tkinter.messagebox
import sys
import platform
import time
import socket
import getpass
import os

info_str = '#ifndef _VERSION_INFO_H\n#define _VERSION_INFO_H\n'
repo = Repo('.')
#if repo.is_dirty():
#    a=tkinter.messagebox.askokcancel('warning', 'not commit info, make sure the version?')
#    if not a:
#        print("update git info cancel")
#        raise

info_str += '\n\n//git base info//\n'
git_base = str(repo.active_branch)
info_str += '#define GIT_BASE_INFO_ACTIVE_BRANCH "' + git_base + '" /* >>>active_branch */\n'

#https://git-scm.com/docs/pretty-formats
#%H  abbreviated commit hash    
#%h  commit hash    
#%T  tree hash    
#%t  abbreviated tree hash    
#%P  parent hashes    
#%p  abbreviated parent hashes    
#%an  author name    
#%aN  author name (respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%ae  author email    
#%aE  author email (respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%al  author email local-part (the part before the @ sign)    
#%aL  author local-part (see 
#%al) respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%ad  author date (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    
#%aI  author date, strict ISO 8601 format    
#%as  author date, short format (YYYY-MM-DD)    
#%ah  author date, human style (like the --date=human option of git-rev-list[1])    
#%cn  committer name    
#%cN  committer name (respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%ce  committer email    
#%cE  committer email (respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%cl  committer email local-part (the part before the @ sign)    
#%cL  committer local-part (see %cl) respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%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)    
#%ch  committer date, human style (like the --date=human option of git-rev-list[1])    
#%d  ref names, like the --decorate option of git-log[1]    
#%D  ref names without the " (", ")" wrapping.    
#%(describe[:options])  human-readable name, like git-describe[1]; empty string for undescribable commits. The describe string may be followed by a colon and zero or more comma-separated options. Descriptions can be inconsistent when tags are added or removed at the same time.    match=<pattern>: Only consider tags matching the given glob(7) pattern, excluding the "refs/tags/" prefix.    exclude=<pattern>: Do not consider tags matching the given glob(7) pattern, excluding the "refs/tags/" prefix.    
#%S  ref name given on the command line by which the commit was reached (like git log --source), only works with git log    
#%e  encoding    
#%s  subject    
#%f  sanitized subject line, suitable for a filename    
#%b  body    
#%B  raw body (unwrapped subject and body)    
#%N  commit notes    
#%GG  raw verification message from GPG for a signed commit    
#%G?  show "G" for a good (valid) signature, "B" for a bad signature, "U" for a good signature with unknown validity, "X" for a good signature that has expired, "Y" for a good signature made by an expired key, "R" for a good signature made by a revoked key, "E" if the signature cannot be checked (e.g. missing key) and "N" for no signature    
#%GS  show the name of the signer for a signed commit    
#%GK  show the key used to sign a signed commit    
#%GF  show the fingerprint of the key used to sign a signed commit    
#%GP  show the fingerprint of the primary key whose subkey was used to sign a signed commit    
#%GT  show the trust level for the key used to sign a signed commit    
#%gD  reflog selector, e.g., refs/stash@{1} or refs/stash@{2 minutes ago}; the format follows the rules described for the -g option. The portion before the @ is the refname as given on the command line (so git log -g refs/heads/master would yield refs/heads/master@{0}).    
#%gd  shortened reflog selector; same as 
#%gD, but the refname portion is shortened for human readability (so refs/heads/master becomes just master).    
#%gn  reflog identity name    
#%gN  reflog identity name (respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%ge  reflog identity email    
#%gE  reflog identity email (respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%gs  reflog subject
info_str += '\n\n//git log info//\n'
log = repo.git.log('-1','--pretty=format:%H')
info_str += '#define GIT_LOG_INFO_COMMIT_HASH "' + log + ' " /* >>>%H commit hash */\n'
log = repo.git.log('-1','--pretty=format:%h')
info_str += '#define GIT_LOG_INFO_ABBREVIATED_COMMIT_HASH "' + log + ' " /* >>>%h abbreviated commit hash */\n'
log = repo.git.log('-1','--pretty=format:%T')
info_str += '#define GIT_LOG_INFO_TREE_HASH "' + log + ' " /* >>>%T tree hash */\n'
log = repo.git.log('-1','--pretty=format:%t')
info_str += '#define GIT_LOG_INFO_ABBREVIATED_TREE_HASH "' + log + ' " /* >>>%t abbreviated tree hash */\n'
log = repo.git.log('-1','--pretty=format:%P')
info_str += '#define GIT_LOG_INFO_PARENT_HASH "' + log + ' " /* >>>%P parent hashs */\n'
log = repo.git.log('-1','--pretty=format:%p')
info_str += '#define GIT_LOG_INFO_ABBREVIATED_PARENT_HASHS "' + log + ' " /* >>>%p abbreviated parent hashs */\n'
log = repo.git.log('-1','--pretty=format:%an')
info_str += '#define GIT_LOG_INFO_AUTHOR_NAME "' + log + ' " /* >>>%an author name */\n'
log = repo.git.log('-1','--pretty=format:%aN')
info_str += '#define GIT_LOG_INFO_AUTHOR_NAME_MAILMAP "' + log + ' " /* >>>%aN author name (respecting .mailmap, see git-shortlog[1] or git-blame[1]) */\n'
log = repo.git.log('-1','--pretty=format:%ae')
info_str += '#define GIT_LOG_INFO_AUTHOR_EMAIL "' + log + ' " /* >>>%ae author email */\n'
log = repo.git.log('-1','--pretty=format:%aE')
info_str += '#define GIT_LOG_INFO_AUTHOR_EMAIL_MAILMAP "' + log + ' " /* >>>%aE author email (respecting .mailmap, see git-shortlog[1] or git-blame[1]) */\n'
log = repo.git.log('-1','--pretty=format:%al')
info_str += '#define GIT_LOG_INFO_AUTHOR_EMAIL_LOCAL_PART "' + log + ' " /* >>>%al author email local-part (the part before the @ sign) */\n'
log = repo.git.log('-1','--pretty=format:%aL')
info_str += '#define GIT_LOG_INFO_AUTHOR_EMAIL_LOCAL_PART_MAILMAP "' + log + ' " /* >>>%aL author local-part (see %al) respecting .mailmap, see git-shortlog[1] or git-blame[1]) */\n'
log = repo.git.log('-1','--pretty=format:%ad')
info_str += '#define GIT_LOG_INFO_AUTHOR_DATA "' + log + ' " /* >>>%ad author date (format respects --date= option) */\n'
log = repo.git.log('-1','--pretty=format:%aD')
info_str += '#define GIT_LOG_INFO_AUTHOR_DATA_RFC2822 "' + log + ' " /* >>>%aD author date, RFC2822 style */\n'
log = repo.git.log('-1','--pretty=format:%ar')
info_str += '#define GIT_LOG_INFO_AUTHOR_DATA_RELATIVE "' + log + ' " /* >>>%ar author date, relative */\n'
log = repo.git.log('-1','--pretty=format:%at')
info_str += '#define GIT_LOG_INFO_AUTHOR_DATA_UNIX_TIMESTAMP "' + log + ' " /* >>>%at author date, UNIX timestamp */\n'
log = repo.git.log('-1','--pretty=format:%ai')
info_str += '#define GIT_LOG_INFO_AUTHOR_DATA_ISO_8601 "' + log + ' " /* >>>%ai author date, ISO 8601-like format */\n'
log = repo.git.log('-1','--pretty=format:%aI')
info_str += '#define GIT_LOG_INFO_AUTHOR_DATA_SHORT_FORMAT "' + log + ' " /* >>>%aI author date, short format (YYYY-MM-DD) */\n'
log = repo.git.log('-1','--pretty=format:%as')
info_str += '#define GIT_LOG_INFO_AUTHOR_DATA_HUMAN "' + log + ' " /* >>>%as author date, human style (like the --date=human option of git-rev-list[1]) */\n'
log = repo.git.log('-1','--pretty=format:%cn')
info_str += '#define GIT_LOG_INFO_COMMITTER_NAME "' + log + ' " /* >>>%cn committer name */\n'
log = repo.git.log('-1','--pretty=format:%cN')
info_str += '#define GIT_LOG_INFO_COMMITTER_NAME_MAILMAP "' + log + ' " /* >>>%cn committer name (respecting .mailmap, see git-shortlog[1] or git-blame[1]) */\n'
log = repo.git.log('-1','--pretty=format:%ce')
info_str += '#define GIT_LOG_INFO_COMMITTER_EMAIL "' + log + ' " /* >>>%ce committer email */\n'
log = repo.git.log('-1','--pretty=format:%cE')
info_str += '#define GIT_LOG_INFO_COMMITTER_EMAIL_MAILMAP "' + log + ' " /* >>>%cE committer email (respecting .mailmap, see git-shortlog[1] or git-blame[1]) */\n'
log = repo.git.log('-1','--pretty=format:%cl')
info_str += '#define GIT_LOG_INFO_COMMITTER_EMAIL_LOCAL_PART "' + log + ' " /* >>>%cl committer email local-part (the part before the @ sign) */\n'
log = repo.git.log('-1','--pretty=format:%cL')
info_str += '#define GIT_LOG_INFO_COMMITTER_EMAIL_LOCAL_PART_MAILMAP "' + log + ' " /* >>>%cL committer email local-part (respecting .mailmap, see git-shortlog[1] or git-blame[1]) */\n'
log = repo.git.log('-1','--pretty=format:%cd')
info_str += '#define GIT_LOG_INFO_COMMITTER_DATA "' + log + ' " /* >>>%ad committer date (format respects --date= option) */\n'
log = repo.git.log('-1','--pretty=format:%cD')
info_str += '#define GIT_LOG_INFO_COMMITTER_DATA_RFC2822 "' + log + ' " /* >>>%aD committer date, RFC2822 style */\n'
log = repo.git.log('-1','--pretty=format:%cr')
info_str += '#define GIT_LOG_INFO_COMMITTER_DATA_RELATIVE "' + log + ' " /* >>>%ar committer date, relative */\n'
log = repo.git.log('-1','--pretty=format:%ct')
info_str += '#define GIT_LOG_INFO_COMMITTER_DATA_UNIX_TIMESTAMP "' + log + ' " /* >>>%at committer date, UNIX timestamp */\n'
log = repo.git.log('-1','--pretty=format:%ci')
info_str += '#define GIT_LOG_INFO_COMMITTER_DATA_ISO_8601 "' + log + ' " /* >>>%ai committer date, ISO 8601-like format */\n'
log = repo.git.log('-1','--pretty=format:%cI')
info_str += '#define GIT_LOG_INFO_COMMITTER_DATA_SHORT_FORMAT "' + log + ' " /* >>>%aI committer date, short format (YYYY-MM-DD) */\n'
log = repo.git.log('-1','--pretty=format:%cs')
info_str += '#define GIT_LOG_INFO_COMMITTER_DATA_HUMAN "' + log + ' " /* >>>%as committer date, human style (like the --date=human option of git-rev-list[1]) */\n'

info_str += '\n\n//build info//\n'
git_base = str(repo.active_branch)
#info_str += '#define BUILD_INFO_BUILD_TIME "' + time.asctime(time.localtime()) + '" /* >>>build time */\n'
info_str += '#define BUILD_INFO_SYS_PLATFORM "' + sys.platform + '" /* >>>sys platform */\n'
#info_str += '#define BUILD_INFO_PLATFORM_ARCHITECTURE "' + platform.architecture() + '" /* >>>platform architecture */\n'
info_str += '#define BUILD_INFO_PLATFORM_PLATFORM "' + platform.platform() + '" /* >>>platform platform */\n'
info_str += '#define BUILD_INFO_PLATFORM_SYSTEM "' + platform.system() + '" /* >>>platform system */\n'
info_str += '#define BUILD_INFO_HOST_NAME "' + socket.gethostname() + '" /* >>>host name */\n'
info_str += '#define BUILD_INFO_USER_NAME "' + getpass.getuser() + '" /* >>>user name */\n'


info_str += '\n\n#endif	/* >>_VERSION_INFO_H<< */\n /* EOF */\n'
fi = open('./version_info.h', 'w', encoding='utf-8')
fi.write(info_str)
fi.close()

print('update version info success!')
# -*- coding: utf-8 -*-
from git import Repo
import tkinter
import tkinter.messagebox
import sys
import platform
import time
import socket
import getpass
import os

info_str = '#ifndef _VERSION_INFO_H\n#define _VERSION_INFO_H\n'
repo = Repo('.')
#if repo.is_dirty():
#    a=tkinter.messagebox.askokcancel('warning', 'not commit info, make sure the version?')
#    if not a:
#        print("update git info cancel")
#        raise

info_str += '\n\n//git base info//\n'
git_base = str(repo.active_branch)
info_str += '#define GIT_BASE_INFO_ACTIVE_BRANCH "' + git_base + '" /* >>>active_branch */\n'

#https://git-scm.com/docs/pretty-formats
#%H  abbreviated commit hash    
#%h  commit hash    
#%T  tree hash    
#%t  abbreviated tree hash    
#%P  parent hashes    
#%p  abbreviated parent hashes    
#%an  author name    
#%aN  author name (respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%ae  author email    
#%aE  author email (respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%al  author email local-part (the part before the @ sign)    
#%aL  author local-part (see 
#%al) respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%ad  author date (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    
#%aI  author date, strict ISO 8601 format    
#%as  author date, short format (YYYY-MM-DD)    
#%ah  author date, human style (like the --date=human option of git-rev-list[1])    
#%cn  committer name    
#%cN  committer name (respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%ce  committer email    
#%cE  committer email (respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%cl  committer email local-part (the part before the @ sign)    
#%cL  committer local-part (see %cl) respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%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)    
#%ch  committer date, human style (like the --date=human option of git-rev-list[1])    
#%d  ref names, like the --decorate option of git-log[1]    
#%D  ref names without the " (", ")" wrapping.    
#%(describe[:options])  human-readable name, like git-describe[1]; empty string for undescribable commits. The describe string may be followed by a colon and zero or more comma-separated options. Descriptions can be inconsistent when tags are added or removed at the same time.    match=<pattern>: Only consider tags matching the given glob(7) pattern, excluding the "refs/tags/" prefix.    exclude=<pattern>: Do not consider tags matching the given glob(7) pattern, excluding the "refs/tags/" prefix.    
#%S  ref name given on the command line by which the commit was reached (like git log --source), only works with git log    
#%e  encoding    
#%s  subject    
#%f  sanitized subject line, suitable for a filename    
#%b  body    
#%B  raw body (unwrapped subject and body)    
#%N  commit notes    
#%GG  raw verification message from GPG for a signed commit    
#%G?  show "G" for a good (valid) signature, "B" for a bad signature, "U" for a good signature with unknown validity, "X" for a good signature that has expired, "Y" for a good signature made by an expired key, "R" for a good signature made by a revoked key, "E" if the signature cannot be checked (e.g. missing key) and "N" for no signature    
#%GS  show the name of the signer for a signed commit    
#%GK  show the key used to sign a signed commit    
#%GF  show the fingerprint of the key used to sign a signed commit    
#%GP  show the fingerprint of the primary key whose subkey was used to sign a signed commit    
#%GT  show the trust level for the key used to sign a signed commit    
#%gD  reflog selector, e.g., refs/stash@{1} or refs/stash@{2 minutes ago}; the format follows the rules described for the -g option. The portion before the @ is the refname as given on the command line (so git log -g refs/heads/master would yield refs/heads/master@{0}).    
#%gd  shortened reflog selector; same as 
#%gD, but the refname portion is shortened for human readability (so refs/heads/master becomes just master).    
#%gn  reflog identity name    
#%gN  reflog identity name (respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%ge  reflog identity email    
#%gE  reflog identity email (respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%gs  reflog subject
info_str += '\n\n//git log info//\n'
log = repo.git.log('-1','--pretty=format:%H')
info_str += '#define GIT_LOG_INFO_COMMIT_HASH "' + log + ' " /* >>>%H commit hash */\n'
log = repo.git.log('-1','--pretty=format:%h')
info_str += '#define GIT_LOG_INFO_ABBREVIATED_COMMIT_HASH "' + log + ' " /* >>>%h abbreviated commit hash */\n'
log = repo.git.log('-1','--pretty=format:%T')
info_str += '#define GIT_LOG_INFO_TREE_HASH "' + log + ' " /* >>>%T tree hash */\n'
log = repo.git.log('-1','--pretty=format:%t')
info_str += '#define GIT_LOG_INFO_ABBREVIATED_TREE_HASH "' + log + ' " /* >>>%t abbreviated tree hash */\n'
log = repo.git.log('-1','--pretty=format:%P')
info_str += '#define GIT_LOG_INFO_PARENT_HASH "' + log + ' " /* >>>%P parent hashs */\n'
log = repo.git.log('-1','--pretty=format:%p')
info_str += '#define GIT_LOG_INFO_ABBREVIATED_PARENT_HASHS "' + log + ' " /* >>>%p abbreviated parent hashs */\n'
log = repo.git.log('-1','--pretty=format:%an')
info_str += '#define GIT_LOG_INFO_AUTHOR_NAME "' + log + ' " /* >>>%an author name */\n'
log = repo.git.log('-1','--pretty=format:%aN')
info_str += '#define GIT_LOG_INFO_AUTHOR_NAME_MAILMAP "' + log + ' " /* >>>%aN author name (respecting .mailmap, see git-shortlog[1] or git-blame[1]) */\n'
log = repo.git.log('-1','--pretty=format:%ae')
info_str += '#define GIT_LOG_INFO_AUTHOR_EMAIL "' + log + ' " /* >>>%ae author email */\n'
log = repo.git.log('-1','--pretty=format:%aE')
info_str += '#define GIT_LOG_INFO_AUTHOR_EMAIL_MAILMAP "' + log + ' " /* >>>%aE author email (respecting .mailmap, see git-shortlog[1] or git-blame[1]) */\n'
log = repo.git.log('-1','--pretty=format:%al')
info_str += '#define GIT_LOG_INFO_AUTHOR_EMAIL_LOCAL_PART "' + log + ' " /* >>>%al author email local-part (the part before the @ sign) */\n'
log = repo.git.log('-1','--pretty=format:%aL')
info_str += '#define GIT_LOG_INFO_AUTHOR_EMAIL_LOCAL_PART_MAILMAP "' + log + ' " /* >>>%aL author local-part (see %al) respecting .mailmap, see git-shortlog[1] or git-blame[1]) */\n'
log = repo.git.log('-1','--pretty=format:%ad')
info_str += '#define GIT_LOG_INFO_AUTHOR_DATA "' + log + ' " /* >>>%ad author date (format respects --date= option) */\n'
log = repo.git.log('-1','--pretty=format:%aD')
info_str += '#define GIT_LOG_INFO_AUTHOR_DATA_RFC2822 "' + log + ' " /* >>>%aD author date, RFC2822 style */\n'
log = repo.git.log('-1','--pretty=format:%ar')
info_str += '#define GIT_LOG_INFO_AUTHOR_DATA_RELATIVE "' + log + ' " /* >>>%ar author date, relative */\n'
log = repo.git.log('-1','--pretty=format:%at')
info_str += '#define GIT_LOG_INFO_AUTHOR_DATA_UNIX_TIMESTAMP "' + log + ' " /* >>>%at author date, UNIX timestamp */\n'
log = repo.git.log('-1','--pretty=format:%ai')
info_str += '#define GIT_LOG_INFO_AUTHOR_DATA_ISO_8601 "' + log + ' " /* >>>%ai author date, ISO 8601-like format */\n'
log = repo.git.log('-1','--pretty=format:%aI')
info_str += '#define GIT_LOG_INFO_AUTHOR_DATA_SHORT_FORMAT "' + log + ' " /* >>>%aI author date, short format (YYYY-MM-DD) */\n'
log = repo.git.log('-1','--pretty=format:%as')
info_str += '#define GIT_LOG_INFO_AUTHOR_DATA_HUMAN "' + log + ' " /* >>>%as author date, human style (like the --date=human option of git-rev-list[1]) */\n'
log = repo.git.log('-1','--pretty=format:%cn')
info_str += '#define GIT_LOG_INFO_COMMITTER_NAME "' + log + ' " /* >>>%cn committer name */\n'
log = repo.git.log('-1','--pretty=format:%cN')
info_str += '#define GIT_LOG_INFO_COMMITTER_NAME_MAILMAP "' + log + ' " /* >>>%cn committer name (respecting .mailmap, see git-shortlog[1] or git-blame[1]) */\n'
log = repo.git.log('-1','--pretty=format:%ce')
info_str += '#define GIT_LOG_INFO_COMMITTER_EMAIL "' + log + ' " /* >>>%ce committer email */\n'
log = repo.git.log('-1','--pretty=format:%cE')
info_str += '#define GIT_LOG_INFO_COMMITTER_EMAIL_MAILMAP "' + log + ' " /* >>>%cE committer email (respecting .mailmap, see git-shortlog[1] or git-blame[1]) */\n'
log = repo.git.log('-1','--pretty=format:%cl')
info_str += '#define GIT_LOG_INFO_COMMITTER_EMAIL_LOCAL_PART "' + log + ' " /* >>>%cl committer email local-part (the part before the @ sign) */\n'
log = repo.git.log('-1','--pretty=format:%cL')
info_str += '#define GIT_LOG_INFO_COMMITTER_EMAIL_LOCAL_PART_MAILMAP "' + log + ' " /* >>>%cL committer email local-part (respecting .mailmap, see git-shortlog[1] or git-blame[1]) */\n'
log = repo.git.log('-1','--pretty=format:%cd')
info_str += '#define GIT_LOG_INFO_COMMITTER_DATA "' + log + ' " /* >>>%ad committer date (format respects --date= option) */\n'
log = repo.git.log('-1','--pretty=format:%cD')
info_str += '#define GIT_LOG_INFO_COMMITTER_DATA_RFC2822 "' + log + ' " /* >>>%aD committer date, RFC2822 style */\n'
log = repo.git.log('-1','--pretty=format:%cr')
info_str += '#define GIT_LOG_INFO_COMMITTER_DATA_RELATIVE "' + log + ' " /* >>>%ar committer date, relative */\n'
log = repo.git.log('-1','--pretty=format:%ct')
info_str += '#define GIT_LOG_INFO_COMMITTER_DATA_UNIX_TIMESTAMP "' + log + ' " /* >>>%at committer date, UNIX timestamp */\n'
log = repo.git.log('-1','--pretty=format:%ci')
info_str += '#define GIT_LOG_INFO_COMMITTER_DATA_ISO_8601 "' + log + ' " /* >>>%ai committer date, ISO 8601-like format */\n'
log = repo.git.log('-1','--pretty=format:%cI')
info_str += '#define GIT_LOG_INFO_COMMITTER_DATA_SHORT_FORMAT "' + log + ' " /* >>>%aI committer date, short format (YYYY-MM-DD) */\n'
log = repo.git.log('-1','--pretty=format:%cs')
info_str += '#define GIT_LOG_INFO_COMMITTER_DATA_HUMAN "' + log + ' " /* >>>%as committer date, human style (like the --date=human option of git-rev-list[1]) */\n'

info_str += '\n\n//build info//\n'
git_base = str(repo.active_branch)
#info_str += '#define BUILD_INFO_BUILD_TIME "' + time.asctime(time.localtime()) + '" /* >>>build time */\n'
info_str += '#define BUILD_INFO_SYS_PLATFORM "' + sys.platform + '" /* >>>sys platform */\n'
#info_str += '#define BUILD_INFO_PLATFORM_ARCHITECTURE "' + platform.architecture() + '" /* >>>platform architecture */\n'
info_str += '#define BUILD_INFO_PLATFORM_PLATFORM "' + platform.platform() + '" /* >>>platform platform */\n'
info_str += '#define BUILD_INFO_PLATFORM_SYSTEM "' + platform.system() + '" /* >>>platform system */\n'
info_str += '#define BUILD_INFO_HOST_NAME "' + socket.gethostname() + '" /* >>>host name */\n'
info_str += '#define BUILD_INFO_USER_NAME "' + getpass.getuser() + '" /* >>>user name */\n'


info_str += '\n\n#endif	/* >>_VERSION_INFO_H<< */\n /* EOF */\n'
fi = open('./version_info.h', 'w', encoding='utf-8')
fi.write(info_str)
fi.close()

print('update version info success!')
# -*- coding: utf-8 -*-
from git import Repo
import tkinter
import tkinter.messagebox
import sys
import platform
import time
import socket
import getpass
import os

info_str = '#ifndef _VERSION_INFO_H\n#define _VERSION_INFO_H\n'
repo = Repo('.')
#if repo.is_dirty():
#    a=tkinter.messagebox.askokcancel('warning', 'not commit info, make sure the version?')
#    if not a:
#        print("update git info cancel")
#        raise

info_str += '\n\n//git base info//\n'
git_base = str(repo.active_branch)
info_str += '#define GIT_BASE_INFO_ACTIVE_BRANCH "' + git_base + '" /* >>>active_branch */\n'

#https://git-scm.com/docs/pretty-formats
#%H  abbreviated commit hash    
#%h  commit hash    
#%T  tree hash    
#%t  abbreviated tree hash    
#%P  parent hashes    
#%p  abbreviated parent hashes    
#%an  author name    
#%aN  author name (respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%ae  author email    
#%aE  author email (respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%al  author email local-part (the part before the @ sign)    
#%aL  author local-part (see 
#%al) respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%ad  author date (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    
#%aI  author date, strict ISO 8601 format    
#%as  author date, short format (YYYY-MM-DD)    
#%ah  author date, human style (like the --date=human option of git-rev-list[1])    
#%cn  committer name    
#%cN  committer name (respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%ce  committer email    
#%cE  committer email (respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%cl  committer email local-part (the part before the @ sign)    
#%cL  committer local-part (see %cl) respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%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)    
#%ch  committer date, human style (like the --date=human option of git-rev-list[1])    
#%d  ref names, like the --decorate option of git-log[1]    
#%D  ref names without the " (", ")" wrapping.    
#%(describe[:options])  human-readable name, like git-describe[1]; empty string for undescribable commits. The describe string may be followed by a colon and zero or more comma-separated options. Descriptions can be inconsistent when tags are added or removed at the same time.    match=<pattern>: Only consider tags matching the given glob(7) pattern, excluding the "refs/tags/" prefix.    exclude=<pattern>: Do not consider tags matching the given glob(7) pattern, excluding the "refs/tags/" prefix.    
#%S  ref name given on the command line by which the commit was reached (like git log --source), only works with git log    
#%e  encoding    
#%s  subject    
#%f  sanitized subject line, suitable for a filename    
#%b  body    
#%B  raw body (unwrapped subject and body)    
#%N  commit notes    
#%GG  raw verification message from GPG for a signed commit    
#%G?  show "G" for a good (valid) signature, "B" for a bad signature, "U" for a good signature with unknown validity, "X" for a good signature that has expired, "Y" for a good signature made by an expired key, "R" for a good signature made by a revoked key, "E" if the signature cannot be checked (e.g. missing key) and "N" for no signature    
#%GS  show the name of the signer for a signed commit    
#%GK  show the key used to sign a signed commit    
#%GF  show the fingerprint of the key used to sign a signed commit    
#%GP  show the fingerprint of the primary key whose subkey was used to sign a signed commit    
#%GT  show the trust level for the key used to sign a signed commit    
#%gD  reflog selector, e.g., refs/stash@{1} or refs/stash@{2 minutes ago}; the format follows the rules described for the -g option. The portion before the @ is the refname as given on the command line (so git log -g refs/heads/master would yield refs/heads/master@{0}).    
#%gd  shortened reflog selector; same as 
#%gD, but the refname portion is shortened for human readability (so refs/heads/master becomes just master).    
#%gn  reflog identity name    
#%gN  reflog identity name (respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%ge  reflog identity email    
#%gE  reflog identity email (respecting .mailmap, see git-shortlog[1] or git-blame[1])    
#%gs  reflog subject
info_str += '\n\n//git log info//\n'
log = repo.git.log('-1','--pretty=format:%H')
info_str += '#define GIT_LOG_INFO_COMMIT_HASH "' + log + ' " /* >>>%H commit hash */\n'
log = repo.git.log('-1','--pretty=format:%h')
info_str += '#define GIT_LOG_INFO_ABBREVIATED_COMMIT_HASH "' + log + ' " /* >>>%h abbreviated commit hash */\n'
log = repo.git.log('-1','--pretty=format:%T')
info_str += '#define GIT_LOG_INFO_TREE_HASH "' + log + ' " /* >>>%T tree hash */\n'
log = repo.git.log('-1','--pretty=format:%t')
info_str += '#define GIT_LOG_INFO_ABBREVIATED_TREE_HASH "' + log + ' " /* >>>%t abbreviated tree hash */\n'
log = repo.git.log('-1','--pretty=format:%P')
info_str += '#define GIT_LOG_INFO_PARENT_HASH "' + log + ' " /* >>>%P parent hashs */\n'
log = repo.git.log('-1','--pretty=format:%p')
info_str += '#define GIT_LOG_INFO_ABBREVIATED_PARENT_HASHS "' + log + ' " /* >>>%p abbreviated parent hashs */\n'
log = repo.git.log('-1','--pretty=format:%an')
info_str += '#define GIT_LOG_INFO_AUTHOR_NAME "' + log + ' " /* >>>%an author name */\n'
log = repo.git.log('-1','--pretty=format:%aN')
info_str += '#define GIT_LOG_INFO_AUTHOR_NAME_MAILMAP "' + log + ' " /* >>>%aN author name (respecting .mailmap, see git-shortlog[1] or git-blame[1]) */\n'
log = repo.git.log('-1','--pretty=format:%ae')
info_str += '#define GIT_LOG_INFO_AUTHOR_EMAIL "' + log + ' " /* >>>%ae author email */\n'
log = repo.git.log('-1','--pretty=format:%aE')
info_str += '#define GIT_LOG_INFO_AUTHOR_EMAIL_MAILMAP "' + log + ' " /* >>>%aE author email (respecting .mailmap, see git-shortlog[1] or git-blame[1]) */\n'
log = repo.git.log('-1','--pretty=format:%al')
info_str += '#define GIT_LOG_INFO_AUTHOR_EMAIL_LOCAL_PART "' + log + ' " /* >>>%al author email local-part (the part before the @ sign) */\n'
log = repo.git.log('-1','--pretty=format:%aL')
info_str += '#define GIT_LOG_INFO_AUTHOR_EMAIL_LOCAL_PART_MAILMAP "' + log + ' " /* >>>%aL author local-part (see %al) respecting .mailmap, see git-shortlog[1] or git-blame[1]) */\n'
log = repo.git.log('-1','--pretty=format:%ad')
info_str += '#define GIT_LOG_INFO_AUTHOR_DATA "' + log + ' " /* >>>%ad author date (format respects --date= option) */\n'
log = repo.git.log('-1','--pretty=format:%aD')
info_str += '#define GIT_LOG_INFO_AUTHOR_DATA_RFC2822 "' + log + ' " /* >>>%aD author date, RFC2822 style */\n'
log = repo.git.log('-1','--pretty=format:%ar')
info_str += '#define GIT_LOG_INFO_AUTHOR_DATA_RELATIVE "' + log + ' " /* >>>%ar author date, relative */\n'
log = repo.git.log('-1','--pretty=format:%at')
info_str += '#define GIT_LOG_INFO_AUTHOR_DATA_UNIX_TIMESTAMP "' + log + ' " /* >>>%at author date, UNIX timestamp */\n'
log = repo.git.log('-1','--pretty=format:%ai')
info_str += '#define GIT_LOG_INFO_AUTHOR_DATA_ISO_8601 "' + log + ' " /* >>>%ai author date, ISO 8601-like format */\n'
log = repo.git.log('-1','--pretty=format:%aI')
info_str += '#define GIT_LOG_INFO_AUTHOR_DATA_SHORT_FORMAT "' + log + ' " /* >>>%aI author date, short format (YYYY-MM-DD) */\n'
log = repo.git.log('-1','--pretty=format:%as')
info_str += '#define GIT_LOG_INFO_AUTHOR_DATA_HUMAN "' + log + ' " /* >>>%as author date, human style (like the --date=human option of git-rev-list[1]) */\n'
log = repo.git.log('-1','--pretty=format:%cn')
info_str += '#define GIT_LOG_INFO_COMMITTER_NAME "' + log + ' " /* >>>%cn committer name */\n'
log = repo.git.log('-1','--pretty=format:%cN')
info_str += '#define GIT_LOG_INFO_COMMITTER_NAME_MAILMAP "' + log + ' " /* >>>%cn committer name (respecting .mailmap, see git-shortlog[1] or git-blame[1]) */\n'
log = repo.git.log('-1','--pretty=format:%ce')
info_str += '#define GIT_LOG_INFO_COMMITTER_EMAIL "' + log + ' " /* >>>%ce committer email */\n'
log = repo.git.log('-1','--pretty=format:%cE')
info_str += '#define GIT_LOG_INFO_COMMITTER_EMAIL_MAILMAP "' + log + ' " /* >>>%cE committer email (respecting .mailmap, see git-shortlog[1] or git-blame[1]) */\n'
log = repo.git.log('-1','--pretty=format:%cl')
info_str += '#define GIT_LOG_INFO_COMMITTER_EMAIL_LOCAL_PART "' + log + ' " /* >>>%cl committer email local-part (the part before the @ sign) */\n'
log = repo.git.log('-1','--pretty=format:%cL')
info_str += '#define GIT_LOG_INFO_COMMITTER_EMAIL_LOCAL_PART_MAILMAP "' + log + ' " /* >>>%cL committer email local-part (respecting .mailmap, see git-shortlog[1] or git-blame[1]) */\n'
log = repo.git.log('-1','--pretty=format:%cd')
info_str += '#define GIT_LOG_INFO_COMMITTER_DATA "' + log + ' " /* >>>%ad committer date (format respects --date= option) */\n'
log = repo.git.log('-1','--pretty=format:%cD')
info_str += '#define GIT_LOG_INFO_COMMITTER_DATA_RFC2822 "' + log + ' " /* >>>%aD committer date, RFC2822 style */\n'
log = repo.git.log('-1','--pretty=format:%cr')
info_str += '#define GIT_LOG_INFO_COMMITTER_DATA_RELATIVE "' + log + ' " /* >>>%ar committer date, relative */\n'
log = repo.git.log('-1','--pretty=format:%ct')
info_str += '#define GIT_LOG_INFO_COMMITTER_DATA_UNIX_TIMESTAMP "' + log + ' " /* >>>%at committer date, UNIX timestamp */\n'
log = repo.git.log('-1','--pretty=format:%ci')
info_str += '#define GIT_LOG_INFO_COMMITTER_DATA_ISO_8601 "' + log + ' " /* >>>%ai committer date, ISO 8601-like format */\n'
log = repo.git.log('-1','--pretty=format:%cI')
info_str += '#define GIT_LOG_INFO_COMMITTER_DATA_SHORT_FORMAT "' + log + ' " /* >>>%aI committer date, short format (YYYY-MM-DD) */\n'
log = repo.git.log('-1','--pretty=format:%cs')
info_str += '#define GIT_LOG_INFO_COMMITTER_DATA_HUMAN "' + log + ' " /* >>>%as committer date, human style (like the --date=human option of git-rev-list[1]) */\n'

info_str += '\n\n//build info//\n'
git_base = str(repo.active_branch)
#info_str += '#define BUILD_INFO_BUILD_TIME "' + time.asctime(time.localtime()) + '" /* >>>build time */\n'
info_str += '#define BUILD_INFO_SYS_PLATFORM "' + sys.platform + '" /* >>>sys platform */\n'
#info_str += '#define BUILD_INFO_PLATFORM_ARCHITECTURE "' + platform.architecture() + '" /* >>>platform architecture */\n'
info_str += '#define BUILD_INFO_PLATFORM_PLATFORM "' + platform.platform() + '" /* >>>platform platform */\n'
info_str += '#define BUILD_INFO_PLATFORM_SYSTEM "' + platform.system() + '" /* >>>platform system */\n'
info_str += '#define BUILD_INFO_HOST_NAME "' + socket.gethostname() + '" /* >>>host name */\n'
info_str += '#define BUILD_INFO_USER_NAME "' + getpass.getuser() + '" /* >>>user name */\n'


info_str += '\n\n#endif	/* >>_VERSION_INFO_H<< */\n /* EOF */\n'
fi = open('./version_info.h', 'w', encoding='utf-8')
fi.write(info_str)
fi.close()

print('update version info success!')

 git&编译信息如下

#ifndef _VERSION_INFO_H
#define _VERSION_INFO_H


//git base info//
#define GIT_BASE_INFO_ACTIVE_BRANCH "zm_develop_jac_s811" /* >>>active_branch */


//git log info//
#define GIT_LOG_INFO_COMMIT_HASH "ee72db0239b5f58582d9a97d9a0c7950e9b5623f " /* >>>%H commit hash */
#define GIT_LOG_INFO_ABBREVIATED_COMMIT_HASH "ee72db02 " /* >>>%h abbreviated commit hash */
#define GIT_LOG_INFO_TREE_HASH "0cd588cb1186f2c9b5f0473d0c02db4ade473a51 " /* >>>%T tree hash */
#define GIT_LOG_INFO_ABBREVIATED_TREE_HASH "0cd588cb " /* >>>%t abbreviated tree hash */
#define GIT_LOG_INFO_PARENT_HASH "40ba67ca66cdfcdce5604097d7c27ff86584ec2a " /* >>>%P parent hashs */
#define GIT_LOG_INFO_ABBREVIATED_PARENT_HASHS "40ba67ca " /* >>>%p abbreviated parent hashs */
#define GIT_LOG_INFO_AUTHOR_NAME "andy.wu " /* >>>%an author name */
#define GIT_LOG_INFO_AUTHOR_NAME_MAILMAP "andy.wu " /* >>>%aN author name (respecting .mailmap, see git-shortlog[1] or git-blame[1]) */
#define GIT_LOG_INFO_AUTHOR_EMAIL "andy.wu@zongmutech.com " /* >>>%ae author email */
#define GIT_LOG_INFO_AUTHOR_EMAIL_MAILMAP "andy.wu@zongmutech.com " /* >>>%aE author email (respecting .mailmap, see git-shortlog[1] or git-blame[1]) */
#define GIT_LOG_INFO_AUTHOR_EMAIL_LOCAL_PART "andy.wu " /* >>>%al author email local-part (the part before the @ sign) */
#define GIT_LOG_INFO_AUTHOR_EMAIL_LOCAL_PART_MAILMAP "andy.wu " /* >>>%aL author local-part (see %al) respecting .mailmap, see git-shortlog[1] or git-blame[1]) */
#define GIT_LOG_INFO_AUTHOR_DATA "Fri Jun 18 13:41:03 2021 +0800 " /* >>>%ad author date (format respects --date= option) */
#define GIT_LOG_INFO_AUTHOR_DATA_RFC2822 "Fri, 18 Jun 2021 13:41:03 +0800 " /* >>>%aD author date, RFC2822 style */
#define GIT_LOG_INFO_AUTHOR_DATA_RELATIVE "7 days ago " /* >>>%ar author date, relative */
#define GIT_LOG_INFO_AUTHOR_DATA_UNIX_TIMESTAMP "1623994863 " /* >>>%at author date, UNIX timestamp */
#define GIT_LOG_INFO_AUTHOR_DATA_ISO_8601 "2021-06-18 13:41:03 +0800 " /* >>>%ai author date, ISO 8601-like format */
#define GIT_LOG_INFO_AUTHOR_DATA_SHORT_FORMAT "2021-06-18T13:41:03+08:00 " /* >>>%aI author date, short format (YYYY-MM-DD) */
#define GIT_LOG_INFO_AUTHOR_DATA_HUMAN "2021-06-18 " /* >>>%as author date, human style (like the --date=human option of git-rev-list[1]) */
#define GIT_LOG_INFO_COMMITTER_NAME "andy.wu " /* >>>%cn committer name */
#define GIT_LOG_INFO_COMMITTER_NAME_MAILMAP "andy.wu " /* >>>%cn committer name (respecting .mailmap, see git-shortlog[1] or git-blame[1]) */
#define GIT_LOG_INFO_COMMITTER_EMAIL "andy.wu@zongmutech.com " /* >>>%ce committer email */
#define GIT_LOG_INFO_COMMITTER_EMAIL_MAILMAP "andy.wu@zongmutech.com " /* >>>%cE committer email (respecting .mailmap, see git-shortlog[1] or git-blame[1]) */
#define GIT_LOG_INFO_COMMITTER_EMAIL_LOCAL_PART "andy.wu " /* >>>%cl committer email local-part (the part before the @ sign) */
#define GIT_LOG_INFO_COMMITTER_EMAIL_LOCAL_PART_MAILMAP "andy.wu " /* >>>%cL committer email local-part (respecting .mailmap, see git-shortlog[1] or git-blame[1]) */
#define GIT_LOG_INFO_COMMITTER_DATA "Fri Jun 18 13:41:03 2021 +0800 " /* >>>%ad committer date (format respects --date= option) */
#define GIT_LOG_INFO_COMMITTER_DATA_RFC2822 "Fri, 18 Jun 2021 13:41:03 +0800 " /* >>>%aD committer date, RFC2822 style */
#define GIT_LOG_INFO_COMMITTER_DATA_RELATIVE "7 days ago " /* >>>%ar committer date, relative */
#define GIT_LOG_INFO_COMMITTER_DATA_UNIX_TIMESTAMP "1623994863 " /* >>>%at committer date, UNIX timestamp */
#define GIT_LOG_INFO_COMMITTER_DATA_ISO_8601 "2021-06-18 13:41:03 +0800 " /* >>>%ai committer date, ISO 8601-like format */
#define GIT_LOG_INFO_COMMITTER_DATA_SHORT_FORMAT "2021-06-18T13:41:03+08:00 " /* >>>%aI committer date, short format (YYYY-MM-DD) */
#define GIT_LOG_INFO_COMMITTER_DATA_HUMAN "2021-06-18 " /* >>>%as committer date, human style (like the --date=human option of git-rev-list[1]) */


//build info//
#define BUILD_INFO_SYS_PLATFORM "win32" /* >>>sys platform */
#define BUILD_INFO_PLATFORM_PLATFORM "Windows-10-10.0.19043-SP0" /* >>>platform platform */
#define BUILD_INFO_PLATFORM_SYSTEM "Windows" /* >>>platform system */
#define BUILD_INFO_HOST_NAME "arvinchen2" /* >>>host name */
#define BUILD_INFO_USER_NAME "arvin.chen" /* >>>user name */


#endif	/* >>_VERSION_INFO_H<< */
 /* EOF */

嵌入到编译工程中,调整下目录结构及路径即可

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值