Skip to content

Commit

Permalink
Merge pull request #396 from lomnido/feat-intro_deep_manifest
Browse files Browse the repository at this point in the history
'cli/manifest.py' should be ready to make use of deep manifest
  • Loading branch information
lomnido authored Mar 11, 2024
2 parents 0600d2b + 8a1af93 commit 0f34579
Show file tree
Hide file tree
Showing 2 changed files with 272 additions and 69 deletions.
143 changes: 76 additions & 67 deletions tsrc/cli/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@
get_workspace_with_repos,
)
from tsrc.executor import process_items
from tsrc.git import run_git_captured
from tsrc.git import get_git_status, run_git_captured
from tsrc.manifest import load_manifest
from tsrc.repo import Repo
from tsrc.status_endpoint import Status, StatusCollector, describe_status
from tsrc.status_endpoint import (
Status,
StatusCollector,
get_l_and_r_sha1_of_branch,
workspace_repositories_summary,
)
from tsrc.workspace_config import WorkspaceConfig


Expand All @@ -37,7 +43,7 @@ def run(args: argparse.Namespace) -> None:
# manifest_branch = workspace.local_manifest.current_branch()
workspace_config = workspace.config

ui.info_1("Manifest's URL: ", ui.purple, workspace_config.manifest_url, ui.reset)
ui.info_1("Manifest's URL:", ui.purple, workspace_config.manifest_url, ui.reset)

status_collector = StatusCollector(workspace)
repos = workspace.repos
Expand All @@ -57,17 +63,19 @@ def run(args: argparse.Namespace) -> None:
current_workspace_manifest_repo_branch = None

if static_manifest_manifest_dest:
ui.info_2("Integrated into Workspace as another repository:")
ui.info_2("Current integration into Workspace:")

