View Javadoc
1   /*
2    * Copyright (C) 2009-2010, Google Inc.
3    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4    * and other copyright owners as documented in the project's IP log.
5    *
6    * This program and the accompanying materials are made available
7    * under the terms of the Eclipse Distribution License v1.0 which
8    * accompanies this distribution, is reproduced below, and is
9    * available at http://www.eclipse.org/org/documents/edl-v10.php
10   *
11   * All rights reserved.
12   *
13   * Redistribution and use in source and binary forms, with or
14   * without modification, are permitted provided that the following
15   * conditions are met:
16   *
17   * - Redistributions of source code must retain the above copyright
18   *   notice, this list of conditions and the following disclaimer.
19   *
20   * - Redistributions in binary form must reproduce the above
21   *   copyright notice, this list of conditions and the following
22   *   disclaimer in the documentation and/or other materials provided
23   *   with the distribution.
24   *
25   * - Neither the name of the Eclipse Foundation, Inc. nor the
26   *   names of its contributors may be used to endorse or promote
27   *   products derived from this software without specific prior
28   *   written permission.
29   *
30   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43   */
44  
45  package org.eclipse.jgit.internal.storage.file;
46  
47  import static org.eclipse.jgit.lib.Constants.encode;
48  
49  import java.io.IOException;
50  
51  import org.eclipse.jgit.lib.Ref;
52  import org.eclipse.jgit.lib.RefUpdate;
53  import org.eclipse.jgit.lib.ReflogEntry;
54  import org.eclipse.jgit.lib.Repository;
55  
56  /** Updates any reference stored by {@link RefDirectory}. */
57  class RefDirectoryUpdate extends RefUpdate {
58  	private final RefDirectory database;
59  
60  	private boolean shouldDeref;
61  	private LockFile lock;
62  
63  	RefDirectoryUpdate(RefDirectory r, Ref ref) {
64  		super(ref);
65  		database = r;
66  	}
67  
68  	/** {@inheritDoc} */
69  	@Override
70  	protected RefDirectory getRefDatabase() {
71  		return database;
72  	}
73  
74  	/** {@inheritDoc} */
75  	@Override
76  	protected Repository getRepository() {
77  		return database.getRepository();
78  	}
79  
80  	/** {@inheritDoc} */
81  	@Override
82  	protected boolean tryLock(boolean deref) throws IOException {
83  		shouldDeref = deref;
84  		Ref dst = getRef();
85  		if (deref)
86  			dst = dst.getLeaf();
87  		String name = dst.getName();
88  		lock = new LockFile(database.fileFor(name));
89  		if (lock.lock()) {
90  			dst = database.findRef(name);
91  			setOldObjectId(dst != null ? dst.getObjectId() : null);
92  			return true;
93  		} else {
94  			return false;
95  		}
96  	}
97  
98  	/** {@inheritDoc} */
99  	@Override
100 	protected void unlock() {
101 		if (lock != null) {
102 			lock.unlock();
103 			lock = null;
104 		}
105 	}
106 
107 	/** {@inheritDoc} */
108 	@Override
109 	protected Result doUpdate(Result status) throws IOException {
110 		WriteConfig wc = database.getRepository().getConfig()
111 				.get(WriteConfig.KEY);
112 
113 		lock.setFSync(wc.getFSyncRefFiles());
114 		lock.setNeedStatInformation(true);
115 		lock.write(getNewObjectId());
116 
117 		String msg = getRefLogMessage();
118 		if (msg != null) {
119 			if (isRefLogIncludingResult()) {
120 				String strResult = toResultString(status);
121 				if (strResult != null) {
122 					if (msg.length() > 0)
123 						msg = msg + ": " + strResult; //$NON-NLS-1$
124 					else
125 						msg = strResult;
126 				}
127 			}
128 			database.log(isForceRefLog(), this, msg, shouldDeref);
129 		}
130 		if (!lock.commit())
131 			return Result.LOCK_FAILURE;
132 		database.stored(this, lock.getCommitSnapshot());
133 		return status;
134 	}
135 
136 	private String toResultString(Result status) {
137 		switch (status) {
138 		case FORCED:
139 			return ReflogEntry.PREFIX_FORCED_UPDATE;
140 		case FAST_FORWARD:
141 			return ReflogEntry.PREFIX_FAST_FORWARD;
142 		case NEW:
143 			return ReflogEntry.PREFIX_CREATED;
144 		default:
145 			return null;
146 		}
147 	}
148 
149 	/** {@inheritDoc} */
150 	@Override
151 	protected Result doDelete(Result status) throws IOException {
152 		if (getRef().getStorage() != Ref.Storage.NEW)
153 			database.delete(this);
154 		return status;
155 	}
156 
157 	/** {@inheritDoc} */
158 	@Override
159 	protected Result doLink(String target) throws IOException {
160 		WriteConfig wc = database.getRepository().getConfig()
161 				.get(WriteConfig.KEY);
162 
163 		lock.setFSync(wc.getFSyncRefFiles());
164 		lock.setNeedStatInformation(true);
165 		lock.write(encode(RefDirectory.SYMREF + target + '\n'));
166 
167 		String msg = getRefLogMessage();
168 		if (msg != null)
169 			database.log(isForceRefLog(), this, msg, false);
170 		if (!lock.commit())
171 			return Result.LOCK_FAILURE;
172 		database.storedSymbolicRef(this, lock.getCommitSnapshot(), target);
173 
174 		if (getRef().getStorage() == Ref.Storage.NEW)
175 			return Result.NEW;
176 		return Result.FORCED;
177 	}
178 }