View Javadoc
1   /*
2    * Copyright (C) 2016, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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  /** Single reference rename to {@link RefTreeDatabase}. */
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  	/** {@inheritDoc} */
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 }