Beispiel #1
0
        /// <summary>Generate and write the bundle to the output stream.</summary>
        /// <remarks>
        /// Generate and write the bundle to the output stream.
        /// <p>
        /// This method can only be called once per BundleWriter instance.
        /// </remarks>
        /// <param name="monitor">progress monitor to report bundle writing status to.</param>
        /// <param name="os">
        /// the stream the bundle is written to. The stream should be
        /// buffered by the caller. The caller is responsible for closing
        /// the stream.
        /// </param>
        /// <exception cref="System.IO.IOException">
        /// an error occurred reading a local object's data to include in
        /// the bundle, or writing compressed object data to the output
        /// stream.
        /// </exception>
        public virtual void WriteBundle(ProgressMonitor monitor, OutputStream os)
        {
            PackConfig pc = packConfig;

            if (pc == null)
            {
                pc = new PackConfig(db);
            }
            PackWriter packWriter = new PackWriter(pc, db.NewObjectReader());

            try
            {
                HashSet <ObjectId> inc = new HashSet <ObjectId>();
                HashSet <ObjectId> exc = new HashSet <ObjectId>();
                Sharpen.Collections.AddAll(inc, include.Values);
                foreach (RevCommit r in assume)
                {
                    exc.AddItem(r.Id);
                }
                packWriter.SetDeltaBaseAsOffset(true);
                packWriter.SetThin(exc.Count > 0);
                packWriter.SetReuseValidatingObjects(false);
                if (exc.Count == 0)
                {
                    packWriter.SetTagTargets(tagTargets);
                }
                packWriter.PreparePack(monitor, inc, exc);
                TextWriter w = new OutputStreamWriter(os, Constants.CHARSET);
                w.Write(NGit.Transport.TransportBundleConstants.V2_BUNDLE_SIGNATURE);
                w.Write('\n');
                char[] tmp = new char[Constants.OBJECT_ID_STRING_LENGTH];
                foreach (RevCommit a in assume)
                {
                    w.Write('-');
                    a.CopyTo(tmp, w);
                    if (a.RawBuffer != null)
                    {
                        w.Write(' ');
                        w.Write(a.GetShortMessage());
                    }
                    w.Write('\n');
                }
                foreach (KeyValuePair <string, ObjectId> e in include.EntrySet())
                {
                    e.Value.CopyTo(tmp, w);
                    w.Write(' ');
                    w.Write(e.Key);
                    w.Write('\n');
                }
                w.Write('\n');
                w.Flush();
                packWriter.WritePack(monitor, monitor, os);
            }
            finally
            {
                packWriter.Release();
            }
        }
Beispiel #2
0
        public void SetCompression(ISyncIOCompression compression)
        {
            if (PackagingConfiguration == null)
            {
                PackagingConfiguration = new PackConfig();
            }

            PackagingConfiguration.Compression = compression;
        }
Beispiel #3
0
        /// <summary>
        /// Sets the encryption for traffic.
        /// </summary>
        /// <param name="encryption">Encryption to use.</param>
        public void SetEncryption(ISyncIOEncryption encryption)
        {
            if (PackagingConfiguration == null)
            {
                PackagingConfiguration = new PackConfig();
            }

            PackagingConfiguration.Encryption = encryption;
        }
Beispiel #4
0
        public override void SetUp()
        {
            base.SetUp();
            os     = new ByteArrayOutputStream();
            config = new PackConfig(db);
            dst    = CreateBareRepository();
            FilePath alt = new FilePath(((ObjectDirectory)dst.ObjectDatabase).GetDirectory(),
                                        "info/alternates");

            alt.GetParentFile().Mkdirs();
            Write(alt, ((ObjectDirectory)db.ObjectDatabase).GetDirectory().GetAbsolutePath()
                  + "\n");
        }
