@@ -735,6 +735,168 @@ def test_delete_tag(self):
735735 repo .delete_tag ("v4.6.0" , remote = "origin" )
736736 self .assertFalse (repo .tag_exists ("v4.6.0" , remote = "origin" ))
737737
738+ def test_lfs_prune (self ):
739+ repo = Repository (
740+ WORKING_REPO_DIR ,
741+ clone_from = f"{ USER } /{ REPO_NAME } " ,
742+ use_auth_token = self ._token ,
743+ git_user = "ci" ,
744+ 745+ revision = "main" ,
746+ )
747+
748+ with repo .commit ("Committing LFS file" ):
749+ with open ("file.bin" , "w+" ) as f :
750+ f .write ("Random string 1" )
751+
752+ with repo .commit ("Committing LFS file" ):
753+ with open ("file.bin" , "w+" ) as f :
754+ f .write ("Random string 2" )
755+
756+ root_directory = pathlib .Path (repo .local_dir ) / ".git" / "lfs"
757+ git_lfs_files_size = sum (
758+ f .stat ().st_size for f in root_directory .glob ("**/*" ) if f .is_file ()
759+ )
760+ repo .lfs_prune ()
761+ post_prune_git_lfs_files_size = sum (
762+ f .stat ().st_size for f in root_directory .glob ("**/*" ) if f .is_file ()
763+ )
764+
765+ # Size of the directory holding LFS files was reduced
766+ self .assertLess (post_prune_git_lfs_files_size , git_lfs_files_size )
767+
768+ def test_lfs_prune_git_push (self ):
769+ repo = Repository (
770+ WORKING_REPO_DIR ,
771+ clone_from = f"{ USER } /{ REPO_NAME } " ,
772+ use_auth_token = self ._token ,
773+ git_user = "ci" ,
774+ 775+ revision = "main" ,
776+ )
777+
778+ with repo .commit ("Committing LFS file" ):
779+ with open ("file.bin" , "w+" ) as f :
780+ f .write ("Random string 1" )
781+
782+ root_directory = pathlib .Path (repo .local_dir ) / ".git" / "lfs"
783+ git_lfs_files_size = sum (
784+ f .stat ().st_size for f in root_directory .glob ("**/*" ) if f .is_file ()
785+ )
786+
787+ with open (os .path .join (repo .local_dir , "file.bin" ), "w+" ) as f :
788+ f .write ("Random string 2" )
789+
790+ repo .git_add ()
791+ repo .git_commit ("New commit" )
792+ repo .git_push (auto_lfs_prune = True )
793+
794+ post_prune_git_lfs_files_size = sum (
795+ f .stat ().st_size for f in root_directory .glob ("**/*" ) if f .is_file ()
796+ )
797+
798+ # Size of the directory holding LFS files is the exact same
799+ self .assertEqual (post_prune_git_lfs_files_size , git_lfs_files_size )
800+
801+ def test_lfs_prune_git_push_non_blocking (self ):
802+ repo = Repository (
803+ WORKING_REPO_DIR ,
804+ clone_from = f"{ USER } /{ REPO_NAME } " ,
805+ use_auth_token = self ._token ,
806+ git_user = "ci" ,
807+ 808+ revision = "main" ,
809+ )
810+
811+ with repo .commit ("Committing LFS file" ):
812+ with open ("file.bin" , "w+" ) as f :
813+ f .write ("Random string 1" )
814+
815+ root_directory = pathlib .Path (repo .local_dir ) / ".git" / "lfs"
816+ git_lfs_files_size = sum (
817+ f .stat ().st_size for f in root_directory .glob ("**/*" ) if f .is_file ()
818+ )
819+
820+ with open (os .path .join (repo .local_dir , "file.bin" ), "w+" ) as f :
821+ f .write ("Random string 2" )
822+
823+ repo .git_add ()
824+ repo .git_commit ("New commit" )
825+ repo .git_push (blocking = False , auto_lfs_prune = True )
826+
827+ while len (repo .commands_in_progress ):
828+ time .sleep (0.2 )
829+
830+ post_prune_git_lfs_files_size = sum (
831+ f .stat ().st_size for f in root_directory .glob ("**/*" ) if f .is_file ()
832+ )
833+
834+ # Size of the directory holding LFS files is the exact same
835+ self .assertEqual (post_prune_git_lfs_files_size , git_lfs_files_size )
836+
837+ def test_lfs_prune_context_manager (self ):
838+ repo = Repository (
839+ WORKING_REPO_DIR ,
840+ clone_from = f"{ USER } /{ REPO_NAME } " ,
841+ use_auth_token = self ._token ,
842+ git_user = "ci" ,
843+ 844+ revision = "main" ,
845+ )
846+
847+ with repo .commit ("Committing LFS file" ):
848+ with open ("file.bin" , "w+" ) as f :
849+ f .write ("Random string 1" )
850+
851+ root_directory = pathlib .Path (repo .local_dir ) / ".git" / "lfs"
852+ git_lfs_files_size = sum (
853+ f .stat ().st_size for f in root_directory .glob ("**/*" ) if f .is_file ()
854+ )
855+
856+ with repo .commit ("Committing LFS file" , auto_lfs_prune = True ):
857+ with open ("file.bin" , "w+" ) as f :
858+ f .write ("Random string 2" )
859+
860+ post_prune_git_lfs_files_size = sum (
861+ f .stat ().st_size for f in root_directory .glob ("**/*" ) if f .is_file ()
862+ )
863+
864+ # Size of the directory holding LFS files is the exact same
865+ self .assertEqual (post_prune_git_lfs_files_size , git_lfs_files_size )
866+
867+ def test_lfs_prune_context_manager_non_blocking (self ):
868+ repo = Repository (
869+ WORKING_REPO_DIR ,
870+ clone_from = f"{ USER } /{ REPO_NAME } " ,
871+ use_auth_token = self ._token ,
872+ git_user = "ci" ,
873+ 874+ revision = "main" ,
875+ )
876+
877+ with repo .commit ("Committing LFS file" ):
878+ with open ("file.bin" , "w+" ) as f :
879+ f .write ("Random string 1" )
880+
881+ root_directory = pathlib .Path (repo .local_dir ) / ".git" / "lfs"
882+ git_lfs_files_size = sum (
883+ f .stat ().st_size for f in root_directory .glob ("**/*" ) if f .is_file ()
884+ )
885+
886+ with repo .commit ("Committing LFS file" , auto_lfs_prune = True , blocking = False ):
887+ with open ("file.bin" , "w+" ) as f :
888+ f .write ("Random string 2" )
889+
890+ while len (repo .commands_in_progress ):
891+ time .sleep (0.2 )
892+
893+ post_prune_git_lfs_files_size = sum (
894+ f .stat ().st_size for f in root_directory .glob ("**/*" ) if f .is_file ()
895+ )
896+
897+ # Size of the directory holding LFS files is the exact same
898+ self .assertEqual (post_prune_git_lfs_files_size , git_lfs_files_size )
899+
738900
739901class RepositoryOfflineTest (RepositoryCommonTest ):
740902 @classmethod
0 commit comments