]> git.ipfire.org Git - thirdparty/gcc.git/blob - contrib/gcc-changelog/git_update_version.py
contrib: Add 8057f9aa1f7e70490064de796d7a8d42d446caf8 to ignored commits.
[thirdparty/gcc.git] / contrib / gcc-changelog / git_update_version.py
1 #!/usr/bin/env python3
2
3 # Copyright (C) 2020-2024 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
26 from git import Repo
27
28 from git_repository import parse_git_revisions
29
30 current_timestamp = datetime.datetime.now().strftime('%Y%m%d\n')
31
32 # Skip the following commits, they cannot be correctly processed
33 IGNORED_COMMITS = (
34 'c2be82058fb40f3ae891c68d185ff53e07f14f45',
35 '04a040d907a83af54e0a98bdba5bfabc0ef4f700',
36 '2e96b5f14e4025691b57d2301d71aa6092ed44bc',
37 '3ab5c8cd03d92bf4ec41e351820349d92fbc40c4',
38 '86d8e0c0652ef5236a460b75c25e4f7093cc0651',
39 'e4cba49413ca429dc82f6aa2e88129ecb3fdd943',
40 '1957bedf29a1b2cc231972aba680fe80199d5498',
41 '040e5b0edbca861196d9e2ea2af5e805769c8d5d',
42 '8057f9aa1f7e70490064de796d7a8d42d446caf8')
43
44 FORMAT = '%(asctime)s:%(levelname)s:%(name)s:%(message)s'
45 logging.basicConfig(level=logging.INFO, format=FORMAT,
46 handlers=[
47 logging.FileHandler('/tmp/git_update_version.txt'),
48 logging.StreamHandler()
49 ])
50
51
52 def read_timestamp(path):
53 with open(path) as f:
54 return f.read()
55
56
57 def prepend_to_changelog_files(repo, folder, git_commit, add_to_git):
58 if not git_commit.success:
59 for error in git_commit.errors:
60 logging.info(error)
61 raise AssertionError()
62 for entry, output in git_commit.to_changelog_entries(use_commit_ts=True):
63 full_path = os.path.join(folder, entry, 'ChangeLog')
64 logging.info('writing to %s' % full_path)
65 if os.path.exists(full_path):
66 with open(full_path) as f:
67 content = f.read()
68 else:
69 content = ''
70 with open(full_path, 'w+') as f:
71 f.write(output)
72 if content:
73 f.write('\n\n')
74 f.write(content)
75 if add_to_git:
76 repo.git.add(full_path)
77
78
79 active_refs = ['master',
80 'releases/gcc-11', 'releases/gcc-12', 'releases/gcc-13']
81
82 parser = argparse.ArgumentParser(description='Update DATESTAMP and generate '
83 'ChangeLog entries')
84 parser.add_argument('-g', '--git-path', default='.',
85 help='Path to git repository')
86 parser.add_argument('-p', '--push', action='store_true',
87 help='Push updated active branches')
88 parser.add_argument('-d', '--dry-mode',
89 help='Generate patch for ChangeLog entries and do it'
90 ' even if DATESTAMP is unchanged; folder argument'
91 ' is expected')
92 parser.add_argument('-c', '--current', action='store_true',
93 help='Modify current branch (--push argument is ignored)')
94 args = parser.parse_args()
95
96 repo = Repo(args.git_path)
97 origin = repo.remotes['origin']
98
99
100 def update_current_branch(ref_name):
101 commit = repo.head.commit
102 commit_count = 1
103 while commit:
104 if (commit.author.email == 'gccadmin@gcc.gnu.org'
105 and commit.message.strip() == 'Daily bump.'):
106 break
107 # We support merge commits but only with 2 parensts
108 assert len(commit.parents) <= 2
109 commit = commit.parents[-1]
110 commit_count += 1
111
112 logging.info('%d revisions since last Daily bump' % commit_count)
113 datestamp_path = os.path.join(args.git_path, 'gcc/DATESTAMP')
114 if (read_timestamp(datestamp_path) != current_timestamp
115 or args.dry_mode or args.current):
116 head = repo.head.commit
117 # if HEAD is a merge commit, start with second parent
118 # (branched that is being merged into the current one)
119 assert len(head.parents) <= 2
120 if len(head.parents) == 2:
121 head = head.parents[1]
122 commits = parse_git_revisions(args.git_path, '%s..%s'
123 % (commit.hexsha, head.hexsha), ref_name)
124 commits = [c for c in commits if c.info.hexsha not in IGNORED_COMMITS]
125 for git_commit in reversed(commits):
126 prepend_to_changelog_files(repo, args.git_path, git_commit,
127 not args.dry_mode)
128 if args.dry_mode:
129 diff = repo.git.diff('HEAD')
130 patch = os.path.join(args.dry_mode,
131 branch.name.split('/')[-1] + '.patch')
132 with open(patch, 'w+') as f:
133 f.write(diff)
134 logging.info('branch diff written to %s' % patch)
135 repo.git.checkout(force=True)
136 else:
137 # update timestamp
138 logging.info('DATESTAMP will be changed:')
139 with open(datestamp_path, 'w+') as f:
140 f.write(current_timestamp)
141 repo.git.add(datestamp_path)
142 if not args.current:
143 repo.index.commit('Daily bump.')
144 logging.info('commit is done')
145 if args.push:
146 try:
147 repo.git.push('origin', branch)
148 logging.info('branch is pushed')
149 except Exception:
150 logging.exception('git push failed')
151 else:
152 logging.info('DATESTAMP unchanged')
153
154
155 if args.current:
156 logging.info('=== Working on the current branch ===')
157 update_current_branch()
158 else:
159 for ref in origin.refs:
160 assert ref.name.startswith('origin/')
161 name = ref.name[len('origin/'):]
162 if name in active_refs:
163 if name in repo.branches:
164 branch = repo.branches[name]
165 else:
166 branch = repo.create_head(name, ref).set_tracking_branch(ref)
167 logging.info('=== Working on: %s ===' % branch)
168 branch.checkout()
169 origin.pull(rebase=True)
170 logging.info('branch pulled and checked out')
171 update_current_branch(name)
172 assert not repo.index.diff(None)
173 logging.info('branch is done')
174 logging.info('')