Beispiel #5
0
        public void TestPackager()
        {
            var packer = new Packager();



            var objArra = new object[] { "SyncIO", 1234, 55f };

            var packedArr   = packer.PackArray(objArra, null);
            var unpackedArr = packer.Unpack(packedArr) as ObjectArrayPacket;

            CollectionAssert.AreEqual(unpackedArr.Data, objArra);

            var arrObj      = new ObjectArrayPacket(objArra);
            var packedObj   = packer.Pack(arrObj);
            var unpackedObj = packer.Unpack(packedObj) as ObjectArrayPacket;

            CollectionAssert.AreEqual(unpackedObj.Data, arrObj.Data);

            //Encryption
            var config = new PackConfig();

            config.Encryption = PackConfig.GenerateNewEncryption(SyncIOKeySize.Bit128);

            packedArr   = packer.PackArray(objArra, config);
            unpackedArr = packer.Unpack(packedArr, config) as ObjectArrayPacket;
            CollectionAssert.AreEqual(unpackedArr.Data, objArra);

            arrObj      = new ObjectArrayPacket(objArra);
            packedObj   = packer.Pack(arrObj, config);
            unpackedObj = packer.Unpack(packedObj, config) as ObjectArrayPacket;
            CollectionAssert.AreEqual(unpackedObj.Data, arrObj.Data);

            //RSA

            var enc = new SyncIOEncryptionRSA();

            config.Encryption = enc;

            var encrypt = packer.PackArray(objArra, config);
            var decrypt = (packer.Unpack(encrypt, config) as ObjectArrayPacket).Data;

            CollectionAssert.AreEqual(objArra, decrypt);

            config.Encryption = new SyncIOEncryptionRSA(enc.PublicKey);
            encrypt           = packer.PackArray(objArra, config);
            config.Encryption = enc;
            decrypt           = (packer.Unpack(encrypt, config) as ObjectArrayPacket).Data;

            CollectionAssert.AreEqual(objArra, decrypt);
        }
Beispiel #6
0
 /// <summary>Set the configuration used by the pack generator.</summary>
 /// <remarks>Set the configuration used by the pack generator.</remarks>
 /// <param name="pc">
 /// configuration controlling packing parameters. If null the
 /// source repository's settings will be used.
 /// </param>
 public virtual void SetPackConfig(PackConfig pc)
 {
     this.packConfig = pc;
 }
