Esempio n. 1
0
        public static string GetSubmoduleText(GitModule superproject, string name, string hash)
        {
            StringBuilder sb = new StringBuilder();

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

            if (module.ValidWorkingDir())
            {
                string     error = "";
                CommitData data  = CommitData.GetCommitData(module, hash, ref error);
                if (data == null)
                {
                    sb.AppendLine("Commit hash:\t" + hash);
                    return(sb.ToString());
                }

                string header = data.GetHeaderPlain();
                string body   = "\n" + data.Body.Trim();
                sb.AppendLine(header);
                sb.Append(body);
            }
            else
            {
                sb.AppendLine("Commit hash:\t" + hash);
            }
            return(sb.ToString());
        }
Esempio n. 2
0
        /// <summary>
        /// Gets the commit info for submodule.
        /// </summary>
        public static void UpdateCommitMessage(CommitData data, GitModule module, string sha1, ref string error)
        {
            if (module == null)
                throw new ArgumentNullException("module");
            if (sha1 == null)
                throw new ArgumentNullException("sha1");

            //Do not cache this command, since notes can be added
            string arguments = string.Format(CultureInfo.InvariantCulture,
                    "log -1 --pretty=\"format:" + ShortLogFormat + "\" {0}", sha1);
            var info = module.RunGitCmd(arguments, GitModule.LosslessEncoding);

            if (info.Trim().StartsWith("fatal"))
            {
                error = "Cannot find commit " + sha1;
                return;
            }

            int index = info.IndexOf(sha1) + sha1.Length;

            if (index < 0)
            {
                error = "Cannot find commit " + sha1;
                return;
            }
            if (index >= info.Length)
            {
                error = info;
                return;
            }

            UpdateBodyInCommitData(data, info, module);
        }
        /// <summary>
        /// Gets the commit info for submodule.
        /// </summary>
        public void UpdateCommitMessage(CommitData data, string sha1, ref string error)
        {
            var module = GetModule();

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

            // Do not cache this command, since notes can be added
            string arguments = string.Format(CultureInfo.InvariantCulture,
                                             "log -1 --pretty=\"format:" + ShortLogFormat + "\" {0}", sha1);
            var info = module.RunGitCmd(arguments, GitModule.LosslessEncoding);

            if (info.Trim().StartsWith("fatal"))
            {
                error = "Cannot find commit " + sha1;
                return;
            }

            int index = info.IndexOf(sha1) + sha1.Length;

            if (index < 0)
            {
                error = "Cannot find commit " + sha1;
                return;
            }
            if (index >= info.Length)
            {
                error = info;
                return;
            }

            UpdateBodyInCommitData(data, info);
        }
        /// <inheritdoc />
        public void UpdateBody(CommitData commitData, out string error)
        {
            if (!TryGetCommitLog(commitData.Guid, ShortLogFormat, out error, out var data))
            {
                return;
            }

            // $ git log --pretty="format:%H%n%e%n%B%nNotes:%n%-N" -1
            // 8c601c9bb040e575af75c9eee6e14441e2a1b207
            //
            // Remove redundant parameter
            //
            // The sha1 parameter must match CommitData.Guid.
            // There's no point passing it. It only creates opportunity for bugs.
            //
            // Notes:

            // commit id
            // encoding
            // commit message
            // ...

            var lines = data.Split('\n');

            var guid           = lines[0];
            var commitEncoding = lines[1];
            var message        = ProcessDiffNotes(startIndex: 2, lines);

            Debug.Assert(commitData.Guid == guid, "Guid in response doesn't match that of request");

            // Commit message is not reencoded by git when format is given
            commitData.Body = GetModule().ReEncodeCommitMessage(message, commitEncoding);
        }
