Esempio n. 1
0
        public static byte[] GetSelectedLinesAsPatch(GitModule module, string text, int selectionPosition, int selectionLength, bool staged, Encoding fileContentEncoding, bool isNewFile)
        {
            string header;

            ChunkList selectedChunks = ChunkList.GetSelectedChunks(text, selectionPosition, selectionLength, staged, out header);

            if (selectedChunks == null)
            {
                return(null);
            }

            //if file is new, --- /dev/null has to be replaced by --- a/fileName
            if (isNewFile)
            {
                header = CorrectHeaderForNewFile(header);
            }

            string body = selectedChunks.ToStagePatch(staged, false);

            if (header == null || body == null)
            {
                return(null);
            }
            else
            {
                return(GetPatchBytes(header, body, fileContentEncoding));
            }
        }
Esempio n. 2
0
        public static byte[] GetSelectedLinesAsNewPatch(GitModule module, string newFileName, string text, int selectionPosition, int selectionLength, Encoding fileContentEncoding, bool reset, byte[] filePreabmle)
        {
            StringBuilder sb       = new StringBuilder();
            const string  fileMode = "100000"; // given fake mode to satisfy patch format, git will override this

            sb.Append(string.Format("diff --git a/{0} b/{0}", newFileName));
            sb.Append("\n");
            if (!reset)
            {
                sb.Append("new file mode " + fileMode);
                sb.Append("\n");
            }

            sb.Append("index 0000000..0000000");
            sb.Append("\n");
            if (reset)
            {
                sb.Append("--- a/" + newFileName);
            }
            else
            {
                sb.Append("--- /dev/null");
            }

            sb.Append("\n");
            sb.Append("+++ b/" + newFileName);
            sb.Append("\n");

            string header = sb.ToString();

            ChunkList selectedChunks = ChunkList.FromNewFile(module, text, selectionPosition, selectionLength, reset, filePreabmle, fileContentEncoding);

            if (selectedChunks == null)
            {
                return(null);
            }

            string body = selectedChunks.ToStagePatch(false, true);

            // git apply has problem with dealing with autocrlf
            // I noticed that patch applies when '\r' chars are removed from patch if autocrlf is set to true
            if (reset && body != null && module.EffectiveConfigFile.core.autocrlf.ValueOrDefault == AutoCRLFType.@true)
            {
                body = body.Replace("\r", "");
            }

            if (header == null || body == null)
            {
                return(null);
            }
            else
            {
                return(GetPatchBytes(header, body, fileContentEncoding));
            }
        }