git-obsolete-branch.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/usr/bin/python
  2. #
  3. # Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
  4. #
  5. # Permission to use, copy, modify, and/or distribute this software for any
  6. # purpose with or without fee is hereby granted, provided that the above
  7. # copyright notice and this permission notice appear in all copies.
  8. #
  9. # THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. # REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. # AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. # INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. # LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. # OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. # PERFORMANCE OF THIS SOFTWARE.
  16. #
  17. # This script lists obsolete (fully merged) branches. It is useful for periodic maintenance
  18. # of our GIT tree.
  19. #
  20. # This script requires python 2.7 or 3.
  21. #
  22. # I have limited experience in Python. If things are done in a strange or uncommon way, there
  23. # are no obscure reasons to do it that way, just plain lack of experience.
  24. #
  25. # tomek
  26. import string
  27. import subprocess
  28. import sys
  29. class Branch:
  30. MERGED=1
  31. NOTMERGED=2
  32. name = ""
  33. status = NOTMERGED
  34. last_commit = ""
  35. def branch_list_get(verbose):
  36. txt_list = subprocess.check_output(["git", "branch", "-r"])
  37. txt_list = txt_list.split(b"\n")
  38. out = []
  39. for branch in txt_list:
  40. if len(branch) == 0:
  41. continue
  42. if branch.find(b"->") != -1:
  43. continue
  44. if branch == b"origin/master":
  45. continue
  46. branch_info = Branch()
  47. # get branch name
  48. branch_info.name = branch.strip(b" ")
  49. branch_info.name = branch_info.name.decode("utf-8")
  50. # check if branch is merged or not
  51. if verbose:
  52. print("Checking branch %s" % branch_info.name)
  53. cmd = ["git", "diff", "master..." + branch_info.name ]
  54. diff = subprocess.check_output(cmd)
  55. if (len(diff) == 0):
  56. branch_info.status = Branch.MERGED
  57. # let's get the last contributor
  58. cmd = [ "git" , "log", "-n", "1", "--pretty=\"%ai,%ae,%an\"", branch_info.name ]
  59. offender = subprocess.check_output(cmd)
  60. offender = offender.strip(b"\n\"")
  61. # comment out this 2 lines to disable obfuscation
  62. offender = offender.replace(b"@", b"(at)")
  63. offender = offender.replace(b".", b"(dot)")
  64. branch_info.last_commit = offender.decode("utf-8")
  65. else:
  66. branch_info.status = Branch.NOTMERGED
  67. out.append(branch_info)
  68. # return (out)
  69. return (out)
  70. def branch_print(branches, csv, print_merged, print_notmerged, print_stats):
  71. merged = 0
  72. notmerged = 0
  73. merged_str = ""
  74. notmerged_str = ""
  75. for branch in branches:
  76. if (branch.status==Branch.MERGED):
  77. merged = merged + 1
  78. if (not print_merged):
  79. continue
  80. if (csv):
  81. print("%s,merged,%s" % (branch.name, branch.last_commit) )
  82. else:
  83. merged_str = merged_str + " " + branch.name
  84. else:
  85. # NOT MERGED
  86. notmerged = notmerged + 1
  87. if (not print_notmerged):
  88. continue
  89. if (csv):
  90. print("%s,notmerged,%s" % (branch.name, branch.last_commit) )
  91. else:
  92. notmerged_str = notmerged_str + " " + branch.name
  93. if (not csv):
  94. if (print_merged):
  95. print("Merged branches : %s" % (merged_str))
  96. if (print_notmerged):
  97. print("NOT merged branches: %s" % (notmerged_str))
  98. if (print_stats):
  99. print("#----------");
  100. print("#Merged : %d" % merged)
  101. print("#Not merged: %d" % notmerged)
  102. def show_help():
  103. print("This script prints out merged and/or unmerged branches of a GIT tree.")
  104. print("Supported command-line options:")
  105. print("")
  106. print("--csv produce CSV (coma separated value) output")
  107. print("--unmerged lists umerged branches")
  108. print("--skip-merged do not print merged branches (that are listed by default)")
  109. print("--stats prints out statistics")
  110. print("--help prints out this help")
  111. def main():
  112. usage = """%prog
  113. Lists all obsolete (fully merged into master) branches.
  114. """
  115. csv = False;
  116. merged = True;
  117. unmerged = False;
  118. stats = False;
  119. for x in sys.argv[1:]:
  120. if x == "--csv":
  121. csv = True;
  122. elif x == "--unmerged":
  123. unmerged = True;
  124. elif x == "--skip-merged":
  125. merged = False;
  126. elif x == "--stats":
  127. stats = True;
  128. elif x == "--help":
  129. show_help()
  130. return
  131. else:
  132. print("Invalid parameter: %s" % x)
  133. print("")
  134. show_help()
  135. return
  136. if csv:
  137. print("branch name,status,date,last commit(mail),last commit(name)")
  138. branch_list = branch_list_get(not csv)
  139. # Uncomment this to print out also merged branches
  140. # branch_print(branch_list, False, True, False)
  141. branch_print(branch_list, csv, merged, unmerged, stats)
  142. if __name__ == '__main__':
  143. main()