Add scripts/make_release.py for releases

Add a Python script that prepares release metadata and prints the
follow-up git and GitHub CLI commands for pre-releases (alpha, beta,
rc), stable releases, and post-release version bumps on master.

Change-Id: I833597cec3977ca14af8a7a026deb9331adc3b52
diff --git a/scripts/make_release.py b/scripts/make_release.py
new file mode 100755
index 0000000..2083f19
--- /dev/null
+++ b/scripts/make_release.py
@@ -0,0 +1,292 @@
+#!/usr/bin/env python3
+# Ceres Solver - A fast non-linear least squares minimizer
+# Copyright 2026 Google Inc. All rights reserved.
+# http://ceres-solver.org/
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+# * Redistributions of source code must retain the above copyright notice,
+#   this list of conditions and the following disclaimer.
+# * Redistributions in binary form must reproduce the above copyright notice,
+#   this list of conditions and the following disclaimer in the documentation
+#   and/or other materials provided with the distribution.
+# * Neither the name of Google Inc. nor the names of its contributors may be
+#   used to endorse or promote products derived from this software without
+#   specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+
+"""Prepare Ceres Solver version metadata and print the release checklist.
+
+Supported workflows:
+  1. Pre-release (alpha, beta, or release candidate):
+       scripts/make_release.py 2.3.0rc1
+       scripts/make_release.py 2.3.0alpha1
+       scripts/make_release.py 2.3.0beta1
+     Updates include/ceres/version.h and package.xml to 2.3.0 while leaving
+     CITATION.cff at the previous stable release.
+
+  2. Stable release:
+       scripts/make_release.py 2.3.0
+     Updates include/ceres/version.h, package.xml,
+     docs/source/version_history.rst, and CITATION.cff (which also drives the
+     BibTeX block in docs/source/index.rst) to the new stable version and
+     release date.
+
+  3. Post-release development version bump on master:
+       scripts/make_release.py --next 2.4.0
+     Updates include/ceres/version.h and package.xml to 2.4.0 for master
+     development while leaving CITATION.cff at the latest stable release.
+"""
+
+from __future__ import annotations
+
+import argparse
+from dataclasses import dataclass
+import datetime
+from pathlib import Path
+import re
+import sys
+
+_VERSION_RE = re.compile(
+    r'^(?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)'
+    r'(?P<pre>(?:alpha|beta|rc)\d+)?$'
+)
+
+
+@dataclass(frozen=True)
+class _Version:
+    raw: str
+    major: str
+    minor: str
+    patch: str
+    pre: str | None
+
+    @property
+    def base(self) -> str:
+        return f'{self.major}.{self.minor}.{self.patch}'
+
+    @property
+    def citation(self) -> str:
+        return f'{self.major}.{self.minor}' if self.patch == '0' else self.base
+
+
+def _parse_version(value: str) -> _Version:
+    match = _VERSION_RE.match(value)
+    if not match:
+        raise argparse.ArgumentTypeError(
+            f'Invalid version "{value}". Expected X.Y.Z or '
+            f'X.Y.Z{{alpha,beta,rc}}N.'
+        )
+    return _Version(
+        raw=value,
+        major=match.group('major'),
+        minor=match.group('minor'),
+        patch=match.group('patch'),
+        pre=match.group('pre'),
+    )
+
+
+def _parse_date(value: str) -> datetime.date:
+    try:
+        return datetime.date.fromisoformat(value)
+    except ValueError as err:
+        raise argparse.ArgumentTypeError(
+            f'Invalid date "{value}". Expected YYYY-MM-DD.'
+        ) from err
+
+
+def _sub_once(
+    pattern: str, repl: str, text: str, path: Path, flags: int = 0
+) -> str:
+    updated, count = re.subn(pattern, repl, text, count=1, flags=flags)
+    if count != 1:
+        raise RuntimeError(
+            f'Expected to match {pattern!r} once in {path}, matched {count} times.'
+        )
+    return updated
+
+
+def _update_version_header(path: Path, major: str, minor: str, patch: str) -> None:
+    text = path.read_text(encoding='utf-8')
+    text = _sub_once(
+        r'^(#define\s+CERES_VERSION_MAJOR\s+)\d+',
+        rf'\g<1>{major}',
+        text,
+        path,
+        flags=re.MULTILINE,
+    )
+    text = _sub_once(
+        r'^(#define\s+CERES_VERSION_MINOR\s+)\d+',
+        rf'\g<1>{minor}',
+        text,
+        path,
+        flags=re.MULTILINE,
+    )
+    text = _sub_once(
+        r'^(#define\s+CERES_VERSION_REVISION\s+)\d+',
+        rf'\g<1>{patch}',
+        text,
+        path,
+        flags=re.MULTILINE,
+    )
+    path.write_text(text, encoding='utf-8')
+
+
+def _update_package_xml(path: Path, base_version: str) -> None:
+    text = path.read_text(encoding='utf-8')
+    text = _sub_once(
+        r'<version>[^<]+</version>',
+        f'<version>{base_version}</version>',
+        text,
+        path,
+    )
+    path.write_text(text, encoding='utf-8')
+
+
+def _update_citation_cff(path: Path, citation_version: str, release_date: str) -> None:
+    text = path.read_text(encoding='utf-8')
+    text = _sub_once(
+        r'^version:\s*\S+',
+        f'version: {citation_version}',
+        text,
+        path,
+        flags=re.MULTILINE,
+    )
+    text = _sub_once(
+        r'^date-released:\s*\S+',
+        f'date-released: {release_date}',
+        text,
+        path,
+        flags=re.MULTILINE,
+    )
+    path.write_text(text, encoding='utf-8')
+
+
+def _update_version_history(path: Path, base_version: str) -> bool:
+    text = path.read_text(encoding='utf-8')
+    underline = '=' * len(base_version)
+    pattern = rf'^{re.escape(base_version)}\n{re.escape(underline)}$'
+    if re.search(pattern, text, flags=re.MULTILINE) is not None:
+        return True
+    updated, count = re.subn(
+        r'^Unreleased\n==========$',
+        f'{base_version}\n{underline}',
+        text,
+        count=1,
+        flags=re.MULTILINE,
+    )
+    if count > 0:
+        path.write_text(updated, encoding='utf-8')
+        return True
+    return False
+
+
+def main() -> int:
+    parser = argparse.ArgumentParser(
+        description='Prepare Ceres Solver release metadata and print next steps.'
+    )
+    parser.add_argument(
+        'version',
+        type=_parse_version,
+        help='Target version (e.g. 2.3.0, 2.3.0rc1, 2.3.0alpha1, 2.3.0beta1).',
+    )
+    parser.add_argument(
+        '--next',
+        action='store_true',
+        dest='is_next',
+        help=(
+            'Bump include/ceres/version.h and package.xml on master for the '
+            'next development cycle without modifying CITATION.cff.'
+        ),
+    )
+    parser.add_argument(
+        '--date',
+        type=_parse_date,
+        default=datetime.date.today(),
+        help='Release date in YYYY-MM-DD format for stable releases (default: today).',
+    )
+    args = parser.parse_args()
+
+    version: _Version = args.version
+    if args.is_next and version.pre is not None:
+        parser.error(
+            '--next expects a base version X.Y.Z without a pre-release suffix.'
+        )
+
+    repo_root = Path(__file__).resolve().parents[1]
+    version_h = repo_root / 'include' / 'ceres' / 'version.h'
+    package_xml = repo_root / 'package.xml'
+    citation_cff = repo_root / 'CITATION.cff'
+    version_history = repo_root / 'docs' / 'source' / 'version_history.rst'
+
+    _update_version_header(version_h, version.major, version.minor, version.patch)
+    _update_package_xml(package_xml, version.base)
+
+    if args.is_next:
+        print(f'Updated {version_h.relative_to(repo_root)} and '
+              f'{package_xml.relative_to(repo_root)} to {version.base}.')
+        print('\nNext steps:')
+        print('  1. Review `git diff` and commit the version bump:')
+        print(f'       git commit -a -m "Bump version to {version.base}"')
+        print('  2. Upload to Gerrit and submit to master.')
+        return 0
+
+    if version.pre is not None:
+        print(
+            f'Prepared pre-release {version.raw} '
+            f'(source version {version.base}; CITATION.cff and '
+            f'version_history.rst left unchanged).'
+        )
+        print('\nNext steps:')
+        print('  1. If `git status` shows changes, commit and submit them to master.')
+        print('  2. Tag the target commit on master and push the tag to Gerrit (mirrored to GitHub):')
+        print(f'       git tag -a {version.raw} -m "{version.raw}"')
+        print(f'       git push origin {version.raw}')
+        print('  3. Once mirrored, publish the pre-release on GitHub (--prerelease keeps /releases/latest unchanged):')
+        print(
+            f'       gh release create {version.raw} --prerelease '
+            f'--title "Ceres Solver {version.raw}" --verify-tag'
+        )
+        return 0
+
+    if not _update_version_history(version_history, version.base):
+        print(
+            f'WARNING: {version_history.relative_to(repo_root)} does not have '
+            f'an "Unreleased" or "{version.base}" section heading.\n'
+        )
+
+    _update_citation_cff(citation_cff, version.citation, args.date.isoformat())
+    print(
+        f'Prepared stable release {version.raw} '
+        f'(updated version.h, package.xml, version_history.rst, and CITATION.cff).'
+    )
+    print('\nNext steps:')
+    print('  1. Review `git diff`, commit, and submit to master:')
+    print(f'       git commit -a -m "Prepare for {version.raw} release"')
+    print('  2. Once submitted to master, tag the commit and push the tag to Gerrit (mirrored to GitHub):')
+    print(f'       git tag -a {version.raw} -m "{version.raw}"')
+    print(f'       git push origin {version.raw}')
+    print('  3. Once mirrored, publish the stable release on GitHub:')
+    print(
+        f'       gh release create {version.raw} --latest '
+        f'--title "Ceres Solver {version.raw}" --verify-tag'
+    )
+    print('  4. When ready to begin the next API development cycle on master, run:')
+    print(f'       scripts/make_release.py --next {version.major}.{int(version.minor) + 1}.0')
+    return 0
+
+
+if __name__ == '__main__':
+    sys.exit(main())