Cleaned up whitespace, fix DOS line endings
[nbgit.git] / src / org / nbgit / ui / log / RepositoryRevision.java
blob58a8d8c700fe702420daa2594784725e3d888464
1 package org.nbgit.ui.log;
3 /*
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
6 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
8 * The contents of this file are subject to the terms of either the GNU
9 * General Public License Version 2 only ("GPL") or the Common
10 * Development and Distribution License("CDDL") (collectively, the
11 * "License"). You may not use this file except in compliance with the
12 * License. You can obtain a copy of the License at
13 * http://www.netbeans.org/cddl-gplv2.html
14 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
15 * specific language governing permissions and limitations under the
16 * License. When distributing the software, include this License Header
17 * Notice in each file and include the License file at
18 * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
19 * particular file as subject to the "Classpath" exception as provided
20 * by Sun in the GPL Version 2 section of the License file that
21 * accompanied this code. If applicable, add the following below the
22 * License Header, with the fields enclosed by brackets [] replaced by
23 * your own identifying information:
24 * "Portions Copyrighted [year] [name of copyright owner]"
26 * Contributor(s):
28 * The Original Software is NetBeans. The Initial Developer of the Original
29 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
30 * Microsystems, Inc. All Rights Reserved.
31 * Portions Copyright 2008 Alexander Coles (Ikonoklastik Productions).
33 * If you wish your version of this file to be governed by only the CDDL
34 * or only the GPL Version 2, indicate your decision by adding
35 * "[Contributor] elects to include this software in this distribution
36 * under the [CDDL or GPL Version 2] license." If you do not indicate a
37 * single choice of license, a recipient has the option to distribute
38 * your version of this file under either the CDDL, the GPL Version 2 or
39 * to extend the choice of license to its licensees as provided above.
40 * However, if you add GPL Version 2 code and therefore, elected the GPL
41 * Version 2 license, then the option applies only if the new code is
42 * made subject to such option by the copyright holder.
44 import java.io.File;
45 import java.io.IOException;
46 import java.util.ArrayList;
47 import java.util.Date;
48 import java.util.List;
49 import org.openide.util.Exceptions;
50 import org.spearce.jgit.errors.CorruptObjectException;
51 import org.spearce.jgit.errors.IncorrectObjectTypeException;
52 import org.spearce.jgit.errors.MissingObjectException;
53 import org.spearce.jgit.lib.AnyObjectId;
54 import org.spearce.jgit.lib.ObjectId;
55 import org.spearce.jgit.lib.Repository;
56 import org.spearce.jgit.revwalk.RevCommit;
57 import org.spearce.jgit.revwalk.RevWalk;
58 import org.spearce.jgit.treewalk.TreeWalk;
60 /**
61 * Describes log information for a file. This is the result of doing a
62 * cvs log command. The fields in instances of this object are populated
63 * by response handlers.
65 * @author Maros Sandor
67 public class RepositoryRevision extends RevCommit {
69 private final Repository repo;
70 /**
71 * List of events associated with the revision.
73 private final List<Event> events = new ArrayList<Event>(1);
75 private RepositoryRevision(AnyObjectId id, Repository repo) {
76 super(id);
77 this.repo = repo;
80 public String getRepositoryRootUrl() {
81 return repo.getDirectory().getAbsolutePath();
84 Iterable<Event> createEvents(Walk walk) {
85 try {
86 initEvents(walk.getTreeWalk());
87 } catch (MissingObjectException ex) {
88 Exceptions.printStackTrace(ex);
89 } catch (IncorrectObjectTypeException ex) {
90 Exceptions.printStackTrace(ex);
91 } catch (CorruptObjectException ex) {
92 Exceptions.printStackTrace(ex);
93 } catch (IOException ex) {
94 Exceptions.printStackTrace(ex);
97 return events;
100 private ObjectId[] getTrees() {
101 final ObjectId[] r = new ObjectId[getParentCount() + 1];
102 for (int i = 0; i < r.length - 1; i++) {
103 r[i] = getParent(i).getTree().getId();
105 r[r.length - 1] = getTree().getId();
106 return r;
109 private char getStatus(TreeWalk walk, int mode0, int mode1) {
110 if (mode0 == 0 && mode1 != 0) {
111 return 'A';
112 } else if (mode0 != 0 && mode1 == 0) {
113 return 'D';
114 } else if (!walk.idEqual(0, 1)) {
115 return 'M';
116 } else if (mode0 != mode1) {
117 return 'm';
119 return 0;
122 private void initEvents(final TreeWalk walk)
123 throws MissingObjectException, IncorrectObjectTypeException,
124 CorruptObjectException, IOException {
125 ObjectId[] trees = getTrees();
126 final int revTree = trees.length - 1;
128 walk.reset(trees);
130 switch (trees.length) {
131 case 1:
132 /* Inital commit. */
133 while (walk.next()) {
134 events.add(new Event(walk.getPathString(), 'A'));
136 break;
137 case 2:
138 while (walk.next()) {
139 int mode0 = walk.getRawMode(0);
140 int mode1 = walk.getRawMode(1);
141 char status = getStatus(walk, mode0, mode1);
142 if (status == 0) {
143 continue;
145 events.add(new Event(walk.getPathString(), status));
147 break;
148 default:
149 /* Merge. */
150 while (walk.next()) {
151 int mode0 = 0;
152 int mode1 = walk.getRawMode(revTree);
153 int i;
155 for (i = 0; i < revTree; i++) {
156 int mode = walk.getRawMode(i);
157 if (mode == mode1 && walk.idEqual(i, revTree)) {
158 break;
160 mode0 |= mode;
163 if (i != revTree) {
164 continue;
166 char status = getStatus(walk, mode0, mode1);
167 if (status == 0) {
168 continue;
170 events.add(new Event(walk.getPathString(), status));
172 break;
177 public List<Event> getEvents() {
178 return events;
181 public String getAuthor() {
182 return getAuthorIdent().getName();
185 String getMessage() {
186 return getFullMessage();
189 public String getRevision() {
190 return getId().name();
193 public Date getDate() {
194 return getCommitterIdent().getWhen();
197 public class Event {
200 * The file or folder that this event is about. It may be null if the File cannot be computed.
202 private File file;
203 private GitLogMessageChangedPath changedPath;
204 private String name;
205 private String path;
207 private Event(GitLogMessageChangedPath changedPath) {
208 this.changedPath = changedPath;
209 name = changedPath.getPath().substring(changedPath.getPath().lastIndexOf('/') + 1);
211 int indexPath = changedPath.getPath().lastIndexOf('/');
212 if (indexPath > -1) {
213 path = changedPath.getPath().substring(0, indexPath);
214 } else {
215 path = "";
219 private Event(String pathString, char c) {
220 this(new GitLogMessageChangedPath(pathString, c));
223 public RepositoryRevision getLogInfoHeader() {
224 return RepositoryRevision.this;
227 public GitLogMessageChangedPath getChangedPath() {
228 return changedPath;
231 /** Getter for property file.
232 * @return Value of property file.
234 public File getFile() {
235 return file;
238 /** Setter for property file.
239 * @param file New value of property file.
241 public void setFile(File file) {
242 this.file = file;
245 public String getName() {
246 return name;
249 public String getPath() {
250 return path;
253 @Override
254 public String toString() {
255 StringBuffer text = new StringBuffer();
256 text.append("\t");
257 text.append(getPath());
258 return text.toString();
262 public static class Walk extends RevWalk {
264 private final TreeWalk walk;
266 public Walk(Repository repo) {
267 super(repo);
268 walk = new TreeWalk(repo);
269 walk.setRecursive(true);
272 @Override
273 protected RevCommit createCommit(final AnyObjectId id) {
274 return new RepositoryRevision(id, getRepository());
277 private TreeWalk getTreeWalk() {
278 return walk;