git-changelog 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env python
  2. # git-changelog
  3. #
  4. # version 2.0, by John Wiegley
  5. #
  6. # The purpose of this code is to turn "git log" output into a complete
  7. # ChangeLog, for projects who wish to begin using a ChangeLog, but haven't
  8. # been.
  9. #
  10. # This version of git-changelog depends on GitPython:
  11. # git://gitorious.org/git-python/mainline.git
  12. import time
  13. import string
  14. import sys
  15. import re
  16. import os
  17. from git import * # GitPython
  18. from subprocess import *
  19. repo = Repo(os.getcwd())
  20. ref = 'origin/master..'
  21. path = ''
  22. # Usage: git changelog [COMMITISH] [-- [PATH]]
  23. saw_dashdash = False
  24. if len(sys.argv) > 1:
  25. for arg in sys.argv[1:]:
  26. if arg == "--":
  27. saw_dashdash = True
  28. elif saw_dashdash:
  29. path = arg
  30. else:
  31. ref = arg
  32. for commit in repo.iter_commits(ref, paths=path):
  33. hash_id = commit.sha
  34. author = commit.author
  35. date = commit.committed_date
  36. log_text = commit.message.split('\n')[0]
  37. log_text_remainder = commit.message.split('\n\n')[1:]
  38. while len(log_text_remainder) > 0 and not log_text_remainder[0]:
  39. log_text_remainder = log_text_remainder[1:]
  40. log_text_remainder = string.join(log_text_remainder, '\n\t')
  41. if log_text_remainder:
  42. log_text_remainder = '\n\t' + log_text_remainder
  43. diff = commit.diff(commit.parents[0])
  44. files = []
  45. for f in diff:
  46. p = f.a_blob.path or f.b_blob.path
  47. p2 = re.sub('^' + path + '/', '', p)
  48. if p != p2:
  49. files.append(p2)
  50. fp = Popen(["fmt", "-72"], shell = True, stdin = PIPE, stdout = PIPE)
  51. fp.stdin.write("\t* %s: %s" % (string.join(files, ",\n\t"), log_text))
  52. fp.stdin.close()
  53. log_text = fp.stdout.read()
  54. del fp
  55. print "%s %s <%s>\n\n%s%s" % \
  56. (time.strftime("%Y-%m-%d", time.gmtime(date)),
  57. author.name, author.email, log_text, log_text_remainder)
  58. # git-changelog ends here