Skip to content

Instantly share code, notes, and snippets.

@JBurkeKF
Created July 19, 2020 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JBurkeKF/8f719fa7e60683f0749e4e283f61aefe to your computer and use it in GitHub Desktop.
Save JBurkeKF/8f719fa7e60683f0749e4e283f61aefe to your computer and use it in GitHub Desktop.
import os
import sys
import subprocess
def Execute( command, cwd=None ):
result = 0
output = ""
print( "Run command [" + command + "]" )
try:
output = subprocess.check_output( command, cwd=cwd )
except subprocess.CalledProcessError as error:
output = error.output
result = error.returncode
if result > 0:
print( output )
raise RuntimeError()
####################################################
def ExecuteReturnOutput( command, cwd=None ):
result = 0
output = ""
print( "Run command [" + command + "]" )
try:
output = subprocess.check_output( command, cwd=cwd )
except subprocess.CalledProcessError as error:
output = error.output
result = error.returncode
if result > 0:
print( output )
raise RuntimeError()
return output
####################################################
# The directory to walk to search for files
searchDir = "."
# The extension of the files to search for
ext = ".ext"
lfsFilesString = ExecuteReturnOutput( "git lfs ls-files" )
#print( lfsFilesString.decode( "utf-8" ) )
lfsFilesRaw = lfsFilesString.decode( "utf-8" ).split( "\n" )
lfsFiles = []
for lfsFileRaw in lfsFilesRaw:
#print( "lfs file [" + lfsFile + "]" )
if ( len( lfsFileRaw.strip() ) > 0 ):
#print( lfsFileRaw.split( " ", 2 ) )
#print( lfsFileRaw )
lfsFiles.append( lfsFileRaw.split( " ", 2 )[2] )
#for lfsFile in lfsFiles:
# print( "lfs file [" + lfsFile + "]" )
count = 0
for root, directories, files in os.walk( searchDir ):
for file in files:
if ( os.path.splitext( file )[1] == ext ):
count += 1
#print( "Found file [" + os.path.join( root, file ) + "]" )
filePath = os.path.join( root, file ).replace( "\\", "/" )
if ( not ( filePath in lfsFiles ) ):
gitCmd = "git lfs migrate import --no-rewrite \"" + filePath + "\""
#print( gitCmd )
Execute( gitCmd )
#else:
# print( "File [" + filePath + "] already in LFS" )
#else:
# print ("Found non-ext file [" + file + "]" )
print( "Found [" + str( count ) + "] files" )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment