private int SortOf(DiffEntry.ChangeType changeType)
            {
                switch (changeType)
                {
                case DiffEntry.ChangeType.DELETE:
                {
                    // Sort deletes before adds so that a major type change for
                    // a file path (such as symlink to regular file) will first
                    // remove the path, then add it back with the new type.
                    //
                    return(1);
                }

                case DiffEntry.ChangeType.ADD:
                {
                    return(2);
                }

                default:
                {
                    return(10);

                    break;
                }
                }
            }
 internal static NGit.Diff.DiffEntry Pair(DiffEntry.ChangeType changeType, NGit.Diff.DiffEntry
                                          src, NGit.Diff.DiffEntry dst, int score)
 {
     NGit.Diff.DiffEntry r = new NGit.Diff.DiffEntry();
     r.oldId      = src.oldId;
     r.oldMode    = src.oldMode;
     r.oldPath    = src.oldPath;
     r.newId      = dst.newId;
     r.newMode    = dst.newMode;
     r.newPath    = dst.newPath;
     r.changeType = changeType;
     r.score      = score;
     return(r);
 }
示例#3
0
        private static FileActionEnum GetAction(DiffEntry.ChangeType changeType)
        {
            switch (changeType)
            {
            case DiffEntry.ChangeType.MODIFY:
            case DiffEntry.ChangeType.COPY:
                return(FileActionEnum.Modify);

            case DiffEntry.ChangeType.ADD:
                return(FileActionEnum.Add);

            case DiffEntry.ChangeType.DELETE:
                return(FileActionEnum.Delete);

            case DiffEntry.ChangeType.RENAME:
                return(FileActionEnum.Rename);

            default:
                return(FileActionEnum.None);
            }
        }
示例#4
0
        private static DiffType ToDiffType(DiffEntry.ChangeType changeType)
        {
            switch (changeType)
            {
            case DiffEntry.ChangeType.MODIFY:
                return(DiffType.Modify);

            case DiffEntry.ChangeType.ADD:
                return(DiffType.Add);

            case DiffEntry.ChangeType.DELETE:
                return(DiffType.Delete);

            case DiffEntry.ChangeType.COPY:
                return(DiffType.Copy);

            case DiffEntry.ChangeType.RENAME:
                return(DiffType.Rename);
            }

            throw new InvalidOperationException();
        }
示例#5
0
        /// <exception cref="System.IO.IOException"></exception>
        private void FormatHeader(ByteArrayOutputStream o, DiffEntry ent)
        {
            DiffEntry.ChangeType type = ent.GetChangeType();
            string   oldp             = ent.GetOldPath();
            string   newp             = ent.GetNewPath();
            FileMode oldMode          = ent.GetOldMode();
            FileMode newMode          = ent.GetNewMode();

            o.Write(Constants.EncodeASCII("diff --git "));
            o.Write(Constants.Encode(QuotePath(oldPrefix + (type == DiffEntry.ChangeType.ADD ?
                                                            newp : oldp))));
            o.Write(' ');
            o.Write(Constants.Encode(QuotePath(newPrefix + (type == DiffEntry.ChangeType.DELETE
                                           ? oldp : newp))));
            o.Write('\n');
            switch (type)
            {
            case DiffEntry.ChangeType.ADD:
            {
                o.Write(Constants.EncodeASCII("new file mode "));
                newMode.CopyTo(o);
                o.Write('\n');
                break;
            }

            case DiffEntry.ChangeType.DELETE:
            {
                o.Write(Constants.EncodeASCII("deleted file mode "));
                oldMode.CopyTo(o);
                o.Write('\n');
                break;
            }

            case DiffEntry.ChangeType.RENAME:
            {
                o.Write(Constants.EncodeASCII("similarity index " + ent.GetScore() + "%"));
                o.Write('\n');
                o.Write(Constants.Encode("rename from " + QuotePath(oldp)));
                o.Write('\n');
                o.Write(Constants.Encode("rename to " + QuotePath(newp)));
                o.Write('\n');
                break;
            }

            case DiffEntry.ChangeType.COPY:
            {
                o.Write(Constants.EncodeASCII("similarity index " + ent.GetScore() + "%"));
                o.Write('\n');
                o.Write(Constants.Encode("copy from " + QuotePath(oldp)));
                o.Write('\n');
                o.Write(Constants.Encode("copy to " + QuotePath(newp)));
                o.Write('\n');
                if (!oldMode.Equals(newMode))
                {
                    o.Write(Constants.EncodeASCII("new file mode "));
                    newMode.CopyTo(o);
                    o.Write('\n');
                }
                break;
            }

            case DiffEntry.ChangeType.MODIFY:
            {
                if (0 < ent.GetScore())
                {
                    o.Write(Constants.EncodeASCII("dissimilarity index " + (100 - ent.GetScore()) + "%"
                                                  ));
                    o.Write('\n');
                }
                break;
            }
            }
            if ((type == DiffEntry.ChangeType.MODIFY || type == DiffEntry.ChangeType.RENAME) &&
                !oldMode.Equals(newMode))
            {
                o.Write(Constants.EncodeASCII("old mode "));
                oldMode.CopyTo(o);
                o.Write('\n');
                o.Write(Constants.EncodeASCII("new mode "));
                newMode.CopyTo(o);
                o.Write('\n');
            }
            if (!ent.GetOldId().Equals(ent.GetNewId()))
            {
                FormatIndexLine(o, ent);
            }
        }
