1#!/usr/bin/env python3
2
3# Copyright (C) 2020 Free Software Foundation, Inc.
4#
5# This file is part of GCC.
6#
7# GCC is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 3, or (at your option)
10# any later version.
11#
12# GCC is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with GCC; see the file COPYING.  If not, write to
19# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20# Boston, MA 02110-1301, USA.
21
22import argparse
23import subprocess
24
25if __name__ == '__main__':
26    parser = argparse.ArgumentParser(description='Backport a git revision and '
27                                     'stash all ChangeLog files.')
28    parser.add_argument('revision', help='Revision')
29    args = parser.parse_args()
30
31    r = subprocess.run('git cherry-pick -x %s' % args.revision, shell=True)
32    if r.returncode == 0:
33        cmd = 'git show --name-only --pretty="" -- "*ChangeLog"'
34        changelogs = subprocess.check_output(cmd, shell=True, encoding='utf8')
35        changelogs = changelogs.strip()
36        if changelogs:
37            for changelog in changelogs.split('\n'):
38                subprocess.check_output('git checkout HEAD~ %s' % changelog,
39                                        shell=True)
40        subprocess.check_output('git commit --amend --no-edit', shell=True)
41    else:
42        # 1) remove all ChangeLog files from conflicts
43        out = subprocess.check_output('git diff --name-only --diff-filter=U',
44                                      shell=True,
45                                      encoding='utf8')
46        conflicts = out.strip().split('\n')
47        changelogs = [c for c in conflicts if c.endswith('ChangeLog')]
48        if changelogs:
49            cmd = 'git checkout --theirs %s' % ' '.join(changelogs)
50            subprocess.check_output(cmd, shell=True)
51        # 2) remove all ChangeLog files from index
52        cmd = 'git diff --name-only --diff-filter=M HEAD'
53        out = subprocess.check_output(cmd, shell=True, encoding='utf8')
54        out = out.strip().split('\n')
55        modified = [c for c in out if c.endswith('ChangeLog')]
56        for m in modified:
57            subprocess.check_output('git reset %s' % m, shell=True)
58            subprocess.check_output('git checkout %s' % m, shell=True)
59
60        # try to continue
61        if len(conflicts) == len(changelogs):
62            cmd = 'git -c core.editor=true cherry-pick --continue'
63            subprocess.check_output(cmd, shell=True)
64        else:
65            print('Please resolve all remaining file conflicts.')
66