コード例 #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));
            }
        }
コード例 #2
0
        public static byte[] GetResetUnstagedLinesAsPatch(GitModule module, string text, int selectionPosition, int selectionLength, bool staged, Encoding fileContentEncoding)
        {
            string header;

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

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

            string body = selectedChunks.ToResetUnstagedLinesPatch();

            //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 (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));
            }
        }
コード例 #3
0
        public static byte[] GetResetUnstagedLinesAsPatch(GitModule module, string text, int selectionPosition, int selectionLength, bool staged, Encoding fileContentEncoding)
        {
            string header;

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

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

            string body = selectedChunks.ToResetUnstagedLinesPatch();

            //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 (body != null && "true".Equals(module.GetEffectiveSetting("core.autocrlf"), StringComparison.InvariantCultureIgnoreCase))
            {
                body = body.Replace("\r", "");
            }

            if (header == null || body == null)
            {
                return(null);
            }
            else
            {
                return(GetPatchBytes(header, body, fileContentEncoding));
            }
        }
コード例 #4
0
        public static ChunkList FromNewFile(GitModule module, string text, int selectionPosition, int selectionLength, bool reset, byte[] FilePreabmle, Encoding fileContentEncoding)
        {
            Chunk     chunk  = Chunk.FromNewFile(module, text, selectionPosition, selectionLength, reset, FilePreabmle, fileContentEncoding);
            ChunkList result = new ChunkList();

            result.Add(chunk);
            return(result);
        }
コード例 #5
0
        public static ChunkList FromNewFile(GitModule module, string text, int selectionPosition, int selectionLength, bool reset)
        {
            Chunk     chunk  = Chunk.FromNewFile(module, text, selectionPosition, selectionLength, reset);
            ChunkList result = new ChunkList();

            result.Add(chunk);
            return(result);
        }
コード例 #6
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));
            }
        }
コード例 #7
0
        public static ChunkList GetSelectedChunks(string text, int selectionPosition, int selectionLength, out string header)
        {
            header = null;

            // When there is no patch, return nothing
            if (string.IsNullOrEmpty(text))
            {
                return(null);
            }

            // TODO: handling submodules
            // Divide diff into header and patch
            int patchPos = text.IndexOf("@@");

            if (patchPos < 1)
            {
                return(null);
            }

            header = text.Substring(0, patchPos);
            string diff = text.Substring(patchPos - 1);

            string[]  chunks         = diff.Split(new[] { "\n@@" }, StringSplitOptions.RemoveEmptyEntries);
            ChunkList selectedChunks = new ChunkList();
            int       i          = 0;
            int       currentPos = patchPos - 1;

            while (i < chunks.Length && currentPos <= selectionPosition + selectionLength)
            {
                string chunkStr = chunks[i];
                currentPos += 3;

                // if selection intersects with chunsk
                if (currentPos + chunkStr.Length >= selectionPosition)
                {
                    Chunk chunk = Chunk.ParseChunk(chunkStr, currentPos, selectionPosition, selectionLength);
                    if (chunk != null)
                    {
                        selectedChunks.Add(chunk);
                    }
                }

                currentPos += chunkStr.Length;
                i++;
            }

            return(selectedChunks);
        }