View Javadoc
1   /*
2    * Copyright (C) 2011, Google Inc. 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.internal.storage.dfs;
12  
13  import java.io.IOException;
14  
15  import org.eclipse.jgit.lib.ObjectIdRef;
16  import org.eclipse.jgit.lib.Ref;
17  import org.eclipse.jgit.lib.Ref.Storage;
18  import org.eclipse.jgit.lib.RefUpdate;
19  import org.eclipse.jgit.lib.SymbolicRef;
20  import org.eclipse.jgit.revwalk.RevObject;
21  import org.eclipse.jgit.revwalk.RevTag;
22  import org.eclipse.jgit.revwalk.RevWalk;
23  
24  final class DfsRefUpdate extends RefUpdate {
25  	private final DfsRefDatabase refdb;
26  
27  	private Ref dstRef;
28  
29  	private RevWalk rw;
30  
31  	DfsRefUpdate(DfsRefDatabase refdb, Ref ref) {
32  		super(ref);
33  		this.refdb = refdb;
34  	}
35  
36  	/** {@inheritDoc} */
37  	@Override
38  	protected DfsRefDatabase getRefDatabase() {
39  		return refdb;
40  	}
41  
42  	/** {@inheritDoc} */
43  	@Override
44  	protected DfsRepository getRepository() {
45  		return refdb.getRepository();
46  	}
47  
48  	/** {@inheritDoc} */
49  	@Override
50  	protected boolean tryLock(boolean deref) throws IOException {
51  		dstRef = getRef();
52  		if (deref)
53  			dstRef = dstRef.getLeaf();
54  
55  		if (dstRef.isSymbolic())
56  			setOldObjectId(null);
57  		else
58  			setOldObjectId(dstRef.getObjectId());
59  
60  		return true;
61  	}
62  
63  	/** {@inheritDoc} */
64  	@Override
65  	protected void unlock() {
66  		// No state is held while "locked".
67  	}
68  
69  	/** {@inheritDoc} */
70  	@Override
71  	public Result update(RevWalk walk) throws IOException {
72  		try {
73  			rw = walk;
74  			return super.update(walk);
75  		} finally {
76  			rw = null;
77  		}
78  	}
79  
80  	/** {@inheritDoc} */
81  	@Override
82  	protected Result doUpdate(Result desiredResult) throws IOException {
83  		ObjectIdRef newRef;
84  		RevObject obj = rw.parseAny(getNewObjectId());
85  		if (obj instanceof RevTag) {
86  			newRef = new ObjectIdRef.PeeledTag(
87  					Storage.PACKED,
88  					dstRef.getName(),
89  					getNewObjectId(),
90  					rw.peel(obj).copy());
91  		} else {
92  			newRef = new ObjectIdRef.PeeledNonTag(
93  					Storage.PACKED,
94  					dstRef.getName(),
95  					getNewObjectId());
96  		}
97  
98  		if (getRefDatabase().compareAndPut(dstRef, newRef)) {
99  			getRefDatabase().stored(newRef);
100 			return desiredResult;
101 		}
102 		return Result.LOCK_FAILURE;
103 	}
104 
105 	/** {@inheritDoc} */
106 	@Override
107 	protected Result doDelete(Result desiredResult) throws IOException {
108 		if (getRefDatabase().compareAndRemove(dstRef)) {
109 			getRefDatabase().removed(dstRef.getName());
110 			return desiredResult;
111 		}
112 		return Result.LOCK_FAILURE;
113 	}
114 
115 	/** {@inheritDoc} */
116 	@Override
117 	protected Result doLink(String target) throws IOException {
118 		final SymbolicReficRef.html#SymbolicRef">SymbolicRef newRef = new SymbolicRef(
119 				dstRef.getName(),
120 				new ObjectIdRef.Unpeeled(
121 						Storage.NEW,
122 						target,
123 						null));
124 		if (getRefDatabase().compareAndPut(dstRef, newRef)) {
125 			getRefDatabase().stored(newRef);
126 			if (dstRef.getStorage() == Ref.Storage.NEW)
127 				return Result.NEW;
128 			return Result.FORCED;
129 		}
130 		return Result.LOCK_FAILURE;
131 	}
132 }