OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Lists branches with closed and abandoned issues.""" | 6 """Lists branches with closed and abandoned issues.""" |
7 | 7 |
8 import optparse | 8 import optparse |
9 import os | 9 import os |
10 import sys | 10 import sys |
11 import urllib2 | 11 import urllib2 |
12 | 12 |
13 BASE_DIR = os.path.dirname(os.path.abspath(__file__)) | 13 BASE_DIR = os.path.dirname(os.path.abspath(__file__)) |
14 DEPOT_TOOLS_DIR = os.path.dirname(BASE_DIR) | 14 DEPOT_TOOLS_DIR = os.path.dirname(BASE_DIR) |
15 sys.path.insert(0, DEPOT_TOOLS_DIR) | 15 sys.path.insert(0, DEPOT_TOOLS_DIR) |
16 | 16 |
17 import git_cl | 17 import git_cl |
18 | 18 |
19 | 19 |
20 def get_branches(): | 20 def get_branches(): |
21 """Get list of all local git branches.""" | 21 """Get list of all local git branches.""" |
22 return [Branch(l[2:]) for l in git_cl.RunGit(["branch"]).splitlines()] | 22 branches = [l.split() for l in git_cl.RunGit( |
| 23 ["for-each-ref", |
| 24 "--format=%(refname:short) %(upstream:short)", |
| 25 "refs/heads"]).splitlines()] |
| 26 return [Branch(*b) for b in branches] |
| 27 |
| 28 def get_change_count(start, end): |
| 29 return int(git_cl.RunGit(["rev-list", "%s..%s" % (start, end), "--count" ])) |
23 | 30 |
24 | 31 |
25 class Branch(git_cl.Changelist): | 32 class Branch(git_cl.Changelist): |
26 def __init__(self, name): | 33 def __init__(self, name, upstream): |
27 git_cl.Changelist.__init__(self, branchref=name) | 34 git_cl.Changelist.__init__(self, branchref=name) |
| 35 self._upstream = upstream |
| 36 self._distance = None |
28 self._issue_status = None | 37 self._issue_status = None |
29 | 38 |
30 def GetStatus(self): | 39 def GetStatus(self): |
31 if not self._issue_status: | 40 if not self._issue_status: |
32 if self.GetIssue(): | 41 if self.GetIssue(): |
33 try: | 42 try: |
34 issue_properties = self.RpcServer().get_issue_properties( | 43 issue_properties = self.RpcServer().get_issue_properties( |
35 self.GetIssue(), None) | 44 self.GetIssue(), None) |
36 if issue_properties['closed']: | 45 if issue_properties['closed']: |
37 self._issue_status = 'closed' | 46 self._issue_status = 'closed' |
38 else: | 47 else: |
39 self._issue_status = 'pending' | 48 self._issue_status = 'pending' |
40 except urllib2.HTTPError, e: | 49 except urllib2.HTTPError, e: |
41 if e.code == 404: | 50 if e.code == 404: |
42 self._issue_status = 'abandoned' | 51 self._issue_status = 'abandoned' |
43 else: | 52 else: |
44 self._issue_status = 'no-issue' | 53 self._issue_status = 'no-issue' |
| 54 if (self._issue_status != 'pending' |
| 55 and not self.GetDistance()[0] |
| 56 and not self._upstream.startswith("origin/")): |
| 57 self._issue_status = 'empty' |
45 return self._issue_status | 58 return self._issue_status |
| 59 |
| 60 def GetDistance(self): |
| 61 if not self._distance: |
| 62 self._distance = [get_change_count(self._upstream, self.GetBranch()), |
| 63 get_change_count(self.GetBranch(), self._upstream)] |
| 64 return self._distance |
46 | 65 |
| 66 def GetDistanceInfo(self): |
| 67 formatted_dist = ", ".join(["%s %d" % (x,y) |
| 68 for (x,y) in zip(["ahead","behind"], self.GetDistance()) if y]) |
| 69 return "[%s%s]" % ( |
| 70 self._upstream, ": " + formatted_dist if formatted_dist else "") |
47 | 71 |
48 def main(): | 72 def main(): |
49 parser = optparse.OptionParser(usage=sys.modules['__main__'].__doc__) | 73 parser = optparse.OptionParser(usage=sys.modules['__main__'].__doc__) |
50 options, args = parser.parse_args() | 74 options, args = parser.parse_args() |
51 if args: | 75 if args: |
52 parser.error('Unsupported arg: %s' % args) | 76 parser.error('Unsupported arg: %s' % args) |
53 | 77 |
54 branches = get_branches() | 78 branches = get_branches() |
55 filtered = { 'closed' : [], | 79 filtered = { 'closed' : [], |
| 80 'empty' : [], |
56 'pending' : [], | 81 'pending' : [], |
57 'abandoned' : [], | 82 'abandoned' : [], |
58 'no-issue' : []} | 83 'no-issue' : []} |
59 | 84 |
60 for branch in branches: | 85 for branch in branches: |
61 filtered[branch.GetStatus()].append(branch) | 86 filtered[branch.GetStatus()].append(branch) |
62 | 87 |
63 print "# Branches with closed issues" | 88 print "# Branches with closed issues" |
64 for branch in filtered['closed']: | 89 for branch in filtered['closed']: |
65 print "git branch -D %s # Issue %s is closed." % (branch.GetBranch(), | 90 print "git branch -D %s # Issue %s is closed." % (branch.GetBranch(), |
66 branch.GetIssue()) | 91 branch.GetIssue()) |
67 | 92 |
| 93 print "# Empty branches" |
| 94 for branch in filtered['empty']: |
| 95 print "git branch -D %s # Empty." % (branch.GetBranch()) |
| 96 |
68 print "\n# Pending Branches" | 97 print "\n# Pending Branches" |
69 for branch in filtered['pending']: | 98 for branch in filtered['pending']: |
70 print "# Branch %s - Issue %s" % (branch.GetBranch(), branch.GetIssue()) | 99 print "# Branch %s - Issue %s - %s" % ( |
| 100 branch.GetBranch(), branch.GetIssue(), branch.GetDistanceInfo()) |
71 | 101 |
72 print "\n# Branches with abandoned issues" | 102 print "\n# Branches with abandoned issues" |
73 for branch in filtered['abandoned']: | 103 for branch in filtered['abandoned']: |
74 print "# Branch %s - was issue %s" % ( | 104 print "# Branch %s - was issue %s - %s" % ( |
75 branch.GetBranch(), branch.GetIssue()) | 105 branch.GetBranch(), branch.GetIssue(), branch.GetDistanceInfo()) |
76 | 106 |
77 print "\n# Branches without associated issues" | 107 print "\n# Branches without associated issues" |
78 for branch in filtered['no-issue']: | 108 for branch in filtered['no-issue']: |
79 print "# Branch %s" % (branch.GetBranch()) | 109 print "# Branch %s - %s" % (branch.GetBranch(), branch.GetDistanceInfo()) |
80 | 110 |
81 return 0 | 111 return 0 |
82 | 112 |
83 | 113 |
84 if __name__ == '__main__': | 114 if __name__ == '__main__': |
85 sys.exit(main()) | 115 sys.exit(main()) |
OLD | NEW |