git-obsolete-branch.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/python
  2. # This script lists obsolete (fully merged) branches. It is useful for periodic maintenance
  3. # of our GIT tree.
  4. #
  5. # This script requires python 2.7 or 3.
  6. #
  7. # I have limited experience in Python. If things are done in a strange or uncommon way, there
  8. # are no obscure reasons to do it that way, just plain lack of experience. :)
  9. #
  10. # tomek
  11. import string
  12. import subprocess
  13. class Branch:
  14. MERGED=1
  15. NOTMERGED=2
  16. name = ""
  17. status = NOTMERGED
  18. last_commit = ""
  19. def branch_list_get():
  20. txt_list = subprocess.check_output(["git", "branch", "-r"])
  21. txt_list = txt_list.split(b"\n")
  22. out = []
  23. for branch in txt_list:
  24. if len(branch) == 0:
  25. continue
  26. if branch.find(b"->") != -1:
  27. continue
  28. if branch == b"origin/master":
  29. continue
  30. branch_info = Branch()
  31. # get branch name
  32. branch_info.name = branch.strip(b" ")
  33. branch_info.name = branch_info.name.decode("utf-8")
  34. # check if branch is merged or not
  35. print ("Checking branch %s" % branch_info.name)
  36. cmd = ["git", "diff", "master..." + branch_info.name ]
  37. diff = subprocess.check_output(cmd)
  38. if (len(diff) == 0):
  39. branch_info.status = Branch.MERGED
  40. # let's get the last contributor
  41. cmd = [ "git" , "log", "-n", "1", "--pretty=\"%ai,%ae,%an\"", branch_info.name ]
  42. offender = subprocess.check_output(cmd)
  43. offender = offender.strip(b"\n\"")
  44. # comment out this 2 lines to disable obfuscation
  45. offender = offender.replace(b"@", b"(at)")
  46. offender = offender.replace(b".", b"(dot)")
  47. branch_info.last_commit = offender.decode("utf-8")
  48. else:
  49. branch_info.status = Branch.NOTMERGED
  50. out.append(branch_info)
  51. # return (out)
  52. return (out)
  53. def branch_print(branches, print_merged, print_notmerged, print_stats):
  54. merged = 0
  55. notmerged = 0
  56. for branch in branches:
  57. if (branch.status==Branch.MERGED):
  58. merged = merged + 1
  59. if (not print_merged):
  60. continue
  61. print("%s,merged,%s" % (branch.name, branch.last_commit) )
  62. else:
  63. # NOT MERGED
  64. notmerged = notmerged + 1
  65. if (not print_notmerged):
  66. continue
  67. print("%s,notmerged,%s" % (branch.name, branch.last_commit) )
  68. if (print_stats):
  69. print("#----------");
  70. print("#Merged : %d" % merged)
  71. print("#Not merged: %d" % notmerged)
  72. def main():
  73. usage = """%prog
  74. Lists all obsolete (fully merged into master) branches.
  75. """
  76. print("branch name,status,date,last commit(mail),last commit(name)")
  77. branch_list = branch_list_get()
  78. # Uncomment this to print out also merged branches
  79. # branch_print(branch_list, False, True, False)
  80. branch_print(branch_list, True, False, True)
  81. if __name__ == '__main__':
  82. main()