1*635a8641SAndroid Build Coastguard Worker#!/usr/bin/env python3 2*635a8641SAndroid Build Coastguard Worker# Copyright 2020 The Chromium OS Authors. All rights reserved. 3*635a8641SAndroid Build Coastguard Worker# Use of this source code is governed by a BSD-style license that can be 4*635a8641SAndroid Build Coastguard Worker# found in the LICENSE file. 5*635a8641SAndroid Build Coastguard Worker 6*635a8641SAndroid Build Coastguard Worker""" 7*635a8641SAndroid Build Coastguard WorkerUtility to apply diffs between given two upstream commit hashes to current 8*635a8641SAndroid Build Coastguard Workerworkspace. 9*635a8641SAndroid Build Coastguard Worker 10*635a8641SAndroid Build Coastguard WorkerThis utility diffs files between old_commit and new_commit, with hard-coded 11*635a8641SAndroid Build Coastguard Workerfilter rules, and apply the diff to current HEAD with 3-way-merge supported. 12*635a8641SAndroid Build Coastguard Worker 13*635a8641SAndroid Build Coastguard WorkerThis can be used to uprev a libchrome directory when this is not git history for 14*635a8641SAndroid Build Coastguard Workergit merge to work. 15*635a8641SAndroid Build Coastguard Worker""" 16*635a8641SAndroid Build Coastguard Worker 17*635a8641SAndroid Build Coastguard Workerimport argparse 18*635a8641SAndroid Build Coastguard Workerimport subprocess 19*635a8641SAndroid Build Coastguard Workerimport sys 20*635a8641SAndroid Build Coastguard Worker 21*635a8641SAndroid Build Coastguard Workerimport filters 22*635a8641SAndroid Build Coastguard Workerimport utils 23*635a8641SAndroid Build Coastguard Worker 24*635a8641SAndroid Build Coastguard Workerdef main(): 25*635a8641SAndroid Build Coastguard Worker # Init args 26*635a8641SAndroid Build Coastguard Worker parser = argparse.ArgumentParser( 27*635a8641SAndroid Build Coastguard Worker description='Copy file from given commits') 28*635a8641SAndroid Build Coastguard Worker parser.add_argument( 29*635a8641SAndroid Build Coastguard Worker 'old_commit', metavar='old_commit', type=str, nargs=1, 30*635a8641SAndroid Build Coastguard Worker help='commit hash in upstream branch or browser repository ' 31*635a8641SAndroid Build Coastguard Worker 'we want to uprev from') 32*635a8641SAndroid Build Coastguard Worker parser.add_argument( 33*635a8641SAndroid Build Coastguard Worker 'new_commit', metavar='new_commit', type=str, nargs=1, 34*635a8641SAndroid Build Coastguard Worker help='commit hash in upstream branch or browser repository ' 35*635a8641SAndroid Build Coastguard Worker 'we want ot uprev to') 36*635a8641SAndroid Build Coastguard Worker parser.add_argument( 37*635a8641SAndroid Build Coastguard Worker '--dry_run', dest='dry_run', action='store_const', const=True, default=False) 38*635a8641SAndroid Build Coastguard Worker parser.add_argument( 39*635a8641SAndroid Build Coastguard Worker '--is_browser', dest='is_browser', action='store_const', const=True, default=False, 40*635a8641SAndroid Build Coastguard Worker help='is the commit hash in browser repository') 41*635a8641SAndroid Build Coastguard Worker arg = parser.parse_args(sys.argv[1:]) 42*635a8641SAndroid Build Coastguard Worker 43*635a8641SAndroid Build Coastguard Worker # Get old and new files. 44*635a8641SAndroid Build Coastguard Worker old_files = utils.get_file_list(arg.old_commit[0]) 45*635a8641SAndroid Build Coastguard Worker new_files = utils.get_file_list(arg.new_commit[0]) 46*635a8641SAndroid Build Coastguard Worker 47*635a8641SAndroid Build Coastguard Worker if arg.is_browser: 48*635a8641SAndroid Build Coastguard Worker old_files = filters.filter_file([], old_files) 49*635a8641SAndroid Build Coastguard Worker new_files = filters.filter_file([], new_files) 50*635a8641SAndroid Build Coastguard Worker assert filters.filter_file(old_files, []) == [] 51*635a8641SAndroid Build Coastguard Worker assert filters.filter_file(new_files, []) == [] 52*635a8641SAndroid Build Coastguard Worker 53*635a8641SAndroid Build Coastguard Worker # Generate a tree object for new files. 54*635a8641SAndroid Build Coastguard Worker old_tree = utils.git_mktree(old_files) 55*635a8641SAndroid Build Coastguard Worker new_tree = utils.git_mktree(new_files) 56*635a8641SAndroid Build Coastguard Worker newroot = utils.git_commit(old_tree, []) 57*635a8641SAndroid Build Coastguard Worker squashed = utils.git_commit(new_tree, [newroot]) 58*635a8641SAndroid Build Coastguard Worker 59*635a8641SAndroid Build Coastguard Worker # Generate patch for git am 60*635a8641SAndroid Build Coastguard Worker patch = subprocess.check_output(['git', 'format-patch', '--stdout', newroot+b'..'+squashed]) 61*635a8641SAndroid Build Coastguard Worker 62*635a8641SAndroid Build Coastguard Worker if arg.dry_run: 63*635a8641SAndroid Build Coastguard Worker print(patch.decode('utf-8')) 64*635a8641SAndroid Build Coastguard Worker else: 65*635a8641SAndroid Build Coastguard Worker subprocess.run(['git', 'am', '-3'], input=patch) 66*635a8641SAndroid Build Coastguard Worker 67*635a8641SAndroid Build Coastguard Worker 68*635a8641SAndroid Build Coastguard Workerif __name__ == '__main__': 69*635a8641SAndroid Build Coastguard Worker main() 70