Example #1
0
        public static string GetSubmoduleText(VsrModule superproject, string name, string hash)
        {
            var sb = new StringBuilder();

            sb.AppendLine("Submodule " + name);
            sb.AppendLine();
            VsrModule module = superproject.GetSubmodule(name);

            // TODO File access for Git revision access
            if (module.IsValidVersionrWorkingDir())
            {
                // TEMP, will be moved in the follow up refactor
                ICommitDataManager commitDataManager = new CommitDataManager(() => module);

                CommitData data = commitDataManager.GetCommitData(hash, out _);
                if (data == null)
                {
                    sb.AppendLine("Commit hash:\t" + hash);
                    return(sb.ToString());
                }

                string header = PlainCommitDataHeaderRenderer.RenderPlain(data);
                string body   = "\n" + data.Body.Trim();
                sb.AppendLine(header);
                sb.Append(body);
            }
            else
            {
                sb.AppendLine("Commit hash:\t" + hash);
            }

            return(sb.ToString());
        }
Example #2
0
        public static GitSubmoduleStatus ParseSubmoduleStatus(string text, VsrModule module, string fileName)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(null);
            }

            string   name           = null;
            string   oldName        = null;
            bool     isDirty        = false;
            ObjectId commitId       = null;
            ObjectId oldCommitId    = null;
            int?     addedCommits   = null;
            int?     removedCommits = null;

            using (var reader = new StringReader(text))
            {
                string line = reader.ReadLine();

                if (line != null)
                {
                    var match = Regex.Match(line, @"diff --git [abic]/(.+)\s[abwi]/(.+)");
                    if (match.Groups.Count > 1)
                    {
                        name    = match.Groups[1].Value;
                        oldName = match.Groups[2].Value;
                    }
                    else
                    {
                        match = Regex.Match(line, @"diff --cc (.+)");
                        if (match.Groups.Count > 1)
                        {
                            name    = match.Groups[1].Value;
                            oldName = match.Groups[1].Value;
                        }
                    }
                }

                while ((line = reader.ReadLine()) != null)
                {
                    // We are looking for lines resembling:
                    //
                    // -Subproject commit bfef4454fc51e345051ee5bf66686dc28deed627
                    // +Subproject commit 8b20498b954609770205c2cc794b868b4ac3ee69-dirty

                    if (!line.Contains("Subproject"))
                    {
                        continue;
                    }

                    char         c         = line[0];
                    const string commitStr = "commit ";
                    string       hash      = "";
                    int          pos       = line.IndexOf(commitStr);
                    if (pos >= 0)
                    {
                        hash = line.Substring(pos + commitStr.Length);
                    }

                    bool endsWithDirty = hash.EndsWith("-dirty");
                    hash = hash.Replace("-dirty", "");
                    if (c == '-')
                    {
                        oldCommitId = ObjectId.Parse(hash);
                    }
                    else if (c == '+')
                    {
                        commitId = ObjectId.Parse(hash);
                        isDirty  = endsWithDirty;
                    }

                    // TODO: Support combined merge
                }
            }

            if (oldCommitId != null && commitId != null)
            {
                var submodule = module.GetSubmodule(fileName);
                addedCommits   = submodule.GetCommitCount(commitId.ToString(), oldCommitId.ToString());
                removedCommits = submodule.GetCommitCount(oldCommitId.ToString(), commitId.ToString());
            }

            return(new GitSubmoduleStatus(name, oldName, isDirty, commitId, oldCommitId, addedCommits, removedCommits));
        }
