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.Repository;
54  
55  /** Updates any reference stored by {@link RefDirectory}. */
56  class RefDirectoryUpdate extends RefUpdate {
57  	private final RefDirectory database;
58  
59  	private LockFile lock;
60  
61  	RefDirectoryUpdate(final RefDirectory r, final Ref ref) {
62  		super(ref);
63  		database = r;
64  	}
65  
66  	@Override
67  	protected RefDirectory getRefDatabase() {
68  		return database;
69  	}
70  
71  	@Override
72  	protected Repository getRepository() {
73  		return database.getRepository();
74  	}
75  
76  	@Override
77  	protected boolean tryLock(boolean deref) throws IOException {
78  		Ref dst = getRef();
79  		if (deref)
80  			dst = dst.getLeaf();
81  		String name = dst.getName();
82  		lock = new LockFile(database.fileFor(name), getRepository().getFS());
83  		if (lock.lock()) {
84  			dst = database.getRef(name);
85  			setOldObjectId(dst != null ? dst.getObjectId() : null);
86  			return true;
87  		} else {
88  			return false;
89  		}
90  	}
91  
92  	@Override
93  	protected void unlock() {
94  		if (lock != null) {
95  			lock.unlock();
96  			lock = null;
97  		}
98  	}
99  
100 	@Override
101 	protected Result doUpdate(final Result status) throws IOException {
102 		WriteConfig wc = database.getRepository().getConfig()
103 				.get(WriteConfig.KEY);
104 
105 		lock.setFSync(wc.getFSyncRefFiles());
106 		lock.setNeedStatInformation(true);
107 		lock.write(getNewObjectId());
108 
109 		String msg = getRefLogMessage();
110 		if (msg != null) {
111 			if (isRefLogIncludingResult()) {
112 				String strResult = toResultString(status);
113 				if (strResult != null) {
114 					if (msg.length() > 0)
115 						msg = msg + ": " + strResult; //$NON-NLS-1$
116 					else
117 						msg = strResult;
118 				}
119 			}
120 			database.log(this, msg, true);
121 		}
122 		if (!lock.commit())
123 			return Result.LOCK_FAILURE;
124 		database.stored(this, lock.getCommitSnapshot());
125 		return status;
126 	}
127 
128 	private String toResultString(final Result status) {
129 		switch (status) {
130 		case FORCED:
131 			return "forced-update"; //$NON-NLS-1$
132 		case FAST_FORWARD:
133 			return "fast forward"; //$NON-NLS-1$
134 		case NEW:
135 			return "created"; //$NON-NLS-1$
136 		default:
137 			return null;
138 		}
139 	}
140 
141 	@Override
142 	protected Result doDelete(final Result status) throws IOException {
143 		if (getRef().getLeaf().getStorage() != Ref.Storage.NEW)
144 			database.delete(this);
145 		return status;
146 	}
147 
148 	@Override
149 	protected Result doLink(final String target) throws IOException {
150 		WriteConfig wc = database.getRepository().getConfig()
151 				.get(WriteConfig.KEY);
152 
153 		lock.setFSync(wc.getFSyncRefFiles());
154 		lock.setNeedStatInformation(true);
155 		lock.write(encode(RefDirectory.SYMREF + target + '\n'));
156 
157 		String msg = getRefLogMessage();
158 		if (msg != null)
159 			database.log(this, msg, false);
160 		if (!lock.commit())
161 			return Result.LOCK_FAILURE;
162 		database.storedSymbolicRef(this, lock.getCommitSnapshot(), target);
163 
164 		if (getRef().getStorage() == Ref.Storage.NEW)
165 			return Result.NEW;
166 		return Result.FORCED;
167 	}
168 }