Fix ticket #902: .git directories can't be checked out

Rename them before the commit
ticket-769
Vadim vtroshchinskiy 2024-10-08 12:50:52 +02:00
parent e76b79090d
commit dbad59b012
1 changed files with 72 additions and 36 deletions

View File

@ -283,6 +283,13 @@ class OpengnsysGitLibrary:
'System Volume Information'
]
"""List of files to rename before commit. This is done for files that may interfere with Git, such as inner git repositories."""
self.rename_list = [
'.git',
'.gitignore',
'.gitattributes'
]
self.kernel_args = self._parse_kernel_cmdline()
self.repo_server = self.kernel_args["ogrepo"]
@ -846,7 +853,7 @@ class OpengnsysGitLibrary:
acls_file = open(os.path.join(meta_dir, "acls.jsonl.new"), "w")
perms_file = open(os.path.join(meta_dir, "unix_permissions.jsonl.new"), "w")
xattrs_file = open(os.path.join(meta_dir, "xattrs.jsonl.new"), "w")
gitignores_file = open(os.path.join(meta_dir, "gitignores.jsonl.new"), "w")
renamed_file = open(os.path.join(meta_dir, "renamed.jsonl.new"), "w")
filesystems_file = open(os.path.join(meta_dir, "filesystems.json.new"), "w")
ntfs = False
@ -988,20 +995,8 @@ class OpengnsysGitLibrary:
xattrs_file.write(xattrs_json + "\n")
acls_file.write(acls_json + "\n")
if os.path.isfile(full_path) and file == ".gitignore" and root != path:
# TODO: tener en cuenta archivos ya renombrados
#self.logger.debug(f"Checking if {file} is ignored")
logger.debug(f"Found .gitignore: {full_path}")
renamed_file_path = full_path + "-opengnsys-renamed"
gitignores_json = json.dumps({"file": full_path_rel})
gitignores_file.write(gitignores_json + "\n")
os.rename(full_path, renamed_file_path)
#print(f"\tXATTRS: {xattrs}")
#print(f"\tACLs: {acls_json}")
if os.path.exists(full_path):
if not os.path.islink(full_path):
@ -1029,6 +1024,39 @@ class OpengnsysGitLibrary:
self.logger.debug(f"Symlink: {full_path_rel}")
return_data['symlinks'].append(full_path_rel)
if os.path.isfile(full_path) and file in self.rename_list and root != path:
# Process this last so that all the metadata references the real names.
logger.debug(f"Found file to rename: {full_path}")
renamed_file_path = full_path + "-opengnsys-renamed"
renamed_file_path_rel = full_path_rel + "-opengnsys-renamed"
renamed_json = json.dumps({"path": full_path_rel, "renamed" : renamed_file_path_rel})
renamed_file.write(renamed_json + "\n")
os.rename(full_path, renamed_file_path)
for subdir in subdirs:
full_path = os.path.join(root, subdir)
full_path_rel = full_path[len(path):None]
# Relative path can't start with a /, git will take it as an
# absolute path pointing to /, and not a file within the repo.
while full_path_rel[0] == '/':
full_path_rel = full_path_rel[1:None]
if os.path.isdir(full_path) and subdir in self.rename_list and root != path:
logger.debug(f"Found directory to rename: {full_path}")
renamed_dir_path = full_path + "-opengnsys-renamed"
renamed_dir_path_rel = full_path_rel + "-opengnsys-renamed"
renamed_json = json.dumps({"path": full_path_rel, "renamed" : renamed_dir_path_rel})
renamed_file.write(renamed_json + "\n")
os.rename(full_path, renamed_dir_path)
self.logger.debug("Finishing...")
@ -1044,7 +1072,7 @@ class OpengnsysGitLibrary:
xattrs_file.close()
acls_file.close()
perms_file.close()
gitignores_file.close()
renamed_file.close()
filesystems_file.close()
metadata_file.close()
@ -1053,7 +1081,7 @@ class OpengnsysGitLibrary:
os.rename(os.path.join(meta_dir, "acls.jsonl.new"), os.path.join(meta_dir, "acls.jsonl"))
os.rename(os.path.join(meta_dir, "unix_permissions.jsonl.new"), os.path.join(meta_dir, "unix_permissions.jsonl"))
os.rename(os.path.join(meta_dir, "xattrs.jsonl.new"), os.path.join(meta_dir, "xattrs.jsonl"))
os.rename(os.path.join(meta_dir, "gitignores.jsonl.new"), os.path.join(meta_dir, "gitignores.jsonl"))
os.rename(os.path.join(meta_dir, "renamed.jsonl.new"), os.path.join(meta_dir, "renamed.jsonl"))
os.rename(os.path.join(meta_dir, "filesystems.json.new"), os.path.join(meta_dir, "filesystems.json"))
os.rename(os.path.join(meta_dir, "metadata.json.new"), os.path.join(meta_dir, "metadata.json"))
@ -1092,6 +1120,32 @@ class OpengnsysGitLibrary:
self.logger.error(f"Metadata directory not found: {meta_dir}")
return
# Process renames first so that all the filenames are as they should be
# for the following steps.
self.logger.debug("Processing renamed.jsonl")
with open(os.path.join(meta_dir, "renamed.jsonl"), "r") as gitignores_file:
for line in gitignores_file:
#self.logger.debug(f"Line: {line}")
renamed_data = json.loads(line)
orig_file = renamed_data['path']
renamed_file = renamed_data['renamed']
if renamed_file.startswith("/"):
renamed_file = renamed_file[1:]
orig_file_path = os.path.join(path, orig_file)
renamed_file_path = os.path.join(path, renamed_file)
#self.logger.debug(f"Checking: {renamed_file_path}")
if os.path.exists(renamed_file_path):
self.logger.debug(f"Renaming {renamed_file_path} => {orig_file_path}")
os.rename(renamed_file_path, orig_file_path)
else:
if os.path.exists(orig_file_path):
self.logger.warning(f"Can't rename {renamed_file_path} => {orig_file_path}: Already renamed")
else:
self.logger.warning(f"Can't rename {renamed_file_path} => {orig_file_path}: Source file not found")
if not destructive_only:
self.logger.debug("Processing empty_directories.jsonl")
with open(os.path.join(meta_dir, "empty_directories.jsonl"), "r") as empties_file:
@ -1136,7 +1190,7 @@ class OpengnsysGitLibrary:
os.chown(perms_file_path, file_uid, file_gid)
os.chmod(perms_file_path, file_perms)
else:
self.logger.warn(f"Can't apply permissions to {perms_file_path}, file doesn't exist.")
self.logger.warning(f"Can't apply permissions to {perms_file_path}, file doesn't exist.")
if not destructive_only:
@ -1177,24 +1231,6 @@ class OpengnsysGitLibrary:
#self.logger.debug(f"Line: {line}")
self.logger.debug("Processing gitignores.jsonl")
with open(os.path.join(meta_dir, "gitignores.jsonl"), "r") as gitignores_file:
for line in gitignores_file:
#self.logger.debug(f"Line: {line}")
gitignores_data = json.loads(line)
gitignores_file = gitignores_data['file']
if gitignores_file.startswith("/"):
gitignores_file = gitignores_file[1:]
orig_file_path = os.path.join(path, gitignores_file)
renamed_file_path = orig_file_path + "-opengnsys-renamed"
#self.logger.debug(f"Checking: {renamed_file_path}")
if os.path.exists(renamed_file_path):
self.logger.debug(f"Renaming {renamed_file_path} => {orig_file_path}")
os.rename(renamed_file_path, orig_file_path)
self.logger.debug("Processing special_files.jsonl")
with open(os.path.join(meta_dir, "special_files.jsonl"), "r") as specials_file:
for line in specials_file:
@ -1619,7 +1655,7 @@ if __name__ == '__main__':
logger.addHandler(streamLog)
logger.addHandler(fileLog)
logger.info("Inicio del programa")
logger.info("Program start")
parser = argparse.ArgumentParser(
prog="OpenGnsys Git Library",