1 /* 2 * Copyright (C) 2008, Google Inc. 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 java.io.IOException; 47 import java.text.MessageFormat; 48 import java.util.ArrayList; 49 import java.util.List; 50 51 import org.eclipse.jgit.internal.JGitText; 52 import org.eclipse.jgit.lib.AnyObjectId; 53 import org.eclipse.jgit.lib.ObjectId; 54 import org.eclipse.jgit.lib.Ref; 55 import org.eclipse.jgit.lib.RefUpdate; 56 import org.eclipse.jgit.revwalk.RevCommit; 57 import org.eclipse.jgit.revwalk.RevObject; 58 import org.eclipse.jgit.revwalk.RevWalk; 59 60 /** 61 * A command being processed by {@link BaseReceivePack}. 62 * <p> 63 * This command instance roughly translates to the server side representation of 64 * the {@link RemoteRefUpdate} created by the client. 65 */ 66 public class ReceiveCommand { 67 /** Type of operation requested. */ 68 public static enum Type { 69 /** Create a new ref; the ref must not already exist. */ 70 CREATE, 71 72 /** 73 * Update an existing ref with a fast-forward update. 74 * <p> 75 * During a fast-forward update no changes will be lost; only new 76 * commits are inserted into the ref. 77 */ 78 UPDATE, 79 80 /** 81 * Update an existing ref by potentially discarding objects. 82 * <p> 83 * The current value of the ref is not fully reachable from the new 84 * value of the ref, so a successful command may result in one or more 85 * objects becoming unreachable. 86 */ 87 UPDATE_NONFASTFORWARD, 88 89 /** Delete an existing ref; the ref should already exist. */ 90 DELETE; 91 } 92 93 /** Result of the update command. */ 94 public static enum Result { 95 /** The command has not yet been attempted by the server. */ 96 NOT_ATTEMPTED, 97 98 /** The server is configured to deny creation of this ref. */ 99 REJECTED_NOCREATE, 100 101 /** The server is configured to deny deletion of this ref. */ 102 REJECTED_NODELETE, 103 104 /** The update is a non-fast-forward update and isn't permitted. */ 105 REJECTED_NONFASTFORWARD, 106 107 /** The update affects <code>HEAD</code> and cannot be permitted. */ 108 REJECTED_CURRENT_BRANCH, 109 110 /** 111 * One or more objects aren't in the repository. 112 * <p> 113 * This is severe indication of either repository corruption on the 114 * server side, or a bug in the client wherein the client did not supply 115 * all required objects during the pack transfer. 116 */ 117 REJECTED_MISSING_OBJECT, 118 119 /** Other failure; see {@link ReceiveCommand#getMessage()}. */ 120 REJECTED_OTHER_REASON, 121 122 /** The ref could not be locked and updated atomically; try again. */ 123 LOCK_FAILURE, 124 125 /** The change was completed successfully. */ 126 OK; 127 } 128 129 /** 130 * Filter a list of commands according to result. 131 * 132 * @param commands 133 * commands to filter. 134 * @param want 135 * desired status to filter by. 136 * @return a copy of the command list containing only those commands with 137 * the desired status. 138 * @since 2.0 139 */ 140 public static List<ReceiveCommand> filter(List<ReceiveCommand> commands, 141 final Result want) { 142 List<ReceiveCommand> r = new ArrayList<ReceiveCommand>(commands.size()); 143 for (final ReceiveCommand cmd : commands) { 144 if (cmd.getResult() == want) 145 r.add(cmd); 146 } 147 return r; 148 } 149 150 private final ObjectId oldId; 151 152 private final ObjectId newId; 153 154 private final String name; 155 156 private Type type; 157 158 private Ref ref; 159 160 private Result status = Result.NOT_ATTEMPTED; 161 162 private String message; 163 164 private boolean typeIsCorrect; 165 166 /** 167 * Create a new command for {@link BaseReceivePack}. 168 * 169 * @param oldId 170 * the old object id; must not be null. Use 171 * {@link ObjectId#zeroId()} to indicate a ref creation. 172 * @param newId 173 * the new object id; must not be null. Use 174 * {@link ObjectId#zeroId()} to indicate a ref deletion. 175 * @param name 176 * name of the ref being affected. 177 */ 178 public ReceiveCommand(final ObjectId oldId, final ObjectId newId, 179 final String name) { 180 this.oldId = oldId; 181 this.newId = newId; 182 this.name = name; 183 184 type = Type.UPDATE; 185 if (ObjectId.zeroId().equals(oldId)) 186 type = Type.CREATE; 187 if (ObjectId.zeroId().equals(newId)) 188 type = Type.DELETE; 189 } 190 191 /** 192 * Create a new command for {@link BaseReceivePack}. 193 * 194 * @param oldId 195 * the old object id; must not be null. Use 196 * {@link ObjectId#zeroId()} to indicate a ref creation. 197 * @param newId 198 * the new object id; must not be null. Use 199 * {@link ObjectId#zeroId()} to indicate a ref deletion. 200 * @param name 201 * name of the ref being affected. 202 * @param type 203 * type of the command. 204 * @since 2.0 205 */ 206 public ReceiveCommand(final ObjectId oldId, final ObjectId newId, 207 final String name, final Type type) { 208 this.oldId = oldId; 209 this.newId = newId; 210 this.name = name; 211 this.type = type; 212 } 213 214 /** @return the old value the client thinks the ref has. */ 215 public ObjectId getOldId() { 216 return oldId; 217 } 218 219 /** @return the requested new value for this ref. */ 220 public ObjectId getNewId() { 221 return newId; 222 } 223 224 /** @return the name of the ref being updated. */ 225 public String getRefName() { 226 return name; 227 } 228 229 /** @return the type of this command; see {@link Type}. */ 230 public Type getType() { 231 return type; 232 } 233 234 /** @return the ref, if this was advertised by the connection. */ 235 public Ref getRef() { 236 return ref; 237 } 238 239 /** @return the current status code of this command. */ 240 public Result getResult() { 241 return status; 242 } 243 244 /** @return the message associated with a failure status. */ 245 public String getMessage() { 246 return message; 247 } 248 249 /** 250 * Set the status of this command. 251 * 252 * @param s 253 * the new status code for this command. 254 */ 255 public void setResult(final Result s) { 256 setResult(s, null); 257 } 258 259 /** 260 * Set the status of this command. 261 * 262 * @param s 263 * new status code for this command. 264 * @param m 265 * optional message explaining the new status. 266 */ 267 public void setResult(final Result s, final String m) { 268 status = s; 269 message = m; 270 } 271 272 /** 273 * Update the type of this command by checking for fast-forward. 274 * <p> 275 * If the command's current type is UPDATE, a merge test will be performed 276 * using the supplied RevWalk to determine if {@link #getOldId()} is fully 277 * merged into {@link #getNewId()}. If some commits are not merged the 278 * update type is changed to {@link Type#UPDATE_NONFASTFORWARD}. 279 * 280 * @param walk 281 * an instance to perform the merge test with. The caller must 282 * allocate and release this object. 283 * @throws IOException 284 * either oldId or newId is not accessible in the repository 285 * used by the RevWalk. This usually indicates data corruption, 286 * and the command cannot be processed. 287 */ 288 public void updateType(RevWalk walk) throws IOException { 289 if (typeIsCorrect) 290 return; 291 if (type == Type.UPDATE && !AnyObjectId.equals(oldId, newId)) { 292 RevObject o = walk.parseAny(oldId); 293 RevObject n = walk.parseAny(newId); 294 if (!(o instanceof RevCommit) 295 || !(n instanceof RevCommit) 296 || !walk.isMergedInto((RevCommit) o, (RevCommit) n)) 297 setType(Type.UPDATE_NONFASTFORWARD); 298 } 299 typeIsCorrect = true; 300 } 301 302 /** 303 * Execute this command during a receive-pack session. 304 * <p> 305 * Sets the status of the command as a side effect. 306 * 307 * @param rp 308 * receive-pack session. 309 * @since 2.0 310 */ 311 public void execute(final BaseReceivePack rp) { 312 try { 313 final RefUpdate ru = rp.getRepository().updateRef(getRefName()); 314 ru.setRefLogIdent(rp.getRefLogIdent()); 315 switch (getType()) { 316 case DELETE: 317 if (!ObjectId.zeroId().equals(getOldId())) { 318 // We can only do a CAS style delete if the client 319 // didn't bork its delete request by sending the 320 // wrong zero id rather than the advertised one. 321 // 322 ru.setExpectedOldObjectId(getOldId()); 323 } 324 ru.setForceUpdate(true); 325 setResult(ru.delete(rp.getRevWalk())); 326 break; 327 328 case CREATE: 329 case UPDATE: 330 case UPDATE_NONFASTFORWARD: 331 ru.setForceUpdate(rp.isAllowNonFastForwards()); 332 ru.setExpectedOldObjectId(getOldId()); 333 ru.setNewObjectId(getNewId()); 334 ru.setRefLogMessage("push", true); //$NON-NLS-1$ 335 setResult(ru.update(rp.getRevWalk())); 336 break; 337 } 338 } catch (IOException err) { 339 reject(err); 340 } 341 } 342 343 void setRef(final Ref r) { 344 ref = r; 345 } 346 347 void setType(final Type t) { 348 type = t; 349 } 350 351 void setTypeFastForwardUpdate() { 352 type = Type.UPDATE; 353 typeIsCorrect = true; 354 } 355 356 /** 357 * Set the result of this command. 358 * 359 * @param r 360 * the new result code for this command. 361 */ 362 public void setResult(RefUpdate.Result r) { 363 switch (r) { 364 case NOT_ATTEMPTED: 365 setResult(Result.NOT_ATTEMPTED); 366 break; 367 368 case LOCK_FAILURE: 369 case IO_FAILURE: 370 setResult(Result.LOCK_FAILURE); 371 break; 372 373 case NO_CHANGE: 374 case NEW: 375 case FORCED: 376 case FAST_FORWARD: 377 setResult(Result.OK); 378 break; 379 380 case REJECTED: 381 setResult(Result.REJECTED_NONFASTFORWARD); 382 break; 383 384 case REJECTED_CURRENT_BRANCH: 385 setResult(Result.REJECTED_CURRENT_BRANCH); 386 break; 387 388 default: 389 setResult(Result.REJECTED_OTHER_REASON, r.name()); 390 break; 391 } 392 } 393 394 void reject(IOException err) { 395 setResult(Result.REJECTED_OTHER_REASON, MessageFormat.format( 396 JGitText.get().lockError, err.getMessage())); 397 } 398 399 @SuppressWarnings("nls") 400 @Override 401 public String toString() { 402 return getType().name() + ": " + getOldId().name() + " " 403 + getNewId().name() + " " + getRefName(); 404 } 405 }