Skip to content
Merged
Show file tree
Hide file tree
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
18 changes: 11 additions & 7 deletions dfetch/project/superproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from dfetch.project.git import GitSubProject
from dfetch.project.subproject import SubProject
from dfetch.project.svn import SvnSubProject
from dfetch.util.util import resolve_absolute_path
from dfetch.vcs.git import GitLocalRepo
from dfetch.vcs.svn import SvnRepo

Expand All @@ -40,10 +41,12 @@ def __init__(self) -> None:

logger.debug(f"Using manifest {manifest_path}")
self._manifest = parse(manifest_path)
self._root_directory = os.path.dirname(self._manifest.path)
self._root_directory = resolve_absolute_path(
os.path.dirname(self._manifest.path)
)

@property
def root_directory(self) -> str:
def root_directory(self) -> pathlib.Path:
"""Return the directory that contains the manifest file."""
return self._root_directory

Expand All @@ -63,11 +66,12 @@ def get_sub_project(self, project: ProjectEntry) -> SubProject | None:

def ignored_files(self, path: str) -> Sequence[str]:
"""Return a list of files that can be ignored in a given path."""
if (
os.path.commonprefix((pathlib.Path(path).resolve(), self.root_directory))
!= self.root_directory
):
raise RuntimeError(f"{path} not in superproject {self.root_directory}!")
resolved_path = resolve_absolute_path(path)

if not resolved_path.is_relative_to(self.root_directory):
raise RuntimeError(
f"{resolved_path} not in superproject {self.root_directory}!"
)

if GitLocalRepo(self.root_directory).is_git():
return GitLocalRepo.ignored_files(path)
Expand Down
17 changes: 15 additions & 2 deletions dfetch/util/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ def safe_rmtree(path: str) -> None:


@contextmanager
def in_directory(path: str) -> Generator[str, None, None]:
def in_directory(path: Union[str, Path]) -> Generator[str, None, None]:
"""Work temporarily in a given directory."""
pwd = os.getcwd()
if not os.path.isdir(path):
path = os.path.dirname(path)
os.chdir(path)
try:
yield path
yield str(path)
finally:
os.chdir(pwd)

Expand Down Expand Up @@ -159,3 +159,16 @@ def str_if_possible(data: list[str]) -> Union[str, list[str]]:
if the list is empty, otherwise the original list.
"""
return "" if not data else data[0] if len(data) == 1 else data


def resolve_absolute_path(path: Union[str, Path]) -> Path:
"""Return a guaranteed absolute Path, resolving symlinks.

Args:
path: A string or Path to resolve.

Notes:
- Uses os.path.realpath for reliable absolute paths across platforms.
- Handles Windows drive-relative paths and expands '~'.
"""
return Path(os.path.realpath(Path(path).expanduser()))
6 changes: 3 additions & 3 deletions dfetch/vcs/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import tempfile
from collections.abc import Generator, Sequence
from pathlib import Path, PurePath
from typing import NamedTuple, Optional
from typing import NamedTuple, Optional, Union

from dfetch.log import get_logger
from dfetch.util.cmdline import SubprocessCommandError, run_on_cmdline
Expand Down Expand Up @@ -234,9 +234,9 @@ class GitLocalRepo:

METADATA_DIR = ".git"

def __init__(self, path: str = ".") -> None:
def __init__(self, path: Union[str, Path] = ".") -> None:
"""Create a local git repo."""
self._path = path
self._path = str(path)

def is_git(self) -> bool:
"""Check if is git."""
Expand Down
6 changes: 3 additions & 3 deletions dfetch/vcs/svn.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pathlib
import re
from collections.abc import Sequence
from typing import NamedTuple, Optional
from typing import NamedTuple, Optional, Union

from dfetch.log import get_logger
from dfetch.util.cmdline import SubprocessCommandError, run_on_cmdline
Expand Down Expand Up @@ -76,10 +76,10 @@ class SvnRepo:

def __init__(
self,
path: str = ".",
path: Union[str, pathlib.Path] = ".",
) -> None:
"""Create a svn repo."""
self._path = path
self._path = str(path)

def is_svn(self) -> bool:
"""Check if is SVN."""
Expand Down
3 changes: 2 additions & 1 deletion tests/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# flake8: noqa

import argparse
from pathlib import Path
from unittest.mock import Mock, patch

import pytest
Expand All @@ -30,7 +31,7 @@ def test_check(name, projects):

fake_superproject = Mock()
fake_superproject.manifest = mock_manifest(projects)
fake_superproject.root_directory = "/tmp"
fake_superproject.root_directory = Path("/tmp")

with patch("dfetch.commands.check.SuperProject", return_value=fake_superproject):
with patch(
Expand Down
3 changes: 2 additions & 1 deletion tests/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# flake8: noqa

import argparse
from pathlib import Path
from unittest.mock import Mock, patch

import pytest
Expand All @@ -30,7 +31,7 @@ def test_report(name, projects):

fake_superproject = Mock()
fake_superproject.manifest = mock_manifest(projects)
fake_superproject.root_directory = "/tmp"
fake_superproject.root_directory = Path("/tmp")

with patch("dfetch.commands.report.SuperProject", return_value=fake_superproject):
with patch("dfetch.log.DLogger.print_info_line") as mocked_print_info_line:
Expand Down
5 changes: 3 additions & 2 deletions tests/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# flake8: noqa

import argparse
from pathlib import Path
from unittest.mock import Mock, patch

import pytest
Expand All @@ -30,7 +31,7 @@ def test_update(name, projects):

fake_superproject = Mock()
fake_superproject.manifest = mock_manifest(projects)
fake_superproject.root_directory = "/tmp"
fake_superproject.root_directory = Path("/tmp")

with patch("dfetch.commands.update.SuperProject", return_value=fake_superproject):
with patch(
Expand All @@ -53,7 +54,7 @@ def test_forced_update():

fake_superproject = Mock()
fake_superproject.manifest = mock_manifest([{"name": "some_project"}])
fake_superproject.root_directory = "/tmp"
fake_superproject.root_directory = Path("/tmp")
fake_superproject.ignored_files.return_value = []

with patch("dfetch.commands.update.SuperProject", return_value=fake_superproject):
Expand Down
Loading