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.
44
using System.Collections.Generic;
47
using NGit.Storage.File;
54
/// Writes out refs to the
55
/// <see cref="Constants.INFO_REFS">Constants.INFO_REFS</see>
57
/// <see cref="Constants.PACKED_REFS">Constants.PACKED_REFS</see>
59
/// This class is abstract as the writing of the files must be handled by the
60
/// caller. This is because it is used by transport classes as well.
62
public abstract class RefWriter
64
private readonly ICollection<Ref> refs;
66
/// <param name="refs">
67
/// the complete set of references. This should have been computed
68
/// by applying updates to the advertised refs already discovered.
70
public RefWriter(ICollection<Ref> refs)
72
this.refs = RefComparator.Sort(refs);
75
/// <param name="refs">
76
/// the complete set of references. This should have been computed
77
/// by applying updates to the advertised refs already discovered.
79
public RefWriter(IDictionary<string, Ref> refs)
83
this.refs = refs.Values;
87
this.refs = RefComparator.Sort(refs.Values);
91
/// <param name="refs">
92
/// the complete set of references. This should have been computed
93
/// by applying updates to the advertised refs already discovered.
95
public RefWriter(RefList<Ref> refs)
97
this.refs = refs.AsList();
102
/// <see cref="Constants.INFO_REFS">Constants.INFO_REFS</see>
105
/// This method rebuilds the contents of the
106
/// <see cref="Constants.INFO_REFS">Constants.INFO_REFS</see>
108
/// to match the passed list of references.
110
/// <exception cref="System.IO.IOException">
111
/// writing is not supported, or attempting to write the file
112
/// failed, possibly due to permissions or remote disk full, etc.
114
public virtual void WriteInfoRefs()
116
StringWriter w = new StringWriter();
117
char[] tmp = new char[Constants.OBJECT_ID_STRING_LENGTH];
118
foreach (Ref r in refs)
120
if (Constants.HEAD.Equals(r.GetName()))
122
// Historically HEAD has never been published through
123
// the INFO_REFS file. This is a mistake, but its the
128
r.GetObjectId().CopyTo(tmp, w);
130
w.Write(r.GetName());
132
if (r.GetPeeledObjectId() != null)
134
r.GetPeeledObjectId().CopyTo(tmp, w);
136
w.Write(r.GetName());
140
WriteFile(Constants.INFO_REFS, Constants.Encode(w.ToString()));
145
/// <see cref="Constants.PACKED_REFS">Constants.PACKED_REFS</see>
148
/// This method rebuilds the contents of the
149
/// <see cref="Constants.PACKED_REFS">Constants.PACKED_REFS</see>
150
/// file to match the passed list of references, including only those refs
151
/// that have a storage type of
152
/// <see cref="RefStorage.PACKED">RefStorage.PACKED</see>
155
/// <exception cref="System.IO.IOException">
156
/// writing is not supported, or attempting to write the file
157
/// failed, possibly due to permissions or remote disk full, etc.
159
public virtual void WritePackedRefs()
162
foreach (Ref r in refs)
164
if (r.GetStorage().IsPacked() && r.IsPeeled())
170
StringWriter w = new StringWriter();
173
w.Write(RefDirectory.PACKED_REFS_HEADER);
176
w.Write(RefDirectory.PACKED_REFS_PEELED);
180
char[] tmp = new char[Constants.OBJECT_ID_STRING_LENGTH];
181
foreach (Ref r_1 in refs)
183
if (r_1.GetStorage() != RefStorage.PACKED)
187
r_1.GetObjectId().CopyTo(tmp, w);
189
w.Write(r_1.GetName());
191
if (r_1.GetPeeledObjectId() != null)
194
r_1.GetPeeledObjectId().CopyTo(tmp, w);
198
WriteFile(Constants.PACKED_REFS, Constants.Encode(w.ToString()));
202
/// Handles actual writing of ref files to the git repository, which may
203
/// differ slightly depending on the destination and transport.
206
/// Handles actual writing of ref files to the git repository, which may
207
/// differ slightly depending on the destination and transport.
209
/// <param name="file">path to ref file.</param>
210
/// <param name="content">byte content of file to be written.</param>
211
/// <exception cref="System.IO.IOException">System.IO.IOException</exception>
212
protected internal abstract void WriteFile(string file, byte[] content);