Beispiel #7
0
        /// <exception cref="System.IO.IOException"></exception>
        private void SendPack()
        {
            bool sideband = options.Contains(OPTION_SIDE_BAND) || options.Contains(OPTION_SIDE_BAND_64K
                                                                                   );
            ProgressMonitor      pm      = NullProgressMonitor.INSTANCE;
            OutputStream         packOut = rawOut;
            SideBandOutputStream msgOut  = null;

            if (sideband)
            {
                int bufsz = SideBandOutputStream.SMALL_BUF;
                if (options.Contains(OPTION_SIDE_BAND_64K))
                {
                    bufsz = SideBandOutputStream.MAX_BUF;
                }
                packOut = new SideBandOutputStream(SideBandOutputStream.CH_DATA, bufsz, rawOut);
                if (!options.Contains(OPTION_NO_PROGRESS))
                {
                    msgOut = new SideBandOutputStream(SideBandOutputStream.CH_PROGRESS, bufsz, rawOut
                                                      );
                    pm = new SideBandProgressMonitor(msgOut);
                }
            }
            PackConfig cfg = packConfig;

            if (cfg == null)
            {
                cfg = new PackConfig(db);
            }
            PackWriter pw = new PackWriter(cfg, walk.GetObjectReader());

            try
            {
                pw.SetUseCachedPacks(true);
                pw.SetReuseDeltaCommits(true);
                pw.SetDeltaBaseAsOffset(options.Contains(OPTION_OFS_DELTA));
                pw.SetThin(options.Contains(OPTION_THIN_PACK));
                pw.SetReuseValidatingObjects(false);
                if (commonBase.IsEmpty())
                {
                    ICollection <ObjectId> tagTargets = new HashSet <ObjectId>();
                    foreach (Ref @ref in refs.Values)
                    {
                        if (@ref.GetPeeledObjectId() != null)
                        {
                            tagTargets.AddItem(@ref.GetPeeledObjectId());
                        }
                        else
                        {
                            if (@ref.GetObjectId() == null)
                            {
                                continue;
                            }
                            else
                            {
                                if (@ref.GetName().StartsWith(Constants.R_HEADS))
                                {
                                    tagTargets.AddItem(@ref.GetObjectId());
                                }
                            }
                        }
                    }
                    pw.SetTagTargets(tagTargets);
                }
                RevWalk rw = walk;
                if (wantAll.IsEmpty())
                {
                    pw.PreparePack(pm, wantIds, commonBase);
                }
                else
                {
                    walk.Reset();
                    ObjectWalk ow = walk.ToObjectWalkWithSameObjects();
                    pw.PreparePack(pm, ow, wantAll, commonBase);
                    rw = ow;
                }
                if (options.Contains(OPTION_INCLUDE_TAG))
                {
                    foreach (Ref vref in refs.Values)
                    {
                        Ref      @ref     = vref;
                        ObjectId objectId = @ref.GetObjectId();
                        // If the object was already requested, skip it.
                        if (wantAll.IsEmpty())
                        {
                            if (wantIds.Contains(objectId))
                            {
                                continue;
                            }
                        }
                        else
                        {
                            RevObject obj = rw.LookupOrNull(objectId);
                            if (obj != null && obj.Has(WANT))
                            {
                                continue;
                            }
                        }
                        if ([email protected]())
                        {
                            @ref = db.Peel(@ref);
                        }
                        ObjectId peeledId = @ref.GetPeeledObjectId();
                        if (peeledId == null)
                        {
                            continue;
                        }
                        objectId = @ref.GetObjectId();
                        if (pw.WillInclude(peeledId) && !pw.WillInclude(objectId))
                        {
                            pw.AddObject(rw.ParseAny(objectId));
                        }
                    }
                }
                pw.WritePack(pm, NullProgressMonitor.INSTANCE, packOut);
                statistics = pw.GetStatistics();
                if (msgOut != null)
                {
                    string msg = pw.GetStatistics().GetMessage() + '\n';
                    msgOut.Write(Constants.Encode(msg));
                    msgOut.Flush();
                }
            }
            finally
            {
                pw.Release();
            }
            if (sideband)
            {
                pckOut.End();
            }
            if (logger != null && statistics != null)
            {
                logger.OnPackStatistics(statistics);
            }
        }
