gitlog2changelog.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/usr/bin/python
  2. # Copyright 2008 Marcus D. Hanwell <marcus@cryos.org>
  3. # Adapted for Claws Mail - Copyright 2013 Colin Leroy <colin@colino.net>
  4. # Distributed under the terms of the GNU General Public License v2 or later
  5. import string, re, os, sys
  6. curRev = ""
  7. prevVer = ""
  8. if len(sys.argv) == 3:
  9. curRev = sys.argv[2]
  10. prevVer = sys.argv[1]
  11. else:
  12. curRevCmd = os.popen("git describe")
  13. curRev = curRevCmd.read()
  14. curRev = curRev[0:len(curRev)-1]
  15. curRevCmd.close()
  16. if len(sys.argv) == 2:
  17. prevVer = sys.argv[1]
  18. else:
  19. prevVer = re.split('-', curRev, 1)[0]
  20. # Execute git log with the desired command line options.
  21. fin = os.popen('git log ' + prevVer + '..' + curRev +' --summary --stat --no-merges --date=short', 'r')
  22. # Create a ChangeLog file in the current directory.
  23. fout = sys.stdout
  24. # Set up the loop variables in order to locate the blocks we want
  25. authorFound = False
  26. dateFound = False
  27. messageFound = False
  28. filesFound = False
  29. message = ""
  30. commit = ""
  31. messageNL = False
  32. files = ""
  33. prevAuthorLine = ""
  34. # The main part of the loop
  35. for line in fin:
  36. # The commit line marks the start of a new commit object.
  37. if re.match('^commit', line) >= 0:
  38. # Start all over again...
  39. authorFound = False
  40. dateFound = False
  41. messageFound = False
  42. messageNL = False
  43. message = ""
  44. filesFound = False
  45. files = ""
  46. commitCmd = os.popen("git describe "+re.split(' ', line, 1)[1])
  47. commit = commitCmd.read()
  48. commitCmd.close()
  49. commit = commit[0:len(commit)-1]
  50. continue
  51. # Match the author line and extract the part we want
  52. elif re.match('^Author:', line) >=0:
  53. authorList = re.split(': ', line, 1)
  54. author = re.split('<', authorList[1], 1)[0]
  55. author = "[" + author[0:len(author)-1]+"]"
  56. authorFound = True
  57. # Match the date line
  58. elif re.match('^Date:', line) >= 0:
  59. dateList = re.split(': ', line, 1)
  60. date = dateList[1]
  61. date = date[0:len(date)-1]
  62. dateFound = True
  63. # The svn-id lines are ignored
  64. elif re.match(' git-svn-id:', line) >= 0:
  65. continue
  66. # The sign off line is ignored too
  67. elif re.search('Signed-off-by', line) >= 0:
  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) >= 0:
  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 + "\n\t* " + fileList[0].strip()
  94. else:
  95. files = "\t* " + 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("\n");
  103. fout.write(authorLine + " " + commit + "\n\n")
  104. # Assemble the actual commit message line(s) and limit the line length
  105. # to 80 characters.
  106. commitLine = message
  107. i = 0
  108. commit = ""
  109. while i < len(commitLine):
  110. if len(commitLine) < i + 70:
  111. commit = commit + "\n\t\t" + commitLine[i:len(commitLine)]
  112. break
  113. index = commitLine.rfind(' ', i, i+70)
  114. if index > i:
  115. commit = commit + "\n\t\t" + commitLine[i:index]
  116. i = index+1
  117. else:
  118. commit = commit + "\n\t\t" + commitLine[i:70]
  119. i = i+71
  120. # Write out the commit line
  121. fout.write(files + "\t\t" + commit + "\n")
  122. #Now reset all the variables ready for a new commit block.
  123. authorFound = False
  124. dateFound = False
  125. messageFound = False
  126. messageNL = False
  127. message = ""
  128. filesFound = False
  129. files = ""
  130. prevAuthorLine = authorLine
  131. # Close the input and output lines now that we are finished.
  132. fin.close()
  133. fout.close()