Skip to content

Commit

Permalink
Merge 409bf29 into e2f267b
Browse files Browse the repository at this point in the history
  • Loading branch information
ropapermaker committed Jul 5, 2023
2 parents e2f267b + 409bf29 commit c1bfda5
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
14 changes: 14 additions & 0 deletions pygount/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def __init__(self):
self._has_duplicates = False
self._has_summary = False
self._is_verbose = False
self._is_graph = False
self._names_to_skip = pygount.common.regexes_from(pygount.analysis.DEFAULT_NAME_PATTERNS_TO_SKIP_TEXT)
self._output = _DEFAULT_OUTPUT
self._output_format = _DEFAULT_OUTPUT_FORMAT
Expand Down Expand Up @@ -174,6 +175,13 @@ def is_verbose(self):
def set_is_verbose(self, is_verbose, source=None):
self._is_verbose = bool(is_verbose)

@property
def is_graph(self):
return self._is_graph

def set_is_graph(self, is_graph, source=None):
self._is_graph = bool(is_graph)

@property
def names_to_skip(self):
return self._names_to_skip
Expand Down Expand Up @@ -246,6 +254,11 @@ def argument_parser(self):
default=pygount.analysis.DEFAULT_GENERATED_PATTERNS_TEXT,
help=_HELP_GENERATED,
)
parser.add_argument(
"--graph",
"-G",
help="create a graph image showing how the SLOC changed over time for each git tag",
)
parser.add_argument(
"--names-to-skip",
"-N",
Expand Down Expand Up @@ -313,6 +326,7 @@ def apply_arguments(self, arguments=None):
self.set_generated_regexps(args.generated, "option --generated")
self.set_has_duplicates(args.duplicates, "option --duplicates")
self.set_is_verbose(args.verbose, "option --verbose")
self.set_is_graph(args.graph, "option --graph")
self.set_names_to_skip(args.names_to_skip, "option --folders-to-skip")
self.set_output(args.out, "option --out")
self.set_output_format(args.format, "option --format")
Expand Down
46 changes: 46 additions & 0 deletions pygount/git_graph.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import re
from typing import Optional

from pygount.analysis import SourceScanner
from pygount.git_storage import GitStorage, git_remote_url_and_revision_if_any


def compile_revisions_regex(revisions_regex_str: str) -> re:
# TODO#112: fix regex input
# input needs to be fixed because it does not return any matches
return re.compile(revisions_regex_str)


class GitGraph:
def __init__(self, source_url: str, revisions_regex_str: Optional[str] = None):
self._revisions_regex = compile_revisions_regex(revisions_regex_str)
self._source_url = source_url
self._git_storage = None

def revisions_to_analyze(self, revisions) -> list[str]:
# TODO#112: fix regex input
# input needs to be fixed because it does not return any matches
return (
list(filter(self._revisions_regex.match, map(str, revisions)))
if self._revisions_regex is not None
else revisions
)

def execute(self):
try:
remote_url, revision = git_remote_url_and_revision_if_any(self._source_url)
if remote_url is not None:
self._git_storage = GitStorage(remote_url, revision)
self._git_storage.extract()
self._git_storage.unshallow()
revisions_to_analyze = self.revisions_to_analyze(self._git_storage.revisions())

for revision in revisions_to_analyze:
print(revision)
self._git_storage.checkout(revision)
scanner = SourceScanner([self._git_storage.temp_folder])
print(list(scanner.source_paths()))
except OSError as error:
raise OSError(f"cannot scan for source files: {error}")

self._git_storage.close()
12 changes: 11 additions & 1 deletion pygount/git_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def __init__(self, remote_url: str, revision: Optional[str] = None):
self._remote_url = remote_url
self._revision = revision
self._temp_folder = mkdtemp()
self._repo = None

@property
def temp_folder(self) -> str:
Expand All @@ -34,7 +35,16 @@ def extract(self):
multi_options = ["--depth", "1"]
if self._revision is not None:
multi_options.extend(["--branch", self._revision])
git.Repo.clone_from(self._remote_url, self._temp_folder, multi_options=multi_options)
self._repo = git.Repo.clone_from(self._remote_url, self._temp_folder, multi_options=multi_options)

def unshallow(self):
self._repo.git.pull("--unshallow")

def checkout(self, revision):
self._repo.git.checkout(revision)

def revisions(self) -> list[str]:
return self._repo.tags

def close(self):
shutil.rmtree(self._temp_folder, ignore_errors=True)
6 changes: 6 additions & 0 deletions tests/test_git_storage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pathlib import Path

from pygount.git_graph import GitGraph
from pygount.git_storage import GitStorage, git_remote_url_and_revision_if_any


Expand Down Expand Up @@ -33,3 +34,8 @@ def test_can_extract_and_close_and_find_files_from_cloned_git_remote_url_with_re
finally:
git_storage.close()
assert not readme_path.exists()


def test_can_analyze_multiple_revisions():
git_graph = GitGraph("https://github.com/roskakori/pygount.git", revisions_regex_str="1.6*")
git_graph.execute()

0 comments on commit c1bfda5

Please sign in to comment.