OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """This module contains functions for performing source control operations.""" | 5 """This module contains functions for performing source control operations.""" |
6 | 6 |
7 import os | |
8 | |
9 import bisect_utils | 7 import bisect_utils |
10 | 8 |
11 CROS_VERSION_PATTERN = 'new version number from %s' | |
12 | |
13 | 9 |
14 def IsInGitRepository(): | 10 def IsInGitRepository(): |
15 output, _ = bisect_utils.RunGit(['rev-parse', '--is-inside-work-tree']) | 11 output, _ = bisect_utils.RunGit(['rev-parse', '--is-inside-work-tree']) |
16 return output.strip() == 'true' | 12 return output.strip() == 'true' |
17 | 13 |
18 | 14 |
19 def SyncToRevisionWithGClient(revision): | 15 def SyncToRevisionWithGClient(revision): |
20 """Uses gclient to sync to the specified revision. | 16 """Uses gclient to sync to the specified revision. |
21 | 17 |
22 This is like running gclient sync --revision <revision>. | 18 This is like running gclient sync --revision <revision>. |
23 | 19 |
24 Args: | 20 Args: |
25 revision: A git SHA1 hash or SVN revision number (depending on workflow). | 21 revision: A git SHA1 hash or SVN revision number (depending on workflow). |
26 | 22 |
27 Returns: | 23 Returns: |
28 The return code of the call. | 24 The return code of the call. |
29 """ | 25 """ |
30 return bisect_utils.RunGClient( | 26 return bisect_utils.RunGClient( |
31 ['sync', '--verbose', '--reset', '--force', | 27 ['sync', '--verbose', '--reset', '--force', |
32 '--delete_unversioned_trees', '--nohooks', '--revision', revision]) | 28 '--delete_unversioned_trees', '--nohooks', '--revision', revision]) |
33 | 29 |
34 | 30 |
35 def SyncToRevisionWithRepo(timestamp): | |
36 return bisect_utils.RunRepoSyncAtTimestamp(timestamp) | |
37 | |
38 | |
39 def GetRevisionList(end_revision_hash, start_revision_hash, cwd=None): | 31 def GetRevisionList(end_revision_hash, start_revision_hash, cwd=None): |
40 """Retrieves a list of git commit hashes in a range. | 32 """Retrieves a list of git commit hashes in a range. |
41 | 33 |
42 Args: | 34 Args: |
43 end_revision_hash: The SHA1 for the end of the range, inclusive. | 35 end_revision_hash: The SHA1 for the end of the range, inclusive. |
44 start_revision_hash: The SHA1 for the beginning of the range, inclusive. | 36 start_revision_hash: The SHA1 for the beginning of the range, inclusive. |
45 | 37 |
46 Returns: | 38 Returns: |
47 A list of the git commit hashes in the range, in reverse time order -- | 39 A list of the git commit hashes in the range, in reverse time order -- |
48 that is, starting with |end_revision_hash|. | 40 that is, starting with |end_revision_hash|. |
49 """ | 41 """ |
50 revision_range = '%s..%s' % (start_revision_hash, end_revision_hash) | 42 revision_range = '%s..%s' % (start_revision_hash, end_revision_hash) |
51 cmd = ['log', '--format=%H', '-10000', '--first-parent', revision_range] | 43 cmd = ['log', '--format=%H', '-10000', '--first-parent', revision_range] |
52 log_output = bisect_utils.CheckRunGit(cmd, cwd=cwd) | 44 log_output = bisect_utils.CheckRunGit(cmd, cwd=cwd) |
53 | 45 |
54 revision_hash_list = log_output.split() | 46 revision_hash_list = log_output.split() |
55 revision_hash_list.append(start_revision_hash) | 47 revision_hash_list.append(start_revision_hash) |
56 | 48 |
57 return revision_hash_list | 49 return revision_hash_list |
58 | 50 |
59 | 51 |
60 def SyncToRevision(revision, sync_client=None): | 52 def SyncToRevision(revision, sync_client=None): |
61 if not sync_client: | 53 if not sync_client: |
62 _, return_code = bisect_utils.RunGit(['checkout', revision]) | 54 _, return_code = bisect_utils.RunGit(['checkout', revision]) |
63 elif sync_client == 'gclient': | 55 elif sync_client == 'gclient': |
64 return_code = SyncToRevisionWithGClient(revision) | 56 return_code = SyncToRevisionWithGClient(revision) |
65 elif sync_client == 'repo': | |
66 return_code = SyncToRevisionWithRepo(revision) | |
67 else: | 57 else: |
68 raise NotImplementedError('Unsupported sync_client: "%s"' % sync_client) | 58 raise NotImplementedError('Unsupported sync_client: "%s"' % sync_client) |
69 | 59 |
70 return not return_code | 60 return not return_code |
71 | 61 |
72 | 62 |
73 def ResolveToRevision(revision_to_check, depot, depot_deps_dict, | 63 def ResolveToRevision(revision_to_check, depot, depot_deps_dict, |
74 search, cwd=None): | 64 search, cwd=None): |
75 """Tries to resolve an SVN revision or commit position to a git SHA1. | 65 """Tries to resolve an SVN revision or commit position to a git SHA1. |
76 | 66 |
77 Args: | 67 Args: |
78 revision_to_check: The user supplied revision string that may need to be | 68 revision_to_check: The user supplied revision string that may need to be |
79 resolved to a git commit hash. This may be an SVN revision, git commit | 69 resolved to a git commit hash. This may be an SVN revision, git commit |
80 position, or a git commit hash. | 70 position, or a git commit hash. |
81 depot: The depot (dependency repository) that |revision_to_check| is from. | 71 depot: The depot (dependency repository) that |revision_to_check| is from. |
82 depot_deps_dict: A dictionary with information about different depots. | 72 depot_deps_dict: A dictionary with information about different depots. |
83 search: How many revisions forward or backward to search. If the value is | 73 search: How many revisions forward or backward to search. If the value is |
84 negative, the function will search backwards chronologically, otherwise | 74 negative, the function will search backwards chronologically, otherwise |
85 it will search forward. | 75 it will search forward. |
86 | 76 |
87 Returns: | 77 Returns: |
88 A string containing a git SHA1 hash, otherwise None. | 78 A string containing a git SHA1 hash, otherwise None. |
89 """ | 79 """ |
90 # Android-chrome is git only, so no need to resolve this to anything else. | 80 # Android-chrome is git only, so no need to resolve this to anything else. |
91 if depot == 'android-chrome': | 81 if depot == 'android-chrome': |
92 return revision_to_check | 82 return revision_to_check |
93 | 83 |
94 if depot == 'cros': | |
95 return ResolveToRevisionCrOS(revision_to_check, cwd) | |
96 | |
97 # If the given revision can't be parsed as an integer, then it may already | 84 # If the given revision can't be parsed as an integer, then it may already |
98 # be a git commit hash. | 85 # be a git commit hash. |
99 if not bisect_utils.IsStringInt(revision_to_check): | 86 if not bisect_utils.IsStringInt(revision_to_check): |
100 return revision_to_check | 87 return revision_to_check |
101 | 88 |
102 depot_svn = 'svn://svn.chromium.org/chrome/trunk/src' | 89 depot_svn = 'svn://svn.chromium.org/chrome/trunk/src' |
103 | 90 |
104 if depot != 'chromium': | 91 if depot != 'chromium': |
105 depot_svn = depot_deps_dict[depot]['svn'] | 92 depot_svn = depot_deps_dict[depot]['svn'] |
106 svn_revision = int(revision_to_check) | 93 svn_revision = int(revision_to_check) |
(...skipping 15 matching lines...) Expand all Loading... |
122 log_output = bisect_utils.CheckRunGit(cmd, cwd=cwd) | 109 log_output = bisect_utils.CheckRunGit(cmd, cwd=cwd) |
123 log_output = log_output.strip() | 110 log_output = log_output.strip() |
124 | 111 |
125 if log_output: | 112 if log_output: |
126 git_revision = log_output | 113 git_revision = log_output |
127 break | 114 break |
128 | 115 |
129 return git_revision | 116 return git_revision |
130 | 117 |
131 | 118 |
132 def ResolveToRevisionCrOS(revision_to_check, cwd=None): | |
133 """Return a git commit hash corresponding to the give version or revision. | |
134 | |
135 TODO(qyearsley): Either verify that this works or delete it. | |
136 """ | |
137 if bisect_utils.IsStringInt(revision_to_check): | |
138 return int(revision_to_check) | |
139 | |
140 cwd = os.getcwd() | |
141 os.chdir(os.path.join(os.getcwd(), 'src', 'third_party', | |
142 'chromiumos-overlay')) | |
143 pattern = CROS_VERSION_PATTERN % revision_to_check | |
144 cmd = ['log', '--format=%ct', '-1', '--grep', pattern] | |
145 | |
146 git_revision = None | |
147 | |
148 log_output = bisect_utils.CheckRunGit(cmd, cwd=cwd) | |
149 if log_output: | |
150 git_revision = log_output | |
151 git_revision = int(log_output.strip()) | |
152 os.chdir(cwd) | |
153 | |
154 return git_revision | |
155 | |
156 | |
157 def IsInProperBranch(): | 119 def IsInProperBranch(): |
158 """Checks whether the current branch is "master".""" | 120 """Checks whether the current branch is "master".""" |
159 cmd = ['rev-parse', '--abbrev-ref', 'HEAD'] | 121 cmd = ['rev-parse', '--abbrev-ref', 'HEAD'] |
160 log_output = bisect_utils.CheckRunGit(cmd) | 122 log_output = bisect_utils.CheckRunGit(cmd) |
161 log_output = log_output.strip() | 123 log_output = log_output.strip() |
162 return log_output == 'master' | 124 return log_output == 'master' |
163 | 125 |
164 | 126 |
165 def GetCommitPosition(git_revision, cwd=None): | 127 def GetCommitPosition(git_revision, cwd=None): |
166 """Finds git commit postion for the given git hash. | 128 """Finds git commit postion for the given git hash. |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 'log', | 216 'log', |
255 '--format=%H', | 217 '--format=%H', |
256 '%s~1..%s' % (revision_start, revision_end), | 218 '%s~1..%s' % (revision_start, revision_end), |
257 '--', | 219 '--', |
258 filename, | 220 filename, |
259 ] | 221 ] |
260 output = bisect_utils.CheckRunGit(cmd) | 222 output = bisect_utils.CheckRunGit(cmd) |
261 lines = output.split('\n') | 223 lines = output.split('\n') |
262 return [o for o in lines if o] | 224 return [o for o in lines if o] |
263 | 225 |
OLD | NEW |