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

Respect .gitignore file in commits #1868

Merged
merged 5 commits into from
Nov 28, 2023
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
Prev Previous commit
Next Next commit
add tests
  • Loading branch information
Wauplin committed Nov 27, 2023
commit 53cd33947a64a9590a37fd20cb999b7747a939ef
2 changes: 2 additions & 0 deletions src/huggingface_hub/hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4026,6 +4026,8 @@ def preupload_lfs_files(
f"Skipped upload for {len(new_lfs_additions) - len(new_lfs_additions_to_upload)} LFS file(s) "
"(ignored by gitignore file)."
)
else:
new_lfs_additions_to_upload = new_lfs_additions

# Upload new LFS files
_upload_lfs_files(
Expand Down
33 changes: 33 additions & 0 deletions tests/test_hf_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,39 @@ def _create_file(*parts) -> None:
{".gitattributes", ".git_something/file.txt", "file.git", "temp", "nested/file.bin"},
)

@use_tmp_repo()
def test_upload_folder_gitignore_already_exists(self, repo_url: RepoUrl) -> None:
# Ignore nested folder
self._api.upload_file(path_or_fileobj=b"nested/*\n", path_in_repo=".gitignore", repo_id=repo_url.repo_id)

# Upload folder
self._api.upload_folder(folder_path=self.tmp_dir, repo_id=repo_url.repo_id)

# Check nested file not uploaded
assert not self._api.file_exists(repo_url.repo_id, "nested/file.bin")

@use_tmp_repo()
def test_upload_folder_gitignore_in_commit(self, repo_url: RepoUrl) -> None:
# Create .gitignore file locally
(Path(self.tmp_dir) / ".gitignore").write_text("nested/*\n")

# Upload folder
self._api.upload_folder(folder_path=self.tmp_dir, repo_id=repo_url.repo_id)

# Check nested file not uploaded
assert not self._api.file_exists(repo_url.repo_id, "nested/file.bin")

@use_tmp_repo()
def test_upload_folder_ignore_gitignore(self, repo_url: RepoUrl) -> None:
# Create .gitignore file locally
(Path(self.tmp_dir) / ".gitignore").write_text("nested/*\n")

# Upload folder with `respect_gitignore=False`
self._api.upload_folder(folder_path=self.tmp_dir, repo_id=repo_url.repo_id, respect_gitignore=False)

# Check nested file is uploaded
assert self._api.file_exists(repo_url.repo_id, "nested/file.bin")

def test_create_commit_create_pr(self):
REPO_NAME = repo_name("create_commit_create_pr")
self._api.create_repo(repo_id=REPO_NAME, exist_ok=False)
Expand Down