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