list_contributors.py 1.5 KB

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