Ejemplo n.º 1
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);
        }