Example #3
0
        public static string ProcessSubmoduleStatus([NotNull] VsrModule module, [NotNull] GitSubmoduleStatus status)
        {
            if (module == null)
            {
                throw new ArgumentNullException(nameof(module));
            }

            if (status == null)
            {
                throw new ArgumentNullException(nameof(status));
            }

            VsrModule gitModule = module.GetSubmodule(status.Name);
            var       sb        = new StringBuilder();

            sb.AppendLine("Submodule " + status.Name + " Change");

            // TEMP, will be moved in the follow up refactor
            ICommitDataManager commitDataManager = new CommitDataManager(() => gitModule);

            sb.AppendLine();
            sb.AppendLine("From:\t" + (status.OldCommit?.ToString() ?? "null"));
            CommitData oldCommitData = null;

            // TODO File access for Git revision access
            if (gitModule.IsValidVersionrWorkingDir())
            {
                if (status.OldCommit != null)
                {
                    oldCommitData = commitDataManager.GetCommitData(status.OldCommit.ToString(), out _);
                }

                if (oldCommitData != null)
                {
                    sb.AppendLine("\t\t\t\t\t" + GetRelativeDateString(DateTime.UtcNow, oldCommitData.CommitDate.UtcDateTime) + " (" + GetFullDateString(oldCommitData.CommitDate) + ")");
                    var delimiter = new[] { '\n', '\r' };
                    var lines     = oldCommitData.Body.Trim(delimiter).Split(new[] { "\r\n" }, 0);
                    foreach (var line in lines)
                    {
                        sb.AppendLine("\t\t" + line);
                    }
                }
            }
            else
            {
                sb.AppendLine();
            }

            sb.AppendLine();
            string dirty = !status.IsDirty ? "" : " (dirty)";

            sb.AppendLine("To:\t\t" + (status.Commit?.ToString() ?? "null") + dirty);
            CommitData commitData = null;

            // TODO File access for Git revision access
            if (gitModule.IsValidVersionrWorkingDir())
            {
                if (status.Commit != null)
                {
                    commitData = commitDataManager.GetCommitData(status.Commit.ToString(), out _);
                }

                if (commitData != null)
                {
                    sb.AppendLine("\t\t\t\t\t" + GetRelativeDateString(DateTime.UtcNow, commitData.CommitDate.UtcDateTime) + " (" + GetFullDateString(commitData.CommitDate) + ")");
                    var delimiter = new[] { '\n', '\r' };
                    var lines     = commitData.Body.Trim(delimiter).Split(new[] { "\r\n" }, 0);
                    foreach (var line in lines)
                    {
                        sb.AppendLine("\t\t" + line);
                    }
                }
            }
            else
            {
                sb.AppendLine();
            }

            sb.AppendLine();
            var submoduleStatus = gitModule.CheckSubmoduleStatus(status.Commit, status.OldCommit, commitData, oldCommitData);

            sb.Append("Type: ");
            switch (submoduleStatus)
            {
            case SubmoduleStatus.NewSubmodule:
                sb.AppendLine("New submodule");
                break;

            case SubmoduleStatus.FastForward:
                sb.AppendLine("Fast Forward");
                break;

            case SubmoduleStatus.Rewind:
                sb.AppendLine("Rewind");
                break;

            case SubmoduleStatus.NewerTime:
                sb.AppendLine("Newer commit time");
                break;

            case SubmoduleStatus.OlderTime:
                sb.AppendLine("Older commit time");
                break;

            case SubmoduleStatus.SameTime:
                sb.AppendLine("Same commit time");
                break;

            default:
                sb.AppendLine("Unknown");
                break;
            }

            if (status.AddedCommits != null && status.RemovedCommits != null &&
                (status.AddedCommits != 0 || status.RemovedCommits != 0))
            {
                sb.Append("\nCommits: ");

                if (status.RemovedCommits > 0)
                {
                    sb.Append(status.RemovedCommits + " removed");

                    if (status.AddedCommits > 0)
                    {
                        sb.Append(", ");
                    }
                }

                if (status.AddedCommits > 0)
                {
                    sb.Append(status.AddedCommits + " added");
                }

                sb.AppendLine();
            }

            if (status.Commit != null && status.OldCommit != null)
            {
                if (status.IsDirty)
                {
                    string statusText = gitModule.GetStatusText(untracked: false);
                    if (!string.IsNullOrEmpty(statusText))
                    {
                        sb.AppendLine("\nStatus:");
                        sb.Append(statusText);
                    }
                }

                string diffs = gitModule.GetDiffFilesText(status.OldCommit.ToString(), status.Commit.ToString());
                if (!string.IsNullOrEmpty(diffs))
                {
                    sb.AppendLine("\nDifferences:");
                    sb.Append(diffs);
                }
            }

            return(sb.ToString());
        }
Example #4
0
 public VsrModule GetSubmodule(VsrModule module)
 {
     return(module.GetSubmodule(Name));
 }