Skip to content

Commit

Permalink
Merge pull request #54 from datarootsio/repo-found-from-paths
Browse files Browse the repository at this point in the history
Repo found from paths
  • Loading branch information
murilo-cunha authored Nov 8, 2022
2 parents 01b9a54 + c125104 commit 512bfd9
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 8 deletions.
4 changes: 4 additions & 0 deletions databooks/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,10 @@ def fix(
[git docs](https://git-scm.com/docs/git-ls-files).
"""
filepaths = expand_paths(paths=paths, ignore=ignore)
if filepaths is None:
raise RuntimeError(
f"Expected `filepaths` to be list of paths, got {filepaths}."
)
conflict_files = path2conflicts(nb_paths=filepaths)
if not conflict_files:
raise BadParameter(
Expand Down
7 changes: 5 additions & 2 deletions databooks/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

def expand_paths(
paths: List[Path], *, ignore: Sequence[str] = ("!*",), rglob: str = "*.ipynb"
) -> List[Path]:
) -> Optional[List[Path]]:
"""
Get paths of existing file from list of directory or file paths.
Expand All @@ -20,13 +20,16 @@ def expand_paths(
existing file paths (i.e.: to retrieve only notebooks)
:return: List of existing file paths
"""
if not paths:
return None
filepaths = set(
chain.from_iterable(
list(path.resolve().rglob(rglob)) if path.is_dir() else [path]
for path in paths
)
)
ignored = set(chain.from_iterable(Path.cwd().rglob(i) for i in ignore))
common_path = find_common_parent(paths=paths)
ignored = set(chain.from_iterable(common_path.rglob(i) for i in ignore))
ignored = {p.resolve() for p in ignored}
logger.debug(
f"{len(ignored)} files will be ignored from {len(filepaths)} file paths."
Expand Down
3 changes: 2 additions & 1 deletion databooks/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
def get_config(target_paths: List[Path], config_filename: str) -> Optional[Path]:
"""Find configuration file from CLI target paths."""
common_path = find_common_parent(paths=target_paths)
repo_dir = get_repo().working_dir
repo = get_repo(common_path)
repo_dir = getattr(repo, "working_dir", None)

return find_obj(
obj_name=config_filename,
Expand Down
2 changes: 2 additions & 0 deletions databooks/conflicts.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def path2conflicts(
)
common_parent = find_common_parent(nb_paths)
repo = get_repo(common_parent) if repo is None else repo
if repo is None:
raise ValueError("No repo found - cannot compute conflict blobs.")
return [
file
for file in get_conflict_blobs(repo=repo)
Expand Down
14 changes: 9 additions & 5 deletions databooks/git_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,18 @@ def diff2contents(
return blob2str(blob)


def get_repo(path: Path = Path.cwd()) -> Repo:
def get_repo(path: Path) -> Optional[Repo]:
"""Find git repo in current or parent directories."""
repo_dir = find_obj(
obj_name=".git", start=Path(path.anchor), finish=path, is_dir=True
)
repo = Repo(path=repo_dir)
logger.debug(f"Repo found at: {repo.working_dir}")
return repo
if repo_dir is not None:
repo = Repo(path=repo_dir)
logger.debug(f"Repo found at: {repo.working_dir}.")
return repo
else:
logger.debug(f"No repo found at {path}.")
return None


def get_conflict_blobs(repo: Repo) -> List[ConflictFile]:
Expand Down Expand Up @@ -164,7 +168,7 @@ def get_nb_diffs(

common_path = find_common_parent(paths or [Path.cwd()])
repo = get_repo(path=common_path) if repo is None else repo
if repo.working_dir is None:
if repo is None or repo.working_dir is None:
raise ValueError("No repo found - cannot compute diffs.")

ref_base = repo.index if ref_base is None else repo.tree(ref_base)
Expand Down
5 changes: 5 additions & 0 deletions tests/test_git_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ def test_get_repo() -> None:
assert Path(repo.working_dir).stem == "databooks"


def test_get_repo_missing(tmp_path: Path) -> None:
"""Return `None` if there is no repo."""
assert get_repo(tmp_path) is None


def test_get_conflict_blobs(tmp_path: Path) -> None:
"""Return `databooks.git_utils.ConflctFile` from git merge conflict."""
filepath = Path("hello.txt")
Expand Down

0 comments on commit 512bfd9

Please sign in to comment.