Beispiel #8
0
        /// <exception cref="System.IO.IOException"></exception>
        private void SendPack()
        {
            bool sideband = options.Contains(OPTION_SIDE_BAND) || options.Contains(OPTION_SIDE_BAND_64K
                                                                                   );

            if (!biDirectionalPipe)
            {
                // Ensure the request was fully consumed. Any remaining input must
                // be a protocol error. If we aren't at EOF the implementation is broken.
                int eof = rawIn.Read();
                if (0 <= eof)
                {
                    throw new CorruptObjectException(MessageFormat.Format(JGitText.Get().expectedEOFReceived
                                                                          , "\\x" + Sharpen.Extensions.ToHexString(eof)));
                }
            }
            ProgressMonitor      pm      = NullProgressMonitor.INSTANCE;
            OutputStream         packOut = rawOut;
            SideBandOutputStream msgOut  = null;

            if (sideband)
            {
                int bufsz = SideBandOutputStream.SMALL_BUF;
                if (options.Contains(OPTION_SIDE_BAND_64K))
                {
                    bufsz = SideBandOutputStream.MAX_BUF;
                }
                packOut = new SideBandOutputStream(SideBandOutputStream.CH_DATA, bufsz, rawOut);
                if (!options.Contains(OPTION_NO_PROGRESS))
                {
                    msgOut = new SideBandOutputStream(SideBandOutputStream.CH_PROGRESS, bufsz, rawOut
                                                      );
                    pm = new SideBandProgressMonitor(msgOut);
                }
            }
            try
            {
                if (wantAll.IsEmpty())
                {
                    preUploadHook.OnSendPack(this, wantIds, commonBase);
                }
                else
                {
                    preUploadHook.OnSendPack(this, wantAll, commonBase);
                }
            }
            catch (UploadPackMayNotContinueException noPack)
            {
                if (sideband && noPack.Message != null)
                {
                    noPack.SetOutput();
                    SideBandOutputStream err = new SideBandOutputStream(SideBandOutputStream.CH_ERROR
                                                                        , SideBandOutputStream.SMALL_BUF, rawOut);
                    err.Write(Constants.Encode(noPack.Message));
                    err.Flush();
                }
                throw;
            }
            PackConfig cfg = packConfig;

            if (cfg == null)
            {
                cfg = new PackConfig(db);
            }
            PackWriter pw = new PackWriter(cfg, walk.GetObjectReader());

            try
            {
                pw.SetUseCachedPacks(true);
                pw.SetReuseDeltaCommits(true);
                pw.SetDeltaBaseAsOffset(options.Contains(OPTION_OFS_DELTA));
                pw.SetThin(options.Contains(OPTION_THIN_PACK));
                pw.SetReuseValidatingObjects(false);
                if (commonBase.IsEmpty())
                {
                    ICollection <ObjectId> tagTargets = new HashSet <ObjectId>();
                    foreach (Ref @ref in refs.Values)
                    {
                        if (@ref.GetPeeledObjectId() != null)
                        {
                            tagTargets.AddItem(@ref.GetPeeledObjectId());
                        }
                        else
                        {
                            if (@ref.GetObjectId() == null)
                            {
                                continue;
                            }
                            else
                            {
                                if (@ref.GetName().StartsWith(Constants.R_HEADS))
                                {
                                    tagTargets.AddItem(@ref.GetObjectId());
                                }
                            }
                        }
                    }
                    pw.SetTagTargets(tagTargets);
                }
                RevWalk rw = walk;
                if (wantAll.IsEmpty())
                {
                    pw.PreparePack(pm, wantIds, commonBase);
                }
                else
                {
                    walk.Reset();
                    ObjectWalk ow = walk.ToObjectWalkWithSameObjects();
                    pw.PreparePack(pm, ow, wantAll, commonBase);
                    rw = ow;
                }
                if (options.Contains(OPTION_INCLUDE_TAG))
                {
                    foreach (Ref vref in refs.Values)
                    {
                        Ref      @ref     = vref;
                        ObjectId objectId = @ref.GetObjectId();
                        // If the object was already requested, skip it.
                        if (wantAll.IsEmpty())
                        {
                            if (wantIds.Contains(objectId))
                            {
                                continue;
                            }
                        }
                        else
                        {
                            RevObject obj = rw.LookupOrNull(objectId);
                            if (obj != null && obj.Has(WANT))
                            {
                                continue;
                            }
                        }
                        if ([email protected]())
                        {
                            @ref = db.Peel(@ref);
                        }
                        ObjectId peeledId = @ref.GetPeeledObjectId();
                        if (peeledId == null)
                        {
                            continue;
                        }
                        objectId = @ref.GetObjectId();
                        if (pw.WillInclude(peeledId) && !pw.WillInclude(objectId))
                        {
                            pw.AddObject(rw.ParseAny(objectId));
                        }
                    }
                }
                pw.WritePack(pm, NullProgressMonitor.INSTANCE, packOut);
                statistics = pw.GetStatistics();
                if (msgOut != null)
                {
                    string msg = pw.GetStatistics().GetMessage() + '\n';
                    msgOut.Write(Constants.Encode(msg));
                    msgOut.Flush();
                }
            }
            finally
            {
                pw.Release();
            }
            if (sideband)
            {
                pckOut.End();
            }
            if (logger != null && statistics != null)
            {
                logger.OnPackStatistics(statistics);
            }
        }