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
Auto track binary files #828
Changes from all commits
a4e9b19
35b480c
57f8613
b8bbe4f
17a1331
5f48d6c
6d27a15
9fe6bb6
cbfdce5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -1128,6 +1128,30 @@ def test_auto_track_large_files(self): | |||||||||||||||||||||||||||||||||||||||
is_tracked_with_lfs(os.path.join(WORKING_REPO_DIR, "small_file.txt")) | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
def test_auto_track_binary_files(self): | ||||||||||||||||||||||||||||||||||||||||
repo = Repository(WORKING_REPO_DIR) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
# This content is non-binary | ||||||||||||||||||||||||||||||||||||||||
non_binary_file = [100] * int(1e6) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
# This content is binary (contains the null character) | ||||||||||||||||||||||||||||||||||||||||
binary_file = "\x00\x00\x00\x00" | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
with open(f"{WORKING_REPO_DIR}/non_binary_file.txt", "w+") as f: | ||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we shouldn't write on the working dir, we should work with python's temp files and folders. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The huggingface_hub/tests/test_repository.py Lines 76 to 91 in 6d27a15
This isn't the working directory in which the test is run, but a folder nested in the huggingface_hub/tests/test_repository.py Lines 50 to 52 in 6d27a15
|
||||||||||||||||||||||||||||||||||||||||
f.write(json.dumps(non_binary_file)) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
with open(f"{WORKING_REPO_DIR}/binary_file.txt", "w+") as f: | ||||||||||||||||||||||||||||||||||||||||
f.write(binary_file) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
repo.auto_track_binary_files() | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
self.assertFalse( | ||||||||||||||||||||||||||||||||||||||||
is_tracked_with_lfs(os.path.join(WORKING_REPO_DIR, "non_binary)file.txt")) | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
self.assertTrue( | ||||||||||||||||||||||||||||||||||||||||
is_tracked_with_lfs(os.path.join(WORKING_REPO_DIR, "binary_file.txt")) | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
def test_auto_track_large_files_ignored_with_gitignore(self): | ||||||||||||||||||||||||||||||||||||||||
repo = Repository(WORKING_REPO_DIR) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
|
@@ -1157,6 +1181,7 @@ def test_auto_track_large_files_ignored_with_gitignore(self): | |||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
repo.auto_track_large_files() | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
# Large files | ||||||||||||||||||||||||||||||||||||||||
self.assertFalse( | ||||||||||||||||||||||||||||||||||||||||
is_tracked_with_lfs(os.path.join(WORKING_REPO_DIR, "large_file.txt")) | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
|
@@ -1175,6 +1200,54 @@ def test_auto_track_large_files_ignored_with_gitignore(self): | |||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
def test_auto_track_binary_files_ignored_with_gitignore(self): | ||||||||||||||||||||||||||||||||||||||||
repo = Repository(WORKING_REPO_DIR) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
# This content is binary (contains the null character) | ||||||||||||||||||||||||||||||||||||||||
binary_file = "\x00\x00\x00\x00" | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
# Test nested gitignores | ||||||||||||||||||||||||||||||||||||||||
os.makedirs(f"{WORKING_REPO_DIR}/directory") | ||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same answer as above :) |
||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
with open(f"{WORKING_REPO_DIR}/.gitignore", "w+") as f: | ||||||||||||||||||||||||||||||||||||||||
f.write("binary_file.txt") | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
with open(f"{WORKING_REPO_DIR}/directory/.gitignore", "w+") as f: | ||||||||||||||||||||||||||||||||||||||||
f.write("binary_file_3.txt") | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
with open(f"{WORKING_REPO_DIR}/binary_file.txt", "w+") as f: | ||||||||||||||||||||||||||||||||||||||||
f.write(binary_file) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
with open(f"{WORKING_REPO_DIR}/binary_file_2.txt", "w+") as f: | ||||||||||||||||||||||||||||||||||||||||
f.write(binary_file) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
with open(f"{WORKING_REPO_DIR}/directory/binary_file_3.txt", "w+") as f: | ||||||||||||||||||||||||||||||||||||||||
f.write(binary_file) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
with open(f"{WORKING_REPO_DIR}/directory/binary_file_4.txt", "w+") as f: | ||||||||||||||||||||||||||||||||||||||||
f.write(binary_file) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
repo.auto_track_binary_files() | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
# Binary files | ||||||||||||||||||||||||||||||||||||||||
self.assertFalse( | ||||||||||||||||||||||||||||||||||||||||
is_tracked_with_lfs(os.path.join(WORKING_REPO_DIR, "binary_file.txt")) | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
self.assertTrue( | ||||||||||||||||||||||||||||||||||||||||
is_tracked_with_lfs(os.path.join(WORKING_REPO_DIR, "binary_file_2.txt")) | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
self.assertFalse( | ||||||||||||||||||||||||||||||||||||||||
is_tracked_with_lfs( | ||||||||||||||||||||||||||||||||||||||||
os.path.join(WORKING_REPO_DIR, "directory/binary_file_3.txt") | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
self.assertTrue( | ||||||||||||||||||||||||||||||||||||||||
is_tracked_with_lfs( | ||||||||||||||||||||||||||||||||||||||||
os.path.join(WORKING_REPO_DIR, "directory/binary_file_4.txt") | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
def test_auto_track_large_files_through_git_add(self): | ||||||||||||||||||||||||||||||||||||||||
repo = Repository(WORKING_REPO_DIR) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
|
@@ -1199,6 +1272,30 @@ def test_auto_track_large_files_through_git_add(self): | |||||||||||||||||||||||||||||||||||||||
is_tracked_with_lfs(os.path.join(WORKING_REPO_DIR, "small_file.txt")) | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
def test_auto_track_binary_files_through_git_add(self): | ||||||||||||||||||||||||||||||||||||||||
repo = Repository(WORKING_REPO_DIR) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
# This content is non binary | ||||||||||||||||||||||||||||||||||||||||
non_binary_file = [100] * int(1e6) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
# This content is binary (contains the null character) | ||||||||||||||||||||||||||||||||||||||||
binary_file = "\x00\x00\x00\x00" | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
with open(f"{WORKING_REPO_DIR}/small_file.txt", "w+") as f: | ||||||||||||||||||||||||||||||||||||||||
f.write(json.dumps(non_binary_file)) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
with open(f"{WORKING_REPO_DIR}/binary_file.txt", "w+") as f: | ||||||||||||||||||||||||||||||||||||||||
f.write(binary_file) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
repo.git_add(auto_lfs_track=True) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
self.assertFalse( | ||||||||||||||||||||||||||||||||||||||||
is_tracked_with_lfs(os.path.join(WORKING_REPO_DIR, "non_binary_file.txt")) | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
self.assertTrue( | ||||||||||||||||||||||||||||||||||||||||
is_tracked_with_lfs(os.path.join(WORKING_REPO_DIR, "binary_file.txt")) | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
def test_auto_no_track_large_files_through_git_add(self): | ||||||||||||||||||||||||||||||||||||||||
repo = Repository(WORKING_REPO_DIR) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
|
@@ -1223,6 +1320,30 @@ def test_auto_no_track_large_files_through_git_add(self): | |||||||||||||||||||||||||||||||||||||||
is_tracked_with_lfs(os.path.join(WORKING_REPO_DIR, "small_file.txt")) | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
def test_auto_no_track_binary_files_through_git_add(self): | ||||||||||||||||||||||||||||||||||||||||
repo = Repository(WORKING_REPO_DIR) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
# This content is non-binary | ||||||||||||||||||||||||||||||||||||||||
non_binary_file = [100] * int(1e6) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
# This content is binary (contains the null character) | ||||||||||||||||||||||||||||||||||||||||
binary_file = "\x00\x00\x00\x00" | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
with open(f"{WORKING_REPO_DIR}/small_file.txt", "w+") as f: | ||||||||||||||||||||||||||||||||||||||||
f.write(json.dumps(non_binary_file)) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
with open(f"{WORKING_REPO_DIR}/binary_file.txt", "w+") as f: | ||||||||||||||||||||||||||||||||||||||||
f.write(binary_file) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
repo.git_add(auto_lfs_track=False) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
self.assertFalse( | ||||||||||||||||||||||||||||||||||||||||
is_tracked_with_lfs(os.path.join(WORKING_REPO_DIR, "non_binary_file.txt")) | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
self.assertFalse( | ||||||||||||||||||||||||||||||||||||||||
is_tracked_with_lfs(os.path.join(WORKING_REPO_DIR, "binary_file.txt")) | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
def test_auto_track_updates_removed_gitattributes(self): | ||||||||||||||||||||||||||||||||||||||||
repo = Repository(WORKING_REPO_DIR) | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other than reading the first few bytes, I think this answer gives a more accurate way of checking if the file is binary or not: https://stackoverflow.com/a/7392391/2536294
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I implemented what you propose in 9fe6bb6, let me know if that's what you had in mind. Cc @coyotte508