~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/NGit/NGit.Transport/BundleWriter.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
This code is derived from jgit (http://eclipse.org/jgit).
 
3
Copyright owners are documented in jgit's IP log.
 
4
 
 
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
 
9
 
 
10
All rights reserved.
 
11
 
 
12
Redistribution and use in source and binary forms, with or
 
13
without modification, are permitted provided that the following
 
14
conditions are met:
 
15
 
 
16
- Redistributions of source code must retain the above copyright
 
17
  notice, this list of conditions and the following disclaimer.
 
18
 
 
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.
 
23
 
 
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
 
27
  written permission.
 
28
 
 
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.
 
42
*/
 
43
 
 
44
using System;
 
45
using System.Collections.Generic;
 
46
using System.IO;
 
47
using NGit;
 
48
using NGit.Revwalk;
 
49
using NGit.Storage.Pack;
 
50
using Sharpen;
 
51
 
 
52
namespace NGit.Transport
 
53
{
 
54
        /// <summary>Creates a Git bundle file, for sneaker-net transport to another system.</summary>
 
55
        /// <remarks>
 
56
        /// Creates a Git bundle file, for sneaker-net transport to another system.
 
57
        /// <p>
 
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
 
60
        /// generic
 
61
        /// <see cref="TransportBundleStream">TransportBundleStream</see>
 
62
        /// .
 
63
        /// <p>
 
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.
 
68
        /// <p>
 
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.
 
73
        /// </remarks>
 
74
        public class BundleWriter
 
75
        {
 
76
                private readonly Repository db;
 
77
 
 
78
                private readonly IDictionary<string, ObjectId> include;
 
79
 
 
80
                private readonly ICollection<RevCommit> assume;
 
81
 
 
82
                private readonly ICollection<ObjectId> tagTargets;
 
83
 
 
84
                private PackConfig packConfig;
 
85
 
 
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)
 
90
                {
 
91
                        db = repo;
 
92
                        include = new SortedDictionary<string, ObjectId>();
 
93
                        assume = new HashSet<RevCommit>();
 
94
                        tagTargets = new HashSet<ObjectId>();
 
95
                }
 
96
 
 
97
                /// <summary>Set the configuration used by the pack generator.</summary>
 
98
                /// <remarks>Set the configuration used by the pack generator.</remarks>
 
99
                /// <param name="pc">
 
100
                /// configuration controlling packing parameters. If null the
 
101
                /// source repository's settings will be used.
 
102
                /// </param>
 
103
                public virtual void SetPackConfig(PackConfig pc)
 
104
                {
 
105
                        this.packConfig = pc;
 
106
                }
 
107
 
 
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
 
114
                /// bundle writer.
 
115
                /// </param>
 
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)
 
118
                {
 
119
                        if (!Repository.IsValidRefName(name))
 
120
                        {
 
121
                                throw new ArgumentException(MessageFormat.Format(JGitText.Get().invalidRefName, name
 
122
                                        ));
 
123
                        }
 
124
                        if (include.ContainsKey(name))
 
125
                        {
 
126
                                throw new InvalidOperationException(JGitText.Get().duplicateRef + name);
 
127
                        }
 
128
                        include.Put(name, id.ToObjectId());
 
129
                }
 
130
 
 
131
                /// <summary>Include a single ref (a name/object pair) in the bundle.</summary>
 
132
                /// <remarks>
 
133
                /// Include a single ref (a name/object pair) in the bundle.
 
134
                /// <p>
 
135
                /// This is a utility function for:
 
136
                /// <code>include(r.getName(), r.getObjectId())</code>.
 
137
                /// </remarks>
 
138
                /// <param name="r">the ref to include.</param>
 
139
                public virtual void Include(Ref r)
 
140
                {
 
141
                        Include(r.GetName(), r.GetObjectId());
 
142
                        if (r.GetPeeledObjectId() != null)
 
143
                        {
 
144
                                tagTargets.AddItem(r.GetPeeledObjectId());
 
145
                        }
 
146
                        else
 
147
                        {
 
148
                                if (r.GetObjectId() != null && r.GetName().StartsWith(Constants.R_HEADS))
 
149
                                {
 
150
                                        tagTargets.AddItem(r.GetObjectId());
 
151
                                }
 
152
                        }
 
153
                }
 
154
 
 
155
                /// <summary>Assume a commit is available on the recipient's side.</summary>
 
156
                /// <remarks>
 
157
                /// Assume a commit is available on the recipient's side.
 
158
                /// <p>
 
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.
 
162
                /// </remarks>
 
163
                /// <param name="c">
 
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.
 
167
                /// </param>
 
168
                public virtual void Assume(RevCommit c)
 
169
                {
 
170
                        if (c != null)
 
171
                        {
 
172
                                assume.AddItem(c);
 
173
                        }
 
174
                }
 
175
 
 
176
                /// <summary>Generate and write the bundle to the output stream.</summary>
 
177
                /// <remarks>
 
178
                /// Generate and write the bundle to the output stream.
 
179
                /// <p>
 
180
                /// This method can only be called once per BundleWriter instance.
 
181
                /// </remarks>
 
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
 
186
                /// the stream.
 
187
                /// </param>
 
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
 
191
                /// stream.
 
192
                /// </exception>
 
193
                public virtual void WriteBundle(ProgressMonitor monitor, OutputStream os)
 
194
                {
 
195
                        PackConfig pc = packConfig;
 
196
                        if (pc == null)
 
197
                        {
 
198
                                pc = new PackConfig(db);
 
199
                        }
 
200
                        PackWriter packWriter = new PackWriter(pc, db.NewObjectReader());
 
201
                        try
 
202
                        {
 
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)
 
207
                                {
 
208
                                        exc.AddItem(r.Id);
 
209
                                }
 
210
                                packWriter.SetDeltaBaseAsOffset(true);
 
211
                                packWriter.SetThin(exc.Count > 0);
 
212
                                packWriter.SetReuseValidatingObjects(false);
 
213
                                if (exc.Count == 0)
 
214
                                {
 
215
                                        packWriter.SetTagTargets(tagTargets);
 
216
                                }
 
217
                                packWriter.PreparePack(monitor, inc, exc);
 
218
                                TextWriter w = new OutputStreamWriter(os, Constants.CHARSET);
 
219
                                w.Write(NGit.Transport.TransportBundleConstants.V2_BUNDLE_SIGNATURE);
 
220
                                w.Write('\n');
 
221
                                char[] tmp = new char[Constants.OBJECT_ID_STRING_LENGTH];
 
222
                                foreach (RevCommit a in assume)
 
223
                                {
 
224
                                        w.Write('-');
 
225
                                        a.CopyTo(tmp, w);
 
226
                                        if (a.RawBuffer != null)
 
227
                                        {
 
228
                                                w.Write(' ');
 
229
                                                w.Write(a.GetShortMessage());
 
230
                                        }
 
231
                                        w.Write('\n');
 
232
                                }
 
233
                                foreach (KeyValuePair<string, ObjectId> e in include.EntrySet())
 
234
                                {
 
235
                                        e.Value.CopyTo(tmp, w);
 
236
                                        w.Write(' ');
 
237
                                        w.Write(e.Key);
 
238
                                        w.Write('\n');
 
239
                                }
 
240
                                w.Write('\n');
 
241
                                w.Flush();
 
242
                                packWriter.WritePack(monitor, monitor, os);
 
243
                        }
 
244
                        finally
 
245
                        {
 
246
                                packWriter.Release();
 
247
                        }
 
248
                }
 
249
        }
 
250
}