Esempio n. 1
0
        /// <exception cref="System.IO.IOException"></exception>
        private void WritePack(IDictionary <string, RemoteRefUpdate> refUpdates, ProgressMonitor
                               monitor)
        {
            ICollection <ObjectId> remoteObjects = new HashSet <ObjectId>();
            ICollection <ObjectId> newObjects    = new HashSet <ObjectId>();
            PackWriter             writer        = new PackWriter(transport.GetPackConfig(), local.NewObjectReader
                                                                      ());

            try
            {
                foreach (Ref r in GetRefs())
                {
                    remoteObjects.AddItem(r.GetObjectId());
                }
                Sharpen.Collections.AddAll(remoteObjects, additionalHaves);
                foreach (RemoteRefUpdate r_1 in refUpdates.Values)
                {
                    if (!ObjectId.ZeroId.Equals(r_1.GetNewObjectId()))
                    {
                        newObjects.AddItem(r_1.GetNewObjectId());
                    }
                }
                writer.SetUseCachedPacks(true);
                writer.SetThin(thinPack);
                writer.SetReuseValidatingObjects(false);
                writer.SetDeltaBaseAsOffset(capableOfsDelta);
                writer.PreparePack(monitor, newObjects, remoteObjects);
                writer.WritePack(monitor, monitor, @out);
            }
            finally
            {
                writer.Release();
            }
            packTransferTime = writer.GetStatistics().GetTimeWriting();
        }
Esempio n. 2
0
        /// <exception cref="System.IO.IOException"></exception>
        private PackIndex WritePack <_T0>(FileRepository repo, ICollection <_T0> want, ICollection
                                          <PackIndex> excludeObjects) where _T0 : ObjectId
        {
            PackWriter pw = new PackWriter(repo);

            pw.SetDeltaBaseAsOffset(true);
            pw.SetReuseDeltaCommits(false);
            foreach (PackIndex idx in excludeObjects)
            {
                pw.ExcludeObjects(idx);
            }
            pw.PreparePack(NullProgressMonitor.INSTANCE, want, Sharpen.Collections.EmptySet <ObjectId
                                                                                             >());
            string           id       = pw.ComputeName().GetName();
            FilePath         packdir  = new FilePath(repo.ObjectsDirectory, "pack");
            FilePath         packFile = new FilePath(packdir, "pack-" + id + ".pack");
            FileOutputStream packOS   = new FileOutputStream(packFile);

            pw.WritePack(NullProgressMonitor.INSTANCE, NullProgressMonitor.INSTANCE, packOS);
            packOS.Close();
            FilePath         idxFile = new FilePath(packdir, "pack-" + id + ".idx");
            FileOutputStream idxOS   = new FileOutputStream(idxFile);

            pw.WriteIndex(idxOS);
            idxOS.Close();
            pw.Release();
            return(PackIndex.Open(idxFile));
        }
Esempio n. 3
0
        public virtual void TestObjectMovedWithinPack()
        {
            // Create an object and pack it.
            //
            Repository eden = CreateBareRepository();
            RevObject  o1   = WriteBlob(eden, "o1");

            FilePath[] out1 = Pack(eden, o1);
            NUnit.Framework.Assert.AreEqual(o1.Name, Parse(o1).Name);
            // Force close the old pack.
            //
            WhackCache();
            // Now overwrite the old pack in place. This method of creating a
            // different pack under the same file name is partially broken. We
            // should also have a different file name because the list of objects
            // within the pack has been modified.
            //
            RevObject  o2 = WriteBlob(eden, "o2");
            PackWriter pw = new PackWriter(eden);

            pw.AddObject(o2);
            pw.AddObject(o1);
            Write(out1, pw);
            pw.Release();
            // Try the old name, then the new name. The old name should cause the
            // pack to reload when it opens and the index and pack mismatch.
            //
            NUnit.Framework.Assert.AreEqual(o1.Name, Parse(o1).Name);
            NUnit.Framework.Assert.AreEqual(o2.Name, Parse(o2).Name);
        }
Esempio n. 4
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();
            }
        }
Esempio n. 5
0
        /// <exception cref="NGit.Errors.MissingObjectException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        private void CreateVerifyOpenPack(Iterator <RevObject> objectSource)
        {
            NullProgressMonitor m = NullProgressMonitor.INSTANCE;

            writer = new PackWriter(config, db.NewObjectReader());
            writer.PreparePack(objectSource);
            writer.WritePack(m, m, os);
            writer.Release();
            VerifyOpenPack(false);
        }
