Skip to content

Commit

Permalink
scripts/version: Correctly handle cases where we cannot find the vers…
Browse files Browse the repository at this point in the history
…ion file at all.

Initiall mentioned here: #70 (comment)
  • Loading branch information
arthurp committed Apr 22, 2021
1 parent 44ef570 commit f105f5e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
10 changes: 10 additions & 0 deletions scripts/katana_version/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Configuration:
github_access: Tuple[str, ...]
enterprise: Optional[Repo]
open: Optional[Repo]
version_file: Optional[Path]
dry_run: bool

def __init__(self, args=Namespace()):
Expand Down Expand Up @@ -68,6 +69,15 @@ def __init__(self, args=Namespace()):
f"Failed to parse version provided in environment variable KATANA_VERSION: {environ.get('KATANA_VERSION')}"
)

self.version_file = None
if (katana_repo_path / CONFIG_VERSION_PATH).is_file():
self.version_file = katana_repo_path / CONFIG_VERSION_PATH
else:
logger.error(
f"Version file does not exist in source: {katana_repo_path / CONFIG_VERSION_PATH}. "
"Your Katana source is incomplete. Using 0.0.0 as a stand-in for the missing version."
)

self.open = None
self.enterprise = None
try:
Expand Down
11 changes: 7 additions & 4 deletions scripts/katana_version/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def bump_checks(args):
git.switch(current_branch, config.enterprise, config.dry_run)
git.switch(current_branch, config.open, config.dry_run)

prev_version, variant = get_explicit_version(git.HEAD, True, config.open, no_dev=False)
prev_version, variant = get_explicit_version(git.HEAD, True, config.open, config.version_file, no_dev=False)
next_version = version.Version(args.next_version)

check_branch_version(current_branch, kind, next_version, prev_version)
Expand Down Expand Up @@ -300,7 +300,7 @@ def bump_subcommand(args):

g = GithubFacade(config)

prev_version, variant = get_explicit_version(git.HEAD, True, config.open, no_dev=True)
prev_version, variant = get_explicit_version(git.HEAD, True, config.open, config.version_file, no_dev=True)
next_version = version.Version(args.next_version)

current_branch = git.get_branch_checked_out(config.open)
Expand Down Expand Up @@ -535,7 +535,7 @@ def release_subcommand(args):
# Perform the checks that bump will do first. That way we will fail before tagging if possible.
bump_checks(args)
# Set some arguments for tag. This is a bit of a hack, but not worth the engineering to fix.
ver, variant = get_explicit_version(git.HEAD, False, config.open, no_dev=True)
ver, variant = get_explicit_version(git.HEAD, False, config.open, config.version_file, no_dev=True)
args.version = str(ver)
args.require_upstream = True
tag_subcommand(args)
Expand Down Expand Up @@ -570,7 +570,7 @@ def release_branch_subcommand(args):
git.switch("master", config.enterprise, config.dry_run)
git.switch("master", config.open, config.dry_run)

prev_version, variant = get_explicit_version(git.HEAD, True, config.open, no_dev=True)
prev_version, variant = get_explicit_version(git.HEAD, True, config.open, config.version_file, no_dev=True)
next_version = version.Version(args.next_version)
rc_version = version.Version(f"{prev_version}rc1")

Expand Down Expand Up @@ -599,6 +599,9 @@ def release_branch_subcommand(args):


def check_clean(args, config):
if not config.open:
raise StateError("Action cannot run in a source tree that is not a git clone.")

is_dirty = git.is_dirty(config.open) or (config.has_enterprise and git.is_dirty(config.enterprise))
if not args.clean and is_dirty:
raise StateError("Action only supported in clean repositories. (Stash your changes.)")
Expand Down
28 changes: 20 additions & 8 deletions scripts/katana_version/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ def get_version(
if config.has_enterprise:
ke_commit = git.simplify_merge_commit(ke_commit, config.enterprise)

k_explicit_version, variant = get_explicit_version(k_commit, use_working_copy, config.open, variant)
k_explicit_version, variant = get_explicit_version(
k_commit, use_working_copy, config.open, config.version_file, variant
)
ke_tag_version = None
if config.has_enterprise and not git.is_dirty(config.enterprise):
ke_tag_version = get_tag_version(ke_commit or "HEAD", config.enterprise)
Expand Down Expand Up @@ -170,9 +172,11 @@ def branch_ahead_count(branch):
return nearest_branch


def get_explicit_version(k_commit: str, use_working_copy: bool, repo, variant=None, no_dev=False):
def get_explicit_version(k_commit: str, use_working_copy: bool, repo, version_file, variant=None, no_dev=False):
tag_version = get_tag_version(k_commit, repo)
explicit_version = tag_version or get_config_version(None if use_working_copy else k_commit, repo, no_dev=no_dev)
explicit_version = tag_version or get_config_version(
None if use_working_copy else k_commit, repo, version_file, no_dev=no_dev
)
if explicit_version.local and variant and variant != explicit_version.local:
logger.warning(
f"You are overriding the repository variant {explicit_version.local} with build-time variant {variant}."
Expand All @@ -181,12 +185,20 @@ def get_explicit_version(k_commit: str, use_working_copy: bool, repo, variant=No
return explicit_version, variant


def get_config_version(k_commit, repo: Repo, no_dev=False) -> version.Version:
if k_commit:
version_str = capture_command("git", *git.dir_arg(repo), "show", "-p", f"{k_commit}:{CONFIG_VERSION_PATH}")
else:
with open(repo.dir / CONFIG_VERSION_PATH, "rt") as version_file:
def get_config_version(k_commit, repo: Repo, version_file, no_dev=False) -> version.Version:
if repo:
if k_commit:
version_str = capture_command("git", *git.dir_arg(repo), "show", "-p", f"{k_commit}:{CONFIG_VERSION_PATH}")
else:
with open(repo.dir / CONFIG_VERSION_PATH, "rt") as version_file:
version_str = version_file.read()
elif version_file:
# We have no git information. Wing it.
with open(Path(__file__).parent.parent.parent / CONFIG_VERSION_PATH, "rt") as version_file:
version_str = version_file.read()
else:
# We have no information. Something is really broken. Still don't crash to allow builds.
version_str = "0.0.0"
ver = version.Version(version_str.strip())

if no_dev:
Expand Down

0 comments on commit f105f5e

Please sign in to comment.