Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filename option to lfs_track #212

Merged
merged 2 commits into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 7 additions & 2 deletions src/huggingface_hub/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,16 +438,21 @@ def list_deleted_files(self) -> List[str]:

return deleted_files

def lfs_track(self, patterns: Union[str, List[str]]):
def lfs_track(self, patterns: Union[str, List[str]], filename: bool = False):
"""
Tell git-lfs to track those files.

Setting the `filename` argument to `True` will treat the arguments as literal filenames,
not as patterns. Any special glob characters in the filename will be escaped when
writing the .gitattributes file.
LysandreJik marked this conversation as resolved.
Show resolved Hide resolved
"""
if isinstance(patterns, str):
patterns = [patterns]
try:
for pattern in patterns:
cmd = f"git lfs track {'--filename' if filename else ''} {pattern}"
subprocess.run(
["git", "lfs", "track", pattern],
cmd.split(),
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
check=True,
Expand Down
24 changes: 24 additions & 0 deletions tests/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,30 @@ def test_init_clone_in_empty_folder(self):

self.assertIn("random_file.txt", os.listdir(WORKING_REPO_DIR))

def test_git_lfs_filename(self):
os.mkdir(WORKING_REPO_DIR)
subprocess.run(
["git", "init"],
stderr=subprocess.PIPE,
stdout=subprocess.PIPE,
check=True,
cwd=WORKING_REPO_DIR,
)

repo = Repository(WORKING_REPO_DIR)

large_file = [100] * int(4e6)
with open(os.path.join(WORKING_REPO_DIR, "[].txt"), "w") as f:
f.write(json.dumps(large_file))

repo.git_add()

repo.lfs_track(["[].txt"])
self.assertFalse(is_tracked_with_lfs(f"{WORKING_REPO_DIR}/[].txt"))

repo.lfs_track(["[].txt"], filename=True)
self.assertTrue(is_tracked_with_lfs(f"{WORKING_REPO_DIR}/[].txt"))

def test_init_clone_in_nonempty_folder(self):
# Create dummy files
# one is lfs-tracked, the other is not.
Expand Down