[email protected] | eebfb75 | 2012-03-19 19:30:55 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 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 |
| 4 | # found in the LICENSE file. |
| 5 | |
[email protected] | 647798c | 2012-05-24 17:43:54 | [diff] [blame] | 6 | """Lists branches with closed and abandoned issues.""" |
[email protected] | eebfb75 | 2012-03-19 19:30:55 | [diff] [blame] | 7 | |
[email protected] | 647798c | 2012-05-24 17:43:54 | [diff] [blame] | 8 | import optparse |
[email protected] | eebfb75 | 2012-03-19 19:30:55 | [diff] [blame] | 9 | import os |
| 10 | import sys |
| 11 | import urllib2 |
| 12 | |
| 13 | BASE_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 14 | DEPOT_TOOLS_DIR = os.path.dirname(BASE_DIR) |
| 15 | sys.path.insert(0, DEPOT_TOOLS_DIR) |
| 16 | |
| 17 | import git_cl |
| 18 | |
| 19 | |
| 20 | def get_branches(): |
| 21 | """Get list of all local git branches.""" |
[email protected] | c0cb457 | 2013-04-24 22:29:00 | [diff] [blame] | 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" ])) |
[email protected] | eebfb75 | 2012-03-19 19:30:55 | [diff] [blame] | 30 | |
| 31 | |
| 32 | class Branch(git_cl.Changelist): |
[email protected] | a3800b2 | 2013-05-02 01:41:35 | [diff] [blame] | 33 | def __init__(self, name, upstream=None): |
[email protected] | eebfb75 | 2012-03-19 19:30:55 | [diff] [blame] | 34 | git_cl.Changelist.__init__(self, branchref=name) |
[email protected] | c0cb457 | 2013-04-24 22:29:00 | [diff] [blame] | 35 | self._upstream = upstream |
| 36 | self._distance = None |
[email protected] | eebfb75 | 2012-03-19 19:30:55 | [diff] [blame] | 37 | self._issue_status = None |
[email protected] | 647798c | 2012-05-24 17:43:54 | [diff] [blame] | 38 | |
[email protected] | eebfb75 | 2012-03-19 19:30:55 | [diff] [blame] | 39 | def GetStatus(self): |
| 40 | if not self._issue_status: |
| 41 | if self.GetIssue(): |
[email protected] | 647798c | 2012-05-24 17:43:54 | [diff] [blame] | 42 | try: |
[email protected] | eebfb75 | 2012-03-19 19:30:55 | [diff] [blame] | 43 | issue_properties = self.RpcServer().get_issue_properties( |
| 44 | self.GetIssue(), None) |
| 45 | if issue_properties['closed']: |
| 46 | self._issue_status = 'closed' |
| 47 | else: |
| 48 | self._issue_status = 'pending' |
| 49 | except urllib2.HTTPError, e: |
| 50 | if e.code == 404: |
| 51 | self._issue_status = 'abandoned' |
| 52 | else: |
| 53 | self._issue_status = 'no-issue' |
[email protected] | c0cb457 | 2013-04-24 22:29:00 | [diff] [blame] | 54 | if (self._issue_status != 'pending' |
[email protected] | a3800b2 | 2013-05-02 01:41:35 | [diff] [blame] | 55 | and self._upstream |
[email protected] | c0cb457 | 2013-04-24 22:29:00 | [diff] [blame] | 56 | and not self.GetDistance()[0] |
| 57 | and not self._upstream.startswith("origin/")): |
| 58 | self._issue_status = 'empty' |
[email protected] | eebfb75 | 2012-03-19 19:30:55 | [diff] [blame] | 59 | return self._issue_status |
[email protected] | c0cb457 | 2013-04-24 22:29:00 | [diff] [blame] | 60 | |
| 61 | def GetDistance(self): |
[email protected] | a3800b2 | 2013-05-02 01:41:35 | [diff] [blame] | 62 | if self._upstream is None: |
| 63 | return None; |
[email protected] | c0cb457 | 2013-04-24 22:29:00 | [diff] [blame] | 64 | if not self._distance: |
| 65 | self._distance = [get_change_count(self._upstream, self.GetBranch()), |
| 66 | get_change_count(self.GetBranch(), self._upstream)] |
| 67 | return self._distance |
[email protected] | eebfb75 | 2012-03-19 19:30:55 | [diff] [blame] | 68 | |
[email protected] | c0cb457 | 2013-04-24 22:29:00 | [diff] [blame] | 69 | def GetDistanceInfo(self): |
[email protected] | a3800b2 | 2013-05-02 01:41:35 | [diff] [blame] | 70 | if not self._upstream: |
| 71 | return "<No upstream branch>" |
[email protected] | c0cb457 | 2013-04-24 22:29:00 | [diff] [blame] | 72 | formatted_dist = ", ".join(["%s %d" % (x,y) |
| 73 | for (x,y) in zip(["ahead","behind"], self.GetDistance()) if y]) |
| 74 | return "[%s%s]" % ( |
| 75 | self._upstream, ": " + formatted_dist if formatted_dist else "") |
[email protected] | eebfb75 | 2012-03-19 19:30:55 | [diff] [blame] | 76 | |
[email protected] | a3800b2 | 2013-05-02 01:41:35 | [diff] [blame] | 77 | def print_branches(title, fmt, branches): |
| 78 | if branches: |
| 79 | print title |
| 80 | for branch in branches: |
| 81 | print fmt.format(branch=branch.GetBranch(), |
| 82 | issue=branch.GetIssue(), |
| 83 | distance=branch.GetDistanceInfo()) |
| 84 | |
[email protected] | 647798c | 2012-05-24 17:43:54 | [diff] [blame] | 85 | def main(): |
| 86 | parser = optparse.OptionParser(usage=sys.modules['__main__'].__doc__) |
| 87 | options, args = parser.parse_args() |
| 88 | if args: |
| 89 | parser.error('Unsupported arg: %s' % args) |
| 90 | |
[email protected] | eebfb75 | 2012-03-19 19:30:55 | [diff] [blame] | 91 | branches = get_branches() |
| 92 | filtered = { 'closed' : [], |
[email protected] | c0cb457 | 2013-04-24 22:29:00 | [diff] [blame] | 93 | 'empty' : [], |
[email protected] | eebfb75 | 2012-03-19 19:30:55 | [diff] [blame] | 94 | 'pending' : [], |
| 95 | 'abandoned' : [], |
| 96 | 'no-issue' : []} |
| 97 | |
| 98 | for branch in branches: |
| 99 | filtered[branch.GetStatus()].append(branch) |
| 100 | |
[email protected] | a3800b2 | 2013-05-02 01:41:35 | [diff] [blame] | 101 | print_branches("# Branches with closed issues", |
| 102 | "git branch -D {branch} # Issue {issue} is closed.", |
| 103 | filtered['closed']) |
| 104 | print_branches("\n# Empty branches", |
| 105 | "git branch -D {branch} # Empty.", |
| 106 | filtered['empty']) |
| 107 | print_branches("\n# Pending Branches", |
| 108 | "# Branch {branch} - Issue {issue} - {distance}", |
| 109 | filtered['pending']); |
| 110 | print_branches("\n# Branches with abandoned issues", |
| 111 | "# Branch {branch} - was issue {issue} - {distance}", |
| 112 | filtered['abandoned']) |
[email protected] | eebfb75 | 2012-03-19 19:30:55 | [diff] [blame] | 113 | |
[email protected] | a3800b2 | 2013-05-02 01:41:35 | [diff] [blame] | 114 | print_branches("\n# Branches without associated issues", |
| 115 | "# Branch {branch} - {distance}", |
| 116 | filtered['no-issue']) |
[email protected] | eebfb75 | 2012-03-19 19:30:55 | [diff] [blame] | 117 | |
| 118 | return 0 |
| 119 | |
| 120 | |
| 121 | if __name__ == '__main__': |
[email protected] | 647798c | 2012-05-24 17:43:54 | [diff] [blame] | 122 | sys.exit(main()) |