Esempio n. 5
0
        /// <inheritdoc />
        public void UpdateBody(CommitData commitData, bool appendNotesOnly, out string?error)
        {
            if (!TryGetCommitLog(commitData.ObjectId.ToString(), appendNotesOnly ? NotesFormat : BodyAndNotesFormat, out error, out var data, cache: false))
            {
                return;
            }

            if (appendNotesOnly)
            {
                if (!string.IsNullOrWhiteSpace(data))
                {
                    commitData.Body += $"\nNotes:\n    {GetModule().ReEncodeCommitMessage(data)}";
                }
            }
            else
            {
                // $ git log --pretty="format:%B%nNotes:%n%-N" -1
                // Remove redundant parameter
                //
                // The sha1 parameter must match CommitData.Guid.
                // There's no point passing it. It only creates opportunity for bugs.
                //
                // Notes:

                var lines = data.Split(Delimiters.LineFeed);

                // Commit message is not re-encoded by Git when format is given
                commitData.Body = GetModule().ReEncodeCommitMessage(ProcessDiffNotes(startIndex: 0, lines));
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Creates a CommitData object from formated commit info data from git.  The string passed in should be
        /// exact output of a log or show command using --format=LogFormat.
        /// </summary>
        /// <param name="data">Formated commit data from git.</param>
        /// <returns>CommitData object populated with parsed info from git string.</returns>
        public static CommitData CreateFromFormatedData(string data, GitModule aModule)
        {
            if (data == null)
            {
                throw new ArgumentNullException("Data");
            }

            var lines = data.Split('\n');

            var guid = lines[0];

            // TODO: we can use this to add more relationship info like gitk does if wanted
            var treeGuid = lines[1];

            // TODO: we can use this to add more relationship info like gitk does if wanted
            string[] parentLines = lines[2].Split(new char[] { ' ' });
            ReadOnlyCollection <string> parentGuids = parentLines.ToList().AsReadOnly();

            var author     = aModule.ReEncodeStringFromLossless(lines[3]);
            var authorDate = DateTimeUtils.ParseUnixTime(lines[4]);

            var committer  = aModule.ReEncodeStringFromLossless(lines[5]);
            var commitDate = DateTimeUtils.ParseUnixTime(lines[6]);

            string commitEncoding = lines[7];

            int startIndex = 8;
            int endIndex   = lines.Length - 1;

            if (lines[endIndex] == "Notes:")
            {
                endIndex--;
            }

            var  message     = new StringBuilder();
            bool bNotesStart = false;

            for (int i = startIndex; i <= endIndex; i++)
            {
                string line = lines[i];
                if (bNotesStart)
                {
                    line = "    " + line;
                }
                message.AppendLine(line);
                if (lines[i] == "Notes:")
                {
                    bNotesStart = true;
                }
            }

            //commit message is not reencoded by git when format is given
            var body = aModule.ReEncodeCommitMessage(message.ToString(), commitEncoding);

            var commitInformation = new CommitData(guid, treeGuid, parentGuids, author, authorDate,
                                                   committer, commitDate, body);

            return(commitInformation);
        }
        /// <summary>
        /// Gets the commit info from CommitData.
        /// </summary>
        /// <returns></returns>
        public static CommitInformation GetCommitInfo(CommitData data)
        {
            if (data == null)
                throw new ArgumentNullException("data");

            string header = data.GetHeader();
            string body = "\n" + WebUtility.HtmlEncode(data.Body.Trim());

            return new CommitInformation(header, body);
        }
Esempio n. 8
0
        public CommitData GetOldCommitData(GitModule submodule)
        {
            if (submodule == null || !submodule.ValidWorkingDir())
            {
                return(null);
            }

            string error = "";

            return(CommitData.GetCommitData(submodule, OldCommit, ref error));
        }
        /// <summary>
        /// Gets the commit info from CommitData.
        /// </summary>
        /// <returns></returns>
        public static CommitInformation GetCommitInfo(CommitData data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            string header = data.GetHeader();
            string body   = "\n" + WebUtility.HtmlEncode(data.Body.Trim());

            return(new CommitInformation(header, body));
        }
Esempio n. 10
0
        /// <summary>
        /// Creates a CommitData object from Git revision.
        /// </summary>
        /// <param name="revision">Git commit.</param>
        /// <returns>CommitData object populated with parsed info from git string.</returns>
        public CommitData CreateFromRevision(GitRevision revision)
        {
            if (revision == null)
            {
                throw new ArgumentNullException("revision");
            }

            CommitData data = new CommitData(revision.Guid, revision.TreeGuid, revision.ParentGuids.ToList().AsReadOnly(),
                                             String.Format("{0} <{1}>", revision.Author, revision.AuthorEmail), revision.AuthorDate,
                                             String.Format("{0} <{1}>", revision.Committer, revision.CommitterEmail), revision.CommitDate,
                                             revision.Body ?? revision.Subject);

            return(data);
        }
Esempio n. 11
0
        /// <summary>
        /// Gets the commit info for module.
        /// </summary>
        /// <param name="module">Git module.</param>
        /// <param name="sha1">The sha1.</param>
        /// <returns></returns>
        public static CommitInformation GetCommitInfo(GitModule module, string sha1)
        {
            string     error = "";
            CommitData data  = CommitData.GetCommitData(module, sha1, ref error);

            if (data == null)
            {
                return(new CommitInformation(error, ""));
            }

            string header = data.GetHeader();
            string body   = "\n" + WebUtility.HtmlEncode(data.Body.Trim());

            return(new CommitInformation(header, body));
        }
Esempio n. 12
0
        /// <summary>
        /// Gets the commit info for submodule.
        /// </summary>
        public static CommitData GetCommitData(GitModule module, string sha1, ref string error)
        {
            if (module == null)
            {
                throw new ArgumentNullException("module");
            }
            if (sha1 == null)
            {
                throw new ArgumentNullException("sha1");
            }

            //Do not cache this command, since notes can be added
            string arguments = string.Format(CultureInfo.InvariantCulture,
                                             "log -1 --pretty=\"format:" + LogFormat + "\" {0}", sha1);
            var info =
                module.RunCmd(
                    Settings.GitCommand,
                    arguments,
                    GitModule.LosslessEncoding
                    );

            if (info.Trim().StartsWith("fatal"))
            {
                error = "Cannot find commit " + sha1;
                return(null);
            }

            int index = info.IndexOf(sha1) + sha1.Length;

            if (index < 0)
            {
                error = "Cannot find commit " + sha1;
                return(null);
            }
            if (index >= info.Length)
            {
                error = info;
                return(null);
            }

            CommitData commitInformation = CreateFromFormatedData(info, module);

            return(commitInformation);
        }
Esempio n. 13
0
        /// <summary>
        /// Creates a CommitData object from formated commit info data from git.  The string passed in should be
        /// exact output of a log or show command using --format=LogFormat.
        /// </summary>
        /// <param name="data">Formated commit data from git.</param>
        /// <returns>CommitData object populated with parsed info from git string.</returns>
        public static void UpdateBodyInCommitData(CommitData commitData, string data, GitModule aModule)
        {
            if (data == null)
            {
                throw new ArgumentNullException("Data");
            }

            var lines = data.Split('\n');

            var guid = lines[0];

            string commitEncoding = lines[1];

            int startIndex = 2;
            int endIndex   = lines.Length - 1;

            if (lines[endIndex] == "Notes:")
            {
                endIndex--;
            }

            var  message     = new StringBuilder();
            bool bNotesStart = false;

            for (int i = startIndex; i <= endIndex; i++)
            {
                string line = lines[i];
                if (bNotesStart)
                {
                    line = "    " + line;
                }
                message.AppendLine(line);
                if (lines[i] == "Notes:")
                {
                    bNotesStart = true;
                }
            }

            //commit message is not reencoded by git when format is given
            Debug.Assert(commitData.Guid == guid);
            commitData.Body = aModule.ReEncodeCommitMessage(message.ToString(), commitEncoding);
        }
Esempio n. 14
0
        /// <summary>
        /// Creates a CommitData object from formated commit info data from git.  The string passed in should be
        /// exact output of a log or show command using --format=LogFormat.
        /// </summary>
        /// <param name="data">Formated commit data from git.</param>
        /// <returns>CommitData object populated with parsed info from git string.</returns>
        public static void UpdateBodyInCommitData(CommitData commitData, string data, GitModule aModule)
        {
            if (data == null)
            {
                throw new ArgumentNullException("Data");
            }

            var lines = data.Split('\n');

            var guid = lines[0];

            string commitEncoding = lines[1];

            const int startIndex = 2;
            string    message    = ProccessDiffNotes(startIndex, lines);

            //commit message is not reencoded by git when format is given
            Debug.Assert(commitData.Guid == guid);
            commitData.Body = aModule.ReEncodeCommitMessage(message, commitEncoding);
        }
Esempio n. 15
0
        /// <summary>
        /// Gets the commit info for submodule.
        /// </summary>
        /// <param name="sha1">The sha1.</param>
        /// <returns></returns>
        public static CommitData GetCommitData(GitModule module, string sha1, ref string error)
        {
            if (module == null)
            {
                throw new ArgumentNullException("module");
            }
            if (sha1 == null)
            {
                throw new ArgumentNullException("sha1");
            }

            //Do not cache this command, since notes can be added
            string info = module.RunGitCmd(
                string.Format(
                    "log -1 --pretty=raw --show-notes=* {0}", sha1));

            if (info.Trim().StartsWith("fatal"))
            {
                error = "Cannot find commit" + sha1;
                return(null);
            }

            info = RemoveRedundancies(info);

            int index = info.IndexOf(sha1) + sha1.Length;

            if (index < 0)
            {
                error = "Cannot find commit" + sha1;
                return(null);
            }
            if (index >= info.Length)
            {
                error = info;
                return(null);
            }

            CommitData commitInformation = CreateFromRawData(info);

            return(commitInformation);
        }
Esempio n. 16
0
        /// <summary>
        /// Creates a CommitData object from formated commit info data from git.  The string passed in should be
        /// exact output of a log or show command using --format=LogFormat.
        /// </summary>
        /// <param name="commitData"></param>
        /// <param name="data">Formated commit data from git.</param>
        /// <returns>CommitData object populated with parsed info from git string.</returns>
        public void UpdateBodyInCommitData(CommitData commitData, string data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            var module = GetModule();

            var lines = data.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);

            var guid = lines[0];

            string commitEncoding = lines[1];

            const int startIndex = 2;
            string    message    = ProccessDiffNotes(startIndex, lines);

            //commit message is not reencoded by git when format is given
            Debug.Assert(commitData.Guid == guid);
            commitData.Body = module.ReEncodeCommitMessage(message, commitEncoding);
        }
Esempio n. 17
0
        /// <summary>
        /// Creates a CommitData object from formated commit info data from git.  The string passed in should be
        /// exact output of a log or show command using --format=LogFormat.
        /// </summary>
        /// <param name="data">Formated commit data from git.</param>
        /// <returns>CommitData object populated with parsed info from git string.</returns>
        public CommitData CreateFromFormatedData(string data)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            var module = GetModule();

            var lines = data.Split('\n');

            var guid = lines[0];

            // TODO: we can use this to add more relationship info like gitk does if wanted
            var treeGuid = lines[1];

            // TODO: we can use this to add more relationship info like gitk does if wanted
            string[] parentLines = lines[2].Split(' ');
            ReadOnlyCollection <string> parentGuids = parentLines.ToList().AsReadOnly();

            var author     = module.ReEncodeStringFromLossless(lines[3]);
            var authorDate = DateTimeUtils.ParseUnixTime(lines[4]);

            var committer  = module.ReEncodeStringFromLossless(lines[5]);
            var commitDate = DateTimeUtils.ParseUnixTime(lines[6]);

            string commitEncoding = lines[7];

            const int startIndex = 8;
            string    message    = ProccessDiffNotes(startIndex, lines);

            // commit message is not reencoded by git when format is given
            var body = module.ReEncodeCommitMessage(message, commitEncoding);

            var commitInformation = new CommitData(guid, treeGuid, parentGuids, author, authorDate,
                                                   committer, commitDate, body);

            return(commitInformation);
        }
        /// <summary>
        /// Gets the commit info from CommitData.
        /// </summary>
        /// <returns></returns>
        public static CommitInformation GetCommitInfo(CommitData data)
        {
            if (data == null)
                throw new ArgumentNullException("data");

            return CreateCommitInformation(data);
        }
