当前位置:   article > 正文

Git封装自定义命令_git 自定义命令

git 自定义命令

1.bashrc增加系统命令

  1. vim /etc/bashrc 增加全用户系统命令
  2. function sky() {
  3. ( sh /opt/sky/hook.sh $* )
  4. }

2.添加hook.sh 用于拉取sky脚本 只增加可读可执行权限

  1. mkdir -p /opt/sky
  2. cd /opt/sky
  3. vim hook.sh
  4. chmod -R 755 /opt/sky
  5. #!/bin/bash
  6. set -e
  7. TOOLS_PATH=~/.sky
  8. TOOLS_BRANCH=main
  9. TOOLS_REMOTE=git@gitlab.raina.tech:zhangxinze/sky.git
  10. current_path=$(pwd)
  11. if [ -d ${TOOLS_PATH}/.git ]; then
  12. cd ${TOOLS_PATH}
  13. git reset --hard ${TOOLS_BRANCH} --quiet
  14. git checkout ${TOOLS_BRANCH} > /dev/null 2>&1
  15. git pull --quiet
  16. git pull --quiet origin ${TOOLS_BRANCH}
  17. # git log -1
  18. echo 'Successfully updated sky'
  19. else
  20. git clone ${TOOLS_REMOTE} -b ${TOOLS_BRANCH} ${TOOLS_PATH} --quiet
  21. echo 'Successfully updated sky'
  22. fi
  23. cd "$current_path"
  24. sh ~/.sky/sky.sh $*

3.配置远程仓库验证脚本

  1. #!/bin/bash
  2. set -e
  3. # Function to run cppcheck on changed files
  4. run_cppcheck() {
  5. changed_files=$(git diff --cached --name-only)
  6. echo "About to detect files:"
  7. echo "$changed_files"
  8. python3 ~/.sky/Cppcheck.py --appoint_path "$changed_files"
  9. python3 ~/.sky/Cppcheck.py --check ON
  10. echo '###'
  11. exit 1
  12. }
  13. # Check if the git command is "commit"
  14. if [[ $* =~ "commit" ]]; then
  15. run_cppcheck
  16. fi
  17. # Run the actual git command with all the passed arguments
  18. git $*
  1. import os
  2. import argparse
  3. def cppcheck(path):
  4. args="cppcheck -v -j 12 --suppress=doubleFree --suppress=resourceLeak --suppress=leakNoVarFunctionCall --suppress=integerOverflow --suppress=objectIndex --suppress=unknownMacro --suppress=syntaxError --suppress=memleak --suppress=memleakOnRealloc --xml --xml-version=2 --output-file=cppcheck_result.xml {} -i Tpl -i Physical -i build".format(path)
  5. print(args,'\n')
  6. if path != '':
  7. os.system(args)
  8. else:
  9. print('No files need cppcheck\n')
  10. def get_all_check_files_appoint(appoint_path):
  11. file_path_list=[]
  12. Cpplint_Checkpath_str=''
  13. for file_path in appoint_path.split('\n'):
  14. try:
  15. if file_path.endswith(tuple([".cpp",".h",".cc",".c",".hpp"])) and 'Tpl' not in file_path and 'Physical'not in file_path:
  16. file_path_list.append(file_path)
  17. Cpplint_Checkpath_str=Cpplint_Checkpath_str+' '+file_path
  18. except:
  19. pass
  20. print('Cpplint_Checkpath_list:', file_path_list, '\n')
  21. # print('Cpplint_Checkpath_str:', Cpplint_Checkpath_str, '\n')
  22. return Cpplint_Checkpath_str
  23. def check_on():
  24. try:
  25. with open('cppcheck_result.xml', 'r') as file:
  26. content = file.read() # 读取文件内容到变量content
  27. except FileNotFoundError:
  28. print("file not find!")
  29. content = ''
  30. if args.check=='ON':
  31. if 'error id' in content:
  32. print("Cppcheck found the problem\n\n\nresult_xml:{}".format(content))
  33. exit(1)
  34. print("Cppcheck not found the problem\n\n\n")
  35. else:
  36. print("Cppcheck is not enabled\n\n\nresult_xml:{}".format(content))
  37. def args_parse():
  38. parser = argparse.ArgumentParser(description='get cpp arg')
  39. parser.add_argument('--path', type=str)
  40. parser.add_argument('--appoint_path', type=str)
  41. parser.add_argument('--check', type=str)
  42. args = parser.parse_args()
  43. return args
  44. def start(args):
  45. if args.path:
  46. print('\n############Start Check Cppcheck!############\n')
  47. cppcheck(args.path)
  48. print('############CPPCHECK END!############\n')
  49. elif args.appoint_path:
  50. print('\n############Start Check Cppcheck!############\n')
  51. check_files_appoint=get_all_check_files_appoint(args.appoint_path)
  52. cppcheck(check_files_appoint)
  53. print('############CPPCHECK END!############\n')
  54. elif args.check:
  55. check_on()
  56. else:
  57. print('Please set parameters for Cppcheck\n')
  58. if __name__ == '__main__':
  59. args = args_parse()
  60. start(args)
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/396656?site
推荐阅读
相关标签
  

闽ICP备14008679号