|
| 1 | +# Copyright (c) 2014 The Chromium Embedded Framework Authors. All rights |
| 2 | +# reserved. Use of this source code is governed by a BSD-style license that |
| 3 | +# can be found in the LICENSE file |
| 4 | + |
| 5 | +from exec_util import exec_cmd |
| 6 | +import os |
| 7 | +import sys |
| 8 | + |
| 9 | +if sys.platform == 'win32': |
| 10 | + # Force use of the git version bundled with depot_tools. |
| 11 | + git_exe = 'git.bat' |
| 12 | +else: |
| 13 | + git_exe = 'git' |
| 14 | + |
| 15 | + |
| 16 | +def is_checkout(path): |
| 17 | + """ Returns true if the path represents a git checkout. """ |
| 18 | + return os.path.isdir(os.path.join(path, '.git')) |
| 19 | + |
| 20 | + |
| 21 | +def get_hash(path='.', branch='HEAD'): |
| 22 | + """ Returns the git hash for the specified branch/tag/hash. """ |
| 23 | + cmd = "%s rev-parse %s" % (git_exe, branch) |
| 24 | + result = exec_cmd(cmd, path) |
| 25 | + if result['out'] != '': |
| 26 | + return result['out'].strip() |
| 27 | + return 'Unknown' |
| 28 | + |
| 29 | + |
| 30 | +def get_url(path='.'): |
| 31 | + """ Returns the origin url for the specified path. """ |
| 32 | + cmd = "%s config --get remote.origin.url" % git_exe |
| 33 | + result = exec_cmd(cmd, path) |
| 34 | + if result['out'] != '': |
| 35 | + return result['out'].strip() |
| 36 | + return 'Unknown' |
| 37 | + |
| 38 | + |
| 39 | +def get_commit_number(path='.', branch='HEAD'): |
| 40 | + """ Returns the number of commits in the specified branch/tag/hash. """ |
| 41 | + cmd = "%s rev-list --count %s" % (git_exe, branch) |
| 42 | + result = exec_cmd(cmd, path) |
| 43 | + if result['out'] != '': |
| 44 | + return result['out'].strip() |
| 45 | + return '0' |
| 46 | + |
| 47 | + |
| 48 | +def get_changed_files(path, hash): |
| 49 | + """ Retrieves the list of changed files. """ |
| 50 | + if hash == 'unstaged': |
| 51 | + cmd = "%s diff --name-only" % git_exe |
| 52 | + elif hash == 'staged': |
| 53 | + cmd = "%s diff --name-only --cached" % git_exe |
| 54 | + else: |
| 55 | + cmd = "%s diff-tree --no-commit-id --name-only -r %s" % (git_exe, hash) |
| 56 | + result = exec_cmd(cmd, path) |
| 57 | + if result['out'] != '': |
| 58 | + files = result['out'] |
| 59 | + if sys.platform == 'win32': |
| 60 | + # Convert to Unix line endings. |
| 61 | + files = files.replace('\r\n', '\n') |
| 62 | + return files.strip().split("\n") |
| 63 | + return [] |
| 64 | + |
| 65 | + |
| 66 | +def write_indented_output(output): |
| 67 | + """ Apply a fixed amount of intent to lines before printing. """ |
| 68 | + if output == '': |
| 69 | + return |
| 70 | + for line in output.split('\n'): |
| 71 | + line = line.strip() |
| 72 | + if len(line) == 0: |
| 73 | + continue |
| 74 | + sys.stdout.write('\t%s\n' % line) |
| 75 | + |
| 76 | + |
| 77 | +def git_apply_patch_file(patch_path, patch_dir): |
| 78 | + """ Apply |patch_path| to files in |patch_dir|. """ |
| 79 | + patch_name = os.path.basename(patch_path) |
| 80 | + sys.stdout.write('\nApply %s in %s\n' % (patch_name, patch_dir)) |
| 81 | + |
| 82 | + if not os.path.isfile(patch_path): |
| 83 | + sys.stdout.write('... patch file does not exist.\n') |
| 84 | + return 'fail' |
| 85 | + |
| 86 | + patch_string = open(patch_path, 'rb').read() |
| 87 | + if sys.platform == 'win32': |
| 88 | + # Convert the patch to Unix line endings. This is necessary to avoid |
| 89 | + # whitespace errors with git apply. |
| 90 | + patch_string = patch_string.replace('\r\n', '\n') |
| 91 | + |
| 92 | + # Git apply fails silently if not run relative to a respository root. |
| 93 | + if not is_checkout(patch_dir): |
| 94 | + sys.stdout.write('... patch directory is not a repository root.\n') |
| 95 | + return 'fail' |
| 96 | + |
| 97 | + # Output patch contents. |
| 98 | + cmd = '%s apply -p0 --numstat' % git_exe |
| 99 | + result = exec_cmd(cmd, patch_dir, patch_string) |
| 100 | + write_indented_output(result['out'].replace('<stdin>', patch_name)) |
| 101 | + |
| 102 | + # Reverse check to see if the patch has already been applied. |
| 103 | + cmd = '%s apply -p0 --reverse --check' % git_exe |
| 104 | + result = exec_cmd(cmd, patch_dir, patch_string) |
| 105 | + if result['err'].find('error:') < 0: |
| 106 | + sys.stdout.write('... already applied (skipping).\n') |
| 107 | + return 'skip' |
| 108 | + |
| 109 | + # Normal check to see if the patch can be applied cleanly. |
| 110 | + cmd = '%s apply -p0 --check' % git_exe |
| 111 | + result = exec_cmd(cmd, patch_dir, patch_string) |
| 112 | + if result['err'].find('error:') >= 0: |
| 113 | + sys.stdout.write('... failed to apply:\n') |
| 114 | + write_indented_output(result['err'].replace('<stdin>', patch_name)) |
| 115 | + return 'fail' |
| 116 | + |
| 117 | + # Apply the patch file. This should always succeed because the previous |
| 118 | + # command succeeded. |
| 119 | + cmd = '%s apply -p0' % git_exe |
| 120 | + result = exec_cmd(cmd, patch_dir, patch_string) |
| 121 | + if result['err'] == '': |
| 122 | + sys.stdout.write('... successfully applied.\n') |
| 123 | + else: |
| 124 | + sys.stdout.write('... successfully applied (with warnings):\n') |
| 125 | + write_indented_output(result['err'].replace('<stdin>', patch_name)) |
| 126 | + return 'apply' |
0 commit comments