Esempio n. 19
0
 private CommitData ProvideCommitData(string commitId)
 {
     if (!_historyGraph.ContainsKey(commitId))
     {
         CommitData dta = new CommitData();
         _historyGraph.Add(commitId, dta);
         return dta;
     }
     else
     {
         return _historyGraph[commitId];
     }
 }
Esempio n. 20
0
        public static string ProcessSubmodulePatch(GitModule module, string text)
        {
            var           status    = GetSubmoduleStatus(text);
            GitModule     gitmodule = module.GetSubmodule(status.Name);
            StringBuilder sb        = new StringBuilder();

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

            sb.AppendLine();
            sb.AppendLine("From:\t" + status.OldCommit);
            if (gitmodule.ValidWorkingDir())
            {
                string     error      = "";
                CommitData commitData = CommitData.GetCommitData(gitmodule, status.OldCommit, 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);
                    }
                }
            }
            else
            {
                sb.AppendLine();
            }

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

            sb.AppendLine("To:\t\t" + status.Commit + dirty);
            if (gitmodule.ValidWorkingDir())
            {
                string     error      = "";
                CommitData commitData = CommitData.GetCommitData(gitmodule, status.Commit, 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);
                    }
                }
            }
            else
            {
                sb.AppendLine();
            }

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

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

            return(sb.ToString());
        }
Esempio n. 21
0
        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");
                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");
                        hash = hash.Replace("-dirty", "");
                        string dirty = !bdirty ? "" : " (dirty)";
                        if (c == '-')
                        {
                            sb.AppendLine("From:\t" + hash + dirty);
                        }
                        else if (c == '+')
                        {
                            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);
                                }
                            }
                        }
                        else
                        {
                            sb.AppendLine();
                        }
                    }
                }
            }
            return(sb.ToString());
        }
Esempio n. 22
0
        /// <summary>
        /// Creates a CommitData object from Git revision.
        /// </summary>
        /// <param name="revision">Git commit.</param>
        /// <returns>CommitData object populated with parsed info from git string.</returns>
        public static CommitData CreateFromRevision(GitRevision revision)
        {
            if (revision == null)
                throw new ArgumentNullException("revision");

            CommitData data = new CommitData(revision.Guid, revision.TreeGuid, revision.ParentGuids.ToList().AsReadOnly(),
                String.Format("{0} <{1}>", revision.Author, revision.AuthorEmail), revision.AuthorDate,
                String.Format("{0} <{1}>", revision.Committer, revision.CommitterEmail), revision.CommitDate,
                revision.Message);
            return data;
        }
Esempio n. 23
0
        /// <summary>
        /// Creates a CommitData object from formated commit info data from git.  The string passed in should be
        /// exact output of a log or show command using --format=LogFormat.
        /// </summary>
        /// <param name="data">Formated commit data from git.</param>
        /// <returns>CommitData object populated with parsed info from git string.</returns>
        public static void UpdateBodyInCommitData(CommitData commitData, string data, GitModule aModule)
        {
            if (data == null)
                throw new ArgumentNullException("Data");

            var lines = data.Split('\n');

            var guid = lines[0];

            string commitEncoding = lines[1];

            int startIndex = 2;
            int endIndex = lines.Length - 1;
            if (lines[endIndex] == "Notes:")
                endIndex--;

            var message = new StringBuilder();
            bool bNotesStart = false;
            for (int i = startIndex; i <= endIndex; i++)
            {
                string line = lines[i];
                if (bNotesStart)
                    line = "    " + line;
                message.AppendLine(line);
                if (lines[i] == "Notes:")
                    bNotesStart = true;
            }

            //commit message is not reencoded by git when format is given
            Debug.Assert(commitData.Guid == guid);
            commitData.Body = aModule.ReEncodeCommitMessage(message.ToString(), commitEncoding);
        }
Esempio n. 24
0
        /// <summary>
        /// Creates a CommitData object from formated commit info data from git.  The string passed in should be
        /// exact output of a log or show command using --format=LogFormat.
        /// </summary>
        /// <param name="data">Formated commit data from git.</param>
        /// <returns>CommitData object populated with parsed info from git string.</returns>
        public static CommitData CreateFromFormatedData(string data)
        {
            if (data == null)
                throw new ArgumentNullException("Data");

            var lines = data.Split('\n');

            var guid = lines[0];

            // TODO: we can use this to add more relationship info like gitk does if wanted
            var treeGuid = lines[1];

            // TODO: we can use this to add more relationship info like gitk does if wanted
            string[] parentLines = lines[2].Split(new char[]{' '});
            ReadOnlyCollection<string> parentGuids = parentLines.ToList().AsReadOnly();

            var author = GitCommandHelpers.ReEncodeStringFromLossless(lines[3]);
            var authorDate = GetTimeFromUtcTimeLine(lines[4]);

            var committer = GitCommandHelpers.ReEncodeStringFromLossless(lines[5]);
            var commitDate = GetTimeFromUtcTimeLine(lines[6]);

            string commitEncoding = lines[7];

            int startIndex = 8;
            int endIndex = lines.Length - 1;
            if (lines[endIndex] == "Notes:")
                endIndex--;

            var message = new StringBuilder();
            bool bNotesStart = false;
            for (int i = startIndex; i <= endIndex; i++)
            {
                string line = lines[i];
                if (bNotesStart)
                    line = "    " + line;
                message.AppendLine(line);
                if (lines[i] == "Notes:")
                    bNotesStart = true;
            }

            //commit message is not reencoded by git when format is given
            var body = GitCommandHelpers.ReEncodeCommitMessage(message.ToString(), commitEncoding);

            var commitInformation = new CommitData(guid, treeGuid, parentGuids, author, authorDate,
                committer, commitDate, body);

            return commitInformation;
        }
        private static CommitInformation CreateCommitInformation(CommitData data)
        {
            string header = data.GetHeader();
            string body = "\n\n" + HttpUtility.HtmlEncode(data.Body.Trim()) + "\n\n";

            return new CommitInformation(header, body);
        }
