Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(120)

Side by Side Diff: appengine/findit/common/repo_util.py

Issue 2435863003: [Findit] Add local git parsers. (Closed)
Patch Set: Fix nits. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright 2016 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import json
6 import re
7 import urllib2
8
9 CODE_REVIEW_URL_PATTERN = re.compile(
10 '^(?:Review URL|Review-Url): (.*\d+).*$', re.IGNORECASE)
11 COMMIT_POSITION_PATTERN = re.compile(
12 '^Cr-Commit-Position: refs/heads/master@{#(\d+)}$', re.IGNORECASE)
13 REVERTED_REVISION_PATTERN = re.compile(
14 '^> Committed: https://.+/([0-9a-fA-F]{40})$', re.IGNORECASE)
15
16
17 def ExtractCommitPositionAndCodeReviewUrl(message):
18 """Returns the commit position and code review url in the commit message.
19
20 A "commit position" is something similar to SVN version ids; i.e.,
21 numeric identifiers which are issued in sequential order. The reason
22 we care about them is that they're easier for humans to read than
23 the hashes that Git uses internally for identifying commits. We
24 should never actually use them for *identifying* commits; they're
25 only for pretty printing to humans.
26
27 Returns:
28 (commit_position, code_review_url)
29 """
30 if not message:
31 return (None, None)
32
33 commit_position = None
34 code_review_url = None
35
36 # Commit position and code review url are in the last 5 lines.
37 lines = message.strip().split('\n')[-5:]
lijeffrey 2016/10/20 23:31:32 move this to a constant
Sharu Jiang 2016/10/21 01:07:42 Cannot think of a good name except a extremely lon
wrengr 2016/10/28 18:15:36 START_OF_CR_COMMIT_POSITION (or expand "CR" if pre
Sharu Jiang 2016/10/28 21:05:36 Sounds good :)
38 lines.reverse()
39
40 for line in lines:
41 if commit_position is None:
42 match = COMMIT_POSITION_PATTERN.match(line)
43 if match:
44 commit_position = int(match.group(1))
45
46 if code_review_url is None:
47 match = CODE_REVIEW_URL_PATTERN.match(line)
48 if match:
49 code_review_url = match.group(1)
50 return (commit_position, code_review_url)
51
52
53 def NormalizeEmail(email):
54 """Normalizes the email from git repo.
55
56 Some email is like: test@chromium.org@bbb929c8-8fbe-4397-9dbb-9b2b20218538.
57 """
58 parts = email.split('@')
59 return '@'.join(parts[0:2])
60
61
62 def GetRevertedRevision(message):
63 """Parse message to get the reverted revision if there is one."""
64 lines = message.strip().splitlines()
65 if not lines[0].lower().startswith('revert'):
66 return None
67
68 for line in reversed(lines): # pragma: no cover
69 # TODO: Handle cases where no reverted_revision in reverting message.
70 reverted_revision_match = REVERTED_REVISION_PATTERN.match(line)
71 if reverted_revision_match:
72 return reverted_revision_match.group(1)
73
74
75 def GetRepoToCloneUrlDict(host_url):
76 # Gerrit prepends )]}' to json-formatted response.
77 content = urllib2.urlopen('%s?format=json' % host_url).read()
78 prefix = ')]}\'\n'
79 repo_infos = json.loads(content[len(prefix):])
80
81 repo_to_clone_url = {}
82 for repo, repo_info in repo_infos.iteritems():
83 repo_to_clone_url[repo] = repo_info['clone_url']
84
85 return repo_to_clone_url
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698