# This fixes symbol link issue you might get when
# there are git symlinks being used on repositories that
# you share in both POSIX (usually the host) and Windows (VM).
#
# No warranties, good luck
import os
def rindex(lst, value):
try:
return lst.rindex(value)
except ValueError:
return -1
# find symbol link files or dirs
fp = os.popen("git ls-files -s | awk '/120000/{print $4}'")
links = fp.read().strip().split("\n")
# get symbol links' parent dir
link_dir = set()
for link in links:
index = rindex(link, "/")
if (index != -1):
link_dir.add(link[:index])
else:
link_dir.add(".")
work_dir = os.getcwd()
# make link for every symbol link
for d in link_dir:
os.chdir("/".join([work_dir,d]))
fp = os.popen("ls -la")
items = fp.read().strip().split("\n")
for item in items:
if "->" in item:
tks = item.split("->")
src = tks[0].strip().split(" ")[-1]
dst = tks[1].strip().split("/")
if (len(dst) > 1):
dst = "\\\\".join(dst)
else:
dst = dst[0]
print ("link " + src + " -> " + dst)
os.popen("rm " + src)
if (os.path.isfile(dst)):
os.popen("cmd /c mklink /H " + src + " " + dst)
else:
os.popen("cmd /c mklink /j " + src + " " + dst)
# make links unchanged
os.popen("git update-index --assume-unchanged " + "/".join([os.getcwd(), src]))
Last active
July 7, 2019 07:22
-
-
Save pkufool/b9ad6830e60058070f84eb1762ad5a14 to your computer and use it in GitHub Desktop.
handle symbol links in windows with python scirpt
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment