2
This code is derived from jgit (http://eclipse.org/jgit).
3
Copyright owners are documented in jgit's IP log.
5
This program and the accompanying materials are made available
6
under the terms of the Eclipse Distribution License v1.0 which
7
accompanies this distribution, is reproduced below, and is
8
available at http://www.eclipse.org/org/documents/edl-v10.php
12
Redistribution and use in source and binary forms, with or
13
without modification, are permitted provided that the following
16
- Redistributions of source code must retain the above copyright
17
notice, this list of conditions and the following disclaimer.
19
- Redistributions in binary form must reproduce the above
20
copyright notice, this list of conditions and the following
21
disclaimer in the documentation and/or other materials provided
22
with the distribution.
24
- Neither the name of the Eclipse Foundation, Inc. nor the
25
names of its contributors may be used to endorse or promote
26
products derived from this software without specific prior
29
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45
using System.Collections.Generic;
49
using NGit.Storage.Pack;
52
namespace NGit.Transport
54
/// <summary>Creates a Git bundle file, for sneaker-net transport to another system.</summary>
56
/// Creates a Git bundle file, for sneaker-net transport to another system.
58
/// Bundles generated by this class can be later read in from a file URI using
59
/// the bundle transport, or from an application controlled buffer by the more
61
/// <see cref="TransportBundleStream">TransportBundleStream</see>
64
/// Applications creating bundles need to call one or more <code>include</code>
65
/// calls to reflect which objects should be available as refs in the bundle for
66
/// the other side to fetch. At least one include is required to create a valid
67
/// bundle file, and duplicate names are not permitted.
69
/// Optional <code>assume</code> calls can be made to declare commits which the
70
/// recipient must have in order to fetch from the bundle file. Objects reachable
71
/// from these assumed commits can be used as delta bases in order to reduce the
72
/// overall bundle size.
74
public class BundleWriter
76
private readonly Repository db;
78
private readonly IDictionary<string, ObjectId> include;
80
private readonly ICollection<RevCommit> assume;
82
private readonly ICollection<ObjectId> tagTargets;
84
private PackConfig packConfig;
86
/// <summary>Create a writer for a bundle.</summary>
87
/// <remarks>Create a writer for a bundle.</remarks>
88
/// <param name="repo">repository where objects are stored.</param>
89
public BundleWriter(Repository repo)
92
include = new SortedDictionary<string, ObjectId>();
93
assume = new HashSet<RevCommit>();
94
tagTargets = new HashSet<ObjectId>();
97
/// <summary>Set the configuration used by the pack generator.</summary>
98
/// <remarks>Set the configuration used by the pack generator.</remarks>
100
/// configuration controlling packing parameters. If null the
101
/// source repository's settings will be used.
103
public virtual void SetPackConfig(PackConfig pc)
105
this.packConfig = pc;
108
/// <summary>Include an object (and everything reachable from it) in the bundle.</summary>
109
/// <remarks>Include an object (and everything reachable from it) in the bundle.</remarks>
110
/// <param name="name">
111
/// name the recipient can discover this object as from the
112
/// bundle's list of advertised refs . The name must be a valid
113
/// ref format and must not have already been included in this
116
/// <param name="id">object to pack. Multiple refs may point to the same object.</param>
117
public virtual void Include(string name, AnyObjectId id)
119
if (!Repository.IsValidRefName(name))
121
throw new ArgumentException(MessageFormat.Format(JGitText.Get().invalidRefName, name
124
if (include.ContainsKey(name))
126
throw new InvalidOperationException(JGitText.Get().duplicateRef + name);
128
include.Put(name, id.ToObjectId());
131
/// <summary>Include a single ref (a name/object pair) in the bundle.</summary>
133
/// Include a single ref (a name/object pair) in the bundle.
135
/// This is a utility function for:
136
/// <code>include(r.getName(), r.getObjectId())</code>.
138
/// <param name="r">the ref to include.</param>
139
public virtual void Include(Ref r)
141
Include(r.GetName(), r.GetObjectId());
142
if (r.GetPeeledObjectId() != null)
144
tagTargets.AddItem(r.GetPeeledObjectId());
148
if (r.GetObjectId() != null && r.GetName().StartsWith(Constants.R_HEADS))
150
tagTargets.AddItem(r.GetObjectId());
155
/// <summary>Assume a commit is available on the recipient's side.</summary>
157
/// Assume a commit is available on the recipient's side.
159
/// In order to fetch from a bundle the recipient must have any assumed
160
/// commit. Each assumed commit is explicitly recorded in the bundle header
161
/// to permit the recipient to validate it has these objects.
164
/// the commit to assume being available. This commit should be
165
/// parsed and not disposed in order to maximize the amount of
166
/// debugging information available in the bundle stream.
168
public virtual void Assume(RevCommit c)
176
/// <summary>Generate and write the bundle to the output stream.</summary>
178
/// Generate and write the bundle to the output stream.
180
/// This method can only be called once per BundleWriter instance.
182
/// <param name="monitor">progress monitor to report bundle writing status to.</param>
183
/// <param name="os">
184
/// the stream the bundle is written to. The stream should be
185
/// buffered by the caller. The caller is responsible for closing
188
/// <exception cref="System.IO.IOException">
189
/// an error occurred reading a local object's data to include in
190
/// the bundle, or writing compressed object data to the output
193
public virtual void WriteBundle(ProgressMonitor monitor, OutputStream os)
195
PackConfig pc = packConfig;
198
pc = new PackConfig(db);
200
PackWriter packWriter = new PackWriter(pc, db.NewObjectReader());
203
HashSet<ObjectId> inc = new HashSet<ObjectId>();
204
HashSet<ObjectId> exc = new HashSet<ObjectId>();
205
Sharpen.Collections.AddAll(inc, include.Values);
206
foreach (RevCommit r in assume)
210
packWriter.SetDeltaBaseAsOffset(true);
211
packWriter.SetThin(exc.Count > 0);
212
packWriter.SetReuseValidatingObjects(false);
215
packWriter.SetTagTargets(tagTargets);
217
packWriter.PreparePack(monitor, inc, exc);
218
TextWriter w = new OutputStreamWriter(os, Constants.CHARSET);
219
w.Write(NGit.Transport.TransportBundleConstants.V2_BUNDLE_SIGNATURE);
221
char[] tmp = new char[Constants.OBJECT_ID_STRING_LENGTH];
222
foreach (RevCommit a in assume)
226
if (a.RawBuffer != null)
229
w.Write(a.GetShortMessage());
233
foreach (KeyValuePair<string, ObjectId> e in include.EntrySet())
235
e.Value.CopyTo(tmp, w);
242
packWriter.WritePack(monitor, monitor, os);
246
packWriter.Release();