Search code examples
c#directory-structurelibgit2libgit2sharp

Building a directory tree of tracked files with libgit2.sharp


not sure if I am on the right track here.

I have been trying to build a folder structure tree in Winforms by recursively building a DictionaryInfo object and with some hacking ignoring files and folders I ignored and not tracked by git.

But I found that there is a git command that basically does all this for me. git ls-tree -r -t --name-only head

Can I achieve this with libgit2?

Thanks!

Edit (Add workaround)

So right now I am doing this

var info = new ProcessStartInfo("git", "ls-tree -r -t --name-only head")
{
  CreateNoWindow = true,
  RedirectStandardOutput = true,
  UseShellExecute = false,
  WorkingDirectory = "<repo workdir>",
};

var list = new List<TreeNode>();
using (var sr = Process.Start(info)?.StandardOutput)
{
  string? line;
  while ((line = sr?.ReadLine()) != null)
  {
    if (line != null)
      list.Add(new TreeNode(line));
  }
}
treeView.Nodes.Clear();
var rootDirectoryInfo = new DirectoryInfo(path);
treeView.Nodes.AddRange(list.ToArray());

Solution

  • There is an example on the LibGit2Sharp wiki page that provides a starting point to iterate through all entries in the index: https://github.com/libgit2/libgit2sharp/wiki/git-ls-files#libgit2sharp

    For your case, I think you will want to remove the block checking the stage level of the entry.