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  	@Override
77  	protected void run() throws Exception {
78  		try (ObjectReader reader = db.newObjectReader();
79  				RevWalk rw = new RevWalk(reader);
80  				ObjectInserter inserter = db.newObjectInserter()) {
81  			RefDatabase refDb = db.getRefDatabase();
82  			if (refDb instanceof RefTreeDatabase) {
83  				RefTreeDatabase d = (RefTreeDatabase) refDb;
84  				refDb = d.getBootstrap();
85  				txnNamespace = d.getTxnNamespace();
86  				txnCommitted = d.getTxnCommitted();
87  			} else {
88  				RefTreeDatabase d = new RefTreeDatabase(db, refDb);
89  				txnNamespace = d.getTxnNamespace();
90  				txnCommitted = d.getTxnCommitted();
91  			}
92  
93  			errw.format("Rebuilding %s from %s", //$NON-NLS-1$
94  					txnCommitted, refDb.getClass().getSimpleName());
95  			errw.println();
96  			errw.flush();
97  
98  			CommitBuilder b = new CommitBuilder();
99  			Ref ref = refDb.exactRef(txnCommitted);
100 			RefUpdate update = refDb.newUpdate(txnCommitted, true);
101 			ObjectId oldTreeId;
102 
103 			if (ref != null && ref.getObjectId() != null) {
104 				ObjectId oldId = ref.getObjectId();
105 				update.setExpectedOldObjectId(oldId);
106 				b.setParentId(oldId);
107 				oldTreeId = rw.parseCommit(oldId).getTree();
108 			} else {
109 				update.setExpectedOldObjectId(ObjectId.zeroId());
110 				oldTreeId = ObjectId.zeroId();
111 			}
112 
113 			RefTree tree = rebuild(refDb);
114 			b.setTreeId(tree.writeTree(inserter));
115 			b.setAuthor(new PersonIdent(db));
116 			b.setCommitter(b.getAuthor());
117 			if (b.getTreeId().equals(oldTreeId)) {
118 				return;
119 			}
120 
121 			update.setNewObjectId(inserter.insert(b));
122 			inserter.flush();
123 
124 			RefUpdate.Result result = update.update(rw);
125 			switch (result) {
126 			case NEW:
127 			case FAST_FORWARD:
128 				break;
129 			default:
130 				throw die(String.format("%s: %s", update.getName(), result)); //$NON-NLS-1$
131 			}
132 
133 			if (enable && !(db.getRefDatabase() instanceof RefTreeDatabase)) {
134 				StoredConfig cfg = db.getConfig();
135 				cfg.setInt("core", null, "repositoryformatversion", 1); //$NON-NLS-1$ //$NON-NLS-2$
136 				cfg.setString("extensions", null, "refsStorage", "reftree"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
137 				cfg.save();
138 				errw.println("Enabled reftree."); //$NON-NLS-1$
139 				errw.flush();
140 			}
141 		}
142 	}
143 
144 	private RefTree rebuild(RefDatabase refdb) throws IOException {
145 		RefTree tree = RefTree.newEmptyTree();
146 		List<org.eclipse.jgit.internal.storage.reftree.Command> cmds
147 			= new ArrayList<>();
148 
149 		Ref head = refdb.exactRef(HEAD);
150 		if (head != null) {
151 			cmds.add(new org.eclipse.jgit.internal.storage.reftree.Command(
152 					null,
153 					head));
154 		}
155 
156 		for (Ref r : refdb.getRefs(RefDatabase.ALL).values()) {
157 			if (r.getName().equals(txnCommitted) || r.getName().equals(HEAD)
158 					|| r.getName().startsWith(txnNamespace)) {
159 				continue;
160 			}
161 			cmds.add(new org.eclipse.jgit.internal.storage.reftree.Command(
162 					null,
163 					db.peel(r)));
164 		}
165 		tree.apply(cmds);
166 		return tree;
167 	}
168 }