Code Monkey home page Code Monkey logo

Comments (2)

bk2204 avatar bk2204 commented on June 16, 2024

Hey,

This isn't surprising to me based on how submodules work. A submodule in a Git repository consists of an entry in a tree object pointing to an external commit (of mode 160000), plus an entry in .gitmodules that says where to access the repository. Because the tree entry refers to a specific object ID, if you remove that object in the submodule, Git will complain about being unable to check it out.

We don't have automatic functionality for rewriting superprojects, but you can use the --object-map option to make a map of the old and new object IDs and then rewrite the superproject using that map with something like git filter-repo or git filter-branch. However, there's no way to avoid rewriting the superproject history here; if you don't, the submodules can't be accessed in the history.

from git-lfs.

Kitiara avatar Kitiara commented on June 16, 2024

Thank you for the idea @bk2204. I have created a python script exactly does that and it looks like it is working as intended. Here it is the script and what i did in case someone may need this in the future.

I got the map exported with git lfs migrate import --everything --object-map=map and then run the script below in the super project as follows: py fixSuperProject.py "./mySubmodule/map" "mySubmodule". Lastly, I pushed my changes with the --force option, that's another important part otherwise you get an error about merging which is not what you want.

import sys
import subprocess

if len(sys.argv) < 2:
    print("Usage: py fixSuperProject.py <object_map_file> <sbmodule_dir_with_respect_to_git_dir>")
    sys.exit(1)

object_map_file = sys.argv[1]
sbmodule_dir_with_respect_to_git_dir = sys.argv[2] # Submodule folder with respect to super project .git folder

id_map = {}

try:
    with open(object_map_file, "r") as f:
        for line in f:
            parts = line.strip().split(",")
            old_id, new_id = parts
            id_map[old_id] = new_id
except FileNotFoundError:
    print(f"File not found: {object_map_file}")

# Write the command to a file, as it can get very long
with open("callback_script.py", "w") as f:
    f.write(f"""
blob_map = {id_map}
for change in commit.file_changes:
    if change.mode is not None:
        if change.mode.decode("utf-8") == "160000" and change.filename.decode("utf-8") == "{sbmodule_dir_with_respect_to_git_dir}":
            if change.blob_id is not None:
                new_blob_id = blob_map.get(change.blob_id.decode("utf-8"))
                if new_blob_id is not None:
                    change.blob_id = new_blob_id.encode("utf-8")
""")

callback_script_path = 'callback_script.py'
command = ['git', 'filter-repo', '--commit-callback', f'{callback_script_path}', '--force']
subprocess.run(command)

from git-lfs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.