Search code examples
c#git-submoduleslibgit2sharp

checkout submodule in libgit2sharp


we have found that git submodule update --recursive -f update takes quite a while (as run from a .bat file in windows 7) and are hoping to use a compiled exe (probably c#.NET via libgit2sharp) to to checkout each submodule independently (there are 4). There is a noticeable difference in the batch file speed when we use four sequential git checkout -f [hash] commands after cd'ing into each submodule vs running submodule update, and we want that speed gain.

Anyone know how to checkout a specific commit of a submodule with libgit2sharp? since the HEAD property of repo.Submodule["name"] isn't set-able, I tried to get creative with this (treating the submodules as their own repos), but libgit2sharp seems to think they aren't their own repo...bummer:

        for (int cntr = (int)argumentIndeces.LiquidsId; cntr < (int)argumentIndeces.MethodConfigId; cntr++)
        {
            Logger.Debug("About to checkout '" + argNames[cntr].Replace('/', '\\') + "' at commit: '" + arguments[argNames[cntr]] + "'");
            Repository sub = new Repository(superProjectPath + "\\" + argNames[cntr].Replace('/', '\\') );
            Commands.Checkout(sub, arguments[argNames[cntr]]);
            Logger.Debug("Checked out '" + argNames[cntr].Replace('/', '\\') + "' at commit: '" + arguments[argNames[cntr]] + "'");
            Console.WriteLine("checked out: " + sub.Tags);
        }

Solution

  • This borrows some ideas from the post by olmobrutall on Github (#482), but removes the need for calling deprecated functions. This just does a git reset --hard rather than a fetch, as this was a bit easier to implement.

                using (Repository repo = new Repository(repositoryPath))
                {
                    foreach (Submodule submodule in repo.Submodules)
                    {
                        String subrepoPath = Path.Combine(repo.Info.WorkingDirectory, submodule.Path);
    
                        using (Repository subRepo = new Repository(subrepoPath))
                        {
                            Branch remoteBranch = subRepo.Branches["origin/master"];
                            subRepo.Reset(ResetMode.Hard, remoteBranch.Tip);
                        }
                    }
                }
    

    EDIT: I just realized I missed part of the question, which asked to checkout a specific commit. To do so, instead of checking out a branch we can checkout a specific hash:

    string commitHash = "517d9bdc49abf6fff00f4d5330245592e2e138b6";
    Commit commit = subRepo.Lookup<Commit>(commitHash);
    subRepo.Reset(ResetMode.Hard, commit);