Esempio n. 1
0
        private bool IsNoNewlineAtEndOfFile(FileHeader fh)
        {
            HunkHeader lastHunk = fh.GetHunks()[fh.GetHunks().Count - 1];
            RawText    lhrt     = new RawText(lastHunk.GetBuffer());

            return(lhrt.GetString(lhrt.Size() - 1).Equals("\\ No newline at end of file"));
        }
Esempio n. 2
0
 private bool IsNoNewlineAtEndOfFile(RawText lastRawTextOrDefault)
 {
     if (lastRawTextOrDefault != null)
     {
         return(lastRawTextOrDefault.GetString(lastRawTextOrDefault.Size() - 1).Equals("\\ No newline at end of file"));
     }
     return(false);
 }
Esempio n. 3
0
        static string[] GetLineStrings(RawText text)
        {
            int lineCount = text.Size();

            string[] lines = new string[lineCount];

            for (int i = 0; i < lineCount; i++)
            {
                lines [i] = text.GetString(i);
            }

            return(lines);
        }
Esempio n. 4
0
        public virtual void TestAddA1()
        {
            ApplyResult result = Init("A1", false, true);

            NUnit.Framework.Assert.AreEqual(1, result.GetUpdatedFiles().Count);
            NUnit.Framework.Assert.AreEqual(new FilePath(db.WorkTree, "A1"), result.GetUpdatedFiles
                                                ()[0]);
            CheckFile(new FilePath(db.WorkTree, "A1"), b.GetString(0, b.Size(), false));
        }
Esempio n. 5
0
        /// <exception cref="System.Exception"></exception>
        private ApplyResult Init(string name, bool preExists, bool postExists)
        {
            Git git = new Git(db);

            if (preExists)
            {
                a = new RawText(ReadFile(name + "_PreImage"));
                Write(new FilePath(db.Directory.GetParent(), name), a.GetString(0, a.Size(), false
                                                                                ));
                git.Add().AddFilepattern(name).Call();
                git.Commit().SetMessage("PreImage").Call();
            }
            if (postExists)
            {
                b = new RawText(ReadFile(name + "_PostImage"));
            }
            return(git.Apply().SetPatch(typeof(DiffFormatterReflowTest).GetResourceAsStream(name
                                                                                            + ".patch")).Call());
        }
Esempio n. 6
0
        /// <exception cref="System.Exception"></exception>
        public ApplyResult Init(string name, bool preExists = true, bool postExists = true)
        {
            Git git = new Git(db);

            if (preExists)
            {
                a = new RawText(ReadFile(name + "_PreImage", useCrlfFiles));
                JGitTestUtil.Write(new FilePath(db.Directory.GetParent(), name), a.GetString(0, a.Size(), false
                                                                                             ));
                git.Add().AddFilepattern(name).Call();
                git.Commit().SetMessage("PreImage").Call();
            }
            if (postExists)
            {
                bool postShouldHaveCrlf = preExists && a.GetLineDelimiter() != null?a.GetLineDelimiter() != "\n" : useCrlfPatches;

                b = new RawText(ReadFile(name + "_PostImage", postShouldHaveCrlf));
            }
            return(git.Apply().SetPatch(typeof(DiffFormatterReflowTest).GetResourceAsStream(name + ".patch", useCrlfPatches)).Call());
        }
Esempio n. 7
0
        public HunkRangeInfo(ITextSnapshot snapshot, Edit edit, RawText originalText, RawText workingText)
        {
            if (snapshot == null)
            {
                throw new ArgumentNullException("snapshot");
            }
            if (edit == null)
            {
                throw new ArgumentNullException("edit");
            }
            if (originalText == null)
            {
                throw new ArgumentNullException("originalText");
            }
            if (workingText == null)
            {
                throw new ArgumentNullException("workingText");
            }

            _snapshot     = snapshot;
            _edit         = edit;
            _originalText = originalText.GetString(edit.GetBeginA(), edit.GetEndA(), true).Split('\n').Select(i => i.TrimEnd('\r')).ToList();
            _canRollback  = true;
        }
