Skip to content

Commit

Permalink
break out parsing of what files in git changed into separate file and
Browse files Browse the repository at this point in the history
use it from git.py and git_worktrees.py for when worktree branches get updated
  • Loading branch information
indy-independence committed Oct 28, 2024
1 parent a810f92 commit 6e3d9ff
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
16 changes: 5 additions & 11 deletions src/cnaas_nms/db/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down
16 changes: 6 additions & 10 deletions src/cnaas_nms/db/git_worktrees.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions src/cnaas_nms/tools/githelpers.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 6e3d9ff

Please sign in to comment.