Search code examples
pythongitvisual-studio-coderepositorygit-submodules

How do I manage the file structure of GIT sub modules/trees?


I use VSC for programming Python in an embedded system. The file structure is critical to the system working as designed and I am unable to change this structure. In the image below I have tried to explain my problem. There you see the main.py file with some imports. The files I am importing MUST be .py files in the SRC folder. In fact they can also be in sub-folders if I use the following import structure: '''import sub_folder_name.libraryName''' What I am struggling with is managing this in version control. I can keep the entire contents of the project, in the right folders, using a GIT repository, however I would really like to manage my libraries in separate repositories, such as GIT submodules or a subtree. The problem I am facing is that if I use these GIT tools they insist on the subs being in a folder with the root as the parent. This means my embedded system can no longer see the libraries as they appear (to it) above the folder it sees as root, which to us us the SRC folder. So what I am looking for is a way to manage libraries separately from my main repository, while keeping the working .py files in the SRC folder. enter image description here


Solution

  • The problem I am facing is that if I use [git submodule or git subtree] they insist on the subs being in a folder with the root as the parent.

    If I understand this correctly, you mean you can't do:

    mkdir src/sub; cd src/sub; git init
    

    to create a submodule named src/sub in the top-level (main) repository, so that you can then have src/sub/foo.py as a file named foo.py in the repository that is acting as a submodule.

    But this is allowed; Git can do this with no problem. Just:

    mkdir src/sub && cd src/sub && git init
    

    Then create some files in the submodule (perhaps use a Terminal or other shell window running bash or whatever to do all this work):

    echo blah blah > README; git add README; git commit -m initial
    git branch -m main         # optional, if you like the new GitHub names
    git remote add origin <url>
    git push origin main       # or git push origin master
    

    Now return to the top level of your main working tree:

    git submodule add <url> src/sub
    

    and you now have src/sub as a submodule of your main repository, with instructions in .gitmodules for how to clone it from the given <url>.

    (You now have to deal with all the pain and agony of submodules, which is probably not a good idea, but that part is up to you.)

    (Separate from all this Git stuff, as a generic Python thing, consider alternatives involving setting $PYTHONPATH, using virtual environments, altering sys.path as needed, and so on.)