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

« back to all changes in this revision

Viewing changes to contrib/NGit/NGit/RefWriter.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.Collections.Generic;
 
45
using System.IO;
 
46
using NGit;
 
47
using NGit.Storage.File;
 
48
using NGit.Util;
 
49
using Sharpen;
 
50
 
 
51
namespace NGit
 
52
{
 
53
        /// <summary>
 
54
        /// Writes out refs to the
 
55
        /// <see cref="Constants.INFO_REFS">Constants.INFO_REFS</see>
 
56
        /// and
 
57
        /// <see cref="Constants.PACKED_REFS">Constants.PACKED_REFS</see>
 
58
        /// files.
 
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.
 
61
        /// </summary>
 
62
        public abstract class RefWriter
 
63
        {
 
64
                private readonly ICollection<Ref> refs;
 
65
 
 
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.
 
69
                /// </param>
 
70
                public RefWriter(ICollection<Ref> refs)
 
71
                {
 
72
                        this.refs = RefComparator.Sort(refs);
 
73
                }
 
74
 
 
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.
 
78
                /// </param>
 
79
                public RefWriter(IDictionary<string, Ref> refs)
 
80
                {
 
81
                        if (refs is RefMap)
 
82
                        {
 
83
                                this.refs = refs.Values;
 
84
                        }
 
85
                        else
 
86
                        {
 
87
                                this.refs = RefComparator.Sort(refs.Values);
 
88
                        }
 
89
                }
 
90
 
 
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.
 
94
                /// </param>
 
95
                public RefWriter(RefList<Ref> refs)
 
96
                {
 
97
                        this.refs = refs.AsList();
 
98
                }
 
99
 
 
100
                /// <summary>
 
101
                /// Rebuild the
 
102
                /// <see cref="Constants.INFO_REFS">Constants.INFO_REFS</see>
 
103
                /// .
 
104
                /// <p>
 
105
                /// This method rebuilds the contents of the
 
106
                /// <see cref="Constants.INFO_REFS">Constants.INFO_REFS</see>
 
107
                /// file
 
108
                /// to match the passed list of references.
 
109
                /// </summary>
 
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.
 
113
                /// </exception>
 
114
                public virtual void WriteInfoRefs()
 
115
                {
 
116
                        StringWriter w = new StringWriter();
 
117
                        char[] tmp = new char[Constants.OBJECT_ID_STRING_LENGTH];
 
118
                        foreach (Ref r in refs)
 
119
                        {
 
120
                                if (Constants.HEAD.Equals(r.GetName()))
 
121
                                {
 
122
                                        // Historically HEAD has never been published through
 
123
                                        // the INFO_REFS file. This is a mistake, but its the
 
124
                                        // way things are.
 
125
                                        //
 
126
                                        continue;
 
127
                                }
 
128
                                r.GetObjectId().CopyTo(tmp, w);
 
129
                                w.Write('\t');
 
130
                                w.Write(r.GetName());
 
131
                                w.Write('\n');
 
132
                                if (r.GetPeeledObjectId() != null)
 
133
                                {
 
134
                                        r.GetPeeledObjectId().CopyTo(tmp, w);
 
135
                                        w.Write('\t');
 
136
                                        w.Write(r.GetName());
 
137
                                        w.Write("^{}\n");
 
138
                                }
 
139
                        }
 
140
                        WriteFile(Constants.INFO_REFS, Constants.Encode(w.ToString()));
 
141
                }
 
142
 
 
143
                /// <summary>
 
144
                /// Rebuild the
 
145
                /// <see cref="Constants.PACKED_REFS">Constants.PACKED_REFS</see>
 
146
                /// file.
 
147
                /// <p>
 
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>
 
153
                /// .
 
154
                /// </summary>
 
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.
 
158
                /// </exception>
 
159
                public virtual void WritePackedRefs()
 
160
                {
 
161
                        bool peeled = false;
 
162
                        foreach (Ref r in refs)
 
163
                        {
 
164
                                if (r.GetStorage().IsPacked() && r.IsPeeled())
 
165
                                {
 
166
                                        peeled = true;
 
167
                                        break;
 
168
                                }
 
169
                        }
 
170
                        StringWriter w = new StringWriter();
 
171
                        if (peeled)
 
172
                        {
 
173
                                w.Write(RefDirectory.PACKED_REFS_HEADER);
 
174
                                if (peeled)
 
175
                                {
 
176
                                        w.Write(RefDirectory.PACKED_REFS_PEELED);
 
177
                                }
 
178
                                w.Write('\n');
 
179
                        }
 
180
                        char[] tmp = new char[Constants.OBJECT_ID_STRING_LENGTH];
 
181
                        foreach (Ref r_1 in refs)
 
182
                        {
 
183
                                if (r_1.GetStorage() != RefStorage.PACKED)
 
184
                                {
 
185
                                        continue;
 
186
                                }
 
187
                                r_1.GetObjectId().CopyTo(tmp, w);
 
188
                                w.Write(' ');
 
189
                                w.Write(r_1.GetName());
 
190
                                w.Write('\n');
 
191
                                if (r_1.GetPeeledObjectId() != null)
 
192
                                {
 
193
                                        w.Write('^');
 
194
                                        r_1.GetPeeledObjectId().CopyTo(tmp, w);
 
195
                                        w.Write('\n');
 
196
                                }
 
197
                        }
 
198
                        WriteFile(Constants.PACKED_REFS, Constants.Encode(w.ToString()));
 
199
                }
 
200
 
 
201
                /// <summary>
 
202
                /// Handles actual writing of ref files to the git repository, which may
 
203
                /// differ slightly depending on the destination and transport.
 
204
                /// </summary>
 
205
                /// <remarks>
 
206
                /// Handles actual writing of ref files to the git repository, which may
 
207
                /// differ slightly depending on the destination and transport.
 
208
                /// </remarks>
 
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);
 
213
        }
 
214
}