| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# Copyright (C) 2020-2025 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 it under |
| 8 |
# the terms of the GNU General Public License as published by the Free |
| 9 |
# Software Foundation; either version 3, or (at your option) any later |
| 10 |
# version. |
| 11 |
# |
| 12 |
# GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 |
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 |
# for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with GCC; see the file COPYING3. If not see |
| 19 |
# <http://www.gnu.org/licenses/>. |
| 20 |
|
| 21 |
import argparse |
| 22 |
import datetime |
| 23 |
import logging |
| 24 |
import os |
| 25 |
import re |
| 26 |
|
| 27 |
from git import Repo |
| 28 |
|
| 29 |
from git_repository import parse_git_revisions |
| 30 |
|
| 31 |
current_timestamp = datetime.datetime.now().strftime('%Y%m%d\n') |
| 32 |
|
| 33 |
# Skip the following commits, they cannot be correctly processed |
| 34 |
ignored_commits = { |
| 35 |
'c2be82058fb40f3ae891c68d185ff53e07f14f45', |
| 36 |
'04a040d907a83af54e0a98bdba5bfabc0ef4f700', |
| 37 |
'2e96b5f14e4025691b57d2301d71aa6092ed44bc', |
| 38 |
'3ab5c8cd03d92bf4ec41e351820349d92fbc40c4', |
| 39 |
'86d8e0c0652ef5236a460b75c25e4f7093cc0651', |
| 40 |
'e4cba49413ca429dc82f6aa2e88129ecb3fdd943', |
| 41 |
'1957bedf29a1b2cc231972aba680fe80199d5498', |
| 42 |
'040e5b0edbca861196d9e2ea2af5e805769c8d5d', |
| 43 |
'8057f9aa1f7e70490064de796d7a8d42d446caf8', |
| 44 |
'109f1b28fc94c93096506e3df0c25e331cef19d0', |
| 45 |
'39f81924d88e3cc197fc3df74204c9b5e01e12f7', |
| 46 |
'8e6a25b01becf449d54154b7e83de5f4955cba37', |
| 47 |
'72677e1119dc40aa680755d009e079ad49446c46', |
| 48 |
'10d76b7f1e5b63ad6d2b92940c39007913ced037', |
| 49 |
'de3b277247ce98d189f121155b75f490725a42f6', |
| 50 |
'13cf22eb557eb5e3d796822247d8d4957bdb25da'} |
| 51 |
|
| 52 |
FORMAT = '%(asctime)s:%(levelname)s:%(name)s:%(message)s' |
| 53 |
logging.basicConfig(level=logging.INFO, format=FORMAT, |
| 54 |
handlers=[ |
| 55 |
logging.FileHandler('/tmp/git_update_version.txt'), |
| 56 |
logging.StreamHandler() |
| 57 |
]) |
| 58 |
|
| 59 |
|
| 60 |
def read_timestamp(path): |
| 61 |
with open(path) as f: |
| 62 |
return f.read() |
| 63 |
|
| 64 |
|
| 65 |
def prepend_to_changelog_files(repo, folder, git_commit, add_to_git): |
| 66 |
if not git_commit.success: |
| 67 |
logging.info(f"While processing {git_commit.info.hexsha}:") |
| 68 |
for error in git_commit.errors: |
| 69 |
logging.info(error) |
| 70 |
raise AssertionError() |
| 71 |
for entry, output in git_commit.to_changelog_entries(use_commit_ts=True): |
| 72 |
full_path = os.path.join(folder, entry, 'ChangeLog') |
| 73 |
logging.info('writing to %s' % full_path) |
| 74 |
if os.path.exists(full_path): |
| 75 |
with open(full_path) as f: |
| 76 |
content = f.read() |
| 77 |
else: |
| 78 |
content = '' |
| 79 |
with open(full_path, 'w+') as f: |
| 80 |
f.write(output) |
| 81 |
if content: |
| 82 |
f.write('\n\n') |
| 83 |
f.write(content) |
| 84 |
if add_to_git: |
| 85 |
repo.git.add(full_path) |
| 86 |
|
| 87 |
|
| 88 |
active_refs = ['master', |
| 89 |
'releases/gcc-12', 'releases/gcc-13', 'releases/gcc-14'] |
| 90 |
|
| 91 |
parser = argparse.ArgumentParser(description='Update DATESTAMP and generate ' |
| 92 |
'ChangeLog entries') |
| 93 |
parser.add_argument('-g', '--git-path', default='.', |
| 94 |
help='Path to git repository') |
| 95 |
parser.add_argument('-p', '--push', action='store_true', |
| 96 |
help='Push updated active branches') |
| 97 |
parser.add_argument('-d', '--dry-mode', |
| 98 |
help='Generate patch for ChangeLog entries and do it' |
| 99 |
' even if DATESTAMP is unchanged; folder argument' |
| 100 |
' is expected') |
| 101 |
parser.add_argument('-c', '--current', action='store_true', |
| 102 |
help='Modify current branch (--push argument is ignored)') |
| 103 |
parser.add_argument('-i', '--ignore', action='append', |
| 104 |
help='list of commits to ignore') |
| 105 |
args = parser.parse_args() |
| 106 |
|
| 107 |
repo = Repo(args.git_path) |
| 108 |
origin = repo.remotes['origin'] |
| 109 |
|
| 110 |
|
| 111 |
def update_current_branch(ref_name=None): |
| 112 |
commit = repo.head.commit |
| 113 |
commit_count = 1 |
| 114 |
while commit: |
| 115 |
if (commit.author.email == '[email protected]' |
| 116 |
and commit.message.strip() == 'Daily bump.'): |
| 117 |
break |
| 118 |
# We support merge commits but only with 2 parensts |
| 119 |
assert len(commit.parents) <= 2 |
| 120 |
commit = commit.parents[-1] |
| 121 |
commit_count += 1 |
| 122 |
|
| 123 |
logging.info('%d revisions since last Daily bump' % commit_count) |
| 124 |
datestamp_path = os.path.join(args.git_path, 'gcc/DATESTAMP') |
| 125 |
if (read_timestamp(datestamp_path) != current_timestamp |
| 126 |
or args.dry_mode or args.current): |
| 127 |
head = repo.head.commit |
| 128 |
# if HEAD is a merge commit, start with second parent |
| 129 |
# (branched that is being merged into the current one) |
| 130 |
assert len(head.parents) <= 2 |
| 131 |
if len(head.parents) == 2: |
| 132 |
head = head.parents[1] |
| 133 |
commits = parse_git_revisions(args.git_path, '%s..%s' |
| 134 |
% (commit.hexsha, head.hexsha), ref_name) |
| 135 |
commits = [c for c in commits if c.info.hexsha not in ignored_commits] |
| 136 |
for git_commit in reversed(commits): |
| 137 |
prepend_to_changelog_files(repo, args.git_path, git_commit, |
| 138 |
not args.dry_mode) |
| 139 |
if args.dry_mode: |
| 140 |
diff = repo.git.diff('HEAD') |
| 141 |
patch = os.path.join(args.dry_mode, |
| 142 |
branch.name.split('/')[-1] + '.patch') |
| 143 |
with open(patch, 'w+') as f: |
| 144 |
f.write(diff) |
| 145 |
logging.info('branch diff written to %s' % patch) |
| 146 |
repo.git.checkout(force=True) |
| 147 |
else: |
| 148 |
# update timestamp |
| 149 |
logging.info('DATESTAMP will be changed:') |
| 150 |
with open(datestamp_path, 'w+') as f: |
| 151 |
f.write(current_timestamp) |
| 152 |
repo.git.add(datestamp_path) |
| 153 |
if not args.current: |
| 154 |
repo.index.commit('Daily bump.') |
| 155 |
logging.info('commit is done') |
| 156 |
if args.push: |
| 157 |
try: |
| 158 |
repo.git.push('origin', branch) |
| 159 |
logging.info('branch is pushed') |
| 160 |
except Exception: |
| 161 |
logging.exception('git push failed') |
| 162 |
else: |
| 163 |
logging.info('DATESTAMP unchanged') |
| 164 |
|
| 165 |
if args.ignore is not None: |
| 166 |
for item in args.ignore: |
| 167 |
ignored_commits.update(set(i for i in re.split(r'\s*,\s*|\s+', item))) |
| 168 |
|
| 169 |
if args.current: |
| 170 |
logging.info('=== Working on the current branch ===') |
| 171 |
update_current_branch() |
| 172 |
else: |
| 173 |
for ref in origin.refs: |
| 174 |
assert ref.name.startswith('origin/') |
| 175 |
name = ref.name[len('origin/'):] |
| 176 |
if name in active_refs: |
| 177 |
if name in repo.branches: |
| 178 |
branch = repo.branches[name] |
| 179 |
else: |
| 180 |
branch = repo.create_head(name, ref).set_tracking_branch(ref) |
| 181 |
logging.info('=== Working on: %s ===' % branch) |
| 182 |
branch.checkout() |
| 183 |
origin.pull(rebase=True) |
| 184 |
logging.info('branch pulled and checked out') |
| 185 |
update_current_branch(name) |
| 186 |
assert not repo.index.diff(None) |
| 187 |
logging.info('branch is done') |
| 188 |
logging.info('') |