diff --git a/src/cnaas_nms/db/git.py b/src/cnaas_nms/db/git.py index 775fb4f5..bb0b39be 100644 --- a/src/cnaas_nms/db/git.py +++ b/src/cnaas_nms/db/git.py @@ -3,11 +3,12 @@ import json import os import shutil -from typing import Dict, Optional, Set, Tuple +from typing import Dict, List, Optional, Set, Tuple from urllib.parse import urldefrag import yaml +import git.remote from cnaas_nms.app_settings import app_settings from cnaas_nms.db.device import Device, DeviceType from cnaas_nms.db.exceptions import ConfigException, RepoStructureException @@ -27,6 +28,7 @@ from cnaas_nms.devicehandler.sync_history import add_sync_event from cnaas_nms.scheduler.thread_data import set_thread_data from cnaas_nms.tools.event import add_event +from cnaas_nms.tools.githelpers import parse_git_changed_files from cnaas_nms.tools.log import get_logger from git import InvalidGitRepositoryError, Repo from git.exc import GitCommandError, NoSuchPathError @@ -224,17 +226,9 @@ def _refresh_repo_task(repo_type: RepoType = RepoType.TEMPLATES, job_id: Optiona prev_commit = local_repo.commit().hexsha logger.debug("git pull from {}".format(remote_repo_path)) - diff = local_repo.remotes.origin.pull() - for item in diff: - if item.ref.remote_head != local_repo.head.ref.name: - continue + diff: List[git.remote.FetchInfo] = local_repo.remotes.origin.pull() + ret, changed_files = parse_git_changed_files(diff, prev_commit, local_repo) - ret += "Commit {} by {} at {}\n".format( - item.commit.name_rev, item.commit.committer, item.commit.committed_datetime - ) - diff_files = local_repo.git.diff("{}..{}".format(prev_commit, item.commit.hexsha), name_only=True).split() - changed_files.update(diff_files) - prev_commit = item.commit.hexsha except (InvalidGitRepositoryError, NoSuchPathError): # noqa: S110 logger.info("Local repository {} not found, cloning from remote".format(local_repo_path)) try: diff --git a/src/cnaas_nms/db/git_worktrees.py b/src/cnaas_nms/db/git_worktrees.py index 322ae2e3..1e4e036f 100644 --- a/src/cnaas_nms/db/git_worktrees.py +++ b/src/cnaas_nms/db/git_worktrees.py @@ -8,6 +8,7 @@ from cnaas_nms.db.groups import get_groups_using_branch from cnaas_nms.db.session import sqla_session from cnaas_nms.devicehandler.sync_history import add_sync_event +from cnaas_nms.tools.githelpers import parse_git_changed_files from cnaas_nms.tools.log import get_logger from git import Repo @@ -25,21 +26,16 @@ def refresh_existing_templates_worktrees(job_id: int, group_settings: dict, devi try: logger.info("Pulling worktree for branch {}".format(subdir)) wt_repo = Repo("/tmp/worktrees/" + subdir) + prev_commit = wt_repo.commit().hexsha diff = wt_repo.remotes.origin.pull() if not diff: continue - ret: str = "" - for item in diff: - # only check for changes in our branch - if item.ref.remote_head != wt_repo.head.ref.name: - continue - - ret += "Commit {} by {} at {}\n".format( - item.commit.name_rev, item.commit.committer, item.commit.committed_datetime - ) + ret: str + changed_files: Set[str] + ret, changed_files = parse_git_changed_files(diff, prev_commit, wt_repo) # don't update updated_groups if changes were only in other branches - if not ret: + if not changed_files: continue except Exception as e: logger.exception(e) diff --git a/src/cnaas_nms/tools/githelpers.py b/src/cnaas_nms/tools/githelpers.py new file mode 100644 index 00000000..3e988bb5 --- /dev/null +++ b/src/cnaas_nms/tools/githelpers.py @@ -0,0 +1,20 @@ +from typing import List, Set + +import git.remote +from git import Repo + + +def parse_git_changed_files(diff: List[git.remote.FetchInfo], prev_commit: str, local_repo: Repo) -> (str, Set[str]): + ret_msg = "" + changed_files: Set[str] = set() + for item in diff: + if item.ref.remote_head != local_repo.head.ref.name: + continue + + ret_msg += "Commit {} by {} at {}\n".format( + item.commit.name_rev, item.commit.committer, item.commit.committed_datetime + ) + diff_files = local_repo.git.diff("{}..{}".format(prev_commit, item.commit.hexsha), name_only=True).split() + changed_files.update(diff_files) + prev_commit = item.commit.hexsha + return ret_msg, changed_files