mirror of
https://bitbucket.org/chromiumembedded/cef
synced 2025-06-05 21:39:12 +02:00
Use git apply for applying patch files (issue #1825)
This commit is contained in:
@ -4,14 +4,21 @@
|
||||
|
||||
from exec_util import exec_cmd
|
||||
import os
|
||||
import sys
|
||||
|
||||
if sys.platform == 'win32':
|
||||
# Force use of the git version bundled with depot_tools.
|
||||
git_exe = 'git.bat'
|
||||
else:
|
||||
git_exe = 'git'
|
||||
|
||||
def is_checkout(path):
|
||||
""" Returns true if the path represents a git checkout. """
|
||||
return os.path.exists(os.path.join(path, '.git'))
|
||||
return os.path.isdir(os.path.join(path, '.git'))
|
||||
|
||||
def get_hash(path = '.', branch = 'HEAD'):
|
||||
""" Returns the git hash for the specified branch/tag/hash. """
|
||||
cmd = "git rev-parse %s" % branch
|
||||
cmd = "%s rev-parse %s" % (git_exe, branch)
|
||||
result = exec_cmd(cmd, path)
|
||||
if result['out'] != '':
|
||||
return result['out'].strip()
|
||||
@ -19,7 +26,7 @@ def get_hash(path = '.', branch = 'HEAD'):
|
||||
|
||||
def get_url(path = '.'):
|
||||
""" Returns the origin url for the specified path. """
|
||||
cmd = "git config --get remote.origin.url"
|
||||
cmd = "%s config --get remote.origin.url" % git_exe
|
||||
result = exec_cmd(cmd, path)
|
||||
if result['out'] != '':
|
||||
return result['out'].strip()
|
||||
@ -27,7 +34,7 @@ def get_url(path = '.'):
|
||||
|
||||
def get_commit_number(path = '.', branch = 'HEAD'):
|
||||
""" Returns the number of commits in the specified branch/tag/hash. """
|
||||
cmd = "git rev-list --count %s" % branch
|
||||
cmd = "%s rev-list --count %s" % (git_exe, branch)
|
||||
result = exec_cmd(cmd, path)
|
||||
if result['out'] != '':
|
||||
return result['out'].strip()
|
||||
@ -37,3 +44,64 @@ def get_changed_files(path = '.'):
|
||||
""" Retrieves the list of changed files. """
|
||||
# not implemented
|
||||
return []
|
||||
|
||||
def write_indented_output(output):
|
||||
""" Apply a fixed amount of intent to lines before printing. """
|
||||
if output == '':
|
||||
return
|
||||
for line in output.split('\n'):
|
||||
line = line.strip()
|
||||
if len(line) == 0:
|
||||
continue
|
||||
sys.stdout.write('\t%s\n' % line)
|
||||
|
||||
def git_apply_patch_file(patch_path, patch_dir):
|
||||
""" Apply |patch_path| to files in |patch_dir|. """
|
||||
patch_name = os.path.basename(patch_path)
|
||||
sys.stdout.write('\nApply %s in %s\n' % (patch_name, patch_dir))
|
||||
|
||||
if not os.path.isfile(patch_path):
|
||||
sys.stdout.write('... patch file does not exist.\n')
|
||||
return 'fail'
|
||||
|
||||
patch_string = open(patch_path, 'rb').read()
|
||||
if sys.platform == 'win32':
|
||||
# Convert the patch to Unix line endings. This is necessary to avoid
|
||||
# whitespace errors with git apply.
|
||||
patch_string = patch_string.replace('\r\n', '\n')
|
||||
|
||||
# Git apply fails silently if not run relative to a respository root.
|
||||
if not is_checkout(patch_dir):
|
||||
sys.stdout.write('... patch directory is not a repository root.\n')
|
||||
return 'fail'
|
||||
|
||||
# Output patch contents.
|
||||
cmd = '%s apply -p0 --numstat' % git_exe
|
||||
result = exec_cmd(cmd, patch_dir, patch_string)
|
||||
write_indented_output(result['out'].replace('<stdin>', patch_name))
|
||||
|
||||
# Reverse check to see if the patch has already been applied.
|
||||
cmd = '%s apply -p0 --reverse --check' % git_exe
|
||||
result = exec_cmd(cmd, patch_dir, patch_string)
|
||||
if result['err'].find('error:') < 0:
|
||||
sys.stdout.write('... already applied (skipping).\n')
|
||||
return 'skip'
|
||||
|
||||
# Normal check to see if the patch can be applied cleanly.
|
||||
cmd = '%s apply -p0 --check' % git_exe
|
||||
result = exec_cmd(cmd, patch_dir, patch_string)
|
||||
if result['err'].find('error:') >= 0:
|
||||
sys.stdout.write('... failed to apply:\n')
|
||||
write_indented_output(result['err'].replace('<stdin>', patch_name))
|
||||
return 'fail'
|
||||
|
||||
# Apply the patch file. This should always succeed because the previous
|
||||
# command succeeded.
|
||||
cmd = '%s apply -p0' % git_exe
|
||||
result = exec_cmd(cmd, patch_dir, patch_string)
|
||||
if result['err'] == '':
|
||||
sys.stdout.write('... successfully applied.\n')
|
||||
else:
|
||||
sys.stdout.write('... successfully applied (with warnings):\n')
|
||||
write_indented_output(result['err'].replace('<stdin>', patch_name))
|
||||
return 'apply'
|
||||
|
Reference in New Issue
Block a user