Skip to content

Commit

Permalink
EdkRepo: Move sync specific strings to a sync specific location
Browse files Browse the repository at this point in the history
Resolves tianocore#137

Signed-off-by: Ashley E Desimone <[email protected]>
Reviewed-by: Nate Desimone <[email protected]>
Reviewed-by: Kevin Sun <[email protected]>
  • Loading branch information
ashedesimone committed Mar 15, 2023
1 parent afd2cc0 commit 33252d4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 68 deletions.
41 changes: 41 additions & 0 deletions edkrepo/commands/humble/sync_humble.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env python3
#
## @file
# sync_humble.py
#
# Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
# SPDX-License-Identifier: BSD-2-Clause-Patent
#

''' Contains informational and error messages outputted by
the run_command function method of the Sync command
'''

from edkrepo.common.humble import BRANCH_BEHIND

SYNC_EXIT = 'Exiting without performing sync operations.'
SYNC_UPDATE_FIX = 'To checkout the new SHA/tag/branch run edkrepo checkout on the current combo.\n'
SYNC_MANIFEST_NOT_FOUND = 'A manifest for project, {}, was not found.\nTo complete this operation please rerun the command with the --override flag\n' + SYNC_EXIT
SYNC_MANIFEST_UPDATE_FAILED = 'Failed to update manifest.'
SYNC_SOURCE_MOVE_WARNING = '{} being moved to {}'
SYNC_REMOVE_WARNING = 'The following repos no longer exist in the new manifest and can no \nlonger be used for submitting code. Please manually delete the following \ndirectories after saving any work you have in them:'
SYNC_MANIFEST_DIFF_WARNING = 'The global manifest for this project differs from the local manifest.'
SYNC_MANIFEST_UPDATE = 'To update to the latest manifest please run edkrepo sync --update-local-manifest.'
SYNC_REPO_CHANGE = 'The latest manifest for project, {}, requires a change in currently cloned repositories.\nTo complete this operation please rerun the command with the --override flag\n' + SYNC_EXIT
SYNCING = 'Syncing {0} to latest {1} branch ...'
FETCHING = 'Fetching latest code for {0} from {1} branch ...'
UPDATING_MANIFEST = 'Updating local manifest file ...'
NO_SYNC_DETACHED_HEAD = 'No need to sync repo {0} since it is in detached HEAD state'
SYNC_COMMITS_ON_TARGET = 'Commits were found on {0} branch.\n (use the "--override" flag to overwrite these commits)\nRepo {1} was not updated.'
SYNC_ERROR = '\nError: Some repositories were not updated.'
SYNC_NEEDS_REBASE = BRANCH_BEHIND + '\n' + ' (use "git rebase {target_remote}/{target_branch} {local_branch}" inside the {repo_folder} folder to rebase your commit)'
SYNC_BRANCH_CHANGE_ON_LOCAL = 'The SHA, tag or branch defined in the current combo has changed from {} to {} for the {} repo.\n The current workspace is not on the SHA/tag/branch defined in the initial combo. Unable to checkout new SHA/tag/branch.\n' + SYNC_UPDATE_FIX
SYNC_REBASE_CALC_FAIL = 'Unable to calculate if a rebase is required for the current branch'
SYNC_MOVE_FAILED = '''Moving {initial_dir} to {new_dir} failed.
Most likely files from the original directory are open in an editor or IDE.
It is likely that {new_dir} contains your original code now.
Please close any open editors to reconcile this problem.
You may need to manually rename {new_dir} to {initial_dir} in some circumstances.\n'''
SYNC_AUTOMATIC_REMOTE_PRUNE = 'Performing automatic remote prune...'


63 changes: 28 additions & 35 deletions edkrepo/commands/sync_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## @file
# sync_command.py
#
# Copyright (c) 2017 - 2022, Intel Corporation. All rights reserved.<BR>
# Copyright (c) 2017 - 2023, Intel Corporation. All rights reserved.<BR>
# SPDX-License-Identifier: BSD-2-Clause-Patent
#

