View Javadoc
1   /*
2    * Copyright (C) 2010, 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.lib;
12  
13  import org.eclipse.jgit.annotations.NonNull;
14  import org.eclipse.jgit.annotations.Nullable;
15  
16  /**
17   * A reference that indirectly points at another
18   * {@link org.eclipse.jgit.lib.Ref}.
19   * <p>
20   * A symbolic reference always derives its current value from the target
21   * reference.
22   */
23  public class SymbolicRef implements Ref {
24  	private final String name;
25  
26  	private final Ref target;
27  
28  	private final long updateIndex;
29  
30  	/**
31  	 * Create a new ref pairing.
32  	 *
33  	 * @param refName
34  	 *            name of this ref.
35  	 * @param target
36  	 *            the ref we reference and derive our value from.
37  	 */
38  	public SymbolicRef(@NonNull String refName, @NonNull Ref target) {
39  		this.name = refName;
40  		this.target = target;
41  		this.updateIndex = UNDEFINED_UPDATE_INDEX;
42  	}
43  
44  	/**
45  	 * Create a new ref pairing.
46  	 *
47  	 * @param refName
48  	 *            name of this ref.
49  	 * @param target
50  	 *            the ref we reference and derive our value from.
51  	 * @param updateIndex
52  	 *            index that increases with each update of the reference
53  	 * @since 5.3
54  	 */
55  	public SymbolicRef(@NonNull String refName, @NonNull Ref target,
56  			long updateIndex) {
57  		this.name = refName;
58  		this.target = target;
59  		this.updateIndex = updateIndex;
60  	}
61  
62  	/** {@inheritDoc} */
63  	@Override
64  	@NonNull
65  	public String getName() {
66  		return name;
67  	}
68  
69  	/** {@inheritDoc} */
70  	@Override
71  	public boolean isSymbolic() {
72  		return true;
73  	}
74  
75  	/** {@inheritDoc} */
76  	@Override
77  	@NonNull
78  	public Ref getLeaf() {
79  		Ref dst = getTarget();
80  		while (dst.isSymbolic())
81  			dst = dst.getTarget();
82  		return dst;
83  	}
84  
85  	/** {@inheritDoc} */
86  	@Override
87  	@NonNull
88  	public Ref getTarget() {
89  		return target;
90  	}
91  
92  	/** {@inheritDoc} */
93  	@Override
94  	@Nullable
95  	public ObjectId getObjectId() {
96  		return getLeaf().getObjectId();
97  	}
98  
99  	/** {@inheritDoc} */
100 	@Override
101 	@NonNull
102 	public Storage getStorage() {
103 		return Storage.LOOSE;
104 	}
105 
106 	/** {@inheritDoc} */
107 	@Override
108 	@Nullable
109 	public ObjectId getPeeledObjectId() {
110 		return getLeaf().getPeeledObjectId();
111 	}
112 
113 	/** {@inheritDoc} */
114 	@Override
115 	public boolean isPeeled() {
116 		return getLeaf().isPeeled();
117 	}
118 
119 	/**
120 	 * {@inheritDoc}
121 	 * @since 5.3
122 	 */
123 	@Override
124 	public long getUpdateIndex() {
125 		if (updateIndex == UNDEFINED_UPDATE_INDEX) {
126 			throw new UnsupportedOperationException();
127 		}
128 		return updateIndex;
129 	}
130 
131 	/** {@inheritDoc} */
132 	@SuppressWarnings("nls")
133 	@Override
134 	public String toString() {
135 		StringBuilder r = new StringBuilder();
136 		r.append("SymbolicRef[");
137 		Ref cur = this;
138 		while (cur.isSymbolic()) {
139 			r.append(cur.getName());
140 			r.append(" -> ");
141 			cur = cur.getTarget();
142 		}
143 		r.append(cur.getName());
144 		r.append('=');
145 		r.append(ObjectId.toString(cur.getObjectId()));
146 		r.append("(");
147 		r.append(updateIndex); // Print value, even if -1
148 		r.append(")]");
149 		return r.toString();
150 	}
151 }