View Javadoc
1   /*
2    * Copyright (C) 2016, 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.internal.storage.reftree;
45  
46  import static org.eclipse.jgit.lib.Ref.Storage.LOOSE;
47  import static org.eclipse.jgit.lib.Ref.Storage.NEW;
48  
49  import java.io.IOException;
50  import java.util.Collections;
51  
52  import org.eclipse.jgit.annotations.Nullable;
53  import org.eclipse.jgit.errors.MissingObjectException;
54  import org.eclipse.jgit.lib.ObjectId;
55  import org.eclipse.jgit.lib.ObjectIdRef;
56  import org.eclipse.jgit.lib.Ref;
57  import org.eclipse.jgit.lib.RefDatabase;
58  import org.eclipse.jgit.lib.RefUpdate;
59  import org.eclipse.jgit.lib.Repository;
60  import org.eclipse.jgit.lib.SymbolicRef;
61  import org.eclipse.jgit.revwalk.RevObject;
62  import org.eclipse.jgit.revwalk.RevTag;
63  import org.eclipse.jgit.revwalk.RevWalk;
64  import org.eclipse.jgit.transport.ReceiveCommand;
65  
66  /** Single reference update to {@link RefTreeDatabase}. */
67  class RefTreeUpdate extends RefUpdate {
68  	private final RefTreeDatabase refdb;
69  	private RevWalk rw;
70  	private RefTreeBatch batch;
71  	private Ref oldRef;
72  
73  	RefTreeUpdate(RefTreeDatabase refdb, Ref ref) {
74  		super(ref);
75  		this.refdb = refdb;
76  		setCheckConflicting(false); // Done automatically by doUpdate.
77  	}
78  
79  	/** {@inheritDoc} */
80  	@Override
81  	protected RefDatabase getRefDatabase() {
82  		return refdb;
83  	}
84  
85  	/** {@inheritDoc} */
86  	@Override
87  	protected Repository getRepository() {
88  		return refdb.getRepository();
89  	}
90  
91  	/** {@inheritDoc} */
92  	@Override
93  	protected boolean tryLock(boolean deref) throws IOException {
94  		rw = new RevWalk(getRepository());
95  		batch = new RefTreeBatch(refdb);
96  		batch.init(rw);
97  		oldRef = batch.exactRef(rw.getObjectReader(), getName());
98  		if (oldRef != null && oldRef.getObjectId() != null) {
99  			setOldObjectId(oldRef.getObjectId());
100 		} else if (oldRef == null && getExpectedOldObjectId() != null) {
101 			setOldObjectId(ObjectId.zeroId());
102 		}
103 		return true;
104 	}
105 
106 	/** {@inheritDoc} */
107 	@Override
108 	protected void unlock() {
109 		batch = null;
110 		if (rw != null) {
111 			rw.close();
112 			rw = null;
113 		}
114 	}
115 
116 	/** {@inheritDoc} */
117 	@Override
118 	protected Result doUpdate(Result desiredResult) throws IOException {
119 		return run(newRef(getName(), getNewObjectId()), desiredResult);
120 	}
121 
122 	private Ref newRef(String name, ObjectId id)
123 			throws MissingObjectException, IOException {
124 		RevObject o = rw.parseAny(id);
125 		if (o instanceof RevTag) {
126 			RevObject p = rw.peel(o);
127 			return new ObjectIdRef.PeeledTag(LOOSE, name, id, p.copy());
128 		}
129 		return new ObjectIdRef.PeeledNonTag(LOOSE, name, id);
130 	}
131 
132 	/** {@inheritDoc} */
133 	@Override
134 	protected Result doDelete(Result desiredResult) throws IOException {
135 		return run(null, desiredResult);
136 	}
137 
138 	/** {@inheritDoc} */
139 	@Override
140 	protected Result doLink(String target) throws IOException {
141 		Ref dst = new ObjectIdRef.Unpeeled(NEW, target, null);
142 		SymbolicRef n = new SymbolicRef(getName(), dst);
143 		Result desiredResult = getRef().getStorage() == NEW
144 			? Result.NEW
145 			: Result.FORCED;
146 		return run(n, desiredResult);
147 	}
148 
149 	private Result run(@Nullable Ref newRef, Result desiredResult)
150 			throws IOException {
151 		Command c = new Command(oldRef, newRef);
152 		batch.setRefLogIdent(getRefLogIdent());
153 		batch.setRefLogMessage(getRefLogMessage(), isRefLogIncludingResult());
154 		batch.execute(rw, Collections.singletonList(c));
155 		return translate(c.getResult(), desiredResult);
156 	}
157 
158 	static Result translate(ReceiveCommand.Result r, Result desiredResult) {
159 		switch (r) {
160 		case OK:
161 			return desiredResult;
162 
163 		case LOCK_FAILURE:
164 			return Result.LOCK_FAILURE;
165 
166 		case NOT_ATTEMPTED:
167 			return Result.NOT_ATTEMPTED;
168 
169 		case REJECTED_MISSING_OBJECT:
170 			return Result.IO_FAILURE;
171 
172 		case REJECTED_CURRENT_BRANCH:
173 			return Result.REJECTED_CURRENT_BRANCH;
174 
175 		case REJECTED_OTHER_REASON:
176 		case REJECTED_NOCREATE:
177 		case REJECTED_NODELETE:
178 		case REJECTED_NONFASTFORWARD:
179 		default:
180 			return Result.REJECTED;
181 		}
182 	}
183 }