git_log_to_array.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 json
  9. def git_log_to_json(init_hash, path_to_repo):
  10. hashes = subprocess.run(['git', 'rev-list', init_hash], cwd=path_to_repo,
  11. stdout=subprocess.PIPE).stdout.decode('ascii').split()
  12. logs = []
  13. i = 0
  14. for hash in hashes:
  15. entry = subprocess.run(['git', 'show', '--quiet', '--date=iso', hash],
  16. cwd=path_to_repo, stdout=subprocess.PIPE)\
  17. .stdout.decode(errors='replace')
  18. logs.append(entry)
  19. i += 1
  20. if i % 10 == 0:
  21. print("{} / {}".format(i, len(hashes)), end='\r')
  22. with open('gitlog.json', 'w') as f:
  23. f.write(json.dumps(logs))
  24. # Commits are saved in reverse chronological order from newest to oldest
  25. if __name__ == '__main__':
  26. parser = argparse.ArgumentParser(description="""Convert a git log output to json.
  27. """)
  28. parser.add_argument('--from-commit', type=str,
  29. help="A SHA-1 representing a commit. Runs git rev-list from this commit.")
  30. parser.add_argument('--repo-path', type=str,
  31. help="The absolute path to a local copy of the git repository from where the git log is taken.")
  32. args = parser.parse_args()
  33. path_to_repo = args.repo_path
  34. init_hash = args.from_commit
  35. git_log_to_json(init_hash, path_to_repo)