def _store_stable_version(version, stable_version_file, dry_run): if os.path.exists(stable_version_file): runcommand.print_trace( ["Storing", version, "as stable version ..."], trace_prefix="", dry_run=dry_run, ) if not dry_run: with open(stable_version_file, "w") as f: f.write(version + "\n") status = runcommand.run_command( ["git", "diff", "-s", "--exit-code", stable_version_file], check=False, show_trace=True, dry_run=dry_run, ) if dry_run or status == 1: commit_command = [ "git", "commit", "-m", "Update stable version to {version}".format(version=version), stable_version_file, ] runcommand.run_command( commit_command, check=True, show_trace=True, dry_run=dry_run, )
def main(*argv): """Do the thing""" (prog, argv) = argparsing.grok_argv(argv) argparser = argparsing.setup_argparse(prog=prog, description=DESCRIPTION) _add_arguments(argparser) args = argparser.parse_args(argv) if args.working_dir is not None: runcommand.print_trace(["cd", args.working_dir], dry_run=args.dry_run) os.chdir(args.working_dir) if args.version_file is None: args.version_file = os.path.join(_get_project_dir(), DEFAULT_VERSION_FILENAME) with open(args.version_file, "r") as version_file: project_version = "".join([ args.tag_prefix, _get_project_version(version_file), args.tag_suffix ]) status = runcommand.run_command( [ "git", "tag", "-a", "-m", project_version, project_version, # Correct, not a duplicate ], check=False, show_trace=True, dry_run=args.dry_run, ) return status
def _get_project_dir(): """Return the top-level directory of the current Git project""" project_dir = runcommand.run_command( ["git", "rev-parse", "--show-toplevel"], dry_run=False, return_output=True, show_trace=False, ) return project_dir[:-1] if project_dir.endswith("\n") else project_dir
def _does_tag_exist(tag): tags = runcommand.run_command( ["git", "tag", "--list", tag], check=False, return_output=True, show_trace=True, dry_run=False, ) return tag in tags.split("\n")
def main(*argv): # pylint: disable=too-many-branches """Do the thing""" (prog, argv) = argparsing.grok_argv(argv) argparser = argparsing.setup_argparse(prog=prog, description=DESCRIPTION) _add_arguments(argparser) args = argparser.parse_args(argv) if args.working_dir is not None: runcommand.print_trace(["cd", args.working_dir], dry_run=args.dry_run) os.chdir(args.working_dir) if args.version_file is None: args.version_file = os.path.join(_get_project_dir(), DEFAULT_VERSION_FILENAME) if args.stable_version_file is None: args.stable_version_file = os.path.join( _get_project_dir(), DEFAULT_STABLE_VERSION_FILENAME) with open(args.version_file, "r") as version_file: bare_project_version = _get_project_version(version_file) project_version = "".join( [args.tag_prefix, bare_project_version, args.tag_suffix]) if args.message is None: args.message = project_version if args.stable_message is None: args.stable_message = project_version base_tag_command = ["git", "tag"] tag_command = list(base_tag_command) tag_command.extend(["-a", "-m", args.message]) if args.rewrite_history: tag_command.append("--force") if _does_tag_exist(project_version): for message in [ "CAUTION!!! History may be rewritten!", _get_safety_message(), ]: runcommand.print_trace([message], trace_prefix="", dry_run=args.dry_run) if args.peril_sensitive_sunglasses: runcommand.print_trace( [ "Peril-sensitive sunglasses deployed, proceeding anyway..." ], trace_prefix="", dry_run=args.dry_run, ) elif not _should_continue(dry_run=args.dry_run): runcommand.print_trace(["Aborting..."], trace_prefix="", dry_run=args.dry_run) return 1 tag_command.append(project_version) if args.commit is not None: tag_command.append(args.commit) try: if args.stable: # Must do this before tagging _store_stable_version(bare_project_version, args.stable_version_file, dry_run=args.dry_run) runcommand.run_command( tag_command, check=True, show_trace=True, dry_run=args.dry_run, ) if args.stable: stable_tag_command = list(base_tag_command) stable_tag_command.extend(["--force", args.stable_tag]) if args.commit is not None: stable_tag_command.append(args.commit) runcommand.print_trace( ["Tagging", project_version, "as stable ..."], trace_prefix="", dry_run=args.dry_run, ) runcommand.run_command( stable_tag_command, check=True, show_trace=True, dry_run=args.dry_run, ) if args.push: base_push_command = ["git", "push"] if args.stable: # Push updated stable version file runcommand.run_command(base_push_command, check=True, show_trace=True, dry_run=args.dry_run) push_command = list(base_push_command) if args.rewrite_history: push_command.append("--force") push_command.extend( ["origin", "+refs/tags/{tag}".format(tag=project_version)]) runcommand.run_command(push_command, check=True, show_trace=True, dry_run=args.dry_run) if args.stable: push_command = list(base_push_command) push_command.extend([ "--force", "origin", "+refs/tags/{tag}".format(tag=args.stable_tag), ]) runcommand.run_command(push_command, check=True, show_trace=True, dry_run=args.dry_run) except subprocess.CalledProcessError as e: print("{prog}: error: {e}".format(prog=prog, e=e), file=sys.stderr) return 1 return 0