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