Esempio n. 26
0
        public static string ProcessSubmodulePatch(GitModule module, string text)
        {
            StringBuilder sb = new StringBuilder();

            using (StringReader reader = new StringReader(text))
            {
                string line       = reader.ReadLine();
                string moduleName = "";
                if (line != null)
                {
                    var match = Regex.Match(line, @"diff --git a/(\S+) b/(\S+)");
                    if (match != null && match.Groups.Count > 0)
                    {
                        moduleName = match.Groups[1].Value;
                    }
                }
                sb.AppendLine("Submodule " + moduleName + " 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);
                        }

                        GitModule gitmodule = module.GetSubmodule(moduleName);

                        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());
        }
        /// <summary>
        /// Creates a CommitData object from raw commit info data from git.  The string passed in should be
        /// exact output of a log or show command using --format=raw.
        /// </summary>
        /// <param name="rawData">Raw commit data from git.</param>
        /// <returns>CommitData object populated with parsed info from git string.</returns>
        public static CommitData CreateFromRawData(string rawData)
        {
            if (rawData == null)
                throw new ArgumentNullException("rawData");

            var lines = new List<string>(rawData.Split('\n'));

            var commit = lines.Single(l => l.StartsWith(COMMIT_LABEL));
            var guid = commit.Substring(COMMIT_LABEL.Length);
            lines.Remove(commit);

            // TODO: we can use this to add more relationship info like gitk does if wanted
            var tree = lines.Single(l => l.StartsWith(TREE_LABEL));
            var treeGuid = tree.Substring(TREE_LABEL.Length);
            lines.Remove(tree);

            // TODO: we can use this to add more relationship info like gitk does if wanted
            List<string> parentLines = lines.FindAll(l => l.StartsWith(PARENT_LABEL));
            ReadOnlyCollection<string> parentGuids = parentLines.Select(parent => parent.Substring(PARENT_LABEL.Length)).ToList().AsReadOnly();
            lines.RemoveAll(parentLines.Contains);

            var authorInfo = lines.Single(l => l.StartsWith(AUTHOR_LABEL));
            var author = GetPersonFromAuthorInfoLine(authorInfo, AUTHOR_LABEL.Length);
            var authorDate = GetTimeFromAuthorInfoLine(authorInfo);
            lines.Remove(authorInfo);

            var committerInfo = lines.Single(l => l.StartsWith(COMMITTER_LABEL));
            var committer = GetPersonFromAuthorInfoLine(committerInfo, COMMITTER_LABEL.Length);
            var commitDate = GetTimeFromAuthorInfoLine(committerInfo);
            lines.Remove(committerInfo);

            var message = new StringBuilder();
            foreach (var line in lines)
                message.AppendLine(line);

            var body = message.ToString();

            //We need to recode the commit message because of a bug in Git.
            //We cannot let git recode the message to Settings.Encoding which is
            //needed to allow the "git log" to print the filename in Settings.Encoding
            Encoding logoutputEncoding = Settings.Module.GetLogoutputEncoding();
            if (logoutputEncoding != Settings.Encoding)
                body = logoutputEncoding.GetString(Settings.Encoding.GetBytes(body));

            var commitInformation = new CommitData(guid, treeGuid, parentGuids, author, authorDate,
                committer, commitDate, body);

            return commitInformation;
        }
