コード例 #1
0
ファイル: FileHeader.cs プロジェクト: kkl713/GitSharp
        /// <summary>
        /// Convert the patch script for this file into a string.
        /// </summary>
        /// <param name="charsetGuess">
        /// optional array to suggest the character set to use when
        /// decoding each file's line. If supplied the array must have a
        /// length of <code><see cref="ParentCount"/> + 1</code>
        /// representing the old revision character sets and the new
        /// revision character set.
        /// </param>
        /// <returns>the patch script, as a Unicode string.</returns>
        public string getScriptText(Encoding[] charsetGuess)
        {
            if (Hunks.Count == 0)
            {
                // If we have no hunks then we can safely assume the entire
                // patch is a binary style patch, or a meta-data only style
                // patch. Either way the encoding of the headers should be
                // strictly 7-bit US-ASCII and the body is either 7-bit ASCII
                // (due to the base 85 encoding used for a BinaryHunk) or is
                // arbitrary noise we have chosen to ignore and not understand
                // (e.g. the message "Binary files ... differ").
                //
                return(RawParseUtils.extractBinaryString(Buffer, StartOffset, EndOffset));
            }

            if (charsetGuess != null && charsetGuess.Length != ParentCount + 1)
            {
                throw new ArgumentException("Expected "
                                            + (ParentCount + 1) + " character encoding guesses");
            }

            if (TrySimpleConversion(charsetGuess))
            {
                Encoding cs = (charsetGuess != null ? charsetGuess[0] : null) ?? Constants.CHARSET;

                try
                {
                    return(RawParseUtils.decodeNoFallback(cs, Buffer, StartOffset, EndOffset));
                }
                catch (EncoderFallbackException)
                {
                    // Try the much slower, more-memory intensive version which
                    // can handle a character set conversion patch.
                }
            }

            var r = new StringBuilder(EndOffset - StartOffset);

            // Always treat the headers as US-ASCII; Git file names are encoded
            // in a C style escape if any character has the high-bit set.
            //
            int hdrEnd = Hunks[0].StartOffset;

            for (int ptr = StartOffset; ptr < hdrEnd;)
            {
                int eol = Math.Min(hdrEnd, RawParseUtils.nextLF(Buffer, ptr));
                r.Append(RawParseUtils.extractBinaryString(Buffer, ptr, eol));
                ptr = eol;
            }

            string[] files   = ExtractFileLines(charsetGuess);
            var      offsets = new int[files.Length];

            foreach (HunkHeader h in Hunks)
            {
                h.extractFileLines(r, files, offsets);
            }

            return(r.ToString());
        }