Skip to content

Commit 5d57e19

Browse files
colinjcr4victorjvstme
authored
Update gitignore logic to catch more cases (#2695)
Existing implementation was missing support for a `**` which was pretty critical. This updates the logic to use an existing library which already supports `**`. Co-authored-by: Victor Skvortsov <vds003@gmail.com> Co-authored-by: Jvst Me <git@jvst.me>
1 parent 25b977e commit 5d57e19

File tree

3 files changed

+20
-105
lines changed

3 files changed

+20
-105
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ dependencies = [
3434
"psutil",
3535
"gpuhunt==0.1.6",
3636
"argcomplete>=3.5.0",
37+
"ignore-python>=0.2.0",
3738
]
3839

3940
[project.urls]

src/dstack/_internal/core/models/repos/local.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22
from pathlib import Path
33
from typing import BinaryIO, Optional
44

5+
import ignore
6+
import ignore.overrides
57
from typing_extensions import Literal
68

79
from dstack._internal.core.models.repos.base import BaseRepoInfo, Repo
10+
from dstack._internal.utils.common import sizeof_fmt
811
from dstack._internal.utils.hash import get_sha256, slugify
9-
from dstack._internal.utils.ignore import GitIgnore
12+
from dstack._internal.utils.logging import get_logger
1013
from dstack._internal.utils.path import PathLike
1114

15+
logger = get_logger(__name__)
16+
1217

1318
class LocalRepoInfo(BaseRepoInfo):
1419
repo_type: Literal["local"] = "local"
@@ -69,22 +74,23 @@ def __init__(
6974
self.run_repo_data = repo_data
7075

7176
def write_code_file(self, fp: BinaryIO) -> str:
77+
repo_path = Path(self.run_repo_data.repo_dir)
7278
with tarfile.TarFile(mode="w", fileobj=fp) as t:
73-
t.add(
74-
self.run_repo_data.repo_dir,
75-
arcname="",
76-
filter=TarIgnore(self.run_repo_data.repo_dir, globs=[".git"]),
77-
)
79+
for entry in (
80+
ignore.WalkBuilder(repo_path)
81+
.overrides(ignore.overrides.OverrideBuilder(repo_path).add("!/.git/").build())
82+
.hidden(False) # do not ignore files that start with a dot
83+
.require_git(False) # respect git ignore rules even if not a git repo
84+
.add_custom_ignore_filename(".dstackignore")
85+
.build()
86+
):
87+
path = entry.path().relative_to(repo_path.absolute())
88+
if path != Path("."):
89+
t.add(path, recursive=False)
90+
logger.debug("Code file size: %s", sizeof_fmt(fp.tell()))
7891
return get_sha256(fp)
7992

8093
def get_repo_info(self) -> LocalRepoInfo:
8194
return LocalRepoInfo(
8295
repo_dir=self.run_repo_data.repo_dir,
8396
)
84-
85-
86-
class TarIgnore(GitIgnore):
87-
def __call__(self, tarinfo: tarfile.TarInfo) -> Optional[tarfile.TarInfo]:
88-
if self.ignore(tarinfo.path):
89-
return None
90-
return tarinfo

src/dstack/_internal/utils/ignore.py

Lines changed: 0 additions & 92 deletions
This file was deleted.

0 commit comments

Comments
 (0)