Esempio n. 6
0
        /// <exception cref="NGit.Errors.MissingObjectException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        private void CreateVerifyOpenPack(IList <RevObject> objectSource)
        {
            NullProgressMonitor m = NullProgressMonitor.INSTANCE;

            writer = new PackWriter(config, db.NewObjectReader());
            writer.PreparePack(objectSource.Iterator());
            NUnit.Framework.Assert.AreEqual(objectSource.Count, writer.GetObjectCount());
            writer.WritePack(m, m, os);
            writer.Release();
            VerifyOpenPack(false);
        }
Esempio n. 7
0
        /// <exception cref="NGit.Errors.MissingObjectException"></exception>
        /// <exception cref="System.IO.IOException"></exception>
        private void CreateVerifyOpenPack(ICollection <ObjectId> interestings, ICollection
                                          <ObjectId> uninterestings, bool thin, bool ignoreMissingUninteresting)
        {
            NullProgressMonitor m = NullProgressMonitor.INSTANCE;

            writer = new PackWriter(config, db.NewObjectReader());
            writer.SetThin(thin);
            writer.SetIgnoreMissingUninteresting(ignoreMissingUninteresting);
            writer.PreparePack(m, interestings, uninterestings);
            writer.WritePack(m, m, os);
            writer.Release();
            VerifyOpenPack(thin);
        }
Esempio n. 8
0
        /// <exception cref="System.IO.IOException"></exception>
        private FilePath[] Pack(Repository src, params RevObject[] list)
        {
            PackWriter pw = new PackWriter(src);

            foreach (RevObject o in list)
            {
                pw.AddObject(o);
            }
            ObjectId name     = pw.ComputeName();
            FilePath packFile = FullPackFileName(name, ".pack");
            FilePath idxFile  = FullPackFileName(name, ".idx");

            FilePath[] files = new FilePath[] { packFile, idxFile };
            Write(files, pw);
            pw.Release();
            return(files);
        }
Esempio n. 9
0
		/// <exception cref="System.IO.IOException"></exception>
		private FilePath[] Pack(Repository src, params RevObject[] list)
		{
			PackWriter pw = new PackWriter(src);
			foreach (RevObject o in list)
			{
				pw.AddObject(o);
			}
			ObjectId name = pw.ComputeName();
			FilePath packFile = FullPackFileName(name, ".pack");
			FilePath idxFile = FullPackFileName(name, ".idx");
			FilePath[] files = new FilePath[] { packFile, idxFile };
			Write(files, pw);
			pw.Release();
			return files;
		}
