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 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 			r.getObjectId().copyTo(tmp, w);
123 			w.write('\t');
124 			w.write(r.getName());
125 			w.write('\n');
126 
127 			if (r.getPeeledObjectId() != null) {
128 				r.getPeeledObjectId().copyTo(tmp, w);
129 				w.write('\t');
130 				w.write(r.getName());
131 				w.write("^{}\n"); //$NON-NLS-1$
132 			}
133 		}
134 		writeFile(Constants.INFO_REFS, Constants.encode(w.toString()));
135 	}
136 
137 	/**
138 	 * Rebuild the {@link Constants#PACKED_REFS} file.
139 	 * <p>
140 	 * This method rebuilds the contents of the {@link Constants#PACKED_REFS}
141 	 * file to match the passed list of references, including only those refs
142 	 * that have a storage type of {@link Ref.Storage#PACKED}.
143 	 *
144 	 * @throws IOException
145 	 *             writing is not supported, or attempting to write the file
146 	 *             failed, possibly due to permissions or remote disk full, etc.
147 	 */
148 	public void writePackedRefs() throws IOException {
149 		boolean peeled = false;
150 		for (final Ref r : refs) {
151 			if (r.getStorage().isPacked() && r.isPeeled()) {
152 				peeled = true;
153 				break;
154 			}
155 		}
156 
157 		final StringWriter w = new StringWriter();
158 		if (peeled) {
159 			w.write(RefDirectory.PACKED_REFS_HEADER);
160 			if (peeled)
161 				w.write(RefDirectory.PACKED_REFS_PEELED);
162 			w.write('\n');
163 		}
164 
165 		final char[] tmp = new char[Constants.OBJECT_ID_STRING_LENGTH];
166 		for (final Ref r : refs) {
167 			if (r.getStorage() != Ref.Storage.PACKED)
168 				continue;
169 
170 			r.getObjectId().copyTo(tmp, w);
171 			w.write(' ');
172 			w.write(r.getName());
173 			w.write('\n');
174 
175 			if (r.getPeeledObjectId() != null) {
176 				w.write('^');
177 				r.getPeeledObjectId().copyTo(tmp, w);
178 				w.write('\n');
179 			}
180 		}
181 		writeFile(Constants.PACKED_REFS, Constants.encode(w.toString()));
182 	}
183 
184 	/**
185 	 * Handles actual writing of ref files to the git repository, which may
186 	 * differ slightly depending on the destination and transport.
187 	 *
188 	 * @param file
189 	 *            path to ref file.
190 	 * @param content
191 	 *            byte content of file to be written.
192 	 * @throws IOException
193 	 */
194 	protected abstract void writeFile(String file, byte[] content)
195 			throws IOException;
196 }