# statuses_items = statuses.items()
current_workspace_manifest_repo_branch = workspace_integration_summary(
current_workspace_manifest_repo_branch = workspace_repositories_summary(
workspace.root_path,
statuses,
static_manifest_manifest_dest,
static_manifest_manifest_branch,
workspace.config.manifest_branch,
only_manifest=True,
)

mi = ManifestInfo(
mi = ManifestReport(
workspace_config,
cfg_path,
workspace.root_path,
Expand All @@ -78,10 +86,10 @@ def run(args: argparse.Namespace) -> None:
current_workspace_manifest_repo_branch,
)

mi.info()
mi.report()


class ManifestInfo:
class ManifestReport:
def __init__(
self,
workspace_config: WorkspaceConfig,
Expand All @@ -102,7 +110,7 @@ def __init__(
self.static_manifest_manifest_branch = static_manifest_manifest_branch
self.c_w_m_r_branch = current_workspace_manifest_repo_branch

def info(self) -> None:
def report(self) -> None:
if self.set_manifest_branch:
self.on_set_branch()
else:
Expand Down Expand Up @@ -133,33 +141,16 @@ def on_set_branch(self) -> None:
self.w_c.save_to_file(self.cfg_path)
"""workspace is now updated"""
self.uip_workspace_updated()
workspace_integration_summary(
workspace_repositories_summary(
self.workspace_root_path,
self.statuses,
self.s_m_m_dest,
self.static_manifest_manifest_branch,
self.w_c.manifest_branch,
True,
do_update=True,
only_manifest=True,
)
if self.s_m_m_dest:
"""when there is Manifest repository in the Workspace"""
if rc_is_on_remote == 0:
if self.c_w_m_r_branch != self.w_c.manifest_branch:
self.uip_after_sync_branch_change()
else:
self.uip_ok_after_sync_same_branch()
else:
self.uip_push_first_for_sync_to_work()
else:
"""use 'manifest_branch_0' to determine if brach will change"""
if self.w_c.manifest_branch != self.w_c.manifest_branch_0:
self.uip_branch_will_change_after_sync(
self.w_c.manifest_branch,
self.w_c.manifest_branch_0,
)
else:
self.uip_branch_will_stay_the_same_after_sync(
self.w_c.manifest_branch
)
self.report_what_wha_sync()
else:
"""branch is nowhere to be found"""
if self.s_m_m_dest:
Expand All @@ -171,8 +162,12 @@ def on_set_branch(self) -> None:

def on_default_display(self) -> None:
"""just report final status of current state, do not update anything"""
if self.s_m_m_dest and self.c_w_m_r_branch != self.w_c.manifest_branch:
self.uip_branch_will_change_after_sync(self.w_c.manifest_branch)
self.report_what_wha_sync()

def report_what_wha_sync(self) -> None:
"""report what will_happen_after sync with Manifest"""
if self.s_m_m_dest:
self.report_iro_m_branch_in_w()
else:
"""use 'manifest_branch_0' to determine if brach will change"""
if self.w_c.manifest_branch != self.w_c.manifest_branch_0:
Expand All @@ -183,11 +178,36 @@ def on_default_display(self) -> None:
else:
self.uip_branch_will_stay_the_same_after_sync(self.w_c.manifest_branch)

def report_iro_m_branch_in_w(self) -> None:
"""report in regards of Manifest branch in Workspace (has change or not)"""
if self.c_w_m_r_branch != self.w_c.manifest_branch:
self.uip_branch_will_change_after_sync(self.w_c.manifest_branch)
else:
deep_m_branch = self.get_w_d_m_branch()
if deep_m_branch:
if deep_m_branch != self.w_c.manifest_branch:
self.uip_branch_will_change_after_sync(self.w_c.manifest_branch)
else:
self.uip_ok_after_sync_same_branch()

def get_w_d_m_branch(self) -> Union[str, None]:
"""get Workspace-deep manifest branch. This means:
Workspace:Manifest repository:Manifest file:Manifest repository:branch"""
if isinstance(self.s_m_m_dest, str):
deep_manifest = load_manifest(
self.workspace_root_path / self.s_m_m_dest / "manifest.yml"
)
return deep_manifest.get_repo(self.s_m_m_dest).branch
else:
return None

"""ui prints|errors segment follows:"""

def uip_skip_set_branch(self) -> None:
ui.info_1("Skipping configuring the same branch")

def uip_using_new_branch(self, branch: str) -> None:
ui.info_2("Using new branch: ", ui.green, branch, ui.reset)
ui.info_2("Using new branch:", ui.green, branch, ui.reset)

def uip_workspace_updated(self) -> None:
ui.info_1("Workspace updated")
Expand All @@ -201,9 +221,30 @@ def uip_after_sync_branch_change(self) -> None:
)

def uip_ok_after_sync_same_branch(self) -> None:
"""check if repository is clean,
and also if remote commit SHA1 is same as local commit SHA1,
as only then we can say for sure, it will stays the same"""
if self.s_m_m_dest:
m_g_status = get_git_status(self.workspace_root_path / self.s_m_m_dest)
if not (
m_g_status.dirty is False # noqa: W503
and m_g_status.ahead == 0 # noqa: W503
and m_g_status.behind == 0 # noqa: W503
and m_g_status.upstreamed is True # noqa: W503
):
ui.info_2("Clean Manifest repository before calling 'sync'")
return
l_m_sha, r_m_sha = get_l_and_r_sha1_of_branch(
self.workspace_root_path,
self.s_m_m_dest,
self.w_c.manifest_branch,
)
if r_m_sha and l_m_sha != r_m_sha:
ui.info_2("Remote branch does not have same HEAD")
return
ui.info_2(
ui.blue,
"OK: After 'sync' the repository will stays on the same branch",
"OK: After 'sync', Manifest repository will stays on the same branch",
)

def uip_push_first_for_sync_to_work(self) -> None:
Expand Down Expand Up @@ -281,38 +322,6 @@ def is_manifest_in_workspace(
return static_manifest_manifest_dest, static_manifest_manifest_branch


def workspace_integration_summary(
statuses: Dict[str, StatusOrError],
st_m_m_dest: Union[str, None],
st_m_m_branch: Union[str, None],
w_c_m_branch: str,
do_update: bool = False,
) -> Union[str, None]:
"""prints a summary of Manifest repository status.
the same output should be used when using 'tsrc status'
but other repositories included"""
cur_w_m_r_branch = None
for dest, status in statuses.items():
if dest == st_m_m_dest:
if do_update:
ui.info_2("Updating configured Manifest branch. See new overall state:")
if isinstance(status, Status):
cur_w_m_r_branch = status.git.branch
message = [ui.green, "*", ui.reset, dest]
message += describe_status(status)
message += [ui.purple, "<---", "MANIFEST:"]
message += [ui.green, st_m_m_branch]
if w_c_m_branch != st_m_m_branch:
message += [
ui.reset,
"~~~>",
ui.green,
w_c_m_branch,
]
ui.info(*message)
return cur_w_m_r_branch


def manifest_remote_branch_exist(url: str, branch: str) -> int:
"""check for manifest remote branch, as only if it exist,
we should allow to set it. as you can see, there is
Expand Down
Loading

0 comments on commit 0f34579

Please sign in to comment.