示例#6
0
        /// <summary>
        /// Executes the
        /// <code>ApplyCommand</code>
        /// command with all the options and
        /// parameters collected by the setter methods (e.g.
        /// <see cref="SetPatch(Sharpen.InputStream)">SetPatch(Sharpen.InputStream)</see>
        /// of this class. Each instance of this class
        /// should only be used for one invocation of the command. Don't call this
        /// method twice on an instance.
        /// </summary>
        /// <returns>
        /// an
        /// <see cref="ApplyResult">ApplyResult</see>
        /// object representing the command result
        /// </returns>
        /// <exception cref="NGit.Api.Errors.GitAPIException">NGit.Api.Errors.GitAPIException
        ///     </exception>
        /// <exception cref="NGit.Api.Errors.PatchFormatException">NGit.Api.Errors.PatchFormatException
        ///     </exception>
        /// <exception cref="NGit.Api.Errors.PatchApplyException">NGit.Api.Errors.PatchApplyException
        ///     </exception>
        public override ApplyResult Call()
        {
            CheckCallable();
            ApplyResult r = new ApplyResult();

            try
            {
                NGit.Patch.Patch p = new NGit.Patch.Patch();
                try
                {
                    p.Parse(@in);
                }
                finally
                {
                    @in.Close();
                }
                if (!p.GetErrors().IsEmpty())
                {
                    throw new PatchFormatException(p.GetErrors());
                }
                foreach (FileHeader fh in p.GetFiles())
                {
                    DiffEntry.ChangeType type = fh.GetChangeType();
                    FilePath             f    = null;
                    switch (type)
                    {
                    case DiffEntry.ChangeType.ADD:
                    {
                        f = GetFile(fh.GetNewPath(), true);
                        Apply(f, fh);
                        break;
                    }

                    case DiffEntry.ChangeType.MODIFY:
                    {
                        f = GetFile(fh.GetOldPath(), false);
                        Apply(f, fh);
                        break;
                    }

                    case DiffEntry.ChangeType.DELETE:
                    {
                        f = GetFile(fh.GetOldPath(), false);
                        if (!f.Delete())
                        {
                            throw new PatchApplyException(MessageFormat.Format(JGitText.Get().cannotDeleteFile
                                                                               , f));
                        }
                        break;
                    }

                    case DiffEntry.ChangeType.RENAME:
                    {
                        f = GetFile(fh.GetOldPath(), false);
                        FilePath dest = GetFile(fh.GetNewPath(), false);
                        if (!f.RenameTo(dest))
                        {
                            throw new PatchApplyException(MessageFormat.Format(JGitText.Get().renameFileFailed
                                                                               , f, dest));
                        }
                        break;
                    }

                    case DiffEntry.ChangeType.COPY:
                    {
                        f = GetFile(fh.GetOldPath(), false);
                        byte[]     bs = IOUtil.ReadFully(f);
                        FileWriter fw = new FileWriter(GetFile(fh.GetNewPath(), true));
                        fw.Write(Sharpen.Runtime.GetStringForBytes(bs));
                        fw.Close();
                        break;
                    }
                    }
                    r.AddUpdatedFile(f);
                }
            }
            catch (IOException e)
            {
                throw new PatchApplyException(MessageFormat.Format(JGitText.Get().patchApplyException
                                                                   , e.Message), e);
            }
            SetCallable(false);
            return(r);
        }