[Tizen] Add prelauncher
[platform/framework/web/crosswalk-tizen.git] / vendor / depot_tools / git_footers.py
1 #!/usr/bin/env python
2 # Copyright 2014 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
6 import argparse
7 import re
8 import sys
9
10 from collections import defaultdict
11
12 import git_common as git
13
14
15 FOOTER_PATTERN = re.compile(r'^\s*([\w-]+): (.*)$')
16 CHROME_COMMIT_POSITION_PATTERN = re.compile(r'^([\w/-]+)@{#(\d+)}$')
17 GIT_SVN_ID_PATTERN = re.compile('^([^\s@]+)@(\d+)')
18
19
20 def normalize_name(header):
21   return '-'.join([ word.title() for word in header.strip().split('-') ])
22
23
24 def parse_footer(line):
25   match = FOOTER_PATTERN.match(line)
26   if match:
27     return (match.group(1), match.group(2))
28   else:
29     return None
30
31
32 def parse_footers(message):
33   """Parses a git commit message into a multimap of footers."""
34   footer_lines = []
35   for line in reversed(message.splitlines()):
36     if line == '' or line.isspace():
37       break
38     footer_lines.append(line)
39
40   footers = map(parse_footer, footer_lines)
41   if not all(footers):
42     return defaultdict(list)
43
44   footer_map = defaultdict(list)
45   for (k, v) in footers:
46     footer_map[normalize_name(k)].append(v.strip())
47
48   return footer_map
49
50
51 def get_unique(footers, key):
52   key = normalize_name(key)
53   values = footers[key]
54   assert len(values) <= 1, 'Multiple %s footers' % key
55   if values:
56     return values[0]
57   else:
58     return None
59
60
61 def get_position(footers):
62   """Get the commit position from the footers multimap using a heuristic.
63
64   Returns:
65     A tuple of the branch and the position on that branch. For example,
66
67     Cr-Commit-Position: refs/heads/master@{#292272}
68
69     would give the return value ('refs/heads/master', 292272).  If
70     Cr-Commit-Position is not defined, we try to infer the ref and position
71     from git-svn-id. The position number can be None if it was not inferrable.
72   """
73
74   position = get_unique(footers, 'Cr-Commit-Position')
75   if position:
76     match = CHROME_COMMIT_POSITION_PATTERN.match(position)
77     assert match, 'Invalid Cr-Commit-Position value: %s' % position
78     return (match.group(1), match.group(2))
79
80   svn_commit = get_unique(footers, 'git-svn-id')
81   if svn_commit:
82     match = GIT_SVN_ID_PATTERN.match(svn_commit)
83     assert match, 'Invalid git-svn-id value: %s' % svn_commit
84     # V8 has different semantics than Chromium.
85     if re.match(r'.*https?://v8\.googlecode\.com/svn/trunk',
86                 match.group(1)):
87       return ('refs/heads/candidates', match.group(2))
88     if re.match(r'.*https?://v8\.googlecode\.com/svn/branches/bleeding_edge',
89                 match.group(1)):
90       return ('refs/heads/master', match.group(2))
91
92     # Assume that any trunk svn revision will match the commit-position
93     # semantics.
94     if re.match('.*/trunk.*$', match.group(1)):
95       return ('refs/heads/master', match.group(2))
96
97     # But for now only support faking branch-heads for chrome.
98     branch_match = re.match('.*/chrome/branches/([\w/-]+)/src$', match.group(1))
99     if branch_match:
100       # svn commit numbers do not map to branches.
101       return ('refs/branch-heads/%s' % branch_match.group(1), None)
102
103   raise ValueError('Unable to infer commit position from footers')
104
105
106 def main(args):
107   parser = argparse.ArgumentParser(
108     formatter_class=argparse.ArgumentDefaultsHelpFormatter
109   )
110   parser.add_argument('ref')
111
112   g = parser.add_mutually_exclusive_group()
113   g.add_argument('--key', metavar='KEY',
114                  help='Get all values for the given footer name, one per '
115                  'line (case insensitive)')
116   g.add_argument('--position', action='store_true')
117   g.add_argument('--position-ref', action='store_true')
118   g.add_argument('--position-num', action='store_true')
119
120
121   opts = parser.parse_args(args)
122
123   message = git.run('log', '-1', '--format=%B', opts.ref)
124   footers = parse_footers(message)
125
126   if opts.key:
127     for v in footers.get(normalize_name(opts.key), []):
128       print v
129   elif opts.position:
130     pos = get_position(footers)
131     print '%s@{#%s}' % (pos[0], pos[1] or '?')
132   elif opts.position_ref:
133     print get_position(footers)[0]
134   elif opts.position_num:
135     pos = get_position(footers)
136     assert pos[1], 'No valid position for commit'
137     print pos[1]
138   else:
139     for k in footers.keys():
140       for v in footers[k]:
141         print '%s: %s' % (k, v)
142
143
144 if __name__ == '__main__':
145   sys.exit(main(sys.argv[1:]))