1 /*
2 * Copyright (C) 2008, Charles O'Farrell <charleso@charleso.org>
3 * Copyright (C) 2009-2010, Google Inc.
4 * Copyright (C) 2009, Robin Rosenberg <robin.rosenberg@dewire.com>
5 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
6 * and other copyright owners as documented in the project's IP log.
7 *
8 * This program and the accompanying materials are made available
9 * under the terms of the Eclipse Distribution License v1.0 which
10 * accompanies this distribution, is reproduced below, and is
11 * available at http://www.eclipse.org/org/documents/edl-v10.php
12 *
13 * All rights reserved.
14 *
15 * Redistribution and use in source and binary forms, with or
16 * without modification, are permitted provided that the following
17 * conditions are met:
18 *
19 * - Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 *
22 * - Redistributions in binary form must reproduce the above
23 * copyright notice, this list of conditions and the following
24 * disclaimer in the documentation and/or other materials provided
25 * with the distribution.
26 *
27 * - Neither the name of the Eclipse Foundation, Inc. nor the
28 * names of its contributors may be used to endorse or promote
29 * products derived from this software without specific prior
30 * written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
33 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
34 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
37 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
39 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
40 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
41 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
42 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
44 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 */
46
47 package org.eclipse.jgit.lib;
48
49 import java.io.IOException;
50 import java.io.StringWriter;
51 import java.util.Collection;
52 import java.util.Map;
53
54 import org.eclipse.jgit.internal.storage.file.RefDirectory;
55 import org.eclipse.jgit.util.RefList;
56 import org.eclipse.jgit.util.RefMap;
57
58 /**
59 * Writes out refs to the {@link Constants#INFO_REFS} and
60 * {@link Constants#PACKED_REFS} files.
61 *
62 * This class is abstract as the writing of the files must be handled by the
63 * caller. This is because it is used by transport classes as well.
64 */
65 public abstract class RefWriter {
66
67 private final Collection<Ref> refs;
68
69 /**
70 * @param refs
71 * the complete set of references. This should have been computed
72 * by applying updates to the advertised refs already discovered.
73 */
74 public RefWriter(Collection<Ref> refs) {
75 this.refs = RefComparator.sort(refs);
76 }
77
78 /**
79 * @param refs
80 * the complete set of references. This should have been computed
81 * by applying updates to the advertised refs already discovered.
82 */
83 public RefWriter(Map<String, Ref> refs) {
84 if (refs instanceof RefMap)
85 this.refs = refs.values();
86 else
87 this.refs = RefComparator.sort(refs.values());
88 }
89
90 /**
91 * @param refs
92 * the complete set of references. This should have been computed
93 * by applying updates to the advertised refs already discovered.
94 */
95 public RefWriter(RefList<Ref> refs) {
96 this.refs = refs.asList();
97 }
98
99 /**
100 * Rebuild the {@link Constants#INFO_REFS}.
101 * <p>
102 * This method rebuilds the contents of the {@link Constants#INFO_REFS} file
103 * to match the passed list of references.
104 *
105 *
106 * @throws IOException
107 * writing is not supported, or attempting to write the file
108 * failed, possibly due to permissions or remote disk full, etc.
109 */
110 public void writeInfoRefs() throws IOException {
111 final StringWriter w = new StringWriter();
112 final char[] tmp = new char[Constants.OBJECT_ID_STRING_LENGTH];
113 for (final Ref r : refs) {
114 if (Constants.HEAD.equals(r.getName())) {
115 // Historically HEAD has never been published through
116 // the INFO_REFS file. This is a mistake, but its the
117 // way things are.
118 //
119 continue;
120 }
121
122 ObjectId objectId = r.getObjectId();
123 if (objectId == null) {
124 // Symrefs to unborn branches aren't advertised in the info/refs
125 // file.
126 continue;
127 }
128 objectId.copyTo(tmp, w);
129 w.write('\t');
130 w.write(r.getName());
131 w.write('\n');
132
133 ObjectId peeledObjectId = r.getPeeledObjectId();
134 if (peeledObjectId != null) {
135 peeledObjectId.copyTo(tmp, w);
136 w.write('\t');
137 w.write(r.getName());
138 w.write("^{}\n"); //$NON-NLS-1$
139 }
140 }
141 writeFile(Constants.INFO_REFS, Constants.encode(w.toString()));
142 }
143
144 /**
145 * Rebuild the {@link Constants#PACKED_REFS} file.
146 * <p>
147 * This method rebuilds the contents of the {@link Constants#PACKED_REFS}
148 * file to match the passed list of references, including only those refs
149 * that have a storage type of {@link Ref.Storage#PACKED}.
150 *
151 * @throws IOException
152 * writing is not supported, or attempting to write the file
153 * failed, possibly due to permissions or remote disk full, etc.
154 */
155 public void writePackedRefs() throws IOException {
156 boolean peeled = false;
157 for (final Ref r : refs) {
158 if (r.getStorage().isPacked() && r.isPeeled()) {
159 peeled = true;
160 break;
161 }
162 }
163
164 final StringWriter w = new StringWriter();
165 if (peeled) {
166 w.write(RefDirectory.PACKED_REFS_HEADER);
167 if (peeled)
168 w.write(RefDirectory.PACKED_REFS_PEELED);
169 w.write('\n');
170 }
171
172 final char[] tmp = new char[Constants.OBJECT_ID_STRING_LENGTH];
173 for (final Ref r : refs) {
174 if (r.getStorage() != Ref.Storage.PACKED)
175 continue;
176
177 ObjectId objectId = r.getObjectId();
178 if (objectId == null) {
179 // A packed ref cannot be a symref, let alone a symref
180 // to an unborn branch.
181 throw new NullPointerException();
182 }
183 objectId.copyTo(tmp, w);
184 w.write(' ');
185 w.write(r.getName());
186 w.write('\n');
187
188 ObjectId peeledObjectId = r.getPeeledObjectId();
189 if (peeledObjectId != null) {
190 w.write('^');
191 peeledObjectId.copyTo(tmp, w);
192 w.write('\n');
193 }
194 }
195 writeFile(Constants.PACKED_REFS, Constants.encode(w.toString()));
196 }
197
198 /**
199 * Handles actual writing of ref files to the git repository, which may
200 * differ slightly depending on the destination and transport.
201 *
202 * @param file
203 * path to ref file.
204 * @param content
205 * byte content of file to be written.
206 * @throws IOException
207 */
208 protected abstract void writeFile(String file, byte[] content)
209 throws IOException;
210 }