Esempio n. 10
0
		public virtual void TestObjectMovedWithinPack()
		{
			// Create an object and pack it.
			//
			Repository eden = CreateBareRepository();
			RevObject o1 = WriteBlob(eden, "o1");
			FilePath[] out1 = Pack(eden, o1);
			NUnit.Framework.Assert.AreEqual(o1.Name, Parse(o1).Name);
			// Force close the old pack.
			//
			WhackCache();
			// Now overwrite the old pack in place. This method of creating a
			// different pack under the same file name is partially broken. We
			// should also have a different file name because the list of objects
			// within the pack has been modified.
			//
			RevObject o2 = WriteBlob(eden, "o2");
			PackWriter pw = new PackWriter(eden);
			pw.AddObject(o2);
			pw.AddObject(o1);
			Write(out1, pw);
			pw.Release();
			// Try the old name, then the new name. The old name should cause the
			// pack to reload when it opens and the index and pack mismatch.
			//
			NUnit.Framework.Assert.AreEqual(o1.Name, Parse(o1).Name);
			NUnit.Framework.Assert.AreEqual(o2.Name, Parse(o2).Name);
		}
		/// <exception cref="System.IO.IOException"></exception>
		private void WritePack(IDictionary<string, RemoteRefUpdate> refUpdates, ProgressMonitor
			 monitor)
		{
			IList<ObjectId> remoteObjects = new AList<ObjectId>(GetRefs().Count);
			IList<ObjectId> newObjects = new AList<ObjectId>(refUpdates.Count);
			long start;
			PackWriter writer = new PackWriter(transport.GetPackConfig(), local.NewObjectReader
				());
			try
			{
				foreach (Ref r in GetRefs())
				{
					remoteObjects.AddItem(r.GetObjectId());
				}
				Sharpen.Collections.AddAll(remoteObjects, additionalHaves);
				foreach (RemoteRefUpdate r_1 in refUpdates.Values)
				{
					if (!ObjectId.ZeroId.Equals(r_1.GetNewObjectId()))
					{
						newObjects.AddItem(r_1.GetNewObjectId());
					}
				}
				writer.SetUseCachedPacks(true);
				writer.SetThin(thinPack);
				writer.SetDeltaBaseAsOffset(capableOfsDelta);
				writer.PreparePack(monitor, newObjects, remoteObjects);
				start = Runtime.CurrentTimeMillis();
				writer.WritePack(monitor, monitor, @out);
			}
			finally
			{
				writer.Release();
			}
			@out.Flush();
			packTransferTime = Runtime.CurrentTimeMillis() - start;
		}
        /// <exception cref="NGit.Errors.TransportException"></exception>
        private void Sendpack(IList <RemoteRefUpdate> updates, ProgressMonitor monitor)
        {
            string     pathPack = null;
            string     pathIdx  = null;
            PackWriter writer   = new PackWriter(transport.GetPackConfig(), local.NewObjectReader
                                                     ());

            try
            {
                ICollection <ObjectId> need = new HashSet <ObjectId>();
                ICollection <ObjectId> have = new HashSet <ObjectId>();
                foreach (RemoteRefUpdate r in updates)
                {
                    need.AddItem(r.GetNewObjectId());
                }
                foreach (Ref r_1 in GetRefs())
                {
                    have.AddItem(r_1.GetObjectId());
                    if (r_1.GetPeeledObjectId() != null)
                    {
                        have.AddItem(r_1.GetPeeledObjectId());
                    }
                }
                writer.PreparePack(monitor, need, have);
                // We don't have to continue further if the pack will
                // be an empty pack, as the remote has all objects it
                // needs to complete this change.
                //
                if (writer.GetObjectCount() == 0)
                {
                    return;
                }
                packNames = new LinkedHashMap <string, string>();
                foreach (string n in dest.GetPackNames())
                {
                    packNames.Put(n, n);
                }
                string @base    = "pack-" + writer.ComputeName().Name;
                string packName = @base + ".pack";
                pathPack = "pack/" + packName;
                pathIdx  = "pack/" + @base + ".idx";
                if (Sharpen.Collections.Remove(packNames, packName) != null)
                {
                    // The remote already contains this pack. We should
                    // remove the index before overwriting to prevent bad
                    // offsets from appearing to clients.
                    //
                    dest.WriteInfoPacks(packNames.Keys);
                    dest.DeleteFile(pathIdx);
                }
                // Write the pack file, then the index, as readers look the
                // other direction (index, then pack file).
                //
                string       wt = "Put " + Sharpen.Runtime.Substring(@base, 0, 12);
                OutputStream os = dest.WriteFile(pathPack, monitor, wt + "..pack");
                try
                {
                    os = new SafeBufferedOutputStream(os);
                    writer.WritePack(monitor, monitor, os);
                }
                finally
                {
                    os.Close();
                }
                os = dest.WriteFile(pathIdx, monitor, wt + "..idx");
                try
                {
                    os = new SafeBufferedOutputStream(os);
                    writer.WriteIndex(os);
                }
                finally
                {
                    os.Close();
                }
                // Record the pack at the start of the pack info list. This
                // way clients are likely to consult the newest pack first,
                // and discover the most recent objects there.
                //
                AList <string> infoPacks = new AList <string>();
                infoPacks.AddItem(packName);
                Sharpen.Collections.AddAll(infoPacks, packNames.Keys);
                dest.WriteInfoPacks(infoPacks);
            }
            catch (IOException err)
            {
                SafeDelete(pathIdx);
                SafeDelete(pathPack);
                throw new TransportException(uri, JGitText.Get().cannotStoreObjects, err);
            }
            finally
            {
                writer.Release();
            }
        }
Esempio n. 13
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;
			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))
				{
					pm = new SideBandProgressMonitor(new SideBandOutputStream(SideBandOutputStream.CH_PROGRESS
						, bufsz, rawOut));
				}
			}
			PackConfig cfg = packConfig;
			if (cfg == null)
			{
				cfg = new PackConfig(db);
			}
			PackWriter pw = new PackWriter(cfg, walk.GetObjectReader());
			try
			{
				pw.SetDeltaBaseAsOffset(options.Contains(OPTION_OFS_DELTA));
				pw.SetThin(options.Contains(OPTION_THIN_PACK));
				pw.PreparePack(pm, wantAll, commonBase);
				if (options.Contains(OPTION_INCLUDE_TAG))
				{
					foreach (Ref r in refs.Values)
					{
						RevObject o;
						try
						{
							o = walk.ParseAny(r.GetObjectId());
						}
						catch (IOException)
						{
							continue;
						}
						if (o.Has(WANT) || !(o is RevTag))
						{
							continue;
						}
						RevTag t = (RevTag)o;
						if (!pw.WillInclude(t) && pw.WillInclude(t.GetObject()))
						{
							pw.AddObject(t);
						}
					}
				}
				pw.WritePack(pm, NullProgressMonitor.INSTANCE, packOut);
			}
			finally
			{
				pw.Release();
			}
			packOut.Flush();
			if (sideband)
			{
				pckOut.End();
			}
		}
