Kuang-che Wu | 6e4beca | 2018-06-27 17:45:02 +0800 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Kuang-che Wu | e41e006 | 2017-09-01 19:04:14 +0800 | [diff] [blame] | 2 | # Copyright 2017 The Chromium OS 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 | """Git utility.""" |
| 6 | |
| 7 | from __future__ import print_function |
| 8 | import logging |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 9 | import os |
Kuang-che Wu | e41e006 | 2017-09-01 19:04:14 +0800 | [diff] [blame] | 10 | import re |
Kuang-che Wu | 3d04eda | 2019-09-05 23:56:40 +0800 | [diff] [blame] | 11 | import shutil |
Kuang-che Wu | e41e006 | 2017-09-01 19:04:14 +0800 | [diff] [blame] | 12 | import subprocess |
Kuang-che Wu | 2b1286b | 2019-05-20 20:37:26 +0800 | [diff] [blame] | 13 | import time |
Kuang-che Wu | e41e006 | 2017-09-01 19:04:14 +0800 | [diff] [blame] | 14 | |
| 15 | from bisect_kit import cli |
| 16 | from bisect_kit import util |
| 17 | |
| 18 | logger = logging.getLogger(__name__) |
| 19 | |
| 20 | GIT_FULL_COMMIT_ID_LENGTH = 40 |
| 21 | |
| 22 | # Minimal acceptable length of git commit id. |
| 23 | # |
| 24 | # For chromium, hash collision rate over number of digits: |
| 25 | # - 6 digits: 4.85% |
| 26 | # - 7 digits: 0.32% |
| 27 | # - 8 digits: 0.01% |
| 28 | # As foolproof check, 7 digits should be enough. |
| 29 | GIT_MIN_COMMIT_ID_LENGTH = 7 |
| 30 | |
| 31 | |
| 32 | def is_git_rev(s): |
| 33 | """Is a git hash-like version string. |
| 34 | |
| 35 | It accepts shortened hash with at least 7 digits. |
| 36 | """ |
| 37 | if not GIT_MIN_COMMIT_ID_LENGTH <= len(s) <= GIT_FULL_COMMIT_ID_LENGTH: |
| 38 | return False |
| 39 | return bool(re.match(r'^[0-9a-f]+$', s)) |
| 40 | |
| 41 | |
| 42 | def argtype_git_rev(s): |
| 43 | """Validates git hash.""" |
| 44 | if not is_git_rev(s): |
| 45 | msg = 'should be git hash, at least %d digits' % GIT_MIN_COMMIT_ID_LENGTH |
| 46 | raise cli.ArgTypeError(msg, '1a2b3c4d5e') |
| 47 | return s |
| 48 | |
| 49 | |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 50 | def is_git_root(path): |
| 51 | """Is given path root of git repo.""" |
| 52 | return os.path.exists(os.path.join(path, '.git')) |
| 53 | |
| 54 | |
Kuang-che Wu | 0836654 | 2019-01-12 12:37:49 +0800 | [diff] [blame] | 55 | def is_git_bare_dir(path): |
| 56 | """Is inside .git folder or bare git checkout.""" |
| 57 | if not os.path.isdir(path): |
| 58 | return False |
| 59 | try: |
| 60 | return util.check_output( |
| 61 | 'git', 'rev-parse', '--is-bare-repository', cwd=path) == 'true\n' |
| 62 | except subprocess.CalledProcessError: |
| 63 | return False |
| 64 | |
| 65 | |
Kuang-che Wu | 6948ecc | 2018-09-11 17:43:49 +0800 | [diff] [blame] | 66 | def clone(git_repo, repo_url, reference=None): |
| 67 | if not os.path.exists(git_repo): |
| 68 | os.makedirs(git_repo) |
| 69 | cmd = ['git', 'clone', repo_url, '.'] |
| 70 | if reference: |
| 71 | cmd += ['--reference', reference] |
| 72 | util.check_call(*cmd, cwd=git_repo) |
| 73 | |
| 74 | |
Kuang-che Wu | e41e006 | 2017-09-01 19:04:14 +0800 | [diff] [blame] | 75 | def checkout_version(git_repo, rev): |
| 76 | """git checkout. |
| 77 | |
| 78 | Args: |
| 79 | git_repo: path of git repo. |
| 80 | rev: git commit revision to checkout. |
| 81 | """ |
| 82 | util.check_call('git', 'checkout', '-q', '-f', rev, cwd=git_repo) |
| 83 | |
| 84 | |
Kuang-che Wu | 8a28a9d | 2018-09-11 17:43:36 +0800 | [diff] [blame] | 85 | def init(git_repo): |
| 86 | """git init. |
| 87 | |
| 88 | git_repo and its parent directories will be created if they don't exist. |
| 89 | |
| 90 | Args: |
| 91 | git_repo: path of git repo. |
| 92 | """ |
| 93 | if not os.path.exists(git_repo): |
| 94 | os.makedirs(git_repo) |
| 95 | |
| 96 | util.check_call('git', 'init', '-q', cwd=git_repo) |
| 97 | |
| 98 | |
| 99 | def commit_file(git_repo, |
| 100 | path, |
| 101 | message, |
| 102 | content, |
| 103 | commit_time=None, |
| 104 | author_time=None): |
| 105 | """Commit a file. |
| 106 | |
| 107 | Args: |
| 108 | git_repo: path of git repo |
| 109 | path: file path, relative to git_repo |
| 110 | message: commit message |
| 111 | content: file content |
| 112 | commit_time: commit timestamp |
| 113 | author_time: author timestamp |
| 114 | """ |
| 115 | if author_time is None: |
| 116 | author_time = commit_time |
| 117 | |
| 118 | env = {} |
| 119 | if author_time: |
| 120 | env['GIT_AUTHOR_DATE'] = str(author_time) |
| 121 | if commit_time: |
| 122 | env['GIT_COMMITTER_DATE'] = str(commit_time) |
| 123 | |
| 124 | full_path = os.path.join(git_repo, path) |
| 125 | dirname = os.path.dirname(full_path) |
| 126 | if not os.path.exists(dirname): |
| 127 | os.makedirs(dirname) |
| 128 | with open(full_path, 'w') as f: |
| 129 | f.write(content) |
| 130 | |
| 131 | util.check_call('git', 'add', path, cwd=git_repo) |
| 132 | util.check_call( |
| 133 | 'git', 'commit', '-q', '-m', message, path, cwd=git_repo, env=env) |
| 134 | |
| 135 | |
Kuang-che Wu | 1e49f51 | 2018-12-06 15:27:42 +0800 | [diff] [blame] | 136 | def config(git_repo, *args): |
| 137 | """Wrapper of 'git config'. |
| 138 | |
| 139 | Args: |
| 140 | git_repo: path of git repo. |
| 141 | args: parameters pass to 'git config' |
| 142 | """ |
| 143 | util.check_call('git', 'config', *args, cwd=git_repo) |
| 144 | |
| 145 | |
| 146 | def fetch(git_repo, *args): |
Kuang-che Wu | 2b1286b | 2019-05-20 20:37:26 +0800 | [diff] [blame] | 147 | """Wrapper of 'git fetch' with retry support. |
Kuang-che Wu | 1e49f51 | 2018-12-06 15:27:42 +0800 | [diff] [blame] | 148 | |
| 149 | Args: |
| 150 | git_repo: path of git repo. |
| 151 | args: parameters pass to 'git fetch' |
| 152 | """ |
Kuang-che Wu | 2b1286b | 2019-05-20 20:37:26 +0800 | [diff] [blame] | 153 | for tries in range(5): |
| 154 | if tries > 0: |
| 155 | delay = min(60, 10 * 2**tries) |
| 156 | logger.warning('git fetch failed, will retry %s seconds later', delay) |
| 157 | time.sleep(delay) |
| 158 | |
| 159 | stderr_lines = [] |
| 160 | try: |
| 161 | util.check_call( |
| 162 | 'git', |
| 163 | 'fetch', |
| 164 | *args, |
| 165 | cwd=git_repo, |
| 166 | stderr_callback=stderr_lines.append) |
| 167 | break |
| 168 | except subprocess.CalledProcessError: |
| 169 | stderr = ''.join(stderr_lines) |
| 170 | # only retry 5xx internal server error |
| 171 | if 'The requested URL returned error: 5' not in stderr: |
| 172 | raise |
| 173 | else: |
| 174 | # Reached retry limit but haven't succeeded. |
| 175 | # In other words, there must be exceptions raised inside above loop. |
| 176 | logger.error('git fetch failed too much times') |
| 177 | # It's okay to raise because we are in the same scope as above loop. |
| 178 | # pylint: disable=misplaced-bare-raise |
| 179 | raise |
Kuang-che Wu | 1e49f51 | 2018-12-06 15:27:42 +0800 | [diff] [blame] | 180 | |
| 181 | |
Kuang-che Wu | e41e006 | 2017-09-01 19:04:14 +0800 | [diff] [blame] | 182 | def is_containing_commit(git_repo, rev): |
| 183 | """Determines given commit exists. |
| 184 | |
| 185 | Args: |
| 186 | git_repo: path of git repo. |
| 187 | rev: git commit revision in query. |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 188 | |
| 189 | Returns: |
| 190 | True if rev is inside given git repo. If git_repo is not a git folder, |
| 191 | returns False as well. |
Kuang-che Wu | e41e006 | 2017-09-01 19:04:14 +0800 | [diff] [blame] | 192 | """ |
| 193 | try: |
| 194 | return util.check_output( |
| 195 | 'git', 'cat-file', '-t', rev, cwd=git_repo) == 'commit\n' |
| 196 | except subprocess.CalledProcessError: |
| 197 | return False |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 198 | except OSError: |
| 199 | return False |
Kuang-che Wu | e41e006 | 2017-09-01 19:04:14 +0800 | [diff] [blame] | 200 | |
| 201 | |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 202 | def is_ancestor_commit(git_repo, old, new): |
| 203 | """Determines `old` commit is ancestor of `new` commit. |
| 204 | |
| 205 | Args: |
| 206 | git_repo: path of git repo. |
| 207 | old: the ancestor commit. |
| 208 | new: the descendant commit. |
| 209 | |
| 210 | Returns: |
| 211 | True only if `old` is the ancestor of `new`. One commit is not considered |
| 212 | as ancestor of itself. |
| 213 | """ |
| 214 | return util.check_output( |
| 215 | 'git', |
| 216 | 'rev-list', |
| 217 | '--ancestry-path', |
| 218 | '-1', |
| 219 | '%s..%s' % (old, new), |
| 220 | cwd=git_repo) != '' |
| 221 | |
| 222 | |
Kuang-che Wu | 8a28a9d | 2018-09-11 17:43:36 +0800 | [diff] [blame] | 223 | def get_commit_metadata(git_repo, rev): |
| 224 | """Get metadata of given commit. |
| 225 | |
| 226 | Args: |
| 227 | git_repo: path of git repo. |
| 228 | rev: git commit revision in query. |
| 229 | |
| 230 | Returns: |
| 231 | dict of metadata, including (if available): |
| 232 | tree: hash of git tree object |
| 233 | parent: list of parent commits; this field is unavailable for the very |
| 234 | first commit of git repo. |
| 235 | author: name and email of author |
| 236 | author_time: author timestamp (without timezone information) |
| 237 | committer: name and email of committer |
| 238 | committer_time: commit timestamp (without timezone information) |
| 239 | message: commit message text |
| 240 | """ |
| 241 | meta = {} |
| 242 | data = util.check_output( |
Kuang-che Wu | bcafc55 | 2019-08-15 15:27:02 +0800 | [diff] [blame] | 243 | 'git', 'cat-file', '-p', rev, cwd=git_repo, log_stdout=False) |
Kuang-che Wu | 8a28a9d | 2018-09-11 17:43:36 +0800 | [diff] [blame] | 244 | header, meta['message'] = data.split('\n\n', 1) |
| 245 | for line in header.splitlines(): |
| 246 | m = re.match(r'^tree (\w+)', line) |
| 247 | if m: |
| 248 | meta['tree'] = m.group(1) |
| 249 | continue |
| 250 | |
| 251 | m = re.match(r'^parent (\w+)', line) |
| 252 | if m: |
| 253 | meta['parent'] = line.split()[1:] |
| 254 | continue |
| 255 | |
| 256 | m = re.match(r'^(author|committer) (.*) (\d+) (\S+)$', line) |
| 257 | if m: |
| 258 | meta[m.group(1)] = m.group(2) |
| 259 | meta['%s_time' % m.group(1)] = int(m.group(3)) |
| 260 | continue |
| 261 | return meta |
| 262 | |
| 263 | |
Kuang-che Wu | e41e006 | 2017-09-01 19:04:14 +0800 | [diff] [blame] | 264 | def get_revlist(git_repo, old, new): |
| 265 | """Enumerates git commit between two revisions (inclusive). |
| 266 | |
| 267 | Args: |
| 268 | git_repo: path of git repo. |
| 269 | old: git commit revision. |
| 270 | new: git commit revision. |
| 271 | |
| 272 | Returns: |
| 273 | list of git revisions. The list contains the input revisions, old and new. |
| 274 | """ |
| 275 | assert old |
| 276 | assert new |
| 277 | cmd = ['git', 'rev-list', '--reverse', '%s^..%s' % (old, new)] |
| 278 | revlist = util.check_output(*cmd, cwd=git_repo).splitlines() |
| 279 | return revlist |
Kuang-che Wu | e2563ea | 2018-01-05 20:30:28 +0800 | [diff] [blame] | 280 | |
| 281 | |
| 282 | def get_commit_log(git_repo, rev): |
| 283 | """Get git commit log. |
| 284 | |
| 285 | Args: |
| 286 | git_repo: path of git repo. |
| 287 | rev: git commit revision. |
| 288 | |
| 289 | Returns: |
| 290 | commit log message |
| 291 | """ |
| 292 | cmd = ['git', 'log', '-1', '--format=%B', rev] |
| 293 | msg = util.check_output(*cmd, cwd=git_repo) |
| 294 | return msg |
| 295 | |
| 296 | |
Kuang-che Wu | 68db08a | 2018-03-30 11:50:34 +0800 | [diff] [blame] | 297 | def get_commit_hash(git_repo, rev): |
Kuang-che Wu | e2563ea | 2018-01-05 20:30:28 +0800 | [diff] [blame] | 298 | """Get git commit hash. |
| 299 | |
| 300 | Args: |
| 301 | git_repo: path of git repo. |
| 302 | rev: could be git tag, branch, or (shortened) commit hash |
| 303 | |
| 304 | Returns: |
| 305 | full git commit hash |
Kuang-che Wu | 5e7c9b0 | 2019-01-03 21:16:01 +0800 | [diff] [blame] | 306 | |
| 307 | Raises: |
| 308 | ValueError: `rev` is not unique or doesn't exist |
Kuang-che Wu | e2563ea | 2018-01-05 20:30:28 +0800 | [diff] [blame] | 309 | """ |
Kuang-che Wu | 5e7c9b0 | 2019-01-03 21:16:01 +0800 | [diff] [blame] | 310 | try: |
| 311 | # Use '^{commit}' to restrict search only commits. |
| 312 | # Use '--' to avoid ambiguity, like matching rev against path name. |
| 313 | output = util.check_output( |
| 314 | 'git', 'rev-parse', '%s^{commit}' % rev, '--', cwd=git_repo) |
| 315 | git_rev = output.rstrip('-\n') |
| 316 | except subprocess.CalledProcessError: |
| 317 | # Do not use 'git rev-parse --disambiguate' to determine uniqueness |
| 318 | # because it searches objects other than commits as well. |
| 319 | raise ValueError('%s is not unique or does not exist' % rev) |
| 320 | assert is_git_rev(git_rev) |
Kuang-che Wu | e2563ea | 2018-01-05 20:30:28 +0800 | [diff] [blame] | 321 | return git_rev |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 322 | |
| 323 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 324 | def get_commit_time(git_repo, rev, path): |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 325 | """Get git commit timestamp. |
| 326 | |
| 327 | Args: |
| 328 | git_repo: path of git repo |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 329 | rev: git commit id, branch name, tag name, or other git object |
| 330 | path: path, relative to git_repo |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 331 | |
| 332 | Returns: |
| 333 | timestamp (int) |
| 334 | """ |
| 335 | line = util.check_output( |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 336 | 'git', 'log', '-1', '--format=%ct', rev, '--', path, cwd=git_repo) |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 337 | return int(line) |
| 338 | |
| 339 | |
| 340 | def get_file_from_revision(git_repo, rev, path): |
| 341 | """Get file content of given revision. |
| 342 | |
| 343 | Args: |
| 344 | git_repo: path of git repo |
| 345 | rev: git commit id |
| 346 | path: file path |
| 347 | |
| 348 | Returns: |
| 349 | file content (str) |
| 350 | """ |
| 351 | return util.check_output( |
Kuang-che Wu | bcafc55 | 2019-08-15 15:27:02 +0800 | [diff] [blame] | 352 | 'git', 'show', '%s:%s' % (rev, path), cwd=git_repo, log_stdout=False) |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 353 | |
| 354 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 355 | def list_dir_from_revision(git_repo, rev, path): |
| 356 | """Lists entries of directory of given revision. |
| 357 | |
| 358 | Args: |
| 359 | git_repo: path of git repo |
| 360 | rev: git commit id |
| 361 | path: directory path, relative to git root |
| 362 | |
| 363 | Returns: |
| 364 | list of names |
| 365 | |
| 366 | Raises: |
| 367 | subprocess.CalledProcessError: if `path` doesn't exists in `rev` |
| 368 | """ |
| 369 | return util.check_output( |
| 370 | 'git', |
| 371 | 'ls-tree', |
| 372 | '--name-only', |
| 373 | '%s:%s' % (rev, path), |
| 374 | cwd=git_repo, |
Kuang-che Wu | bcafc55 | 2019-08-15 15:27:02 +0800 | [diff] [blame] | 375 | log_stdout=False).splitlines() |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 376 | |
| 377 | |
Kuang-che Wu | 8a28a9d | 2018-09-11 17:43:36 +0800 | [diff] [blame] | 378 | def get_rev_by_time(git_repo, timestamp, branch, path=None): |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 379 | """Query commit of given time. |
| 380 | |
| 381 | Args: |
| 382 | git_repo: path of git repo. |
| 383 | timestamp: timestamp |
Kuang-che Wu | 8a28a9d | 2018-09-11 17:43:36 +0800 | [diff] [blame] | 384 | branch: only query parent of the `branch`. If branch=None, it means 'HEAD' |
| 385 | (current branch, usually). |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 386 | path: only query history of path, relative to git_repo |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 387 | |
| 388 | Returns: |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 389 | git commit hash. None if path didn't exist at the given time. |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 390 | """ |
Kuang-che Wu | 8a28a9d | 2018-09-11 17:43:36 +0800 | [diff] [blame] | 391 | if not branch: |
| 392 | branch = 'HEAD' |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 393 | |
| 394 | cmd = [ |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 395 | 'git', |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 396 | 'rev-list', |
| 397 | '--first-parent', |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 398 | '-1', |
| 399 | '--before', |
| 400 | str(timestamp), |
Kuang-che Wu | 89ac2e7 | 2018-07-25 17:39:07 +0800 | [diff] [blame] | 401 | branch, |
| 402 | ] |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 403 | if path: |
| 404 | cmd += ['--', path] |
| 405 | |
| 406 | result = util.check_output(*cmd, cwd=git_repo).strip() |
| 407 | return result or None |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 408 | |
| 409 | |
Kuang-che Wu | 3d04eda | 2019-09-05 23:56:40 +0800 | [diff] [blame] | 410 | def reset_hard(git_repo): |
| 411 | """Restore modified and deleted files. |
| 412 | |
| 413 | This is simply wrapper of "git reset --hard". |
| 414 | |
| 415 | Args: |
| 416 | git_repo: path of git repo. |
| 417 | """ |
| 418 | util.check_call('git', 'reset', '--hard', cwd=git_repo) |
| 419 | |
| 420 | |
| 421 | def list_untracked(git_repo, excludes=None): |
| 422 | """List untracked files and directories. |
| 423 | |
| 424 | Args: |
| 425 | git_repo: path of git repo. |
| 426 | excludes: files and/or directories to ignore, relative to git_repo |
| 427 | |
| 428 | Returns: |
| 429 | list of paths, relative to git_repo |
| 430 | """ |
| 431 | exclude_flags = [] |
| 432 | if excludes: |
| 433 | for exclude in excludes: |
| 434 | assert not os.path.isabs(exclude), 'should be relative' |
| 435 | exclude_flags += ['--exclude', '/' + re.escape(exclude)] |
| 436 | |
| 437 | result = [] |
| 438 | for path in util.check_output( |
| 439 | 'git', |
| 440 | 'ls-files', |
| 441 | '--others', |
| 442 | '--exclude-standard', |
| 443 | *exclude_flags, |
| 444 | cwd=git_repo).splitlines(): |
| 445 | # Remove the trailing slash, which means directory. |
| 446 | path = path.rstrip('/') |
| 447 | result.append(path) |
| 448 | return result |
| 449 | |
| 450 | |
| 451 | def distclean(git_repo, excludes=None): |
| 452 | """Clean up git repo directory. |
| 453 | |
| 454 | Restore modified and deleted files. Delete untracked files. |
| 455 | |
| 456 | Args: |
| 457 | git_repo: path of git repo. |
| 458 | excludes: files and/or directories to ignore, relative to git_repo |
| 459 | """ |
| 460 | reset_hard(git_repo) |
| 461 | |
| 462 | # Delete untracked files. |
| 463 | for untracked in list_untracked(git_repo, excludes=excludes): |
| 464 | path = os.path.join(git_repo, untracked) |
| 465 | logger.debug('delete untracked: %s', path) |
| 466 | if os.path.isdir(path): |
| 467 | shutil.rmtree(path) |
| 468 | else: |
| 469 | os.unlink(path) |
| 470 | |
| 471 | |
Kuang-che Wu | 8a28a9d | 2018-09-11 17:43:36 +0800 | [diff] [blame] | 472 | def get_history(git_repo, |
| 473 | path, |
| 474 | branch=None, |
| 475 | after=None, |
| 476 | before=None, |
| 477 | padding=False): |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 478 | """Get commit history of given path. |
| 479 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 480 | `after` and `before` could be outside of lifetime of `path`. `padding` is |
| 481 | used to control what to return for such cases. |
| 482 | |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 483 | Args: |
| 484 | git_repo: path of git repo. |
| 485 | path: path to query, relative to git_repo |
Kuang-che Wu | 8a28a9d | 2018-09-11 17:43:36 +0800 | [diff] [blame] | 486 | branch: branch name or ref name |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 487 | after: limit history after given time (inclusive) |
| 488 | before: limit history before given time (inclusive) |
| 489 | padding: If True, pads returned result with dummy record at exact 'after' |
| 490 | and 'before' time, if 'path' existed at that time. Otherwise, only |
| 491 | returns real commits. |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 492 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 493 | Returns: |
| 494 | List of (timestamp, git hash); They are all events when `path` was added, |
| 495 | removed, modified, and start and end time if `padding` is true. |
| 496 | |
| 497 | For each pair, at `timestamp`, the repo state is `git hash`. In other |
| 498 | words, `timestamp` is not necessary the commit time of `git hash` for the |
| 499 | padded entries. |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 500 | """ |
Kuang-che Wu | d1d45b4 | 2018-07-05 00:46:45 +0800 | [diff] [blame] | 501 | cmd = ['git', 'log', '--reverse', '--first-parent', '--format=%ct %H'] |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 502 | if after: |
| 503 | cmd += ['--after', str(after)] |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 504 | if before: |
| 505 | cmd += ['--before', str(before)] |
Kuang-che Wu | 8a28a9d | 2018-09-11 17:43:36 +0800 | [diff] [blame] | 506 | if branch: |
| 507 | assert not is_git_rev(branch) |
| 508 | cmd += [branch] |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 509 | # '--' is necessary otherwise if `path` is removed in current revision, git |
| 510 | # will complain it's an ambiguous argument which may be path or something |
| 511 | # else (like git branch name, tag name, etc.) |
| 512 | cmd += ['--', path] |
| 513 | |
| 514 | result = [] |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 515 | for line in util.check_output(*cmd, cwd=git_repo).splitlines(): |
| 516 | commit_time, git_rev = line.split() |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 517 | result.append((int(commit_time), git_rev)) |
| 518 | |
| 519 | if padding: |
Kuang-che Wu | ae6824b | 2019-08-27 22:20:01 +0800 | [diff] [blame] | 520 | assert before or after, 'padding=True make no sense if they are both None' |
Kuang-che Wu | 8a28a9d | 2018-09-11 17:43:36 +0800 | [diff] [blame] | 521 | if before is not None and get_rev_by_time( |
| 522 | git_repo, before, branch, path=path): |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 523 | before = int(before) |
| 524 | if not result or result[-1][0] != before: |
Kuang-che Wu | 8a28a9d | 2018-09-11 17:43:36 +0800 | [diff] [blame] | 525 | git_rev = get_rev_by_time(git_repo, before, branch) |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 526 | assert git_rev |
| 527 | result.append((before, git_rev)) |
Kuang-che Wu | 8a28a9d | 2018-09-11 17:43:36 +0800 | [diff] [blame] | 528 | if after is not None and get_rev_by_time( |
| 529 | git_repo, after, branch, path=path): |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 530 | after = int(after) |
| 531 | if not result or result[0][0] != after: |
Kuang-che Wu | 8a28a9d | 2018-09-11 17:43:36 +0800 | [diff] [blame] | 532 | git_rev = get_rev_by_time(git_repo, after, branch) |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 533 | assert git_rev |
| 534 | result.insert(0, (after, git_rev)) |
| 535 | |
| 536 | return result |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 537 | |
| 538 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 539 | def get_history_recursively(git_repo, path, after, before, parser_callback): |
| 540 | """Get commit history of given path and its dependencies. |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 541 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 542 | In comparison to get_history(), get_history_recursively also takes |
| 543 | dependencies into consideration. For example, if file A referenced file B, |
| 544 | get_history_recursively(A) will return commits of B in addition to A. This |
| 545 | applies recursively, so commits of C will be included if file B referenced |
| 546 | file C, and so on. |
| 547 | |
| 548 | This function is file type neutral. `parser_callback(filename, content)` will |
| 549 | be invoked to parse file content and should return list of filename of |
| 550 | dependencies. |
| 551 | |
| 552 | Args: |
| 553 | git_repo: path of git repo |
| 554 | path: path to query, relative to git_repo |
| 555 | after: limit history after given time (inclusive) |
| 556 | before: limit history before given time (inclusive) |
| 557 | parser_callback: callback to parse file content. See above comment. |
| 558 | |
| 559 | Returns: |
| 560 | list of (commit timestamp, git hash) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 561 | """ |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 562 | history = get_history( |
| 563 | git_repo, path, after=after, before=before, padding=True) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 564 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 565 | # Collect include information of each commit. |
| 566 | includes = {} |
| 567 | for commit_time, git_rev in history: |
| 568 | content = get_file_from_revision(git_repo, git_rev, path) |
| 569 | for include_name in parser_callback(path, content): |
| 570 | if include_name not in includes: |
| 571 | includes[include_name] = set() |
| 572 | includes[include_name].add(git_rev) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 573 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 574 | # Analyze the start time and end time of each include. |
| 575 | dependencies = [] |
| 576 | for include in includes: |
| 577 | appeared = None |
| 578 | for commit_time, git_rev in history: |
| 579 | if git_rev in includes[include]: |
| 580 | if not appeared: |
| 581 | appeared = commit_time |
| 582 | else: |
| 583 | if appeared: |
| 584 | dependencies.append((include, appeared, commit_time)) |
| 585 | appeared = None |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 586 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 587 | if appeared is not None: |
| 588 | dependencies.append((include, appeared, before)) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 589 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 590 | # Recursion and merge. |
| 591 | result = list(history) |
| 592 | for include, appeared, disappeared in dependencies: |
| 593 | result += get_history_recursively(git_repo, include, appeared, disappeared, |
| 594 | parser_callback) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 595 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 596 | # Sort and dedup. |
| 597 | result2 = [] |
Kuang-che Wu | ebb023c | 2018-11-29 15:49:32 +0800 | [diff] [blame] | 598 | for x in sorted(result, key=lambda x: x[0]): |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 599 | if result2 and result2[-1] == x: |
| 600 | continue |
| 601 | result2.append(x) |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 602 | |
Kuang-che Wu | e4bae0b | 2018-07-19 12:10:14 +0800 | [diff] [blame] | 603 | return result2 |
Kuang-che Wu | bfc4a64 | 2018-04-19 11:54:08 +0800 | [diff] [blame] | 604 | |
| 605 | |
Kuang-che Wu | 3eb6b50 | 2018-06-06 16:15:18 +0800 | [diff] [blame] | 606 | def list_commits_between_commits(git_repo, old, new): |
| 607 | """Get all commits between (old, new]. |
| 608 | |
| 609 | Args: |
| 610 | git_repo: path of git repo. |
| 611 | old: old commit hash (exclusive) |
| 612 | new: new commit hash (inclusive) |
| 613 | |
| 614 | Returns: |
| 615 | list of (timestamp, rev) |
| 616 | """ |
| 617 | assert old and new |
| 618 | assert old == new or is_ancestor_commit(git_repo, old, new) |
| 619 | commits = [] |
| 620 | # --first-parent is necessary for Android, see following link for more |
| 621 | # discussion. |
| 622 | # https://docs.google.com/document/d/1c8qiq14_ObRRjLT62sk9r5V5cyCGHX66dLYab4MVnks/edit#heading=h.n3i6mt2n6xuu |
| 623 | for line in util.check_output( |
| 624 | 'git', |
| 625 | 'rev-list', |
| 626 | '--timestamp', |
| 627 | '--reverse', |
| 628 | '--first-parent', |
| 629 | '%s..%s' % (old, new), |
| 630 | cwd=git_repo).splitlines(): |
| 631 | timestamp, git_rev = line.split() |
| 632 | commits.append([int(timestamp), git_rev]) |
| 633 | |
| 634 | # bisect-kit has a fundamental assumption that commit timestamps are |
| 635 | # increasing because we sort and bisect the commits by timestamp across git |
| 636 | # repos. If not increasing, we have to adjust the timestamp as workaround. |
| 637 | # This might lead to bad bisect result, however the bad probability is low in |
| 638 | # practice since most machines' clocks are good enough. |
| 639 | if commits != sorted(commits, key=lambda x: x[0]): |
| 640 | logger.warning('Commit timestamps are not increasing') |
| 641 | last_timestamp = -1 |
| 642 | adjusted = 0 |
| 643 | for commit in commits: |
| 644 | if commit[0] < last_timestamp: |
| 645 | commit[0] = last_timestamp |
| 646 | adjusted += 1 |
| 647 | |
| 648 | last_timestamp = commit[0] |
| 649 | logger.warning('%d timestamps adjusted', adjusted) |
| 650 | |
| 651 | return commits |