Expand All @@ -22,16 +22,10 @@
from edkrepo.commands.edkrepo_command import EdkrepoCommand
from edkrepo.commands.edkrepo_command import SubmoduleSkipArgument, SourceManifestRepoArgument
import edkrepo.commands.arguments.sync_args as arguments
import edkrepo.commands.humble.sync_command as humble
from edkrepo.common.edkrepo_exception import EdkrepoException, EdkrepoManifestNotFoundException
from edkrepo.common.edkrepo_exception import EdkrepoManifestChangedException
from edkrepo.common.humble import SYNC_MANIFEST_NOT_FOUND, SYNC_MANIFEST_UPDATE_FAILED
from edkrepo.common.humble import SYNC_SOURCE_MOVE_WARNING, SYNC_REMOVE_WARNING, SYNC_REMOVE_LIST_END_FORMATTING
from edkrepo.common.humble import SYNC_MANIFEST_DIFF_WARNING, SYNC_MANIFEST_UPDATE
from edkrepo.common.humble import SPARSE_RESET, SPARSE_CHECKOUT, SYNC_REPO_CHANGE, SYNCING, FETCHING, UPDATING_MANIFEST
from edkrepo.common.humble import NO_SYNC_DETACHED_HEAD, SYNC_COMMITS_ON_TARGET, SYNC_ERROR
from edkrepo.common.humble import MIRROR_BEHIND_PRIMARY_REPO, SYNC_NEEDS_REBASE, INCLUDED_FILE_NAME
from edkrepo.common.humble import SYNC_BRANCH_CHANGE_ON_LOCAL
from edkrepo.common.humble import SYNC_REBASE_CALC_FAIL, SYNC_MOVE_FAILED, SYNC_AUTOMATIC_REMOTE_PRUNE
from edkrepo.common.humble import SPARSE_RESET, SPARSE_CHECKOUT, INCLUDED_FILE_NAME
from edkrepo.common.workspace_maintenance.humble.manifest_repos_maintenance_humble import SOURCE_MANIFEST_REPO_NOT_FOUND
from edkrepo.common.pathfix import get_actual_path, expanduser
from edkrepo.common.common_cache_functions import get_repo_cache_obj
Expand Down Expand Up @@ -136,7 +130,7 @@ def run_command(self, args, config):
except EdkrepoException as e:
if args.verbose:
print(e)
print(SYNC_MANIFEST_UPDATE_FAILED)
print(humble.SYNC_MANIFEST_UPDATE_FAILED)
manifest = get_workspace_manifest()
if args.update_local_manifest:
try:
Expand Down Expand Up @@ -189,9 +183,9 @@ def run_command(self, args, config):
#The new branch may not exist in the heads list yet if it is a new branch
repo.git.checkout(repo_to_sync.branch)
if not args.fetch:
ui_functions.print_info_msg(SYNCING.format(repo_to_sync.root, repo.active_branch), header = False)
ui_functions.print_info_msg(humble.SYNCING.format(repo_to_sync.root, repo.active_branch), header = False)
else:
ui_functions.print_info_msg(FETCHING.format(repo_to_sync.root, repo.active_branch), header = False)
ui_functions.print_info_msg(humble.FETCHING.format(repo_to_sync.root, repo.active_branch), header = False)
try:
repo.remotes.origin.fetch()
except GitCommandError as e:
Expand All @@ -207,7 +201,7 @@ def run_command(self, args, config):
if e.stderr.strip().find(prune_needed_heuristic_str) != -1:
prune_needed = True
if prune_needed:
ui_functions.print_info_msg(SYNC_AUTOMATIC_REMOTE_PRUNE)
ui_functions.print_info_msg(humble.SYNC_AUTOMATIC_REMOTE_PRUNE)
time.sleep(1.0)
repo.git.remote('prune', 'origin')
time.sleep(1.0)
Expand All @@ -216,7 +210,7 @@ def run_command(self, args, config):
raise

if not args.override and not repo.is_ancestor(ancestor_rev='HEAD', rev='origin/{}'.format(repo_to_sync.branch)):
ui_functions.print_info_msg(SYNC_COMMITS_ON_TARGET.format(repo_to_sync.branch, repo_to_sync.root), header = False)
ui_functions.print_info_msg(humble.SYNC_COMMITS_ON_TARGET.format(repo_to_sync.branch, repo_to_sync.root), header=False)
local_commits = True
sync_error = True
if not args.fetch and (not local_commits or args.override):
Expand All @@ -232,23 +226,23 @@ def run_command(self, args, config):
branch_origin = next(itertools.islice(repo.iter_commits(), commit_count, commit_count + 1))
behind_count = int(repo.git.rev_list('--count', '{}..{}'.format(branch_origin.hexsha, latest_sha)))
if behind_count:
ui_functions.print_info_msg(SYNC_NEEDS_REBASE.format(
ui_functions.print_info_msg(humble.SYNC_NEEDS_REBASE.format(
behind_count=behind_count,
target_remote='origin',
target_branch=repo_to_sync.branch,
local_branch=initial_active_branch.name,
repo_folder=repo_to_sync.root), header = False)
repo_folder=repo_to_sync.root), header=False)
except:
ui_functions.print_error_msg(SYNC_REBASE_CALC_FAIL, header = False)
ui_functions.print_error_msg(humble.SYNC_REBASE_CALC_FAIL, header=False)
elif args.verbose:
ui_functions.print_warning_msg(NO_SYNC_DETACHED_HEAD.format(repo_to_sync.root), header = False)
ui_functions.print_warning_msg(humble.NO_SYNC_DETACHED_HEAD.format(repo_to_sync.root), header=False)

