util: Force the m5 utility to be built using c++14.
[gem5.git] / util / maint / list_changes.py
1 #!/usr/bin/env python3
2 #
3 # Copyright (c) 2017-2018 Arm Limited
4 # All rights reserved
5 #
6 # The license below extends only to copyright in the software and shall
7 # not be construed as granting a license to any other intellectual
8 # property including but not limited to intellectual property relating
9 # to a hardware implementation of the functionality of the software
10 # licensed hereunder. You may use the software subject to the license
11 # terms below provided that you ensure that this notice is replicated
12 # unmodified and in its entirety in all distributions of the software,
13 # modified or unmodified, in source code or in binary form.
14 #
15 # Redistribution and use in source and binary forms, with or without
16 # modification, are permitted provided that the following conditions are
17 # met: redistributions of source code must retain the above copyright
18 # notice, this list of conditions and the following disclaimer;
19 # redistributions in binary form must reproduce the above copyright
20 # notice, this list of conditions and the following disclaimer in the
21 # documentation and/or other materials provided with the distribution;
22 # neither the name of the copyright holders nor the names of its
23 # contributors may be used to endorse or promote products derived from
24 # this software without specific prior written permission.
25 #
26 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
38
39 import subprocess
40 import re
41 from functools import wraps
42
43 class Commit(object):
44 _re_tag = re.compile(r"^((?:\w|-)+): (.*)$")
45
46 def __init__(self, rev):
47 self.rev = rev
48 self._log = None
49 self._tags = None
50
51 def _git(self, args):
52 return subprocess.check_output([ "git", ] + args).decode()
53
54 @property
55 def log(self):
56 """Log message belonging to a commit returned as a list with on line
57 per element.
58
59 """
60 if self._log is None:
61 self._log = self._git(
62 ["show", "--format=%B", "--no-patch", str(self.rev) ]
63 ).rstrip("\n").split("\n")
64 return self._log
65
66 @property
67 def tags(self):
68 """Get all commit message tags in the current commit.
69
70 Returns: { tag, [ value, ... ] }
71
72 """
73 if self._tags is None:
74 tags = {}
75 for l in self.log[1:]:
76 m = Commit._re_tag.match(l)
77 if m:
78 key, value = m.group(1), m.group(2)
79 try:
80 tags[key].append(value)
81 except KeyError:
82 tags[key] = [ value ]
83 self._tags = tags
84
85 return self._tags
86
87 @property
88 def change_id(self):
89 """Get the Change-Id tag from the commit
90
91 Returns: A change ID or None if no change ID has been
92 specified.
93
94 """
95 try:
96 cids = self.tags["Change-Id"]
97 except KeyError:
98 return None
99
100 assert len(cids) == 1
101 return cids[0]
102
103 def __str__(self):
104 return "%s: %s" % (self.rev[0:8], self.log[0])
105
106 def list_revs(branch, baseline=None, paths=[]):
107 """Get a generator that lists git revisions that exist in 'branch'. If
108 the optional parameter 'baseline' is specified, the generator
109 excludes commits that exist on that branch.
110
111 Returns: Generator of Commit objects
112
113 """
114
115 if baseline is not None:
116 query = "%s..%s" % (branch, baseline)
117 else:
118 query = str(branch)
119
120 changes = subprocess.check_output(
121 [ "git", "rev-list", query, '--'] + paths
122 ).decode()
123
124 if changes == "":
125 return
126
127 for rev in changes.rstrip("\n").split("\n"):
128 assert rev != ""
129 yield Commit(rev)
130
131 def list_changes(upstream, feature, paths=[]):
132 feature_revs = tuple(list_revs(upstream, feature, paths=paths))
133 upstream_revs = tuple(list_revs(feature, upstream, paths=paths))
134
135 feature_cids = dict([
136 (c.change_id, c) for c in feature_revs if c.change_id is not None ])
137 upstream_cids = dict([
138 (c.change_id, c) for c in upstream_revs if c.change_id is not None ])
139
140 incoming = [r for r in reversed(upstream_revs) \
141 if r.change_id and r.change_id not in feature_cids]
142 outgoing = [r for r in reversed(feature_revs) \
143 if r.change_id and r.change_id not in upstream_cids]
144 common = [r for r in reversed(feature_revs) \
145 if r.change_id in upstream_cids]
146 upstream_unknown = [r for r in reversed(upstream_revs) \
147 if r.change_id is None]
148 feature_unknown = [r for r in reversed(feature_revs) \
149 if r.change_id is None]
150
151 return incoming, outgoing, common, upstream_unknown, feature_unknown
152
153 def _main():
154 import argparse
155 parser = argparse.ArgumentParser(
156 description="List incoming and outgoing changes in a feature branch")
157
158 parser.add_argument("--upstream", "-u", type=str, default="origin/master",
159 help="Upstream branch for comparison. " \
160 "Default: %(default)s")
161 parser.add_argument("--feature", "-f", type=str, default="HEAD",
162 help="Feature branch for comparison. " \
163 "Default: %(default)s")
164 parser.add_argument("--show-unknown", action="store_true",
165 help="Print changes without Change-Id tags")
166 parser.add_argument("--show-common", action="store_true",
167 help="Print common changes")
168 parser.add_argument("--deep-search", action="store_true",
169 help="Use a deep search to find incorrectly " \
170 "rebased changes")
171 parser.add_argument("paths", metavar="PATH", type=str, nargs="*",
172 help="Paths to list changes for")
173
174 args = parser.parse_args()
175
176 incoming, outgoing, common, upstream_unknown, feature_unknown = \
177 list_changes(args.upstream, args.feature, paths=args.paths)
178
179 if incoming:
180 print("Incoming changes:")
181 for rev in incoming:
182 print(rev)
183 print()
184
185 if args.show_unknown and upstream_unknown:
186 print("Upstream changes without change IDs:")
187 for rev in upstream_unknown:
188 print(rev)
189 print()
190
191 if outgoing:
192 print("Outgoing changes:")
193 for rev in outgoing:
194 print(rev)
195 print()
196
197 if args.show_common and common:
198 print("Common changes:")
199 for rev in common:
200 print(rev)
201 print()
202
203 if args.show_unknown and feature_unknown:
204 print("Outgoing changes without change IDs:")
205 for rev in feature_unknown:
206 print(rev)
207
208 if args.deep_search:
209 print("Incorrectly rebased changes:")
210 all_upstream_revs = list_revs(args.upstream, paths=args.paths)
211 all_upstream_cids = dict([
212 (c.change_id, c) for c in all_upstream_revs \
213 if c.change_id is not None ])
214 incorrect_outgoing = [r for r in outgoing if r.change_id in all_upstream_cids]
215 for rev in incorrect_outgoing:
216 print(rev)
217
218
219
220
221 if __name__ == "__main__":
222 _main()