-
Notifications
You must be signed in to change notification settings - Fork 525
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 3 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 | ||||
---|---|---|---|---|---|---|
|
@@ -226,6 +226,27 @@ def is_git_ignored(filename: Union[str, Path]) -> bool: | |||||
return is_ignored | ||||||
|
||||||
|
||||||
def is_binary_file(filename: Union[str, Path]) -> bool: | ||||||
""" | ||||||
Check if file is a binary file. | ||||||
|
||||||
Args: | ||||||
filename (`str` or `Path`): | ||||||
The filename to check. | ||||||
|
||||||
Returns: | ||||||
`bool`: `True` if the file passed is a binary file, `False` otherwise. | ||||||
""" | ||||||
try: | ||||||
with open(filename) as f: | ||||||
content = f.read() | ||||||
LysandreJik marked this conversation as resolved.
Show resolved
Hide resolved
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.
Suggested change
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. This was discussed above here: #828 (comment) Do you disagree with the conclusion? 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. Didn't see that. Yeah I don't think we should ever read 11GB of data into memory. This will most certainly crash most people's systems. I'd be happy if we read like 10MB instead of 1MB to reduce the probability of a false detection, which should address those concerns. If we really do want to read a lot more, we should read in chunks. As python's docs state:
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 solution currently implemented loads a maximum of 10MB in memory when calling When tracking binary files, it looks at files which are not yet tracked with lfs, eliminating large files. Instead of the 1MB limit that you propose here, we could instead put a max of 10MB here, which will only be triggered when 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. yes, but you also want to have this method public, which means people can call it before having tracked large files. I'm happy with your suggestion of 10MB. 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. Sounds good, thanks for your review. Addressed in cbfdce5 |
||||||
|
||||||
# Check for the presence of the null character in the string | ||||||
return "\x00" in content | ||||||
except UnicodeDecodeError: | ||||||
return True | ||||||
|
||||||
|
||||||
def files_to_be_staged(pattern: str, folder: Union[str, Path]) -> List[str]: | ||||||
""" | ||||||
Returns a list of filenames that are to be staged. | ||||||
|
@@ -485,8 +506,8 @@ def __init__( | |||||
skip_lfs_files (`bool`, *optional*, defaults to `False`): | ||||||
whether to skip git-LFS files or not. | ||||||
client (`HfApi`, *optional*): | ||||||
Instance of HfApi to use when calling the HF Hub API. | ||||||
A new instance will be created if this is left to `None`. | ||||||
Instance of HfApi to use when calling the HF Hub API. A new | ||||||
instance will be created if this is left to `None`. | ||||||
""" | ||||||
|
||||||
os.makedirs(local_dir, exist_ok=True) | ||||||
|
@@ -981,6 +1002,42 @@ def lfs_enable_largefiles(self): | |||||
except subprocess.CalledProcessError as exc: | ||||||
raise EnvironmentError(exc.stderr) | ||||||
|
||||||
def auto_track_binary_files(self, pattern: Optional[str] = ".") -> List[str]: | ||||||
adrinjalali marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
""" | ||||||
Automatically track binary files with git-lfs. | ||||||
|
||||||
Args: | ||||||
pattern (`str`, *optional*, defaults to "."): | ||||||
The pattern with which to track files that are above 10MBs. | ||||||
LysandreJik marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
Returns: | ||||||
`List[str]`: List of filenames that are now tracked due to being | ||||||
binary files | ||||||
""" | ||||||
files_to_be_tracked_with_lfs = [] | ||||||
|
||||||
deleted_files = self.list_deleted_files() | ||||||
|
||||||
for filename in files_to_be_staged(pattern, folder=self.local_dir): | ||||||
if filename in deleted_files: | ||||||
continue | ||||||
|
||||||
path_to_file = os.path.join(os.getcwd(), self.local_dir, filename) | ||||||
is_binary = is_binary_file(path_to_file) | ||||||
|
||||||
if ( | ||||||
is_binary | ||||||
and not is_tracked_with_lfs(path_to_file) | ||||||
and not is_git_ignored(path_to_file) | ||||||
): | ||||||
self.lfs_track(filename) | ||||||
files_to_be_tracked_with_lfs.append(filename) | ||||||
|
||||||
# Cleanup the .gitattributes if files were deleted | ||||||
self.lfs_untrack(deleted_files) | ||||||
|
||||||
return files_to_be_tracked_with_lfs | ||||||
|
||||||
def auto_track_large_files(self, pattern: Optional[str] = ".") -> List[str]: | ||||||
""" | ||||||
Automatically track large files (files that weigh more than 10MBs) with | ||||||
|
@@ -1090,11 +1147,15 @@ def git_add( | |||||
pattern (`str`, *optional*, defaults to "."): | ||||||
The pattern with which to add files to staging. | ||||||
auto_lfs_track (`bool`, *optional*, defaults to `False`): | ||||||
Whether to automatically track large files with git-lfs. Any | ||||||
file over 10MB in size will be automatically tracked. | ||||||
Whether to automatically track large and binaryfiles with | ||||||
LysandreJik marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
git-lfs. Any file over 10MB in size, or in binary format, will | ||||||
be automatically tracked. | ||||||
""" | ||||||
if auto_lfs_track: | ||||||
tracked_files = self.auto_track_large_files(pattern) | ||||||
tracked_files = [ | ||||||
*self.auto_track_large_files(pattern), | ||||||
*self.auto_track_binary_files(pattern), | ||||||
] | ||||||
if tracked_files: | ||||||
logger.warning( | ||||||
f"Adding files tracked by Git LFS: {tracked_files}. This may take a bit of time if the files are large." | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1075,12 +1075,18 @@ def test_is_tracked_with_lfs_with_pattern(self): | |
# This content is 20MB (over 10MB) | ||
large_file = [100] * int(4e6) | ||
|
||
# This content is binary (contains the null character) | ||
binary_file = "\x00\x00\x00\x00" | ||
|
||
with open(f"{WORKING_REPO_DIR}/large_file.txt", "w+") as f: | ||
f.write(json.dumps(large_file)) | ||
|
||
with open(f"{WORKING_REPO_DIR}/small_file.txt", "w+") as f: | ||
f.write(json.dumps(small_file)) | ||
|
||
with open(f"{WORKING_REPO_DIR}/binary_file.txt", "w+") as f: | ||
f.write(binary_file) | ||
|
||
os.makedirs(f"{WORKING_REPO_DIR}/dir", exist_ok=True) | ||
|
||
with open(f"{WORKING_REPO_DIR}/dir/large_file.txt", "w+") as f: | ||
|
@@ -1089,20 +1095,30 @@ def test_is_tracked_with_lfs_with_pattern(self): | |
with open(f"{WORKING_REPO_DIR}/dir/small_file.txt", "w+") as f: | ||
f.write(json.dumps(small_file)) | ||
|
||
with open(f"{WORKING_REPO_DIR}/dir/binary_file.txt", "w+") as f: | ||
f.write(binary_file) | ||
|
||
repo.auto_track_large_files("dir") | ||
repo.auto_track_binary_files("dir") | ||
|
||
self.assertFalse( | ||
is_tracked_with_lfs(os.path.join(WORKING_REPO_DIR, "large_file.txt")) | ||
) | ||
self.assertFalse( | ||
is_tracked_with_lfs(os.path.join(WORKING_REPO_DIR, "small_file.txt")) | ||
) | ||
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, "dir/large_file.txt")) | ||
) | ||
self.assertFalse( | ||
is_tracked_with_lfs(os.path.join(WORKING_REPO_DIR, "dir/small_file.txt")) | ||
) | ||
self.assertTrue( | ||
is_tracked_with_lfs(os.path.join(WORKING_REPO_DIR, "dir/binary_file.txt")) | ||
) | ||
|
||
def test_auto_track_large_files(self): | ||
repo = Repository(WORKING_REPO_DIR) | ||
|
@@ -1113,35 +1129,48 @@ def test_auto_track_large_files(self): | |
# This content is 20MB (over 10MB) | ||
large_file = [100] * int(4e6) | ||
|
||
# This content is binary (contains the null character) | ||
binary_file = "\x00\x00\x00\x00" | ||
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. I would consider moving this to another test to keep this test focused in large files (or rename this test, although I think different tests would be better) 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. Right, what I was mentioning in the description is that this effectively doubles the time spent on these tests, which is already significant. We're currently sitting at 10+ minutes for the tests, which isn't ideal. I'm open to moving it around, but would like to emphasize that it will likely end up taking even longer. Let me know if this compromise is okay for you and I'll move it to its own separate test. 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. Good point! Let's keep as is then 😄 |
||
|
||
with open(f"{WORKING_REPO_DIR}/large_file.txt", "w+") as f: | ||
f.write(json.dumps(large_file)) | ||
|
||
with open(f"{WORKING_REPO_DIR}/small_file.txt", "w+") as f: | ||
f.write(json.dumps(small_file)) | ||
|
||
with open(f"{WORKING_REPO_DIR}/binary_file.txt", "w+") as f: | ||
f.write(binary_file) | ||
|
||
repo.auto_track_large_files() | ||
repo.auto_track_binary_files() | ||
|
||
self.assertTrue( | ||
is_tracked_with_lfs(os.path.join(WORKING_REPO_DIR, "large_file.txt")) | ||
) | ||
self.assertFalse( | ||
is_tracked_with_lfs(os.path.join(WORKING_REPO_DIR, "small_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) | ||
|
||
# This content is 20MB (over 10MB) | ||
large_file = [100] * int(4e6) | ||
|
||
# This content is binary (contains the null character) | ||
binary_file = "\x00\x00\x00\x00" | ||
|
||
# Test nested gitignores | ||
os.makedirs(f"{WORKING_REPO_DIR}/directory") | ||
|
||
with open(f"{WORKING_REPO_DIR}/.gitignore", "w+") as f: | ||
f.write("large_file.txt") | ||
f.write("large_file.txt\nbinary_file.txt") | ||
|
||
with open(f"{WORKING_REPO_DIR}/directory/.gitignore", "w+") as f: | ||
f.write("large_file_3.txt") | ||
f.write("large_file_3.txt\nbinary_file_3.txt") | ||
|
||
with open(f"{WORKING_REPO_DIR}/large_file.txt", "w+") as f: | ||
f.write(json.dumps(large_file)) | ||
|
@@ -1157,6 +1186,21 @@ def test_auto_track_large_files_ignored_with_gitignore(self): | |
|
||
repo.auto_track_large_files() | ||
|
||
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() | ||
|
||
# Large files | ||
self.assertFalse( | ||
is_tracked_with_lfs(os.path.join(WORKING_REPO_DIR, "large_file.txt")) | ||
) | ||
|
@@ -1175,7 +1219,26 @@ def test_auto_track_large_files_ignored_with_gitignore(self): | |
) | ||
) | ||
|
||
def test_auto_track_large_files_through_git_add(self): | ||
# 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_files_through_git_add(self): | ||
repo = Repository(WORKING_REPO_DIR) | ||
|
||
# This content is 5MB (under 10MB) | ||
|
@@ -1184,12 +1247,18 @@ def test_auto_track_large_files_through_git_add(self): | |
# This content is 20MB (over 10MB) | ||
large_file = [100] * int(4e6) | ||
|
||
# This content is binary (contains the null character) | ||
binary_file = "\x00\x00\x00\x00" | ||
|
||
with open(f"{WORKING_REPO_DIR}/large_file.txt", "w+") as f: | ||
f.write(json.dumps(large_file)) | ||
|
||
with open(f"{WORKING_REPO_DIR}/small_file.txt", "w+") as f: | ||
f.write(json.dumps(small_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.assertTrue( | ||
|
@@ -1198,8 +1267,11 @@ def test_auto_track_large_files_through_git_add(self): | |
self.assertFalse( | ||
is_tracked_with_lfs(os.path.join(WORKING_REPO_DIR, "small_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): | ||
def test_auto_no_track_files_through_git_add(self): | ||
repo = Repository(WORKING_REPO_DIR) | ||
|
||
# This content is 5MB (under 10MB) | ||
|
@@ -1208,12 +1280,18 @@ def test_auto_no_track_large_files_through_git_add(self): | |
# This content is 20MB (over 10MB) | ||
large_file = [100] * int(4e6) | ||
|
||
# This content is binary (contains the null character) | ||
binary_file = "\x00\x00\x00\x00" | ||
|
||
with open(f"{WORKING_REPO_DIR}/large_file.txt", "w+") as f: | ||
f.write(json.dumps(large_file)) | ||
|
||
with open(f"{WORKING_REPO_DIR}/small_file.txt", "w+") as f: | ||
f.write(json.dumps(small_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( | ||
|
@@ -1222,6 +1300,9 @@ def test_auto_no_track_large_files_through_git_add(self): | |
self.assertFalse( | ||
is_tracked_with_lfs(os.path.join(WORKING_REPO_DIR, "small_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