Browse Source
This commit actually does three major changes to the way references are handled within JGit. Unfortunately they were easier to do as a single massive commit than to break them up into smaller units. Disambiguate symbolic references: --------------------------------- Reporting a symbolic reference such as HEAD as though it were any other normal reference like refs/heads/master causes subtle programming errors. We have been bitten by this error on several occasions, as have some downstream applications written by myself. Instead of reporting HEAD as a reference whose name differs from its "original name", report it as an actual SymbolicRef object that the application can test the type and examine the target of. With this change, Ref is now an abstract type with different subclasses for the different types. In the classical example of "HEAD" being a symbolic reference to branch "refs/heads/master", the Repository.getAllRefs() method will now return: Map<String, Ref> all = repository.getAllRefs(); SymbolicRef HEAD = (SymbolicRef) all.get("HEAD"); ObjectIdRef master = (ObjectIdRef) all.get("refs/heads/master"); assertSame(master, HEAD.getTarget()); assertSame(master.getObjectId(), HEAD.getObjectId()); assertEquals("HEAD", HEAD.getName()); assertEquals("refs/heads/master", master.getName()); A nice side-effect of this change is the storage type of the symbolic reference is no longer ambiguous with the storge type of the underlying reference it targets. In the above example, if master was only available in the packed-refs file, then the following is also true: assertSame(Ref.Storage.LOOSE, HEAD.getStorage()); assertSame(Ref.Storage.PACKED, master.getStorage()); (Prior to this change we returned the ambiguous storage of LOOSE_PACKED for HEAD, which was confusing since it wasn't actually true on disk). Another nice side-effect of this change is all intermediate symbolic references are preserved, and are therefore visible to the application when they walk the target chain. We can now correctly inspect chains of symbolic references. As a result of this change the Ref.getOrigName() method has been removed from the API. Applications should identify a symbolic reference by testing for isSymbolic() and not by using an arcane string comparsion between properties. Abstract the RefDatabase storage: --------------------------------- RefDatabase is now abstract, similar to ObjectDatabase, and a new concrete implementation called RefDirectory is used for the traditional on-disk storage layout. In the future we plan to support additional implementations, such as a pure in-memory RefDatabase for unit testing purposes. Optimize RefDirectory: ---------------------- The implementation of the in-memory reference cache, reading, and update routines has been completely rewritten. Much of the code was heavily borrowed or cribbed from the prior implementation, so copyright notices have been left intact as much as possible. The RefDirectory cache no longer confuses symbolic references with normal references. This permits the cache to resolve the value of a symbolic reference as late as possible, ensuring it is always current, without needing to maintain reverse pointers. The cache is now 2 sorted RefLists, rather than 3 HashMaps. Using sorted lists allows the implementation to reduce the in-memory footprint when storing many refs. Using specialized types for the elements allows the code to avoid additional map lookups for auxiliary stat information. To improve scan time during getRefs(), the lists are returned via a copy-on-write contract. Most callers of getRefs() do not modify the returned collections, so the copy-on-write semantics improves access on repositories with a large number of packed references. Iterator traversals of the returned Map<String,Ref> are performed using a simple merge-join of the two cache lists, ensuring we can perform the entire traversal in linear time as a function of the number of references: O(PackedRefs + LooseRefs). Scans of the loose reference space to update the cache run in O(LooseRefs log LooseRefs) time, as the directory contents are sorted before being merged against the in-memory cache. Since the majority of stable references are kept packed, there typically are only a handful of reference names to be sorted, so the sorting cost should not be very high. Locking is reduced during getRefs() by taking advantage of the copy-on-write semantics of the improved cache data structure. This permits concurrent readers to pull back references without blocking each other. If there is contention updating the cache during a scan, one or more updates are simply skipped and will get picked up again in a future scan. Writing to the $GIT_DIR/packed-refs during reference delete is now fully atomic. The file is locked, reparsed fresh, and written back out if a change is necessary. This avoids all race conditions with concurrent external updates of the packed-refs file. The RefLogWriter class has been fully folded into RefDirectory and is therefore deleted. Maintaining the reference's log is the responsiblity of the database implementation, and not all implementations will use java.io for access. Future work still remains to be done to abstract the ReflogReader class away from local disk IO. Change-Id: I26b9287c45a4b2d2be35ba2849daa316f5eec85d Signed-off-by: Shawn O. Pearce <spearce@spearce.org>stable-0.7
Shawn O. Pearce
15 years ago
39 changed files with 3766 additions and 1241 deletions
@ -0,0 +1,115 @@
|
||||
/* |
||||
* Copyright (C) 2010, Google Inc. |
||||
* and other copyright owners as documented in the project's IP log. |
||||
* |
||||
* This program and the accompanying materials are made available |
||||
* under the terms of the Eclipse Distribution License v1.0 which |
||||
* accompanies this distribution, is reproduced below, and is |
||||
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||
* |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or |
||||
* without modification, are permitted provided that the following |
||||
* conditions are met: |
||||
* |
||||
* - Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* - Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following |
||||
* disclaimer in the documentation and/or other materials provided |
||||
* with the distribution. |
||||
* |
||||
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||
* names of its contributors may be used to endorse or promote |
||||
* products derived from this software without specific prior |
||||
* written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package org.eclipse.jgit.lib; |
||||
|
||||
import junit.framework.TestCase; |
||||
|
||||
public class ObjectIdRefTest extends TestCase { |
||||
private static final ObjectId ID_A = ObjectId |
||||
.fromString("41eb0d88f833b558bddeb269b7ab77399cdf98ed"); |
||||
|
||||
private static final ObjectId ID_B = ObjectId |
||||
.fromString("698dd0b8d0c299f080559a1cffc7fe029479a408"); |
||||
|
||||
private static final String name = "refs/heads/a.test.ref"; |
||||
|
||||
public void testConstructor_PeeledStatusNotKnown() { |
||||
ObjectIdRef r; |
||||
|
||||
r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, name, ID_A); |
||||
assertSame(Ref.Storage.LOOSE, r.getStorage()); |
||||
assertSame(name, r.getName()); |
||||
assertSame(ID_A, r.getObjectId()); |
||||
assertFalse("not peeled", r.isPeeled()); |
||||
assertNull("no peel id", r.getPeeledObjectId()); |
||||
assertSame("leaf is this", r, r.getLeaf()); |
||||
assertSame("target is this", r, r.getTarget()); |
||||
assertFalse("not symbolic", r.isSymbolic()); |
||||
|
||||
r = new ObjectIdRef.Unpeeled(Ref.Storage.PACKED, name, ID_A); |
||||
assertSame(Ref.Storage.PACKED, r.getStorage()); |
||||
|
||||
r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE_PACKED, name, ID_A); |
||||
assertSame(Ref.Storage.LOOSE_PACKED, r.getStorage()); |
||||
|
||||
r = new ObjectIdRef.Unpeeled(Ref.Storage.NEW, name, null); |
||||
assertSame(Ref.Storage.NEW, r.getStorage()); |
||||
assertSame(name, r.getName()); |
||||
assertNull("no id on new ref", r.getObjectId()); |
||||
assertFalse("not peeled", r.isPeeled()); |
||||
assertNull("no peel id", r.getPeeledObjectId()); |
||||
assertSame("leaf is this", r, r.getLeaf()); |
||||
assertSame("target is this", r, r.getTarget()); |
||||
assertFalse("not symbolic", r.isSymbolic()); |
||||
} |
||||
|
||||
public void testConstructor_Peeled() { |
||||
ObjectIdRef r; |
||||
|
||||
r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, name, ID_A); |
||||
assertSame(Ref.Storage.LOOSE, r.getStorage()); |
||||
assertSame(name, r.getName()); |
||||
assertSame(ID_A, r.getObjectId()); |
||||
assertFalse("not peeled", r.isPeeled()); |
||||
assertNull("no peel id", r.getPeeledObjectId()); |
||||
assertSame("leaf is this", r, r.getLeaf()); |
||||
assertSame("target is this", r, r.getTarget()); |
||||
assertFalse("not symbolic", r.isSymbolic()); |
||||
|
||||
r = new ObjectIdRef.PeeledNonTag(Ref.Storage.LOOSE, name, ID_A); |
||||
assertTrue("is peeled", r.isPeeled()); |
||||
assertNull("no peel id", r.getPeeledObjectId()); |
||||
|
||||
r = new ObjectIdRef.PeeledTag(Ref.Storage.LOOSE, name, ID_A, ID_B); |
||||
assertTrue("is peeled", r.isPeeled()); |
||||
assertSame(ID_B, r.getPeeledObjectId()); |
||||
} |
||||
|
||||
public void testToString() { |
||||
ObjectIdRef r; |
||||
|
||||
r = new ObjectIdRef.Unpeeled(Ref.Storage.LOOSE, name, ID_A); |
||||
assertEquals("Ref[" + name + "=" + ID_A.name() + "]", r.toString()); |
||||
} |
||||
} |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,129 @@
|
||||
/* |
||||
* Copyright (C) 2010, Google Inc. |
||||
* and other copyright owners as documented in the project's IP log. |
||||
* |
||||
* This program and the accompanying materials are made available |
||||
* under the terms of the Eclipse Distribution License v1.0 which |
||||
* accompanies this distribution, is reproduced below, and is |
||||
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||
* |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or |
||||
* without modification, are permitted provided that the following |
||||
* conditions are met: |
||||
* |
||||
* - Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* - Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following |
||||
* disclaimer in the documentation and/or other materials provided |
||||
* with the distribution. |
||||
* |
||||
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||
* names of its contributors may be used to endorse or promote |
||||
* products derived from this software without specific prior |
||||
* written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package org.eclipse.jgit.lib; |
||||
|
||||
import junit.framework.TestCase; |
||||
|
||||
public class SymbolicRefTest extends TestCase { |
||||
private static final ObjectId ID_A = ObjectId |
||||
.fromString("41eb0d88f833b558bddeb269b7ab77399cdf98ed"); |
||||
|
||||
private static final ObjectId ID_B = ObjectId |
||||
.fromString("698dd0b8d0c299f080559a1cffc7fe029479a408"); |
||||
|
||||
private static final String targetName = "refs/heads/a.test.ref"; |
||||
|
||||
private static final String name = "refs/remotes/origin/HEAD"; |
||||
|
||||
public void testConstructor() { |
||||
Ref t; |
||||
SymbolicRef r; |
||||
|
||||
t = new ObjectIdRef.Unpeeled(Ref.Storage.NEW, targetName, null); |
||||
r = new SymbolicRef(name, t); |
||||
assertSame(Ref.Storage.LOOSE, r.getStorage()); |
||||
assertSame(name, r.getName()); |
||||
assertNull("no id on new ref", r.getObjectId()); |
||||
assertFalse("not peeled", r.isPeeled()); |
||||
assertNull("no peel id", r.getPeeledObjectId()); |
||||
assertSame("leaf is t", t, r.getLeaf()); |
||||
assertSame("target is t", t, r.getTarget()); |
||||
assertTrue("is symbolic", r.isSymbolic()); |
||||
|
||||
t = new ObjectIdRef.Unpeeled(Ref.Storage.PACKED, targetName, ID_A); |
||||
r = new SymbolicRef(name, t); |
||||
assertSame(Ref.Storage.LOOSE, r.getStorage()); |
||||
assertSame(name, r.getName()); |
||||
assertSame(ID_A, r.getObjectId()); |
||||
assertFalse("not peeled", r.isPeeled()); |
||||
assertNull("no peel id", r.getPeeledObjectId()); |
||||
assertSame("leaf is t", t, r.getLeaf()); |
||||
assertSame("target is t", t, r.getTarget()); |
||||
assertTrue("is symbolic", r.isSymbolic()); |
||||
} |
||||
|
||||
public void testLeaf() { |
||||
Ref a; |
||||
SymbolicRef b, c, d; |
||||
|
||||
a = new ObjectIdRef.PeeledTag(Ref.Storage.PACKED, targetName, ID_A, ID_B); |
||||
b = new SymbolicRef("B", a); |
||||
c = new SymbolicRef("C", b); |
||||
d = new SymbolicRef("D", c); |
||||
|
||||
assertSame(c, d.getTarget()); |
||||
assertSame(b, c.getTarget()); |
||||
assertSame(a, b.getTarget()); |
||||
|
||||
assertSame(a, d.getLeaf()); |
||||
assertSame(a, c.getLeaf()); |
||||
assertSame(a, b.getLeaf()); |
||||
assertSame(a, a.getLeaf()); |
||||
|
||||
assertSame(ID_A, d.getObjectId()); |
||||
assertSame(ID_A, c.getObjectId()); |
||||
assertSame(ID_A, b.getObjectId()); |
||||
|
||||
assertTrue(d.isPeeled()); |
||||
assertTrue(c.isPeeled()); |
||||
assertTrue(b.isPeeled()); |
||||
|
||||
assertSame(ID_B, d.getPeeledObjectId()); |
||||
assertSame(ID_B, c.getPeeledObjectId()); |
||||
assertSame(ID_B, b.getPeeledObjectId()); |
||||
} |
||||
|
||||
public void testToString() { |
||||
Ref a; |
||||
SymbolicRef b, c, d; |
||||
|
||||
a = new ObjectIdRef.PeeledTag(Ref.Storage.PACKED, targetName, ID_A, ID_B); |
||||
b = new SymbolicRef("B", a); |
||||
c = new SymbolicRef("C", b); |
||||
d = new SymbolicRef("D", c); |
||||
|
||||
assertEquals("SymbolicRef[D -> C -> B -> " + targetName + "=" |
||||
+ ID_A.name() + "]", d.toString()); |
||||
} |
||||
} |
@ -0,0 +1,188 @@
|
||||
/* |
||||
* Copyright (C) 2010, Google Inc. |
||||
* Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> |
||||
* and other copyright owners as documented in the project's IP log. |
||||
* |
||||
* This program and the accompanying materials are made available |
||||
* under the terms of the Eclipse Distribution License v1.0 which |
||||
* accompanies this distribution, is reproduced below, and is |
||||
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||
* |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or |
||||
* without modification, are permitted provided that the following |
||||
* conditions are met: |
||||
* |
||||
* - Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* - Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following |
||||
* disclaimer in the documentation and/or other materials provided |
||||
* with the distribution. |
||||
* |
||||
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||
* names of its contributors may be used to endorse or promote |
||||
* products derived from this software without specific prior |
||||
* written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package org.eclipse.jgit.lib; |
||||
|
||||
/** A {@link Ref} that points directly at an {@link ObjectId}. */ |
||||
public abstract class ObjectIdRef implements Ref { |
||||
/** Any reference whose peeled value is not yet known. */ |
||||
public static class Unpeeled extends ObjectIdRef { |
||||
/** |
||||
* Create a new ref pairing. |
||||
* |
||||
* @param st |
||||
* method used to store this ref. |
||||
* @param name |
||||
* name of this ref. |
||||
* @param id |
||||
* current value of the ref. May be null to indicate a ref |
||||
* that does not exist yet. |
||||
*/ |
||||
public Unpeeled(Storage st, String name, ObjectId id) { |
||||
super(st, name, id); |
||||
} |
||||
|
||||
public ObjectId getPeeledObjectId() { |
||||
return null; |
||||
} |
||||
|
||||
public boolean isPeeled() { |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
/** An annotated tag whose peeled object has been cached. */ |
||||
public static class PeeledTag extends ObjectIdRef { |
||||
private final ObjectId peeledObjectId; |
||||
|
||||
/** |
||||
* Create a new ref pairing. |
||||
* |
||||
* @param st |
||||
* method used to store this ref. |
||||
* @param name |
||||
* name of this ref. |
||||
* @param id |
||||
* current value of the ref. |
||||
* @param p |
||||
* the first non-tag object that tag {@code id} points to. |
||||
*/ |
||||
public PeeledTag(Storage st, String name, ObjectId id, ObjectId p) { |
||||
super(st, name, id); |
||||
peeledObjectId = p; |
||||
} |
||||
|
||||
public ObjectId getPeeledObjectId() { |
||||
return peeledObjectId; |
||||
} |
||||
|
||||
public boolean isPeeled() { |
||||
return true; |
||||
} |
||||
} |
||||
|
||||
/** A reference to a non-tag object coming from a cached source. */ |
||||
public static class PeeledNonTag extends ObjectIdRef { |
||||
/** |
||||
* Create a new ref pairing. |
||||
* |
||||
* @param st |
||||
* method used to store this ref. |
||||
* @param name |
||||
* name of this ref. |
||||
* @param id |
||||
* current value of the ref. May be null to indicate a ref |
||||
* that does not exist yet. |
||||
*/ |
||||
public PeeledNonTag(Storage st, String name, ObjectId id) { |
||||
super(st, name, id); |
||||
} |
||||
|
||||
public ObjectId getPeeledObjectId() { |
||||
return null; |
||||
} |
||||
|
||||
public boolean isPeeled() { |
||||
return true; |
||||
} |
||||
} |
||||
|
||||
private final String name; |
||||
|
||||
private final Storage storage; |
||||
|
||||
private final ObjectId objectId; |
||||
|
||||
/** |
||||
* Create a new ref pairing. |
||||
* |
||||
* @param st |
||||
* method used to store this ref. |
||||
* @param name |
||||
* name of this ref. |
||||
* @param id |
||||
* current value of the ref. May be null to indicate a ref that |
||||
* does not exist yet. |
||||
*/ |
||||
protected ObjectIdRef(Storage st, String name, ObjectId id) { |
||||
this.name = name; |
||||
this.storage = st; |
||||
this.objectId = id; |
||||
} |
||||
|
||||
public String getName() { |
||||
return name; |
||||
} |
||||
|
||||
public boolean isSymbolic() { |
||||
return false; |
||||
} |
||||
|
||||
public Ref getLeaf() { |
||||
return this; |
||||
} |
||||
|
||||
public Ref getTarget() { |
||||
return this; |
||||
} |
||||
|
||||
public ObjectId getObjectId() { |
||||
return objectId; |
||||
} |
||||
|
||||
public Storage getStorage() { |
||||
return storage; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
StringBuilder r = new StringBuilder(); |
||||
r.append("Ref["); |
||||
r.append(getName()); |
||||
r.append('='); |
||||
r.append(ObjectId.toString(getObjectId())); |
||||
r.append(']'); |
||||
return r.toString(); |
||||
} |
||||
} |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,217 @@
|
||||
/* |
||||
* Copyright (C) 2010, Google Inc. |
||||
* Copyright (C) 2009, Robin Rosenberg |
||||
* and other copyright owners as documented in the project's IP log. |
||||
* |
||||
* This program and the accompanying materials are made available |
||||
* under the terms of the Eclipse Distribution License v1.0 which |
||||
* accompanies this distribution, is reproduced below, and is |
||||
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||
* |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or |
||||
* without modification, are permitted provided that the following |
||||
* conditions are met: |
||||
* |
||||
* - Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* - Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following |
||||
* disclaimer in the documentation and/or other materials provided |
||||
* with the distribution. |
||||
* |
||||
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||
* names of its contributors may be used to endorse or promote |
||||
* products derived from this software without specific prior |
||||
* written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package org.eclipse.jgit.lib; |
||||
|
||||
import java.io.File; |
||||
import java.io.IOException; |
||||
|
||||
import org.eclipse.jgit.lib.RefUpdate.Result; |
||||
import org.eclipse.jgit.revwalk.RevWalk; |
||||
|
||||
/** |
||||
* Rename any reference stored by {@link RefDirectory}. |
||||
* <p> |
||||
* This class works by first renaming the source reference to a temporary name, |
||||
* then renaming the temporary name to the final destination reference. |
||||
* <p> |
||||
* This strategy permits switching a reference like {@code refs/heads/foo}, |
||||
* which is a file, to {@code refs/heads/foo/bar}, which is stored inside a |
||||
* directory that happens to match the source name. |
||||
*/ |
||||
class RefDirectoryRename extends RefRename { |
||||
private final RefDirectory refdb; |
||||
|
||||
/** |
||||
* The value of the source reference at the start of the rename. |
||||
* <p> |
||||
* At the end of the rename the destination reference must have this same |
||||
* value, otherwise we have a concurrent update and the rename must fail |
||||
* without making any changes. |
||||
*/ |
||||
private ObjectId objId; |
||||
|
||||
/** True if HEAD must be moved to the destination reference. */ |
||||
private boolean updateHEAD; |
||||
|
||||
/** A reference we backup {@link #objId} into during the rename. */ |
||||
private RefDirectoryUpdate tmp; |
||||
|
||||
RefDirectoryRename(RefDirectoryUpdate src, RefDirectoryUpdate dst) { |
||||
super(src, dst); |
||||
refdb = src.getRefDatabase(); |
||||
} |
||||
|
||||
@Override |
||||
protected Result doRename() throws IOException { |
||||
if (source.getRef().isSymbolic()) |
||||
return Result.IO_FAILURE; // not supported
|
||||
|
||||
final RevWalk rw = new RevWalk(refdb.getRepository()); |
||||
objId = source.getOldObjectId(); |
||||
updateHEAD = needToUpdateHEAD(); |
||||
tmp = refdb.newTemporaryUpdate(); |
||||
try { |
||||
// First backup the source so its never unreachable.
|
||||
tmp.setNewObjectId(objId); |
||||
tmp.setForceUpdate(true); |
||||
tmp.disableRefLog(); |
||||
switch (tmp.update(rw)) { |
||||
case NEW: |
||||
case FORCED: |
||||
case NO_CHANGE: |
||||
break; |
||||
default: |
||||
return tmp.getResult(); |
||||
} |
||||
|
||||
// Save the source's log under the temporary name, we must do
|
||||
// this before we delete the source, otherwise we lose the log.
|
||||
if (!renameLog(source, tmp)) |
||||
return Result.IO_FAILURE; |
||||
|
||||
// If HEAD has to be updated, link it now to destination.
|
||||
// We have to link before we delete, otherwise the delete
|
||||
// fails because its the current branch.
|
||||
RefUpdate dst = destination; |
||||
if (updateHEAD) { |
||||
if (!linkHEAD(destination)) { |
||||
renameLog(tmp, source); |
||||
return Result.LOCK_FAILURE; |
||||
} |
||||
|
||||
// Replace the update operation so HEAD will log the rename.
|
||||
dst = refdb.newUpdate(Constants.HEAD, false); |
||||
dst.setRefLogIdent(destination.getRefLogIdent()); |
||||
dst.setRefLogMessage(destination.getRefLogMessage(), false); |
||||
} |
||||
|
||||
// Delete the source name so its path is free for replacement.
|
||||
source.setExpectedOldObjectId(objId); |
||||
source.setForceUpdate(true); |
||||
source.disableRefLog(); |
||||
if (source.delete(rw) != Result.FORCED) { |
||||
renameLog(tmp, source); |
||||
if (updateHEAD) |
||||
linkHEAD(source); |
||||
return source.getResult(); |
||||
} |
||||
|
||||
// Move the log to the destination.
|
||||
if (!renameLog(tmp, destination)) { |
||||
renameLog(tmp, source); |
||||
source.setExpectedOldObjectId(ObjectId.zeroId()); |
||||
source.setNewObjectId(objId); |
||||
source.update(rw); |
||||
if (updateHEAD) |
||||
linkHEAD(source); |
||||
return Result.IO_FAILURE; |
||||
} |
||||
|
||||
// Create the destination, logging the rename during the creation.
|
||||
dst.setExpectedOldObjectId(ObjectId.zeroId()); |
||||
dst.setNewObjectId(objId); |
||||
if (dst.update(rw) != Result.NEW) { |
||||
// If we didn't create the destination we have to undo
|
||||
// our work. Put the log back and restore source.
|
||||
if (renameLog(destination, tmp)) |
||||
renameLog(tmp, source); |
||||
source.setExpectedOldObjectId(ObjectId.zeroId()); |
||||
source.setNewObjectId(objId); |
||||
source.update(rw); |
||||
if (updateHEAD) |
||||
linkHEAD(source); |
||||
return dst.getResult(); |
||||
} |
||||
|
||||
return Result.RENAMED; |
||||
} finally { |
||||
// Always try to free the temporary name.
|
||||
try { |
||||
refdb.delete(tmp); |
||||
} catch (IOException err) { |
||||
refdb.fileFor(tmp.getName()).delete(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
private boolean renameLog(RefUpdate src, RefUpdate dst) { |
||||
File srcLog = refdb.logFor(src.getName()); |
||||
File dstLog = refdb.logFor(dst.getName()); |
||||
|
||||
if (!srcLog.exists()) |
||||
return true; |
||||
|
||||
if (!rename(srcLog, dstLog)) |
||||
return false; |
||||
|
||||
try { |
||||
final int levels = RefDirectory.levelsIn(src.getName()) - 2; |
||||
RefDirectory.delete(srcLog, levels); |
||||
return true; |
||||
} catch (IOException e) { |
||||
rename(dstLog, srcLog); |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
private static boolean rename(File src, File dst) { |
||||
if (src.renameTo(dst)) |
||||
return true; |
||||
|
||||
File dir = dst.getParentFile(); |
||||
if ((dir.exists() || !dir.mkdirs()) && !dir.isDirectory()) |
||||
return false; |
||||
return src.renameTo(dst); |
||||
} |
||||
|
||||
private boolean linkHEAD(RefUpdate target) { |
||||
try { |
||||
refdb.link(Constants.HEAD, target.getName()); |
||||
return true; |
||||
} catch (IOException e) { |
||||
return false; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,135 @@
|
||||
/* |
||||
* Copyright (C) 2009-2010, Google Inc. |
||||
* Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> |
||||
* and other copyright owners as documented in the project's IP log. |
||||
* |
||||
* This program and the accompanying materials are made available |
||||
* under the terms of the Eclipse Distribution License v1.0 which |
||||
* accompanies this distribution, is reproduced below, and is |
||||
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||
* |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or |
||||
* without modification, are permitted provided that the following |
||||
* conditions are met: |
||||
* |
||||
* - Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* - Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following |
||||
* disclaimer in the documentation and/or other materials provided |
||||
* with the distribution. |
||||
* |
||||
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||
* names of its contributors may be used to endorse or promote |
||||
* products derived from this software without specific prior |
||||
* written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package org.eclipse.jgit.lib; |
||||
|
||||
import java.io.IOException; |
||||
|
||||
/** Updates any reference stored by {@link RefDirectory}. */ |
||||
class RefDirectoryUpdate extends RefUpdate { |
||||
private final RefDirectory database; |
||||
|
||||
private LockFile lock; |
||||
|
||||
RefDirectoryUpdate(final RefDirectory r, final Ref ref) { |
||||
super(ref); |
||||
database = r; |
||||
} |
||||
|
||||
@Override |
||||
protected RefDirectory getRefDatabase() { |
||||
return database; |
||||
} |
||||
|
||||
@Override |
||||
protected Repository getRepository() { |
||||
return database.getRepository(); |
||||
} |
||||
|
||||
@Override |
||||
protected boolean tryLock() throws IOException { |
||||
Ref dst = getRef().getLeaf(); |
||||
String name = dst.getName(); |
||||
lock = new LockFile(database.fileFor(name)); |
||||
if (lock.lock()) { |
||||
dst = database.getRef(name); |
||||
setOldObjectId(dst != null ? dst.getObjectId() : null); |
||||
return true; |
||||
} else { |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
protected void unlock() { |
||||
if (lock != null) { |
||||
lock.unlock(); |
||||
lock = null; |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
protected Result doUpdate(final Result status) throws IOException { |
||||
lock.setNeedStatInformation(true); |
||||
lock.write(getNewObjectId()); |
||||
|
||||
String msg = getRefLogMessage(); |
||||
if (msg != null) { |
||||
if (isRefLogIncludingResult()) { |
||||
String strResult = toResultString(status); |
||||
if (strResult != null) { |
||||
if (msg.length() > 0) |
||||
msg = msg + ": " + strResult; |
||||
else |
||||
msg = strResult; |
||||
} |
||||
} |
||||
database.log(this, msg); |
||||
} |
||||
if (!lock.commit()) |
||||
return Result.LOCK_FAILURE; |
||||
database.stored(this, lock.getCommitLastModified()); |
||||
return status; |
||||
} |
||||
|
||||
private String toResultString(final Result status) { |
||||
switch (status) { |
||||
case FORCED: |
||||
return "forced-update"; |
||||
case FAST_FORWARD: |
||||
return "fast forward"; |
||||
case NEW: |
||||
return "created"; |
||||
default: |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
protected Result doDelete(final Result status) throws IOException { |
||||
if (getRef().getLeaf().getStorage() != Ref.Storage.NEW) |
||||
database.delete(this); |
||||
return status; |
||||
} |
||||
} |
@ -1,158 +0,0 @@
|
||||
/* |
||||
* Copyright (C) 2009, Christian Halstrick <christian.halstrick@sap.com> |
||||
* Copyright (C) 2007, Dave Watson <dwatson@mimvista.com> |
||||
* Copyright (C) 2009, Google Inc. |
||||
* Copyright (C) 2007-2009, Robin Rosenberg <robin.rosenberg@dewire.com> |
||||
* Copyright (C) 2006, Shawn O. Pearce <spearce@spearce.org> |
||||
* and other copyright owners as documented in the project's IP log. |
||||
* |
||||
* This program and the accompanying materials are made available |
||||
* under the terms of the Eclipse Distribution License v1.0 which |
||||
* accompanies this distribution, is reproduced below, and is |
||||
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||
* |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or |
||||
* without modification, are permitted provided that the following |
||||
* conditions are met: |
||||
* |
||||
* - Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* - Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following |
||||
* disclaimer in the documentation and/or other materials provided |
||||
* with the distribution. |
||||
* |
||||
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||
* names of its contributors may be used to endorse or promote |
||||
* products derived from this software without specific prior |
||||
* written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package org.eclipse.jgit.lib; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileOutputStream; |
||||
import java.io.IOException; |
||||
|
||||
/** |
||||
* Utility class to work with reflog files |
||||
* |
||||
* @author Dave Watson |
||||
*/ |
||||
public class RefLogWriter { |
||||
static void append(final RefUpdate u, final String msg) throws IOException { |
||||
final ObjectId oldId = u.getOldObjectId(); |
||||
final ObjectId newId = u.getNewObjectId(); |
||||
final Repository db = u.getRepository(); |
||||
final PersonIdent ident = u.getRefLogIdent(); |
||||
|
||||
appendOneRecord(oldId, newId, ident, msg, db, u.getName()); |
||||
if (!u.getName().equals(u.getOrigName())) |
||||
appendOneRecord(oldId, newId, ident, msg, db, u.getOrigName()); |
||||
} |
||||
|
||||
static void append(RefRename refRename, String logName, String msg) throws IOException { |
||||
final ObjectId id = refRename.getObjectId(); |
||||
final Repository db = refRename.getRepository(); |
||||
final PersonIdent ident = refRename.getRefLogIdent(); |
||||
appendOneRecord(id, id, ident, msg, db, logName); |
||||
} |
||||
|
||||
static void renameTo(final Repository db, final RefUpdate from, |
||||
final RefUpdate to) throws IOException { |
||||
final File logdir = new File(db.getDirectory(), Constants.LOGS); |
||||
final File reflogFrom = new File(logdir, from.getName()); |
||||
if (!reflogFrom.exists()) |
||||
return; |
||||
final File reflogTo = new File(logdir, to.getName()); |
||||
final File reflogToDir = reflogTo.getParentFile(); |
||||
File tmp = new File(logdir, "tmp-renamed-log.." + Thread.currentThread().getId()); |
||||
if (!reflogFrom.renameTo(tmp)) { |
||||
throw new IOException("Cannot rename " + reflogFrom + " to (" + tmp |
||||
+ ")" + reflogTo); |
||||
} |
||||
RefUpdate.deleteEmptyDir(reflogFrom, RefUpdate.count(from.getName(), |
||||
'/')); |
||||
if (!reflogToDir.exists() && !reflogToDir.mkdirs()) { |
||||
throw new IOException("Cannot create directory " + reflogToDir); |
||||
} |
||||
if (!tmp.renameTo(reflogTo)) { |
||||
throw new IOException("Cannot rename (" + tmp + ")" + reflogFrom |
||||
+ " to " + reflogTo); |
||||
} |
||||
} |
||||
|
||||
private static void appendOneRecord(final ObjectId oldId, |
||||
final ObjectId newId, PersonIdent ident, final String msg, |
||||
final Repository db, final String refName) throws IOException { |
||||
if (ident == null) |
||||
ident = new PersonIdent(db); |
||||
else |
||||
ident = new PersonIdent(ident); |
||||
|
||||
final StringBuilder r = new StringBuilder(); |
||||
r.append(ObjectId.toString(oldId)); |
||||
r.append(' '); |
||||
r.append(ObjectId.toString(newId)); |
||||
r.append(' '); |
||||
r.append(ident.toExternalString()); |
||||
r.append('\t'); |
||||
r.append(msg); |
||||
r.append('\n'); |
||||
|
||||
final byte[] rec = Constants.encode(r.toString()); |
||||
final File logdir = new File(db.getDirectory(), Constants.LOGS); |
||||
final File reflog = new File(logdir, refName); |
||||
if (reflog.exists() || db.getConfig().getCore().isLogAllRefUpdates()) { |
||||
final File refdir = reflog.getParentFile(); |
||||
|
||||
if (!refdir.exists() && !refdir.mkdirs()) |
||||
throw new IOException("Cannot create directory " + refdir); |
||||
|
||||
final FileOutputStream out = new FileOutputStream(reflog, true); |
||||
try { |
||||
out.write(rec); |
||||
} finally { |
||||
out.close(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Writes reflog entry for ref specified by refName |
||||
* |
||||
* @param repo |
||||
* repository to use |
||||
* @param oldCommit |
||||
* previous commit |
||||
* @param commit |
||||
* new commit |
||||
* @param message |
||||
* reflog message |
||||
* @param refName |
||||
* full ref name |
||||
* @throws IOException |
||||
* @deprecated rely upon {@link RefUpdate}'s automatic logging instead. |
||||
*/ |
||||
public static void writeReflog(Repository repo, ObjectId oldCommit, |
||||
ObjectId commit, String message, String refName) throws IOException { |
||||
appendOneRecord(oldCommit, commit, null, message, repo, refName); |
||||
} |
||||
} |
@ -0,0 +1,121 @@
|
||||
/* |
||||
* Copyright (C) 2010, Google Inc. |
||||
* and other copyright owners as documented in the project's IP log. |
||||
* |
||||
* This program and the accompanying materials are made available |
||||
* under the terms of the Eclipse Distribution License v1.0 which |
||||
* accompanies this distribution, is reproduced below, and is |
||||
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||
* |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or |
||||
* without modification, are permitted provided that the following |
||||
* conditions are met: |
||||
* |
||||
* - Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* - Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following |
||||
* disclaimer in the documentation and/or other materials provided |
||||
* with the distribution. |
||||
* |
||||
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||
* names of its contributors may be used to endorse or promote |
||||
* products derived from this software without specific prior |
||||
* written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package org.eclipse.jgit.lib; |
||||
|
||||
/** |
||||
* A reference that indirectly points at another {@link Ref}. |
||||
* <p> |
||||
* A symbolic reference always derives its current value from the target |
||||
* reference. |
||||
*/ |
||||
public class SymbolicRef implements Ref { |
||||
private final String name; |
||||
|
||||
private final Ref target; |
||||
|
||||
/** |
||||
* Create a new ref pairing. |
||||
* |
||||
* @param refName |
||||
* name of this ref. |
||||
* @param target |
||||
* the ref we reference and derive our value from. |
||||
*/ |
||||
public SymbolicRef(String refName, Ref target) { |
||||
this.name = refName; |
||||
this.target = target; |
||||
} |
||||
|
||||
public String getName() { |
||||
return name; |
||||
} |
||||
|
||||
public boolean isSymbolic() { |
||||
return true; |
||||
} |
||||
|
||||
public Ref getLeaf() { |
||||
Ref dst = getTarget(); |
||||
while (dst.isSymbolic()) |
||||
dst = dst.getTarget(); |
||||
return dst; |
||||
} |
||||
|
||||
public Ref getTarget() { |
||||
return target; |
||||
} |
||||
|
||||
public ObjectId getObjectId() { |
||||
return getLeaf().getObjectId(); |
||||
} |
||||
|
||||
public Storage getStorage() { |
||||
return Storage.LOOSE; |
||||
} |
||||
|
||||
public ObjectId getPeeledObjectId() { |
||||
return getLeaf().getPeeledObjectId(); |
||||
} |
||||
|
||||
public boolean isPeeled() { |
||||
return getLeaf().isPeeled(); |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
StringBuilder r = new StringBuilder(); |
||||
r.append("SymbolicRef["); |
||||
Ref cur = this; |
||||
while (cur.isSymbolic()) { |
||||
r.append(cur.getName()); |
||||
r.append(" -> "); |
||||
cur = cur.getTarget(); |
||||
} |
||||
r.append(cur.getName()); |
||||
r.append('='); |
||||
r.append(ObjectId.toString(cur.getObjectId())); |
||||
r.append("]"); |
||||
return r.toString(); |
||||
} |
||||
} |
Loading…
Reference in new issue