git_log_to_array.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. """ Dump the git log to a file """
  2. __author__ = "Kristian Berg"
  3. __copyright__ = "Copyright (c) 2018 Axis Communications AB"
  4. __license__ = "MIT"
  5. __credits__ = ["Kristian Berg", "Oscar Svensson"]
  6. import argparse
  7. import subprocess
  8. import sys
  9. import json
  10. def git_log_to_json(init_hash, path_to_repo):
  11. hashes = subprocess.run(['git', 'rev-list', init_hash], cwd=path_to_repo,
  12. stdout=subprocess.PIPE).stdout.decode('ascii').split()
  13. logs = []
  14. i = 0
  15. for hash in hashes:
  16. entry = subprocess.run(['git', 'show', '--quiet', '--date=iso', hash],
  17. cwd=path_to_repo, stdout=subprocess.PIPE)\
  18. .stdout.decode(errors='replace')
  19. logs.append(entry)
  20. i += 1
  21. if i % 10 == 0:
  22. print(i, end='\r')
  23. with open('gitlog.json', 'w') as f:
  24. f.write(json.dumps(logs))
  25. # Commits are saved in reverse chronological order from newest to oldest
  26. if __name__ == '__main__':
  27. parser = argparse.ArgumentParser(description="""Convert a git log output to json.
  28. """)
  29. parser.add_argument('--from-commit', type=str,
  30. help="A SHA-1 representing a commit. Runs git rev-list from this commit.")
  31. parser.add_argument('--repo-path', type=str,
  32. help="The absolute path to a local copy of the git repository from where the git log is taken.")
  33. args = parser.parse_args()
  34. path_to_repo = args.repo_path
  35. init_hash = args.from_commit
  36. git_log_to_json(init_hash, path_to_repo)