Changeset View
Changeset View
Standalone View
Standalone View
contrib/source-control-tools/prepare-commit.py
- This file was added.
Property | Old Value | New Value |
---|---|---|
File Mode | null | 100755 |
#!/usr/bin/env python3 | |||||
# | |||||
# Copyright (c) 2020 The Bitcoin developers | |||||
# Distributed under the MIT software license, see the accompanying | |||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | |||||
import argparse | |||||
import os | |||||
import subprocess | |||||
import sys | |||||
import yaml | |||||
def prepare_commit(toplevel, config_path, build_name): | |||||
with open(config_path, 'r', encoding='utf-8') as config_file: | |||||
# Load configuration file | |||||
builds = yaml.safe_load(config_file).get('builds') | |||||
if not builds: | |||||
return ( | |||||
1, "Error: Configuration file missing expected 'builds' at the top level") | |||||
config = builds.get(build_name) | |||||
if not config: | |||||
return (2, "Error: Configuration file does not have a build named '{}'".format( | |||||
build_name)) | |||||
# Stage specified files | |||||
commit_files = config.get('commit_files', []) | |||||
subprocess.run(['git', 'add'] + [os.path.join(toplevel, f) | |||||
for f in commit_files], check=True) | |||||
# Get the commit message associated with this build | |||||
commit_message = config.get('commit_message') | |||||
if not commit_message: | |||||
return ( | |||||
10, "Error: Expected commit_message in '{}' config".format(build_name)) | |||||
# Commit the staged files | |||||
subprocess.run( | |||||
['git', 'commit', '-m', '[Automated] {}'.format(commit_message)], check=True) | |||||
return (0, "Finished preparing commit") | |||||
def main(): | |||||
toplevel = subprocess.check_output( | |||||
['git', 'rev-parse', '--show-toplevel']).rstrip().decode('utf-8') | |||||
default_config_path = os.path.join( | |||||
toplevel, 'contrib/teamcity/automated-commits.yml') | |||||
parser = argparse.ArgumentParser( | |||||
description="Prepare a git commit based on some config") | |||||
parser.add_argument( | |||||
"build_name", | |||||
help="The name of the build to fetch the commit information from" | |||||
) | |||||
parser.add_argument( | |||||
"--config", | |||||
"-c", | |||||
default=default_config_path, | |||||
help="Path to the configuration file (default: {})".format( | |||||
default_config_path) | |||||
) | |||||
args = parser.parse_args() | |||||
return_code, message = prepare_commit( | |||||
toplevel, args.config, args.build_name) | |||||
print(message) | |||||
sys.exit(return_code) | |||||
if __name__ == '__main__': | |||||
main() |