1*a448f87cSmrg#!/usr/bin/env python3 2*a448f87cSmrg 3*a448f87cSmrg# Copyright (C) 2020 Free Software Foundation, Inc. 4*a448f87cSmrg# 5*a448f87cSmrg# This file is part of GCC. 6*a448f87cSmrg# 7*a448f87cSmrg# GCC is free software; you can redistribute it and/or modify 8*a448f87cSmrg# it under the terms of the GNU General Public License as published by 9*a448f87cSmrg# the Free Software Foundation; either version 3, or (at your option) 10*a448f87cSmrg# any later version. 11*a448f87cSmrg# 12*a448f87cSmrg# GCC is distributed in the hope that it will be useful, 13*a448f87cSmrg# but WITHOUT ANY WARRANTY; without even the implied warranty of 14*a448f87cSmrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15*a448f87cSmrg# GNU General Public License for more details. 16*a448f87cSmrg# 17*a448f87cSmrg# You should have received a copy of the GNU General Public License 18*a448f87cSmrg# along with GCC; see the file COPYING. If not, write to 19*a448f87cSmrg# the Free Software Foundation, 51 Franklin Street, Fifth Floor, 20*a448f87cSmrg# Boston, MA 02110-1301, USA. 21*a448f87cSmrg# 22*a448f87cSmrg# The script tries to fix commit message where ChangeLog entries 23*a448f87cSmrg# can point to .cc renamed files. 24*a448f87cSmrg 25*a448f87cSmrgimport argparse 26*a448f87cSmrgimport os 27*a448f87cSmrgimport subprocess 28*a448f87cSmrgimport tempfile 29*a448f87cSmrg 30*a448f87cSmrgDESCRIPTION = 'Fix up ChangeLog of the current commit.' 31*a448f87cSmrg 32*a448f87cSmrgscript_folder = os.path.dirname(os.path.abspath(__file__)) 33*a448f87cSmrgverify_script = os.path.join(script_folder, 34*a448f87cSmrg 'gcc-changelog/git_check_commit.py') 35*a448f87cSmrg 36*a448f87cSmrg 37*a448f87cSmrgdef replace_file_in_changelog(lines, filename, fixed): 38*a448f87cSmrg # consider all componenets of a path: gcc/ipa-icf.cc 39*a448f87cSmrg while filename: 40*a448f87cSmrg for i, line in enumerate(lines): 41*a448f87cSmrg if filename in line: 42*a448f87cSmrg lines[i] = line.replace(filename, fixed) 43*a448f87cSmrg return 44*a448f87cSmrg 45*a448f87cSmrg parts = filename.split('/') 46*a448f87cSmrg if len(parts) == 1: 47*a448f87cSmrg return 48*a448f87cSmrg filename = '/'.join(parts[1:]) 49*a448f87cSmrg fixed = '/'.join(fixed.split('/')[1:]) 50*a448f87cSmrg 51*a448f87cSmrg 52*a448f87cSmrgif __name__ == '__main__': 53*a448f87cSmrg parser = argparse.ArgumentParser(description=DESCRIPTION) 54*a448f87cSmrg args = parser.parse_args() 55*a448f87cSmrg 56*a448f87cSmrg # Update commit message if change for a .cc file was taken 57*a448f87cSmrg r = subprocess.run(f'{verify_script} HEAD', shell=True, encoding='utf8', 58*a448f87cSmrg stdout=subprocess.PIPE, stderr=subprocess.PIPE) 59*a448f87cSmrg if r.returncode != 0: 60*a448f87cSmrg lines = r.stdout.splitlines() 61*a448f87cSmrg cmd = 'git show -s --format=%B' 62*a448f87cSmrg commit_message = subprocess.check_output(cmd, shell=True, 63*a448f87cSmrg encoding='utf8').strip() 64*a448f87cSmrg commit_message = commit_message.splitlines() 65*a448f87cSmrg 66*a448f87cSmrg # Parse the following lines: 67*a448f87cSmrg # ERR: unchanged file mentioned in a ChangeLog \ 68*a448f87cSmrg # (did you mean "gcc/ipa-icf.cc"?): "gcc/ipa-icf.c" 69*a448f87cSmrg replaced = 0 70*a448f87cSmrg for line in lines: 71*a448f87cSmrg if ('unchanged file mentioned' in line and 72*a448f87cSmrg 'did you mean' in line): 73*a448f87cSmrg filename = line.split()[-1].strip('"') 74*a448f87cSmrg fixed = line[line.index('did you mean'):] 75*a448f87cSmrg fixed = fixed[fixed.index('"') + 1:] 76*a448f87cSmrg fixed = fixed[:fixed.index('"')] 77*a448f87cSmrg 78*a448f87cSmrg if filename.count('/') == fixed.count('/'): 79*a448f87cSmrg replace_file_in_changelog(commit_message, filename, fixed) 80*a448f87cSmrg replaced += 1 81*a448f87cSmrg 82*a448f87cSmrg if replaced: 83*a448f87cSmrg with tempfile.NamedTemporaryFile('w', encoding='utf8', 84*a448f87cSmrg delete=False) as w: 85*a448f87cSmrg w.write('\n'.join(commit_message)) 86*a448f87cSmrg w.close() 87*a448f87cSmrg subprocess.check_output(f'git commit --amend -F {w.name}', 88*a448f87cSmrg shell=True, encoding='utf8') 89*a448f87cSmrg os.unlink(w.name) 90*a448f87cSmrg print(f'Commit message updated: {replaced} file(s) renamed.') 91*a448f87cSmrg else: 92*a448f87cSmrg print('Commit message has not been updated.') 93