Search code examples
gittfsgit-submodulestfs-sdk

TFS Git Rest Api - how to add submodule?


I'm trying to add submodule to my git repo on TFS via TFS GIT REST API however no luck so far. I created new repository and added .gitmodules to repository but can't figure out how to add the submodule folder/reference.

Here is the raw request I'm trying to construct for the initial commit with the .gitignore, .gitmodules and submodule named "SomeTools".

POST http://tfs:8080/tfs/My_Collection/My_Projekt/_apis/git/repositories/TestRepo/pushes?api-version=2.0 HTTP/1.1
Accept: */*
Content-Type: application/json
Host: tfs:8080
Content-Length: 7213

{
    "refUpdates":  [
                       {
                           "name":  "refs/heads/develop",
                           "oldObjectId":  "0000000000000000000000000000000000000000"
                       }
                   ],
    "commits":  [
                    {
                        "changes":  [
                                        {
                                            "newContent":  {
                                                               "content":  *.suo\r\n",
                                                               "contentType":  "rawtext"
                                                           },
                                            "changeType":  "add",
                                            "item":  {
                                                         "path":  "/.gitignore"
                                                     }
                                        },
                                        {
                                            "newContent":  {
                                                               "content":  "[submodule \"SomeTools\"]\n\tpath = SomeTools\n\turl = http://tfs:8080/tfs/My_Collection/My_Projekt/_git/SomeTools\n",
                                                               "contentType":  "rawtext"
                                                           },
                                            "changeType":  "add",
                                            "item":  {
                                                         "path":  "/.gitmodules"
                                                     }
                                        },
                                        {
                                            "newContent":  {
                                                               "content":  "198abf113d8baf48aa55ab1897b30fdb7b23c4cc",
                                                               "contentType":  "rawtext"
                                                           },
                                            "changeType":  "add",
                                            "item":  {
                                                         "path":  "/SomeTools",
                                                         "versionType":  "commit"
                                                     }
                                        }
                                    ],
                        "comment":  "Initial commit."
                    }
                ]
}

Solution

  • We can not achieve that by the pushes REST API, it can create a new branch but not submodule.

    Seems you manually create the repository and added the .gitmodules files, if it is, then you need to also manually create the submodule folder/reference, then commit > push to Git server.

    The easiest way is running git submodule add command to add the Git submodule:

    Assuming you have 2 Git repositories:

    http://server:8080/tfs/DefaultCollection/TeamProjectName/_git/Repo1
    http://server:8080/tfs/DefaultCollection/TeamProjectName/_git/Repo2
    

    Add git submodule for Repo1:

    git clone http://server:8080/tfs/DefaultCollection/TeamProjectName/_git/Repo1
    
    git submodule add http://server:8080/tfs/DefaultCollection/TeamProjectName/_git/Repo2
    

    Then commit the changes and push to Git repository.

    enter image description here