Exemple #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, 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>
        /// Parses <paramref name="data"/> into a <see cref="CommitData"/> object.
        /// </summary>
        /// <param name="data">Data produced by a <c>git log</c> or <c>git show</c> command where <c>--format</c>
        /// was provided the string <see cref="CommitDataFormat"/>.</param>
        /// <returns>CommitData object populated with parsed info from git string.</returns>
        internal CommitData CreateFromFormattedData(string data)
        {
            if (data is null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            var module = GetModule();

            // $ git log --pretty="format:%H%n%T%n%P%n%aN <%aE>%n%at%n%cN <%cE>%n%ct%n%e%n%B%nNotes:%n%-N" -1
            // 4bc1049fc3b9191dbd390e1ae6885aedd1a4e34b
            // a59c21f0b2e6f43ae89b76a216f9f6124fc359f8
            // 8e3873685d89f8cb543657d1b9e66e516cae7e1d dfd353d3b02d24a0d98855f6a1848c51d9ba4d6b
            // RussKie <*****@*****.**>
            // 1521115435
            // GitHub <*****@*****.**>
            // 1521115435
            //
            // Merge pull request #4615 from drewnoakes/modernise-3
            //
            // New language features
            // Notes:

            // commit id
            // tree id
            // parent ids (separated by spaces)
            // author
            // authored date (unix time)
            // committer
            // committed date (unix time)
            // encoding (may be blank)
            // diff notes
            // ...

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

            var guid = ObjectId.Parse(lines[0]);

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

            // TODO: we can use this to add more relationship info like gitk does if wanted
            var parentIds      = lines[2].LazySplit(' ').Where(id => !string.IsNullOrWhiteSpace(id)).Select(id => ObjectId.Parse(id)).ToList();
            var author         = module.ReEncodeStringFromLossless(lines[3]);
            var authorDate     = DateTimeUtils.ParseUnixTime(lines[4]);
            var committer      = module.ReEncodeStringFromLossless(lines[5]);
            var commitDate     = DateTimeUtils.ParseUnixTime(lines[6]);
            var commitEncoding = lines[7];
            var message        = ProcessDiffNotes(startIndex: 8, lines);

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

            Validates.NotNull(author);
            Validates.NotNull(committer);

            return(new CommitData(guid, treeGuid, parentIds, author, authorDate, committer, commitDate, body));
        }
Exemple #3
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);
        }