Esempio n. 28
0
        /// <summary>
        /// Creates a CommitData object from formated commit info data from git.  The string passed in should be
        /// exact output of a log or show command using --format=LogFormat.
        /// </summary>
        /// <param name="data">Formated commit data from git.</param>
        /// <returns>CommitData object populated with parsed info from git string.</returns>
        public static void UpdateBodyInCommitData(CommitData commitData, string data, GitModule aModule)
        {
            if (data == null)
                throw new ArgumentNullException("data");

            var lines = data.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);

            var guid = lines[0];

            string commitEncoding = lines[1];

            const int startIndex = 2;
            string message = ProccessDiffNotes(startIndex, lines);

            //commit message is not reencoded by git when format is given
            Debug.Assert(commitData.Guid == guid);
            commitData.Body = aModule.ReEncodeCommitMessage(message, commitEncoding);
        }
Esempio n. 29
0
        /// <summary>
        /// Creates a CommitData object from raw commit info data from git.  The string passed in should be
        /// exact output of a log or show command using --format=raw.
        /// </summary>
        /// <param name="rawData">Raw commit data from git.</param>
        /// <returns>CommitData object populated with parsed info from git string.</returns>
        public static CommitData CreateFromRawData(string rawData)
        {
            if (rawData == null)
            {
                throw new ArgumentNullException("rawData");
            }

            var lines = new List <string>(rawData.Split('\n'));

            var commit = lines.Single(l => l.StartsWith(COMMIT_LABEL));
            var guid   = commit.Substring(COMMIT_LABEL.Length);

            lines.Remove(commit);

            // TODO: we can use this to add more relationship info like gitk does if wanted
            var tree     = lines.Single(l => l.StartsWith(TREE_LABEL));
            var treeGuid = tree.Substring(TREE_LABEL.Length);

            lines.Remove(tree);

            // TODO: we can use this to add more relationship info like gitk does if wanted
            List <string> parentLines = lines.FindAll(l => l.StartsWith(PARENT_LABEL));
            ReadOnlyCollection <string> parentGuids = parentLines.Select(parent => parent.Substring(PARENT_LABEL.Length)).ToList().AsReadOnly();

            lines.RemoveAll(parentLines.Contains);

            var authorInfo = lines.Single(l => l.StartsWith(AUTHOR_LABEL));
            var author     = GetPersonFromAuthorInfoLine(authorInfo, AUTHOR_LABEL.Length);
            var authorDate = GetTimeFromAuthorInfoLine(authorInfo);

            lines.Remove(authorInfo);

            var committerInfo = lines.Single(l => l.StartsWith(COMMITTER_LABEL));
            var committer     = GetPersonFromAuthorInfoLine(committerInfo, COMMITTER_LABEL.Length);
            var commitDate    = GetTimeFromAuthorInfoLine(committerInfo);

            lines.Remove(committerInfo);

            var message = new StringBuilder();

            foreach (var line in lines)
            {
                message.AppendLine(line);
            }

            var body = message.ToString();

            //We need to recode the commit message because of a bug in Git.
            //We cannot let git recode the message to Settings.Encoding which is
            //needed to allow the "git log" to print the filename in Settings.Encoding
            Encoding logoutputEncoding = Settings.Module.GetLogoutputEncoding();

            if (logoutputEncoding != Settings.Encoding)
            {
                body = logoutputEncoding.GetString(Settings.Encoding.GetBytes(body));
            }

            var commitInformation = new CommitData(guid, treeGuid, parentGuids, author, authorDate,
                                                   committer, commitDate, body);

            return(commitInformation);
        }
