Skip to content

Commit

Permalink
An util function which returns if the current git repo has been modif…
Browse files Browse the repository at this point in the history
…ied (#12)
  • Loading branch information
mikicz committed Feb 18, 2018
1 parent 1af1640 commit 7a98bf2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
11 changes: 10 additions & 1 deletion arca/_arca.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import re
from dogpile.cache import make_region, CacheRegion
from git import Repo
from git import Repo, InvalidGitRepositoryError

from .exceptions import ArcaMisconfigured, FileOutOfRangeError
from .backend import BaseBackend
Expand Down Expand Up @@ -230,6 +230,15 @@ def cache_key(self, repo: str, branch: str, task: Task, git_repo: Repo) -> str:
hash=self.current_git_hash(repo, branch, git_repo),
task=task.serialize())

def is_dirty(self) -> bool:
""" Returns if the repository the code is launched from was modified in any way.
Returns False if not in a repository.
"""
try:
return Repo(".", search_parent_directories=True).is_dirty(untracked_files=True)
except InvalidGitRepositoryError:
return False

def run(self, repo: str, branch: str, task: Task, *,
depth: DepthDefinitionType=None,
shallow_since: ShallowSinceDefinitionType=None,
Expand Down
24 changes: 23 additions & 1 deletion tests/test_arca_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from datetime import datetime, timedelta, date
from git import Repo

from arca import Arca, VenvBackend
from arca import Arca, VenvBackend, CurrentEnvironmentBackend
from arca.exceptions import ArcaMisconfigured, FileOutOfRangeError
from common import BASE_DIR

Expand Down Expand Up @@ -343,3 +343,25 @@ def test_shallow_since_validate(shallow_since, valid):
else:
with pytest.raises(ValueError):
arca.static_filename(git_url, branch, relative_path, shallow_since=shallow_since)


def test_is_dirty():
arca = Arca(backend=CurrentEnvironmentBackend(
verbosity=2,
current_environment_requirements=None,
requirements_strategy="ignore"
), base_dir=BASE_DIR)

if arca.is_dirty():
pytest.skip("Can't test is_dirty method when the current repo is dirty.")

assert not arca.is_dirty()

fl = Path(str(uuid4()))
fl.touch()

assert arca.is_dirty()

fl.unlink()

assert not arca.is_dirty()

0 comments on commit 7a98bf2

Please sign in to comment.