View Javadoc
1   /*
2    * Copyright (C) 2009-2010, Google Inc.
3    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v. 1.0 which is available at
7    * https://www.eclipse.org/org/documents/edl-v10.php.
8    *
9    * SPDX-License-Identifier: BSD-3-Clause
10   */
11  
12  package org.eclipse.jgit.internal.storage.file;
13  
14  import static org.eclipse.jgit.lib.Constants.encode;
15  
16  import java.io.IOException;
17  
18  import org.eclipse.jgit.lib.Ref;
19  import org.eclipse.jgit.lib.RefUpdate;
20  import org.eclipse.jgit.lib.ReflogEntry;
21  import org.eclipse.jgit.lib.Repository;
22  
23  /** Updates any reference stored by {@link RefDirectory}. */
24  class RefDirectoryUpdate extends RefUpdate {
25  	private final RefDirectory database;
26  
27  	private boolean shouldDeref;
28  	private LockFile lock;
29  
30  	RefDirectoryUpdate(RefDirectory r, Ref ref) {
31  		super(ref);
32  		database = r;
33  	}
34  
35  	/** {@inheritDoc} */
36  	@Override
37  	protected RefDirectory getRefDatabase() {
38  		return database;
39  	}
40  
41  	/** {@inheritDoc} */
42  	@Override
43  	protected Repository getRepository() {
44  		return database.getRepository();
45  	}
46  
47  	/** {@inheritDoc} */
48  	@Override
49  	protected boolean tryLock(boolean deref) throws IOException {
50  		shouldDeref = deref;
51  		Ref dst = getRef();
52  		if (deref)
53  			dst = dst.getLeaf();
54  		String name = dst.getName();
55  		lock = new LockFile(database.fileFor(name));
56  		if (lock.lock()) {
57  			dst = database.findRef(name);
58  			setOldObjectId(dst != null ? dst.getObjectId() : null);
59  			return true;
60  		}
61  		return false;
62  	}
63  
64  	/** {@inheritDoc} */
65  	@Override
66  	protected void unlock() {
67  		if (lock != null) {
68  			lock.unlock();
69  			lock = null;
70  		}
71  	}
72  
73  	/** {@inheritDoc} */
74  	@Override
75  	protected Result doUpdate(Result status) throws IOException {
76  		WriteConfig wc = database.getRepository().getConfig()
77  				.get(WriteConfig.KEY);
78  
79  		lock.setFSync(wc.getFSyncRefFiles());
80  		lock.setNeedStatInformation(true);
81  		lock.write(getNewObjectId());
82  
83  		String msg = getRefLogMessage();
84  		if (msg != null) {
85  			if (isRefLogIncludingResult()) {
86  				String strResult = toResultString(status);
87  				if (strResult != null) {
88  					if (msg.length() > 0)
89  						msg = msg + ": " + strResult; //$NON-NLS-1$
90  					else
91  						msg = strResult;
92  				}
93  			}
94  			database.log(isForceRefLog(), this, msg, shouldDeref);
95  		}
96  		if (!lock.commit())
97  			return Result.LOCK_FAILURE;
98  		database.stored(this, lock.getCommitSnapshot());
99  		return status;
100 	}
101 
102 	private String toResultString(Result status) {
103 		switch (status) {
104 		case FORCED:
105 			return ReflogEntry.PREFIX_FORCED_UPDATE;
106 		case FAST_FORWARD:
107 			return ReflogEntry.PREFIX_FAST_FORWARD;
108 		case NEW:
109 			return ReflogEntry.PREFIX_CREATED;
110 		default:
111 			return null;
112 		}
113 	}
114 
115 	/** {@inheritDoc} */
116 	@Override
117 	protected Result doDelete(Result status) throws IOException {
118 		if (getRef().getStorage() != Ref.Storage.NEW)
119 			database.delete(this);
120 		return status;
121 	}
122 
123 	/** {@inheritDoc} */
124 	@Override
125 	protected Result doLink(String target) throws IOException {
126 		WriteConfig wc = database.getRepository().getConfig()
127 				.get(WriteConfig.KEY);
128 
129 		lock.setFSync(wc.getFSyncRefFiles());
130 		lock.setNeedStatInformation(true);
131 		lock.write(encode(RefDirectory.SYMREF + target + '\n'));
132 
133 		String msg = getRefLogMessage();
134 		if (msg != null)
135 			database.log(isForceRefLog(), this, msg, false);
136 		if (!lock.commit())
137 			return Result.LOCK_FAILURE;
138 		database.storedSymbolicRef(this, lock.getCommitSnapshot(), target);
139 
140 		if (getRef().getStorage() == Ref.Storage.NEW)
141 			return Result.NEW;
142 		return Result.FORCED;
143 	}
144 }