Esempio n. 14
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);
            }
        }
		/// <exception cref="System.IO.IOException"></exception>
		private void WritePack(IDictionary<string, RemoteRefUpdate> refUpdates, ProgressMonitor
			 monitor)
		{
			ICollection<ObjectId> remoteObjects = new HashSet<ObjectId>();
			ICollection<ObjectId> newObjects = new HashSet<ObjectId>();
			PackWriter writer = new PackWriter(transport.GetPackConfig(), local.NewObjectReader
				());
			try
			{
				foreach (Ref r in GetRefs())
				{
					remoteObjects.AddItem(r.GetObjectId());
				}
				Sharpen.Collections.AddAll(remoteObjects, additionalHaves);
				foreach (RemoteRefUpdate r_1 in refUpdates.Values)
				{
					if (!ObjectId.ZeroId.Equals(r_1.GetNewObjectId()))
					{
						newObjects.AddItem(r_1.GetNewObjectId());
					}
				}
				writer.SetUseCachedPacks(true);
				writer.SetThin(thinPack);
				writer.SetReuseValidatingObjects(false);
				writer.SetDeltaBaseAsOffset(capableOfsDelta);
				writer.PreparePack(monitor, newObjects, remoteObjects);
				writer.WritePack(monitor, monitor, @out);
			}
			finally
			{
				writer.Release();
			}
			packTransferTime = writer.GetStatistics().GetTimeWriting();
		}
Esempio n. 16
0
        /// <exception cref="System.IO.IOException"></exception>
        private PackFile WritePack <_T0, _T1>(ICollection <_T0> want, ICollection <_T1> have
                                              , ICollection <ObjectId> tagTargets, IList <PackIndex> excludeObjects) where _T0 : ObjectId
            where _T1 : ObjectId
        {
            FilePath   tmpPack = null;
            FilePath   tmpIdx  = null;
            PackWriter pw      = new PackWriter(repo);

            try
            {
                // prepare the PackWriter
                pw.SetDeltaBaseAsOffset(true);
                pw.SetReuseDeltaCommits(false);
                if (tagTargets != null)
                {
                    pw.SetTagTargets(tagTargets);
                }
                if (excludeObjects != null)
                {
                    foreach (PackIndex idx in excludeObjects)
                    {
                        pw.ExcludeObjects(idx);
                    }
                }
                pw.PreparePack(pm, want, have);
                if (pw.GetObjectCount() == 0)
                {
                    return(null);
                }
                // create temporary files
                string   id      = pw.ComputeName().GetName();
                FilePath packdir = new FilePath(repo.ObjectsDirectory, "pack");
                tmpPack = FilePath.CreateTempFile("gc_", ".pack_tmp", packdir);
                tmpIdx  = new FilePath(packdir, Sharpen.Runtime.Substring(tmpPack.GetName(), 0, tmpPack
                                                                          .GetName().LastIndexOf('.')) + ".idx_tmp");
                if (!tmpIdx.CreateNewFile())
                {
                    throw new IOException(MessageFormat.Format(JGitText.Get().cannotCreateIndexfile,
                                                               tmpIdx.GetPath()));
                }
                // write the packfile
                FileChannel  channel       = new FileOutputStream(tmpPack).GetChannel();
                OutputStream channelStream = Channels.NewOutputStream(channel);
                try
                {
                    pw.WritePack(pm, pm, channelStream);
                }
                finally
                {
                    channel.Force(true);
                    channelStream.Close();
                    channel.Close();
                }
                // write the packindex
                FileChannel  idxChannel = new FileOutputStream(tmpIdx).GetChannel();
                OutputStream idxStream  = Channels.NewOutputStream(idxChannel);
                try
                {
                    pw.WriteIndex(idxStream);
                }
                finally
                {
                    idxChannel.Force(true);
                    idxStream.Close();
                    idxChannel.Close();
                }
                // rename the temporary files to real files
                FilePath realPack = NameFor(id, ".pack");
                tmpPack.SetReadOnly();
                FilePath realIdx = NameFor(id, ".idx");
                realIdx.SetReadOnly();
                bool delete = true;
                try
                {
                    if (!tmpPack.RenameTo(realPack))
                    {
                        return(null);
                    }
                    delete = false;
                    if (!tmpIdx.RenameTo(realIdx))
                    {
                        FilePath newIdx = new FilePath(realIdx.GetParentFile(), realIdx.GetName() + ".new"
                                                       );
                        if (!tmpIdx.RenameTo(newIdx))
                        {
                            newIdx = tmpIdx;
                        }
                        throw new IOException(MessageFormat.Format(JGitText.Get().panicCantRenameIndexFile
                                                                   , newIdx, realIdx));
                    }
                }
                finally
                {
                    if (delete && tmpPack.Exists())
                    {
                        tmpPack.Delete();
                    }
                    if (delete && tmpIdx.Exists())
                    {
                        tmpIdx.Delete();
                    }
                }
                return(((ObjectDirectory)repo.ObjectDatabase).OpenPack(realPack, realIdx));
            }
            finally
            {
                pw.Release();
                if (tmpPack != null && tmpPack.Exists())
                {
                    tmpPack.Delete();
                }
                if (tmpIdx != null && tmpIdx.Exists())
                {
                    tmpIdx.Delete();
                }
            }
        }
