View Javadoc
1   /*
2    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
3    * and other copyright owners as documented in the project'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  package org.eclipse.jgit.transport;
45  
46  import static org.eclipse.jgit.transport.WalkRemoteObjectDatabase.ROOT_DIR;
47  
48  import java.io.BufferedOutputStream;
49  import java.io.IOException;
50  import java.io.OutputStream;
51  import java.util.ArrayList;
52  import java.util.Collection;
53  import java.util.HashSet;
54  import java.util.LinkedHashMap;
55  import java.util.List;
56  import java.util.Map;
57  import java.util.Set;
58  import java.util.TreeMap;
59  
60  import org.eclipse.jgit.errors.TransportException;
61  import org.eclipse.jgit.internal.JGitText;
62  import org.eclipse.jgit.internal.storage.pack.PackWriter;
63  import org.eclipse.jgit.lib.AnyObjectId;
64  import org.eclipse.jgit.lib.Constants;
65  import org.eclipse.jgit.lib.ObjectId;
66  import org.eclipse.jgit.lib.ObjectIdRef;
67  import org.eclipse.jgit.lib.ProgressMonitor;
68  import org.eclipse.jgit.lib.Ref;
69  import org.eclipse.jgit.lib.Ref.Storage;
70  import org.eclipse.jgit.lib.RefWriter;
71  import org.eclipse.jgit.lib.Repository;
72  import org.eclipse.jgit.transport.RemoteRefUpdate.Status;
73  
74  /**
75   * Generic push support for dumb transport protocols.
76   * <p>
77   * Since there are no Git-specific smarts on the remote side of the connection
78   * the client side must handle everything on its own. The generic push support
79   * requires being able to delete, create and overwrite files on the remote side,
80   * as well as create any missing directories (if necessary). Typically this can
81   * be handled through an FTP style protocol.
82   * <p>
83   * Objects not on the remote side are uploaded as pack files, using one pack
84   * file per invocation. This simplifies the implementation as only two data
85   * files need to be written to the remote repository.
86   * <p>
87   * Push support supplied by this class is not multiuser safe. Concurrent pushes
88   * to the same repository may yield an inconsistent reference database which may
89   * confuse fetch clients.
90   * <p>
91   * A single push is concurrently safe with multiple fetch requests, due to the
92   * careful order of operations used to update the repository. Clients fetching
93   * may receive transient failures due to short reads on certain files if the
94   * protocol does not support atomic file replacement.
95   *
96   * @see WalkRemoteObjectDatabase
97   */
98  class WalkPushConnection extends BaseConnection implements PushConnection {
99  	/** The repository this transport pushes out of. */
100 	private final Repository local;
101 
102 	/** Location of the remote repository we are writing to. */
103 	private final URIish uri;
104 
105 	/** Database connection to the remote repository. */
106 	final WalkRemoteObjectDatabase dest;
107 
108 	/** The configured transport we were constructed by. */
109 	private final Transport transport;
110 
111 	/**
112 	 * Packs already known to reside in the remote repository.
113 	 * <p>
114 	 * This is a LinkedHashMap to maintain the original order.
115 	 */
116 	private LinkedHashMap<String, String> packNames;
117 
118 	/** Complete listing of refs the remote will have after our push. */
119 	private Map<String, Ref> newRefs;
120 
121 	/**
122 	 * Updates which require altering the packed-refs file to complete.
123 	 * <p>
124 	 * If this collection is non-empty then any refs listed in {@link #newRefs}
125 	 * with a storage class of {@link Storage#PACKED} will be written.
126 	 */
127 	private Collection<RemoteRefUpdate> packedRefUpdates;
128 
129 	WalkPushConnection(final WalkTransport walkTransport,
130 			final WalkRemoteObjectDatabase w) {
131 		transport = (Transport) walkTransport;
132 		local = transport.local;
133 		uri = transport.getURI();
134 		dest = w;
135 	}
136 
137 	/** {@inheritDoc} */
138 	@Override
139 	public void push(final ProgressMonitor monitor,
140 			final Map<String, RemoteRefUpdate> refUpdates)
141 			throws TransportException {
142 		push(monitor, refUpdates, null);
143 	}
144 
145 	/** {@inheritDoc} */
146 	@Override
147 	public void push(final ProgressMonitor monitor,
148 			final Map<String, RemoteRefUpdate> refUpdates, OutputStream out)
149 			throws TransportException {
150 		markStartedOperation();
151 		packNames = null;
152 		newRefs = new TreeMap<>(getRefsMap());
153 		packedRefUpdates = new ArrayList<>(refUpdates.size());
154 
155 		// Filter the commands and issue all deletes first. This way we
156 		// can correctly handle a directory being cleared out and a new
157 		// ref using the directory name being created.
158 		//
159 		final List<RemoteRefUpdate> updates = new ArrayList<>();
160 		for (RemoteRefUpdate u : refUpdates.values()) {
161 			final String n = u.getRemoteName();
162 			if (!n.startsWith("refs/") || !Repository.isValidRefName(n)) { //$NON-NLS-1$
163 				u.setStatus(Status.REJECTED_OTHER_REASON);
164 				u.setMessage(JGitText.get().funnyRefname);
165 				continue;
166 			}
167 
168 			if (AnyObjectId.equals(ObjectId.zeroId(), u.getNewObjectId()))
169 				deleteCommand(u);
170 			else
171 				updates.add(u);
172 		}
173 
174 		// If we have any updates we need to upload the objects first, to
175 		// prevent creating refs pointing at non-existent data. Then we
176 		// can update the refs, and the info-refs file for dumb transports.
177 		//
178 		if (!updates.isEmpty())
179 			sendpack(updates, monitor);
180 		for (RemoteRefUpdate u : updates)
181 			updateCommand(u);
182 
183 		// Is this a new repository? If so we should create additional
184 		// metadata files so it is properly initialized during the push.
185 		//
186 		if (!updates.isEmpty() && isNewRepository())
187 			createNewRepository(updates);
188 
189 		RefWriter refWriter = new RefWriter(newRefs.values()) {
190 			@Override
191 			protected void writeFile(String file, byte[] content)
192 					throws IOException {
193 				dest.writeFile(ROOT_DIR + file, content);
194 			}
195 		};
196 		if (!packedRefUpdates.isEmpty()) {
197 			try {
198 				refWriter.writePackedRefs();
199 				for (RemoteRefUpdate u : packedRefUpdates)
200 					u.setStatus(Status.OK);
201 			} catch (IOException err) {
202 				for (RemoteRefUpdate u : packedRefUpdates) {
203 					u.setStatus(Status.REJECTED_OTHER_REASON);
204 					u.setMessage(err.getMessage());
205 				}
206 				throw new TransportException(uri, JGitText.get().failedUpdatingRefs, err);
207 			}
208 		}
209 
210 		try {
211 			refWriter.writeInfoRefs();
212 		} catch (IOException err) {
213 			throw new TransportException(uri, JGitText.get().failedUpdatingRefs, err);
214 		}
215 	}
216 
217 	/** {@inheritDoc} */
218 	@Override
219 	public void close() {
220 		dest.close();
221 	}
222 
223 	private void sendpack(final List<RemoteRefUpdate> updates,
224 			final ProgressMonitor monitor) throws TransportException {
225 		String pathPack = null;
226 		String pathIdx = null;
227 
228 		try (PackWriter writer = new PackWriter(transport.getPackConfig(),
229 				local.newObjectReader())) {
230 
231 			final Set<ObjectId> need = new HashSet<>();
232 			final Set<ObjectId> have = new HashSet<>();
233 			for (RemoteRefUpdate r : updates)
234 				need.add(r.getNewObjectId());
235 			for (Ref r : getRefs()) {
236 				have.add(r.getObjectId());
237 				if (r.getPeeledObjectId() != null)
238 					have.add(r.getPeeledObjectId());
239 			}
240 			writer.preparePack(monitor, need, have);
241 
242 			// We don't have to continue further if the pack will
243 			// be an empty pack, as the remote has all objects it
244 			// needs to complete this change.
245 			//
246 			if (writer.getObjectCount() == 0)
247 				return;
248 
249 			packNames = new LinkedHashMap<>();
250 			for (String n : dest.getPackNames())
251 				packNames.put(n, n);
252 
253 			final String base = "pack-" + writer.computeName().name(); //$NON-NLS-1$
254 			final String packName = base + ".pack"; //$NON-NLS-1$
255 			pathPack = "pack/" + packName; //$NON-NLS-1$
256 			pathIdx = "pack/" + base + ".idx"; //$NON-NLS-1$ //$NON-NLS-2$
257 
258 			if (packNames.remove(packName) != null) {
259 				// The remote already contains this pack. We should
260 				// remove the index before overwriting to prevent bad
261 				// offsets from appearing to clients.
262 				//
263 				dest.writeInfoPacks(packNames.keySet());
264 				dest.deleteFile(pathIdx);
265 			}
266 
267 			// Write the pack file, then the index, as readers look the
268 			// other direction (index, then pack file).
269 			//
270 			String wt = "Put " + base.substring(0, 12); //$NON-NLS-1$
271 			try (OutputStream os = new BufferedOutputStream(
272 					dest.writeFile(pathPack, monitor, wt + "..pack"))) { //$NON-NLS-1$
273 				writer.writePack(monitor, monitor, os);
274 			}
275 
276 			try (OutputStream os = new BufferedOutputStream(
277 					dest.writeFile(pathIdx, monitor, wt + "..idx"))) { //$NON-NLS-1$
278 				writer.writeIndex(os);
279 			}
280 
281 			// Record the pack at the start of the pack info list. This
282 			// way clients are likely to consult the newest pack first,
283 			// and discover the most recent objects there.
284 			//
285 			final ArrayList<String> infoPacks = new ArrayList<>();
286 			infoPacks.add(packName);
287 			infoPacks.addAll(packNames.keySet());
288 			dest.writeInfoPacks(infoPacks);
289 
290 		} catch (IOException err) {
291 			safeDelete(pathIdx);
292 			safeDelete(pathPack);
293 
294 			throw new TransportException(uri, JGitText.get().cannotStoreObjects, err);
295 		}
296 	}
297 
298 	private void safeDelete(String path) {
299 		if (path != null) {
300 			try {
301 				dest.deleteFile(path);
302 			} catch (IOException cleanupFailure) {
303 				// Ignore the deletion failure. We probably are
304 				// already failing and were just trying to pick
305 				// up after ourselves.
306 			}
307 		}
308 	}
309 
310 	private void deleteCommand(RemoteRefUpdate u) {
311 		final Ref r = newRefs.remove(u.getRemoteName());
312 		if (r == null) {
313 			// Already gone.
314 			//
315 			u.setStatus(Status.OK);
316 			return;
317 		}
318 
319 		if (r.getStorage().isPacked())
320 			packedRefUpdates.add(u);
321 
322 		if (r.getStorage().isLoose()) {
323 			try {
324 				dest.deleteRef(u.getRemoteName());
325 				u.setStatus(Status.OK);
326 			} catch (IOException e) {
327 				u.setStatus(Status.REJECTED_OTHER_REASON);
328 				u.setMessage(e.getMessage());
329 			}
330 		}
331 
332 		try {
333 			dest.deleteRefLog(u.getRemoteName());
334 		} catch (IOException e) {
335 			u.setStatus(Status.REJECTED_OTHER_REASON);
336 			u.setMessage(e.getMessage());
337 		}
338 	}
339 
340 	private void updateCommand(RemoteRefUpdate u) {
341 		try {
342 			dest.writeRef(u.getRemoteName(), u.getNewObjectId());
343 			newRefs.put(u.getRemoteName(), new ObjectIdRef.Unpeeled(
344 					Storage.LOOSE, u.getRemoteName(), u.getNewObjectId()));
345 			u.setStatus(Status.OK);
346 		} catch (IOException e) {
347 			u.setStatus(Status.REJECTED_OTHER_REASON);
348 			u.setMessage(e.getMessage());
349 		}
350 	}
351 
352 	private boolean isNewRepository() {
353 		return getRefsMap().isEmpty() && packNames != null
354 				&& packNames.isEmpty();
355 	}
356 
357 	private void createNewRepository(List<RemoteRefUpdate> updates)
358 			throws TransportException {
359 		try {
360 			final String ref = "ref: " + pickHEAD(updates) + "\n"; //$NON-NLS-1$ //$NON-NLS-2$
361 			final byte[] bytes = Constants.encode(ref);
362 			dest.writeFile(ROOT_DIR + Constants.HEAD, bytes);
363 		} catch (IOException e) {
364 			throw new TransportException(uri, JGitText.get().cannotCreateHEAD, e);
365 		}
366 
367 		try {
368 			final String config = "[core]\n" //$NON-NLS-1$
369 					+ "\trepositoryformatversion = 0\n"; //$NON-NLS-1$
370 			final byte[] bytes = Constants.encode(config);
371 			dest.writeFile(ROOT_DIR + Constants.CONFIG, bytes);
372 		} catch (IOException e) {
373 			throw new TransportException(uri, JGitText.get().cannotCreateConfig, e);
374 		}
375 	}
376 
377 	private static String pickHEAD(List<RemoteRefUpdate> updates) {
378 		// Try to use master if the user is pushing that, it is the
379 		// default branch and is likely what they want to remain as
380 		// the default on the new remote.
381 		//
382 		for (RemoteRefUpdate u : updates) {
383 			final String n = u.getRemoteName();
384 			if (n.equals(Constants.R_HEADS + Constants.MASTER))
385 				return n;
386 		}
387 
388 		// Pick any branch, under the assumption the user pushed only
389 		// one to the remote side.
390 		//
391 		for (RemoteRefUpdate u : updates) {
392 			final String n = u.getRemoteName();
393 			if (n.startsWith(Constants.R_HEADS))
394 				return n;
395 		}
396 		return updates.get(0).getRemoteName();
397 	}
398 }