# Update commit message templates
if global_manifest_directory is not None:
update_repo_commit_template(workspace_path, repo, repo_to_sync, global_manifest_directory)

if sync_error:
ui_functions.print_error_msg(SYNC_ERROR, header = False)
ui_functions.print_error_msg(humble.SYNC_ERROR, header=False)

# Initialize submodules
if not args.skip_submodule:
Expand Down Expand Up @@ -292,7 +286,7 @@ def __update_local_manifest(self, args, config, initial_manifest, workspace_path
if e.stderr.strip().find(prune_needed_heuristic_str) != -1:
prune_needed = True
if prune_needed:
ui_functions.print_info_msg(SYNC_AUTOMATIC_REMOTE_PRUNE)
ui_functions.print_info_msg(humble.SYNC_AUTOMATIC_REMOTE_PRUNE)
# The sleep is to give the operating system time to close all the file handles that Git has open
time.sleep(1.0)
repo.git.remote('prune', 'origin')
Expand All @@ -305,7 +299,7 @@ def __update_local_manifest(self, args, config, initial_manifest, workspace_path
index_path = os.path.join(global_manifest_directory, 'CiIndex.xml')
ci_index_xml = CiIndexXml(index_path)
if initial_manifest.project_info.codename not in ci_index_xml.project_list:
raise EdkrepoManifestNotFoundException(SYNC_MANIFEST_NOT_FOUND.format(initial_manifest.project_info.codename))
raise EdkrepoManifestNotFoundException(humble.SYNC_MANIFEST_NOT_FOUND.format(initial_manifest.project_info.codename))
initial_manifest_remotes = {name:url for name, url in initial_manifest.remotes}
ci_index_xml_rel_path = os.path.normpath(ci_index_xml.get_project_xml(initial_manifest.project_info.codename))
global_manifest = os.path.join(global_manifest_directory, ci_index_xml_rel_path)
Expand All @@ -329,7 +323,7 @@ def __update_local_manifest(self, args, config, initial_manifest, workspace_path
initial_source_repos = set([(source.remote_name, source.remote_url, source.root) for source in set(initial_sources)])
new_source_repos = set([(source.remote_name, source.remote_url, source.root) for source in set(new_sources)])
if not args.override and initial_source_repos != new_source_repos:
raise EdkrepoManifestChangedException(SYNC_REPO_CHANGE.format(initial_manifest.project_info.codename))
raise EdkrepoManifestChangedException(humble.SYNC_REPO_CHANGE.format(initial_manifest.project_info.codename))
elif args.override and initial_source_repos != new_source_repos:
# get a set of repo source tuples that are not in both the new and old manifest
uncommon_sources = []
Expand Down Expand Up @@ -394,21 +388,20 @@ def __update_local_manifest(self, args, config, initial_manifest, workspace_path
for source in sources_to_move:
old_dir = os.path.join(workspace_path, source.root)
new_dir = generate_name_for_obsolete_backup(old_dir)
ui_functions.print_warning_msg(SYNC_SOURCE_MOVE_WARNING.format(source.root, new_dir), header = False)
ui_functions.print_warning_msg(humble.SYNC_SOURCE_MOVE_WARNING.format(source.root, new_dir), header = False)
new_dir = os.path.join(workspace_path, new_dir)
try:
shutil.move(old_dir, new_dir)
except:
ui_functions.print_error_msg(SYNC_MOVE_FAILED.format(initial_dir=source.root, new_dir=new_dir), header = False)
ui_functions.print_error_msg(humble.SYNC_MOVE_FAILED.format(initial_dir=source.root, new_dir=new_dir), header=False)
raise
# Tell the user about any Git repositories that are no longer used.
if len(sources_to_remove) > 0:
ui_functions.print_warning_msg(SYNC_REMOVE_WARNING, header = False)
ui_functions.print_warning_msg(humble.SYNC_REMOVE_WARNING, header = False)
for source in sources_to_remove:
path_to_source = os.path.join(workspace_path, source.root)
ui_functions.print_warning_msg(path_to_source, header = False)
if len(sources_to_remove) > 0:
ui_functions.print_warning_msg(SYNC_REMOVE_LIST_END_FORMATTING, header = False)

# Clone any new Git repositories
clone_repos(args, workspace_path, sources_to_clone, new_manifest_to_check.repo_hooks, config, new_manifest_to_check, global_manifest_directory)
# Make a list of and only checkout repos that were newly cloned. Sync keeps repos on their initial active branches
Expand Down Expand Up @@ -437,7 +430,7 @@ def __update_local_manifest(self, args, config, initial_manifest, workspace_path
create_repos(repos_to_create, workspace_path, new_manifest_to_check, global_manifest_directory)

#remove the old manifest file and copy the new one
ui_functions.print_info_msg(UPDATING_MANIFEST, header = False)
ui_functions.print_info_msg(humble.UPDATING_MANIFEST, header=False)
local_manifest_path = os.path.join(local_manifest_dir, 'Manifest.xml')
os.remove(local_manifest_path)
shutil.copy(global_manifest, local_manifest_path)
Expand Down Expand Up @@ -476,18 +469,18 @@ def __check_combo_patchset_sha_tag_branch(self, workspace_path, initial_sources,
break
elif initial_source.commit and initial_source.commit != new_source.commit:
if repo.head.object.hexsha != initial_source.commit:
ui_functions.print_info_msg(SYNC_BRANCH_CHANGE_ON_LOCAL.format(initial_source.branch, new_source.branch, initial_source.root), header = False)
ui_functions.print_info_msg(humble.SYNC_BRANCH_CHANGE_ON_LOCAL.format(initial_source.branch, new_source.branch, initial_source.root), header=False)
repos_to_checkout.append(new_source)
break
elif initial_source.tag and initial_source.tag != new_source.tag:
tag_sha = repo.git.rev_list('-n 1', initial_source.tag) #according to gitpython docs must change - to _
if tag_sha != repo.head.object.hexsha:
ui_functions.print_info_msg(SYNC_BRANCH_CHANGE_ON_LOCAL.format(initial_source.branch, new_source.branch, initial_source.root), header = False)
ui_functions.print_info_msg(humble.SYNC_BRANCH_CHANGE_ON_LOCAL.format(initial_source.branch, new_source.branch, initial_source.root), header=False)
repos_to_checkout.append(new_source)
break
elif initial_source.branch and initial_source.branch != new_source.branch:
if repo.active_branch.name != initial_source.branch:
ui_functions.print_info_msg(SYNC_BRANCH_CHANGE_ON_LOCAL.format(initial_source.branch, new_source.branch, initial_source.root), header = False)
ui_functions.print_info_msg(humble.SYNC_BRANCH_CHANGE_ON_LOCAL.format(initial_source.branch, new_source.branch, initial_source.root), header = False)
repos_to_checkout.append(new_source)
break
return repos_to_checkout, repos_to_create
Expand All @@ -499,7 +492,7 @@ def __check_for_new_manifest(self, args, config, initial_manifest, workspace_pat
if args.override:
return
else:
raise EdkrepoManifestNotFoundException(SYNC_MANIFEST_NOT_FOUND.format(initial_manifest.project_info.codename))
raise EdkrepoManifestNotFoundException(humble.SYNC_MANIFEST_NOT_FOUND.format(initial_manifest.project_info.codename))

#see if there is an entry in CiIndex.xml that matches the prject name of the current manifest
index_path = os.path.join(global_manifest_directory, 'CiIndex.xml')
Expand All @@ -508,13 +501,13 @@ def __check_for_new_manifest(self, args, config, initial_manifest, workspace_pat
if args.override:
return
else:
raise EdkrepoManifestNotFoundException(SYNC_MANIFEST_NOT_FOUND.format(initial_manifest.project_info.codename))
raise EdkrepoManifestNotFoundException(humble.SYNC_MANIFEST_NOT_FOUND.format(initial_manifest.project_info.codename))
ci_index_xml_rel_path = ci_index_xml.get_project_xml(initial_manifest.project_info.codename)
global_manifest_path = os.path.join(global_manifest_directory, os.path.normpath(ci_index_xml_rel_path))
global_manifest = ManifestXml(global_manifest_path)
if not initial_manifest.equals(global_manifest, True):
ui_functions.print_warning_msg(SYNC_MANIFEST_DIFF_WARNING, header = False)
ui_functions.print_info_msg(SYNC_MANIFEST_UPDATE, header = False)
ui_functions.print_warning_msg(humble.SYNC_MANIFEST_DIFF_WARNING, header=False)
ui_functions.print_info_msg(humble.SYNC_MANIFEST_UPDATE, header = False)

def __check_submodule_config(self, workspace_path, manifest, repo_sources):
gitconfigpath = os.path.normpath(expanduser("~/.gitconfig"))
Expand Down
Loading

0 comments on commit 33252d4

Please sign in to comment.