nextcloud-helper/nextcloud/conflicts/resolve_conflicts.py

108 lines
No EOL
3 KiB
Python

import os
import sys
import shutil
import glob
try:
input = raw_input
except NameError:
pass
def program_path():
pp = os.path.realpath(sys.argv[0])
return os.path.split(pp)[0]
pp = program_path()
def resolve(cf, local, backup, time):
for f, fp, fn, fe, i in cf:
fpc = fp
fc = fn
base = os.path.split(fp)[0]
fo = fn[:i] + fe
fpo = os.path.join(base, fo)
fb = fn[:i] + " (resolved confict " + fn[i+18:] + fe
fpb = os.path.join(base, fb)
if time:
timeo = os.stat(fpo).st_mtime
timec = os.stat(fpc).st_mtime
if timeo > timec:
local = True
else:
local = False
if local:
if backup:
print("Renaming %s to %s" % (fpc, fpb))
os.rename(fpc, fpb)
else:
print("Deleting %s" % (fpc,))
os.remove(fpc)
else:
if backup:
print("Renaming %s to %s" % (fpo, fpb))
os.rename(fpo, fpb)
elif os.path.exists(fpo):
print("Deleting %s" % (fpo,))
os.remove(fpo)
print("Renaming %s to %s" % (fpc, fpo))
os.rename(fpc, fpo)
request = None
fixed_answer = False
rootdir=os.path.join(pp, '..', 'hardlinks')
if len(sys.argv) > 1:
rootdir=sys.argv[1]
for base, dirs, files in os.walk(rootdir):
conflicts = {}
for f in files:
fp = os.path.join(base, f)
fn, fe = os.path.splitext(f)
i = fn.find(" (conflicted copy ")
if i != -1:
stat = os.stat
print(f"Found conflict {fn}")
conflict_id = fn[i+18:-1]
conflict_id = ''
if not conflict_id in conflicts:
conflicts[conflict_id] = []
conflicts[conflict_id] += [(f,fp,fn,fe,i)]
if "Keepass" in fp:
print("%s %s" % (fp, i))
for cd, cf in conflicts.items():
print("Conflict %s in %s (%d files):" % (cd, base, len(cf)))
if not fixed_answer:
print("Select which version to keep...")
print(" L for local version")
print(" R for remote version")
print(" T for most recent")
#print(" B for backout (swaps backup with active)")
#print(" C for cleanup (removes old backups)")
print(" UL for unsafe local (resolves conflict with local files without backups of remote files)")
print(" UR for unsafe remote (resolves conflict with remote files without backups of local files)")
print(" UT for unsafe time (most recent, no backups)")
print(" ... add A (for example ULA) for all directories")
print(" X for nothing (conflict not resolved, manual conflict resolution)")
print(" XA to print the rest of the conflicts and exit without changing anything else")
print("")
request = input().strip().upper()
if request[-1] == "A":
request = request[:-1]
fixed_answer = True
if request == "UL":
resolve(cf, local=True, backup=False, time=False)
elif request == "L":
resolve(cf, local=True, backup=True, time=False)
elif request == "UR":
resolve(cf, local=False, backup=False, time=False)
elif request == "R":
resolve(cf, local=False, backup=True, time=False)
elif request == "T":
resolve(cf, local=False, backup=True, time=True)
elif request == "UT":
resolve(cf, local=False, backup=False, time=True)