Esempio n. 8
0
        /// <param name="f"></param>
        /// <param name="fh"></param>
        /// <exception cref="System.IO.IOException">System.IO.IOException</exception>
        /// <exception cref="NGit.Api.Errors.PatchApplyException">NGit.Api.Errors.PatchApplyException
        ///     </exception>
        private void Apply(FilePath f, FileHeader fh)
        {
            RawText        rt       = new RawText(f);
            IList <string> oldLines = new AList <string>(rt.Size());

            for (int i = 0; i < rt.Size(); i++)
            {
                oldLines.AddItem(rt.GetString(i));
            }
            IList <string> newLines = new AList <string>(oldLines);

            foreach (HunkHeader hh in fh.GetHunks())
            {
                StringBuilder hunk = new StringBuilder();
                for (int j = hh.GetStartOffset(); j < hh.GetEndOffset(); j++)
                {
                    hunk.Append((char)hh.GetBuffer()[j]);
                }
                RawText        hrt       = new RawText(Sharpen.Runtime.GetBytesForString(hunk.ToString()));
                IList <string> hunkLines = new AList <string>(hrt.Size());
                for (int i_1 = 0; i_1 < hrt.Size(); i_1++)
                {
                    hunkLines.AddItem(hrt.GetString(i_1));
                }
                int pos = 0;
                for (int j_1 = 1; j_1 < hunkLines.Count; j_1++)
                {
                    string hunkLine = hunkLines[j_1];
                    switch (hunkLine[0])
                    {
                    case ' ':
                    {
                        if (!newLines[hh.GetNewStartLine() - 1 + pos].Equals(Sharpen.Runtime.Substring(hunkLine
                                                                                                       , 1)))
                        {
                            throw new PatchApplyException(MessageFormat.Format(JGitText.Get().patchApplyException
                                                                               , hh));
                        }
                        pos++;
                        break;
                    }

                    case '-':
                    {
                        if (!newLines[hh.GetNewStartLine() - 1 + pos].Equals(Sharpen.Runtime.Substring(hunkLine
                                                                                                       , 1)))
                        {
                            throw new PatchApplyException(MessageFormat.Format(JGitText.Get().patchApplyException
                                                                               , hh));
                        }
                        newLines.Remove(hh.GetNewStartLine() - 1 + pos);
                        break;
                    }

                    case '+':
                    {
                        newLines.Add(hh.GetNewStartLine() - 1 + pos, Sharpen.Runtime.Substring(hunkLine,
                                                                                               1));
                        pos++;
                        break;
                    }
                    }
                }
            }
            if (!IsNoNewlineAtEndOfFile(fh))
            {
                newLines.AddItem(string.Empty);
            }
            if (!rt.IsMissingNewlineAtEnd())
            {
                oldLines.AddItem(string.Empty);
            }
            if (!IsChanged(oldLines, newLines))
            {
                return;
            }
            // don't touch the file
            StringBuilder sb  = new StringBuilder();
            string        eol = rt.Size() == 0 || (rt.Size() == 1 && rt.IsMissingNewlineAtEnd()) ? "\n"
                                 : rt.GetLineDelimiter();

            foreach (string l in newLines)
            {
                sb.Append(l);
                if (eol != null)
                {
                    sb.Append(eol);
                }
            }
            Sharpen.Runtime.DeleteCharAt(sb, sb.Length - 1);
            FileWriter fw = new FileWriter(f);

            fw.Write(sb.ToString());
            fw.Close();
        }
Esempio n. 9
0
        /// <param name="f"></param>
        /// <param name="fh"></param>
        /// <exception cref="System.IO.IOException">System.IO.IOException</exception>
        /// <exception cref="NGit.Api.Errors.PatchApplyException">NGit.Api.Errors.PatchApplyException
        ///     </exception>
        private void Apply(FilePath f, FileHeader fh)
        {
            RawText        rt       = new RawText(f);
            IList <string> oldLines = new AList <string>(rt.Size());

            for (int i = 0; i < rt.Size(); i++)
            {
                oldLines.AddItem(rt.GetString(i));
            }
            IList <string> newLines      = new AList <string>(oldLines);
            RawText        hrt           = null;
            var            containedCRLF = false;

            foreach (HunkHeader hh in fh.GetHunks())
            {
                var buffer = Sharpen.Runtime.GetStringForBytes(hh.GetBuffer(), hh.GetStartOffset(), hh.GetEndOffset() - hh.GetStartOffset());

                if (!containedCRLF)
                {
                    containedCRLF |= buffer.Contains("\r\n");
                }

                hrt = new RawText(Sharpen.Runtime.GetBytesForString(buffer));
                IList <string> hunkLines = new AList <string>(hrt.Size());
                for (int i_1 = 0; i_1 < hrt.Size(); i_1++)
                {
                    hunkLines.AddItem(hrt.GetString(i_1));
                }
                int pos = 0;
                for (int j_1 = 1; j_1 < hunkLines.Count; j_1++)
                {
                    string hunkLine = hunkLines[j_1];
                    switch (hunkLine[0])
                    {
                    case ' ':
                    {
                        if (!newLines[hh.GetNewStartLine() - 1 + pos].Equals(Sharpen.Runtime.Substring(hunkLine
                                                                                                       , 1)))
                        {
                            throw new PatchApplyException(MessageFormat.Format(JGitText.Get().patchApplyException
                                                                               , hh), new PatchApplyModifiedException(f.GetAbsolutePath(), buffer, hunkLine, newLines[hh.GetNewStartLine() - 1 + pos]));
                        }
                        pos++;
                        break;
                    }

                    case '-':
                    {
                        var index = Math.Max(hh.GetNewStartLine() - 1 + pos, 0);
                        if (!newLines[index].Equals(Sharpen.Runtime.Substring(hunkLine
                                                                              , 1)))
                        {
                            throw new PatchApplyException(MessageFormat.Format(JGitText.Get().patchApplyException
                                                                               , hh), new PatchApplyModifiedException(f.GetAbsolutePath(), buffer, hunkLine, newLines[index]));
                        }
                        newLines.Remove(index);
                        break;
                    }

                    case '+':
                    {
                        newLines.Add(hh.GetNewStartLine() - 1 + pos, Sharpen.Runtime.Substring(hunkLine,
                                                                                               1));
                        pos++;
                        break;
                    }
                    }
                }
            }
            if (!IsNoNewlineAtEndOfFile(hrt))
            {
                newLines.AddItem(string.Empty);
            }
            if (!rt.IsMissingNewlineAtEnd())
            {
                oldLines.AddItem(string.Empty);
            }
            if (!IsChanged(oldLines, newLines))
            {
                return;
            }
            // don't touch the file
            StringBuilder sb  = new StringBuilder();
            string        eol = rt.Size() == 0 || (rt.Size() == 1 && rt.IsMissingNewlineAtEnd())
                        ? (containedCRLF ? "\r\n" : "\n")
                        : rt.GetLineDelimiter();

            for (int index = 0; index < newLines.Count; index++)
            {
                sb.Append(newLines[index]);

                var moreLinesToCome = index < newLines.Count - 1;
                if (moreLinesToCome && eol != null)
                {
                    sb.Append(eol);
                }
            }
            FileWriter fw = new FileWriter(f);

            fw.Write(sb.ToString());
            fw.Close();
        }