1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.internal.storage.reftree;
12
13 import static org.eclipse.jgit.lib.Constants.HEAD;
14 import static org.eclipse.jgit.lib.RefUpdate.Result.REJECTED;
15 import static org.eclipse.jgit.lib.RefUpdate.Result.RENAMED;
16
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.eclipse.jgit.lib.ObjectId;
22 import org.eclipse.jgit.lib.ObjectIdRef;
23 import org.eclipse.jgit.lib.Ref;
24 import org.eclipse.jgit.lib.RefRename;
25 import org.eclipse.jgit.lib.RefUpdate;
26 import org.eclipse.jgit.lib.RefUpdate.Result;
27 import org.eclipse.jgit.lib.SymbolicRef;
28 import org.eclipse.jgit.revwalk.RevWalk;
29
30
31 class RefTreeRename extends RefRename {
32 private final RefTreeDatabase refdb;
33
34 RefTreeRename(RefTreeDatabase refdb, RefUpdate="../../../../../../org/eclipse/jgit/lib/RefUpdate.html#RefUpdate">RefUpdate src, RefUpdate dst) {
35 super(src, dst);
36 this.refdb = refdb;
37 }
38
39
40 @Override
41 protected Result doRename() throws IOException {
42 try (RevWalkvwalk/RevWalk.html#RevWalk">RevWalk rw = new RevWalk(refdb.getRepository())) {
43 RefTreeBatch batch = new RefTreeBatch(refdb);
44 batch.setRefLogIdent(getRefLogIdent());
45 batch.setRefLogMessage(getRefLogMessage(), false);
46 batch.init(rw);
47
48 Ref head = batch.exactRef(rw.getObjectReader(), HEAD);
49 Ref oldRef = batch.exactRef(rw.getObjectReader(), source.getName());
50 if (oldRef == null) {
51 return REJECTED;
52 }
53
54 Ref newRef = asNew(oldRef);
55 List<Command> mv = new ArrayList<>(3);
56 mv.add(new Command(oldRef, null));
57 mv.add(new Command(null, newRef));
58 if (head != null && head.isSymbolic()
59 && head.getTarget().getName().equals(oldRef.getName())) {
60 mv.add(new Command(
61 head,
62 new SymbolicRef(head.getName(), newRef)));
63 }
64 batch.execute(rw, mv);
65 return RefTreeUpdate.translate(mv.get(1).getResult(), RENAMED);
66 }
67 }
68
69 private Ref href="../../../../../../org/eclipse/jgit/lib/Ref.html#Ref">Ref asNew(Ref src) {
70 String name = destination.getName();
71 if (src.isSymbolic()) {
72 return new SymbolicRef(name, src.getTarget());
73 }
74
75 ObjectId peeled = src.getPeeledObjectId();
76 if (peeled != null) {
77 return new ObjectIdRef.PeeledTag(
78 src.getStorage(),
79 name,
80 src.getObjectId(),
81 peeled);
82 }
83
84 return new ObjectIdRef.PeeledNonTag(
85 src.getStorage(),
86 name,
87 src.getObjectId());
88 }
89 }