public static string ProcessSubmodulePatch(string text)
{
StringBuilder sb = new StringBuilder();
using (StringReader reader = new StringReader(text))
{
string line;
string module = "";
while ((line = reader.ReadLine()) != null)
{
if (line.StartsWith("+++ "))
{
module = line.Substring("+++ ".Length);
var list = module.Split(new char[] { ' ' }, 2);
module = list.Length > 0 ? list[0] : "";
if (module.Length > 2 && module[1] == '/')
{
module = module.Substring(2);
break;
}
}
}
sb.AppendLine("Submodule " + module + " Change");
string fromHash = null;
string toHash = null;
bool dirtyFlag = false;
while ((line = reader.ReadLine()) != null)
{
if (line.Contains("Subproject"))
{
sb.AppendLine();
char c = line[0];
const string commit = "commit ";
string hash = "";
int pos = line.IndexOf(commit);
if (pos >= 0)
hash = line.Substring(pos + commit.Length);
bool bdirty = hash.EndsWith("-dirty");
dirtyFlag |= bdirty;
hash = hash.Replace("-dirty", "");
string dirty = !bdirty ? "" : " (dirty)";
if (c == '-')
{
fromHash = hash;
sb.AppendLine("From:\t" + hash + dirty);
}
else if (c == '+')
{
toHash = hash;
sb.AppendLine("To:\t\t" + hash + dirty);
}
string path = Settings.Module.GetSubmoduleFullPath(module);
GitModule gitmodule = new GitModule(path);
if (gitmodule.ValidWorkingDir())
{
string error = "";
CommitData commitData = CommitData.GetCommitData(gitmodule, hash, ref error);
if (commitData != null)
{
sb.AppendLine("\t\t\t\t\t" + GitCommandHelpers.GetRelativeDateString(DateTime.UtcNow, commitData.CommitDate.UtcDateTime) + commitData.CommitDate.LocalDateTime.ToString(" (ddd MMM dd HH':'mm':'ss yyyy)"));
var delim = new char[] { '\n', '\r' };
var lines = commitData.Body.Trim(delim).Split(new string[] { "\r\n" }, 0);
foreach (var curline in lines)
sb.AppendLine("\t\t" + curline);
}
if (fromHash != null && toHash != null)
{
if (dirtyFlag)
{
string status = gitmodule.GetStatusText(false);
if (!String.IsNullOrEmpty(status))
{
sb.AppendLine("\nStatus:");
sb.Append(status);
}
}
string diffs = gitmodule.GetDiffFilesText(fromHash, toHash);
if (!String.IsNullOrEmpty(diffs))
{
sb.AppendLine("\nDifferences:");
sb.Append(diffs);
}
}
}
else
sb.AppendLine();
}
}
}
return sb.ToString();
}