gitlog2changelog.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/usr/bin/env python3
  2. # Copyright 2008 Marcus D. Hanwell <marcus@cryos.org>
  3. # Minor changes for NUT by Charles Lepple
  4. # Distributed under the terms of the GNU General Public License v2 or later
  5. import string
  6. import re
  7. import os
  8. from textwrap import TextWrapper
  9. import sys
  10. rev_range = ''
  11. if len(sys.argv) > 1:
  12. base = sys.argv[1]
  13. rev_range = '%s..HEAD' % base
  14. # Execute git log with the desired command line options.
  15. fin = os.popen('git log --summary --stat --no-merges --date=short %s' % rev_range, 'r')
  16. # Create a ChangeLog file in the current directory.
  17. fout = open('ChangeLog', 'w')
  18. # Set up the loop variables in order to locate the blocks we want
  19. authorFound = False
  20. dateFound = False
  21. messageFound = False
  22. filesFound = False
  23. message = ""
  24. messageNL = False
  25. files = ""
  26. prevAuthorLine = ""
  27. wrapper = TextWrapper(initial_indent="\t", subsequent_indent="\t ")
  28. # The main part of the loop
  29. for line in fin:
  30. # The commit line marks the start of a new commit object.
  31. if line.startswith('commit'):
  32. # Start all over again...
  33. authorFound = False
  34. dateFound = False
  35. messageFound = False
  36. messageNL = False
  37. message = ""
  38. filesFound = False
  39. files = ""
  40. continue
  41. # Match the author line and extract the part we want
  42. # (Don't use startswith to allow Author override inside commit message.)
  43. elif 'Author:' in line:
  44. authorList = re.split(': ', line, 1)
  45. try:
  46. author = authorList[1]
  47. author = author[0:len(author)-1]
  48. authorFound = True
  49. except:
  50. print ("Could not parse authorList = '%s'" % (line))
  51. # Match the date line
  52. elif line.startswith('Date:'):
  53. dateList = re.split(': ', line, 1)
  54. try:
  55. date = dateList[1]
  56. date = date[0:len(date)-1]
  57. dateFound = True
  58. except:
  59. print ("Could not parse dateList = '%s'" % (line))
  60. # The Fossil-IDs are ignored:
  61. elif line.startswith(' Fossil-ID:') or line.startswith(' [[SVN:'):
  62. continue
  63. # The svn-id lines are ignored
  64. elif ' git-svn-id:' in line:
  65. continue
  66. # The sign off line is ignored too
  67. elif 'Signed-off-by' in line:
  68. continue
  69. # Extract the actual commit message for this commit
  70. elif authorFound & dateFound & messageFound == False:
  71. # Find the commit message if we can
  72. if len(line) == 1:
  73. if messageNL:
  74. messageFound = True
  75. else:
  76. messageNL = True
  77. elif len(line) == 4:
  78. messageFound = True
  79. else:
  80. if len(message) == 0:
  81. message = message + line.strip()
  82. else:
  83. message = message + " " + line.strip()
  84. # If this line is hit all of the files have been stored for this commit
  85. elif re.search('files? changed', line):
  86. filesFound = True
  87. continue
  88. # Collect the files for this commit. FIXME: Still need to add +/- to files
  89. elif authorFound & dateFound & messageFound:
  90. fileList = re.split(' \| ', line, 2)
  91. if len(fileList) > 1:
  92. if len(files) > 0:
  93. files = files + ", " + fileList[0].strip()
  94. else:
  95. files = fileList[0].strip()
  96. # All of the parts of the commit have been found - write out the entry
  97. if authorFound & dateFound & messageFound & filesFound:
  98. # First the author line, only outputted if it is the first for that
  99. # author on this day
  100. authorLine = date + " " + author
  101. if len(prevAuthorLine) == 0:
  102. fout.write(authorLine + "\n\n")
  103. elif authorLine == prevAuthorLine:
  104. pass
  105. else:
  106. fout.write("\n" + authorLine + "\n\n")
  107. # Assemble the actual commit message line(s) and limit the line length
  108. # to 80 characters.
  109. commitLine = "* " + files + ": " + message
  110. # Write out the commit line
  111. fout.write(wrapper.fill(commitLine) + "\n")
  112. #Now reset all the variables ready for a new commit block.
  113. authorFound = False
  114. dateFound = False
  115. messageFound = False
  116. messageNL = False
  117. message = ""
  118. filesFound = False
  119. files = ""
  120. prevAuthorLine = authorLine
  121. # Close the input and output lines now that we are finished.
  122. fin.close()
  123. fout.close()