checkout submodule in libgit2sharp

Viewed 910

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);
        }
2 Answers

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);

As it turns out, most of the problem was that the submodules were not init'd right in the first place (like, on the HDD). they were in some weird half-setup state. (superproject had its data about them, but they were missing their own .git file, etc).

Once I fixed that, and built an array of submodule names, this worked:

        for (int cntr = 0; cntr < argNames.Length; cntr++)
        {
            Commands.Checkout(
                new Repository(
                    superProjectPath + "\\" + argNames[cntr].Replace('/', '\\')
                    ), 
                arguments[argNames[cntr]], options
                );
        }

For what it's worth, that Commands.Checkout call is well-worth wrapping in a try-catch. Especially if, like me, the commit/branch spec is an argument to Main. the caller could easily get the hash or even branch name wrong.

Now the issue is if I can get the superproject's checkout to NOT do a submodule update because it's a waste (since I'm checking out a specific commit of each submodule immediately afterwards)

Related