View Javadoc
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 org.eclipse.jgit.lib.Constants#INFO_REFS} and
60   * {@link org.eclipse.jgit.lib.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  	 * <p>Constructor for RefWriter.</p>
71  	 *
72  	 * @param refs
73  	 *            the complete set of references. This should have been computed
74  	 *            by applying updates to the advertised refs already discovered.
75  	 */
76  	public RefWriter(Collection<Ref> refs) {
77  		this.refs = RefComparator.sort(refs);
78  	}
79  
80  	/**
81  	 * <p>Constructor for RefWriter.</p>
82  	 *
83  	 * @param refs
84  	 *            the complete set of references. This should have been computed
85  	 *            by applying updates to the advertised refs already discovered.
86  	 */
87  	public RefWriter(Map<String, Ref> refs) {
88  		if (refs instanceof RefMap)
89  			this.refs = refs.values();
90  		else
91  			this.refs = RefComparator.sort(refs.values());
92  	}
93  
94  	/**
95  	 * <p>Constructor for RefWriter.</p>
96  	 *
97  	 * @param refs
98  	 *            the complete set of references. This should have been computed
99  	 *            by applying updates to the advertised refs already discovered.
100 	 */
101 	public RefWriter(RefList<Ref> refs) {
102 		this.refs = refs.asList();
103 	}
104 
105 	/**
106 	 * Rebuild the {@link org.eclipse.jgit.lib.Constants#INFO_REFS}.
107 	 * <p>
108 	 * This method rebuilds the contents of the
109 	 * {@link org.eclipse.jgit.lib.Constants#INFO_REFS} file to match the passed
110 	 * list of references.
111 	 *
112 	 * @throws java.io.IOException
113 	 *             writing is not supported, or attempting to write the file
114 	 *             failed, possibly due to permissions or remote disk full, etc.
115 	 */
116 	public void writeInfoRefs() throws IOException {
117 		final StringWriter w = new StringWriter();
118 		final char[] tmp = new char[Constants.OBJECT_ID_STRING_LENGTH];
119 		for (Ref r : refs) {
120 			if (Constants.HEAD.equals(r.getName())) {
121 				// Historically HEAD has never been published through
122 				// the INFO_REFS file. This is a mistake, but its the
123 				// way things are.
124 				//
125 				continue;
126 			}
127 
128 			ObjectId objectId = r.getObjectId();
129 			if (objectId == null) {
130 				// Symrefs to unborn branches aren't advertised in the info/refs
131 				// file.
132 				continue;
133 			}
134 			objectId.copyTo(tmp, w);
135 			w.write('\t');
136 			w.write(r.getName());
137 			w.write('\n');
138 
139 			ObjectId peeledObjectId = r.getPeeledObjectId();
140 			if (peeledObjectId != null) {
141 				peeledObjectId.copyTo(tmp, w);
142 				w.write('\t');
143 				w.write(r.getName());
144 				w.write("^{}\n"); //$NON-NLS-1$
145 			}
146 		}
147 		writeFile(Constants.INFO_REFS, Constants.encode(w.toString()));
148 	}
149 
150 	/**
151 	 * Rebuild the {@link org.eclipse.jgit.lib.Constants#PACKED_REFS} file.
152 	 * <p>
153 	 * This method rebuilds the contents of the
154 	 * {@link org.eclipse.jgit.lib.Constants#PACKED_REFS} file to match the
155 	 * passed list of references, including only those refs that have a storage
156 	 * type of {@link org.eclipse.jgit.lib.Ref.Storage#PACKED}.
157 	 *
158 	 * @throws java.io.IOException
159 	 *             writing is not supported, or attempting to write the file
160 	 *             failed, possibly due to permissions or remote disk full, etc.
161 	 */
162 	public void writePackedRefs() throws IOException {
163 		boolean peeled = false;
164 		for (Ref r : refs) {
165 			if (r.getStorage().isPacked() && r.isPeeled()) {
166 				peeled = true;
167 				break;
168 			}
169 		}
170 
171 		final StringWriter w = new StringWriter();
172 		if (peeled) {
173 			w.write(RefDirectory.PACKED_REFS_HEADER);
174 			if (peeled)
175 				w.write(RefDirectory.PACKED_REFS_PEELED);
176 			w.write('\n');
177 		}
178 
179 		final char[] tmp = new char[Constants.OBJECT_ID_STRING_LENGTH];
180 		for (Ref r : refs) {
181 			if (r.getStorage() != Ref.Storage.PACKED)
182 				continue;
183 
184 			ObjectId objectId = r.getObjectId();
185 			if (objectId == null) {
186 				// A packed ref cannot be a symref, let alone a symref
187 				// to an unborn branch.
188 				throw new NullPointerException();
189 			}
190 			objectId.copyTo(tmp, w);
191 			w.write(' ');
192 			w.write(r.getName());
193 			w.write('\n');
194 
195 			ObjectId peeledObjectId = r.getPeeledObjectId();
196 			if (peeledObjectId != null) {
197 				w.write('^');
198 				peeledObjectId.copyTo(tmp, w);
199 				w.write('\n');
200 			}
201 		}
202 		writeFile(Constants.PACKED_REFS, Constants.encode(w.toString()));
203 	}
204 
205 	/**
206 	 * Handles actual writing of ref files to the git repository, which may
207 	 * differ slightly depending on the destination and transport.
208 	 *
209 	 * @param file
210 	 *            path to ref file.
211 	 * @param content
212 	 *            byte content of file to be written.
213 	 * @throws java.io.IOException
214 	 */
215 	protected abstract void writeFile(String file, byte[] content)
216 			throws IOException;
217 }