Esempio n. 30
0
        /// <summary>
        /// Creates a CommitData object from raw commit info data from git.  The string passed in should be
        /// exact output of a log or show command using --format=raw.
        /// </summary>
        /// <param name="rawData">Raw commit data from git.</param>
        /// <returns>CommitData object populated with parsed info from git string.</returns>
        public static CommitData CreateFromRawData(string rawData)
        {
            if (rawData == null)
                throw new ArgumentNullException("rawData");

            var lines = new List<string>(rawData.Split('\n'));

            var commit = lines.Single(l => l.StartsWith(COMMIT_LABEL));
            var guid = commit.Substring(COMMIT_LABEL.Length);
            lines.Remove(commit);

            // TODO: we can use this to add more relationship info like gitk does if wanted
            var tree = lines.Single(l => l.StartsWith(TREE_LABEL));
            var treeGuid = tree.Substring(TREE_LABEL.Length);
            lines.Remove(tree);

            // TODO: we can use this to add more relationship info like gitk does if wanted
            List<string> parentLines = lines.FindAll(l => l.StartsWith(PARENT_LABEL));
            ReadOnlyCollection<string> parentGuids = parentLines.Select(parent => parent.Substring(PARENT_LABEL.Length)).ToList().AsReadOnly();
            lines.RemoveAll(parentLines.Contains);

            var authorInfo = lines.Single(l => l.StartsWith(AUTHOR_LABEL));
            var author = GetPersonFromAuthorInfoLine(authorInfo, AUTHOR_LABEL.Length);
            var authorDate = GetTimeFromAuthorInfoLine(authorInfo);
            lines.Remove(authorInfo);

            var committerInfo = lines.Single(l => l.StartsWith(COMMITTER_LABEL));
            var committer = GetPersonFromAuthorInfoLine(committerInfo, COMMITTER_LABEL.Length);
            var commitDate = GetTimeFromAuthorInfoLine(committerInfo);
            lines.Remove(committerInfo);

            var message = new StringBuilder();
            foreach (var line in lines)
                message.AppendLine(line);

            var body = message.ToString();

            var commitInformation = new CommitData(guid, treeGuid, parentGuids, author, authorDate,
                committer, commitDate, body);

            return commitInformation;
        }
Esempio n. 31
0
        public SubmoduleStatus CheckSubmoduleStatus(string commit, string oldCommit, CommitData data, CommitData olddata, bool loaddata = false)
        {
            if (!IsValidGitWorkingDir() || oldCommit == null)
                return SubmoduleStatus.NewSubmodule;

            if (commit == null || commit == oldCommit)
                return SubmoduleStatus.Unknown;

            string baseCommit = GetMergeBase(commit, oldCommit);
            if (baseCommit == oldCommit)
                return SubmoduleStatus.FastForward;
            else if (baseCommit == commit)
                return SubmoduleStatus.Rewind;

            string error = "";
            if (loaddata)
                olddata = CommitData.GetCommitData(this, oldCommit, ref error);
            if (olddata == null)
                return SubmoduleStatus.NewSubmodule;
            if (loaddata)
                data = CommitData.GetCommitData(this, commit, ref error);
            if (data == null)
                return SubmoduleStatus.Unknown;
            if (data.CommitDate > olddata.CommitDate)
                return SubmoduleStatus.NewerTime;
            else if (data.CommitDate < olddata.CommitDate)
                return SubmoduleStatus.OlderTime;
            else if (data.CommitDate == olddata.CommitDate)
                return SubmoduleStatus.SameTime;
            return SubmoduleStatus.Unknown;
        }
Esempio n. 32
0
        /// <summary>
        /// Creates a CommitData object from formated commit info data from git.  The string passed in should be
        /// exact output of a log or show command using --format=LogFormat.
        /// </summary>
        /// <param name="data">Formated commit data from git.</param>
        /// <returns>CommitData object populated with parsed info from git string.</returns>
        public static CommitData CreateFromFormatedData(string data, GitModule aModule)
        {
            if (data == null)
                throw new ArgumentNullException("data");

            var lines = data.Split('\n');
            
            var guid = lines[0];

            // TODO: we can use this to add more relationship info like gitk does if wanted
            var treeGuid = lines[1];

            // TODO: we can use this to add more relationship info like gitk does if wanted
            string[] parentLines = lines[2].Split(new char[]{' '});
            ReadOnlyCollection<string> parentGuids = parentLines.ToList().AsReadOnly();

            var author = aModule.ReEncodeStringFromLossless(lines[3]);
            var authorDate = DateTimeUtils.ParseUnixTime(lines[4]);

            var committer = aModule.ReEncodeStringFromLossless(lines[5]);
            var commitDate = DateTimeUtils.ParseUnixTime(lines[6]);

            string commitEncoding = lines[7];

            const int startIndex = 8;
            string message = ProccessDiffNotes(startIndex, lines);

            //commit message is not reencoded by git when format is given
            var body = aModule.ReEncodeCommitMessage(message, commitEncoding);

            var commitInformation = new CommitData(guid, treeGuid, parentGuids, author, authorDate,
                committer, commitDate, body);

            return commitInformation;
        }