public string FindGitSuperprojectPath(out string submoduleName, out string submodulePath)
{
submoduleName = null;
submodulePath = null;
if (!IsValidGitWorkingDir())
return null;
string superprojectPath = null;
string currentPath = Path.GetDirectoryName(_workingDir); // remove last slash
if (!string.IsNullOrEmpty(currentPath))
{
string path = Path.GetDirectoryName(currentPath);
for (int i = 0; i < 5; i++)
{
if (string.IsNullOrEmpty(path))
break;
if (File.Exists(Path.Combine(path, ".gitmodules")) &&
IsValidGitWorkingDir(path))
{
superprojectPath = path.EnsureTrailingPathSeparator();
break;
}
// Check upper directory
path = Path.GetDirectoryName(path);
}
}
if (File.Exists(_workingDir + ".git") &&
superprojectPath == null)
{
var lines = File.ReadLines(_workingDir + ".git");
foreach (string line in lines)
{
if (line.StartsWith("gitdir:"))
{
string gitpath = line.Substring(7).Trim();
int pos = gitpath.IndexOf("/.git/modules/");
if (pos != -1)
{
gitpath = gitpath.Substring(0, pos + 1).Replace('/', '\\');
gitpath = Path.GetFullPath(Path.Combine(_workingDir, gitpath));
if (File.Exists(gitpath + ".gitmodules") && IsValidGitWorkingDir(gitpath))
superprojectPath = gitpath;
}
}
}
}
if (!string.IsNullOrEmpty(superprojectPath))
{
submodulePath = currentPath.Substring(superprojectPath.Length).ToPosixPath();
var configFile = new ConfigFile(superprojectPath + ".gitmodules", true);
foreach (ConfigSection configSection in configFile.ConfigSections)
{
if (configSection.GetPathValue("path") == submodulePath.ToPosixPath())
{
submoduleName = configSection.SubSection;
return superprojectPath;
}
}
}
return null;
}