Esempio n. 17
0
		/// <exception cref="System.IO.IOException"></exception>
		private void SendPack(bool sideband)
		{
			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 (ServiceMayNotContinueException 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() && refs != null)
				{
					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);
				}
				if (depth > 0)
				{
					pw.SetShallowPack(depth, unshallowCommits);
				}
				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) && refs != null)
				{
					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 (statistics != null)
			{
				logger.OnPackStatistics(statistics);
			}
		}
Esempio n. 18
0
		/// <exception cref="NGit.Errors.TransportException"></exception>
		private void Sendpack(IList<RemoteRefUpdate> updates, ProgressMonitor monitor)
		{
			string pathPack = null;
			string pathIdx = null;
			PackWriter writer = new PackWriter(transport.GetPackConfig(), local.NewObjectReader
				());
			try
			{
				IList<ObjectId> need = new AList<ObjectId>();
				IList<ObjectId> have = new AList<ObjectId>();
				foreach (RemoteRefUpdate r in updates)
				{
					need.AddItem(r.GetNewObjectId());
				}
				foreach (Ref r_1 in GetRefs())
				{
					have.AddItem(r_1.GetObjectId());
					if (r_1.GetPeeledObjectId() != null)
					{
						have.AddItem(r_1.GetPeeledObjectId());
					}
				}
				writer.PreparePack(monitor, need, have);
				// We don't have to continue further if the pack will
				// be an empty pack, as the remote has all objects it
				// needs to complete this change.
				//
				if (writer.GetObjectCount() == 0)
				{
					return;
				}
				packNames = new LinkedHashMap<string, string>();
				foreach (string n in dest.GetPackNames())
				{
					packNames.Put(n, n);
				}
				string @base = "pack-" + writer.ComputeName().Name;
				string packName = @base + ".pack";
				pathPack = "pack/" + packName;
				pathIdx = "pack/" + @base + ".idx";
				if (Sharpen.Collections.Remove(packNames, packName) != null)
				{
					// The remote already contains this pack. We should
					// remove the index before overwriting to prevent bad
					// offsets from appearing to clients.
					//
					dest.WriteInfoPacks(packNames.Keys);
					dest.DeleteFile(pathIdx);
				}
				// Write the pack file, then the index, as readers look the
				// other direction (index, then pack file).
				//
				string wt = "Put " + Sharpen.Runtime.Substring(@base, 0, 12);
				OutputStream os = dest.WriteFile(pathPack, monitor, wt + "..pack");
				try
				{
					os = new BufferedOutputStream(os);
					writer.WritePack(monitor, monitor, os);
				}
				finally
				{
					os.Close();
				}
				os = dest.WriteFile(pathIdx, monitor, wt + "..idx");
				try
				{
					os = new BufferedOutputStream(os);
					writer.WriteIndex(os);
				}
				finally
				{
					os.Close();
				}
				// Record the pack at the start of the pack info list. This
				// way clients are likely to consult the newest pack first,
				// and discover the most recent objects there.
				//
				AList<string> infoPacks = new AList<string>();
				infoPacks.AddItem(packName);
				Sharpen.Collections.AddAll(infoPacks, packNames.Keys);
				dest.WriteInfoPacks(infoPacks);
			}
			catch (IOException err)
			{
				SafeDelete(pathIdx);
				SafeDelete(pathPack);
				throw new TransportException(uri, JGitText.Get().cannotStoreObjects, err);
			}
			finally
			{
				writer.Release();
			}
		}
Esempio n. 19
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);
            }
        }
Esempio n. 20
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();
			}
		}
Esempio n. 21
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);
			}
		}