Exemplo n.º 1
0
 /// <summary>Push a candidate blob onto the generator's traversal stack.</summary>
 /// <remarks>
 /// Push a candidate blob onto the generator's traversal stack.
 /// <p>
 /// Candidates should be pushed in history order from oldest-to-newest.
 /// Applications should push the starting commit first, then the index
 /// revision (if the index is interesting), and finally the working tree copy
 /// (if the working tree is interesting).
 /// </remarks>
 /// <param name="description">description of the blob revision, such as "Working Tree".
 ///     </param>
 /// <param name="contents">contents of the file.</param>
 /// <returns>
 ///
 /// <code>this</code>
 /// </returns>
 /// <exception cref="System.IO.IOException">the repository cannot be read.</exception>
 public virtual NGit.Blame.BlameGenerator Push(string description, RawText contents
                                               )
 {
     if (description == null)
     {
         description = JGitText.Get().blameNotCommittedYet;
     }
     Candidate.BlobCandidate c = new Candidate.BlobCandidate(description, resultPath);
     c.sourceText = contents;
     c.regionList = new Region(0, 0, contents.Size());
     remaining    = contents.Size();
     Push(c);
     return(this);
 }
Exemplo n.º 2
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"));
        }
Exemplo n.º 3
0
 private bool IsNoNewlineAtEndOfFile(RawText lastRawTextOrDefault)
 {
     if (lastRawTextOrDefault != null)
     {
         return(lastRawTextOrDefault.GetString(lastRawTextOrDefault.Size() - 1).Equals("\\ No newline at end of file"));
     }
     return(false);
 }
Exemplo n.º 4
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);
        }
        internal BlameResult(BlameGenerator bg, string path, RawText text)
        {
            generator      = bg;
            resultPath     = path;
            resultContents = text;
            int cnt = text.Size();

            sourceCommits    = new RevCommit[cnt];
            sourceAuthors    = new PersonIdent[cnt];
            sourceCommitters = new PersonIdent[cnt];
            sourceLines      = new int[cnt];
            sourcePaths      = new string[cnt];
        }
Exemplo n.º 6
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));
        }
Exemplo n.º 7
0
        static int[] GetDiffCodes(ref int codeCounter, Dictionary <string, int> codeDictionary, RawText text)
        {
            int lineCount = text.Size();

            int[]    result = new int[lineCount];
            string[] lines  = GetLineStrings(text);
            for (int i = 0; i < lineCount; i++)
            {
                string lineText = lines [i];
                int    curCode;
                if (!codeDictionary.TryGetValue(lineText, out curCode))
                {
                    codeDictionary [lineText] = curCode = ++codeCounter;
                }
                result [i] = curCode;
            }
            return(result);
        }
Exemplo n.º 8
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());
        }
Exemplo n.º 9
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());
        }
Exemplo n.º 10
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();
        }
Exemplo n.º 11
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();
        }