Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get changed files from total diff #67

Merged
merged 1 commit into from
Jan 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 11 additions & 18 deletions ckan_meta_tester/ckan_meta_tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from os import environ
from shutil import copy
import logging
from git import Repo, Commit
from git import Repo, Commit, DiffIndex
from subprocess import run, Popen, PIPE, STDOUT
from pathlib import Path
from importlib.resources import read_text
Expand Down Expand Up @@ -170,7 +170,7 @@ def files_to_test(self, source: Optional[str]) -> Iterable[Path]:
elif source == 'netkans':
return self.netkans()
elif source == 'commits':
return self.paths_from_commits(self.branch_commits(Repo('.')))
return self.paths_from_diff(self.branch_diff(Repo('.')))
else:
raise ValueError(f'Source {source} is not valid, must be netkans or commits')

Expand All @@ -179,8 +179,8 @@ def netkans(self) -> Iterable[Path]:
return (f for f in Path().rglob('*')
if f.is_file() and f.suffix.lower() == '.netkan')

def branch_commits(self, repo: Repo) -> Iterable[Commit]:
return repo.iter_commits(f'{self.get_start_ref()}..{repo.head}')
def branch_diff(self, repo: Repo) -> DiffIndex:
return repo.commit(self.get_start_ref()).diff(repo.head.commit)

def get_start_ref(self, default: str = 'origin/master') -> str:
ref = None
Expand All @@ -195,27 +195,20 @@ def get_start_ref(self, default: str = 'origin/master') -> str:
break
return ref if ref is not None else default

def paths_from_commits(self, commits: Iterable[Commit]) -> Iterable[Path]:
logging.debug('Searching commits for changed files')
all_adds: Set[str] = set()
all_mods: Set[str] = set()
for commit in commits:
added, modified = self.filenames_from_commit(commit)
all_adds |= added
all_mods |= modified
def paths_from_diff(self, diff: DiffIndex) -> Iterable[Path]:
logging.debug('Searching diff for changed files')
all_adds, all_mods = self.filenames_from_diff(diff)
# Existing files probably have valid names, new ones need to be checked
for f in all_adds:
if not self.check_added_path(Path(f)):
self.failed = True
files = all_adds | all_mods
return (Path(f) for f in files if self.netkan_or_ckan(f))

def filenames_from_commit(self, commit: Commit) -> Tuple[Set[str], Set[str]]:
logging.debug('Checking commit %s for changed files', commit.hexsha)
changes = commit.parents[0].diff(commit)
added = {ch.b_path for ch in changes.iter_change_type('A')}
modified = {ch.b_path for ch in changes.iter_change_type('M')}
renamed = {ch.b_path for ch in changes.iter_change_type('R')}
def filenames_from_diff(self, diff: DiffIndex) -> Tuple[Set[str], Set[str]]:
added = {ch.b_path for ch in diff.iter_change_type('A')}
modified = {ch.b_path for ch in diff.iter_change_type('M')}
renamed = {ch.b_path for ch in diff.iter_change_type('R')}
logging.debug('%s added, %s modified, %s renamed',
len(added), len(modified), len(renamed))
return added, modified | renamed
Expand Down