View Javadoc
1   /*
2    * Copyright (C) 2015, 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.pgm.debug;
45  
46  import static org.eclipse.jgit.lib.Constants.HEAD;
47  
48  import java.io.IOException;
49  import java.util.ArrayList;
50  import java.util.List;
51  
52  import org.eclipse.jgit.internal.storage.reftree.RefTree;
53  import org.eclipse.jgit.internal.storage.reftree.RefTreeDatabase;
54  import org.eclipse.jgit.lib.CommitBuilder;
55  import org.eclipse.jgit.lib.ObjectId;
56  import org.eclipse.jgit.lib.ObjectInserter;
57  import org.eclipse.jgit.lib.ObjectReader;
58  import org.eclipse.jgit.lib.PersonIdent;
59  import org.eclipse.jgit.lib.Ref;
60  import org.eclipse.jgit.lib.RefDatabase;
61  import org.eclipse.jgit.lib.RefUpdate;
62  import org.eclipse.jgit.lib.StoredConfig;
63  import org.eclipse.jgit.pgm.Command;
64  import org.eclipse.jgit.pgm.TextBuiltin;
65  import org.eclipse.jgit.revwalk.RevWalk;
66  import org.kohsuke.args4j.Option;
67  
68  @Command(usage = "usage_RebuildRefTree")
69  class RebuildRefTree extends TextBuiltin {
70  	@Option(name = "--enable", usage = "usage_RebuildRefTreeEnable")
71  	boolean enable;
72  
73  	private String txnNamespace;
74  	private String txnCommitted;
75  
76  	/** {@inheritDoc} */
77  	@Override
78  	protected void run() throws Exception {
79  		try (ObjectReader reader = db.newObjectReader();
80  				RevWalk rw = new RevWalk(reader);
81  				ObjectInserter inserter = db.newObjectInserter()) {
82  			RefDatabase refDb = db.getRefDatabase();
83  			if (refDb instanceof RefTreeDatabase) {
84  				RefTreeDatabase d = (RefTreeDatabase) refDb;
85  				refDb = d.getBootstrap();
86  				txnNamespace = d.getTxnNamespace();
87  				txnCommitted = d.getTxnCommitted();
88  			} else {
89  				RefTreeDatabase d = new RefTreeDatabase(db, refDb);
90  				txnNamespace = d.getTxnNamespace();
91  				txnCommitted = d.getTxnCommitted();
92  			}
93  
94  			errw.format("Rebuilding %s from %s", //$NON-NLS-1$
95  					txnCommitted, refDb.getClass().getSimpleName());
96  			errw.println();
97  			errw.flush();
98  
99  			CommitBuilder b = new CommitBuilder();
100 			Ref ref = refDb.exactRef(txnCommitted);
101 			RefUpdate update = refDb.newUpdate(txnCommitted, true);
102 			ObjectId oldTreeId;
103 
104 			if (ref != null && ref.getObjectId() != null) {
105 				ObjectId oldId = ref.getObjectId();
106 				update.setExpectedOldObjectId(oldId);
107 				b.setParentId(oldId);
108 				oldTreeId = rw.parseCommit(oldId).getTree();
109 			} else {
110 				update.setExpectedOldObjectId(ObjectId.zeroId());
111 				oldTreeId = ObjectId.zeroId();
112 			}
113 
114 			RefTree tree = rebuild(refDb);
115 			b.setTreeId(tree.writeTree(inserter));
116 			b.setAuthor(new PersonIdent(db));
117 			b.setCommitter(b.getAuthor());
118 			if (b.getTreeId().equals(oldTreeId)) {
119 				return;
120 			}
121 
122 			update.setNewObjectId(inserter.insert(b));
123 			inserter.flush();
124 
125 			RefUpdate.Result result = update.update(rw);
126 			switch (result) {
127 			case NEW:
128 			case FAST_FORWARD:
129 				break;
130 			default:
131 				throw die(String.format("%s: %s", update.getName(), result)); //$NON-NLS-1$
132 			}
133 
134 			if (enable && !(db.getRefDatabase() instanceof RefTreeDatabase)) {
135 				StoredConfig cfg = db.getConfig();
136 				cfg.setInt("core", null, "repositoryformatversion", 1); //$NON-NLS-1$ //$NON-NLS-2$
137 				cfg.setString("extensions", null, "refStorage", "reftree"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
138 				cfg.save();
139 				errw.println("Enabled reftree."); //$NON-NLS-1$
140 				errw.flush();
141 			}
142 		}
143 	}
144 
145 	private RefTree rebuild(RefDatabase refdb) throws IOException {
146 		RefTree tree = RefTree.newEmptyTree();
147 		List<org.eclipse.jgit.internal.storage.reftree.Command> cmds
148 			= new ArrayList<>();
149 
150 		Ref head = refdb.exactRef(HEAD);
151 		if (head != null) {
152 			cmds.add(new org.eclipse.jgit.internal.storage.reftree.Command(
153 					null,
154 					head));
155 		}
156 
157 		for (Ref r : refdb.getRefs()) {
158 			if (r.getName().equals(txnCommitted) || r.getName().equals(HEAD)
159 					|| r.getName().startsWith(txnNamespace)) {
160 				continue;
161 			}
162 			cmds.add(new org.eclipse.jgit.internal.storage.reftree.Command(
163 					null,
164 					db.getRefDatabase().peel(r)));
165 		}
166 		tree.apply(cmds);
167 		return tree;
168 	}
169 }