list_contributors.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # pip install gitpython
  2. import argparse
  3. import sys
  4. from git import Repo
  5. import os
  6. import json
  7. parser = argparse.ArgumentParser(
  8. description='List contributors of DragonOS project')
  9. parser.add_argument('--since', type=str, help='Since date')
  10. parser.add_argument('--until', type=str, help='Until date')
  11. parser.add_argument('--mode', type=str, help='脚本的运行模式 可选:<all> 输出所有信息\n' +
  12. ' <short> 输出贡献者名单、邮箱以及提交数量', default='all')
  13. args = parser.parse_args()
  14. repo = Repo(os.path.dirname(os.path.realpath(__file__)) + "/..")
  15. # Get the list of contributors
  16. format = '--pretty={"commit":"%h", "author":"%an", "email":"%ae", "date":"%cd"}'
  17. logs = repo.git.log(format, since=args.since, until=args.until)
  18. if args.mode == 'all':
  19. print(logs)
  20. elif args.mode == 'short':
  21. logs = logs.splitlines()
  22. print("指定时间范围内总共有", len(logs), "次提交")
  23. logs = [json.loads(line) for line in logs]
  24. print("贡献者名单:")
  25. authors = dict()
  26. for line in logs:
  27. if line['email'] not in authors.keys():
  28. authors[line['email']] = {
  29. 'author': line['author'],
  30. 'email': line['email'],
  31. 'count': 1
  32. }
  33. else:
  34. authors[line['email']]['count'] += 1
  35. # 排序输出
  36. authors = sorted(authors.values(), key=lambda x: x['count'], reverse=True)
  37. for author in authors:
  38. print(author['author'], author['email'], author['count'])