Last active
May 6, 2017 11:43
-
-
Save thendrix/0dbb9cf167ec14d8693a630925a3e282 to your computer and use it in GitHub Desktop.
Manually patch git lfs missing files for gitlab
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This was written to fix a long standing bug with gitlab LFS and some repos that were migrated over a year ago. | |
# | |
# @todo Pull in shell commands to 100% python if I ever need this again | |
# | |
# I make no fitness for any purpose claims | |
# I wrote this at 0300 after a concert... it worked for me at the time. | |
# Let me know if you fork this or have comments. | |
# | |
# Consider this MIT licensed | |
# | |
# Run from root of a new clone to find missing files. eg /tmp/goblin | |
# | |
# 1. Generate list using find: | |
# find . -type f -size -256c -exec grep "version https://git-lfs.github.com" -l {} \; > lfs-missing.log | |
# | |
# 2. Process list with this script and a backup of the missing files in the same structure. | |
# | |
# 3. Upload to git lfs server; scp -r lfs-objects tzu:/tmp/ | |
# | |
# 3.a. Clean filenames if needed | |
# find . -iname "*^J" -type f -exec rename -f 's/\\\^J//g' "{}" \; | |
# | |
# 4. chown -R git:git /tmp/lfs-objects | |
# If git lfs is under /srv: | |
# cd /srv/storaqe/lfs-objects | |
# cp -a /tmp/lfs-objects/* . | |
import os, shutil | |
test = False | |
# Replace this path with your backup path with binaries. | |
fixPath = '/home/thendrix/Projects/goblin-20170504' | |
fd = open("lfs-missing.log", 'r') | |
for line in fd: | |
if line.startswith('./'): | |
line = line[len('./'):] | |
line = line.replace('\n', '') | |
try: | |
filename = line | |
fix = os.path.join(fixPath, filename) | |
lfsPtr = open(filename, 'r') | |
objectId = None | |
for linePtr in lfsPtr: | |
if linePtr.startswith('oid'): | |
objectId = linePtr.split(':')[1] | |
if os.path.isfile(fix): | |
# Generate LFS object entry. | |
ab = objectId[:4] | |
a = ab[:2] | |
b = ab[2:] | |
c = objectId[4:] | |
entryPath = os.path.join('lfs-objects', a, b) | |
entry = os.path.join(entryPath, c) | |
if not os.path.isdir(entryPath): | |
print('mkdir "'+entryPath+'"') | |
if not test: | |
os.makedirs(entryPath) | |
print('cp "' + fix + '" "'+c+'"') | |
if not test: | |
shutil.copyfile(fix, entry) | |
else: | |
print('NOT FOUND : ' + fix) | |
except Exception, e: | |
print('ERROR ' + line + ' ' + str(e)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment