View Javadoc
1   /*
2    * Copyright (C) 2008-2018, Robin Rosenberg <robin.rosenberg@dewire.com>
3    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4    * and other copyright owners as documented in the project's IP log.
5    *
6    * This program and the accompanying materials are made available
7    * under the terms of the Eclipse Distribution License v1.0 which
8    * accompanies this distribution, is reproduced below, and is
9    * available at http://www.eclipse.org/org/documents/edl-v10.php
10   *
11   * All rights reserved.
12   *
13   * Redistribution and use in source and binary forms, with or
14   * without modification, are permitted provided that the following
15   * conditions are met:
16   *
17   * - Redistributions of source code must retain the above copyright
18   *   notice, this list of conditions and the following disclaimer.
19   *
20   * - Redistributions in binary form must reproduce the above
21   *   copyright notice, this list of conditions and the following
22   *   disclaimer in the documentation and/or other materials provided
23   *   with the distribution.
24   *
25   * - Neither the name of the Eclipse Foundation, Inc. nor the
26   *   names of its contributors may be used to endorse or promote
27   *   products derived from this software without specific prior
28   *   written permission.
29   *
30   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43   */
44  
45  package org.eclipse.jgit.revplot;
46  
47  import static org.eclipse.jgit.lib.Constants.R_HEADS;
48  import static org.eclipse.jgit.lib.Constants.R_REMOTES;
49  import static org.eclipse.jgit.lib.Constants.R_TAGS;
50  
51  import java.io.IOException;
52  import java.util.Arrays;
53  import java.util.Collection;
54  import java.util.Collections;
55  import java.util.Comparator;
56  import java.util.HashMap;
57  import java.util.HashSet;
58  import java.util.Map;
59  import java.util.Set;
60  
61  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
62  import org.eclipse.jgit.errors.MissingObjectException;
63  import org.eclipse.jgit.internal.JGitText;
64  import org.eclipse.jgit.lib.AnyObjectId;
65  import org.eclipse.jgit.lib.PersonIdent;
66  import org.eclipse.jgit.lib.Ref;
67  import org.eclipse.jgit.lib.Repository;
68  import org.eclipse.jgit.revwalk.RevCommit;
69  import org.eclipse.jgit.revwalk.RevObject;
70  import org.eclipse.jgit.revwalk.RevSort;
71  import org.eclipse.jgit.revwalk.RevTag;
72  import org.eclipse.jgit.revwalk.RevWalk;
73  
74  /**
75   * Specialized RevWalk for visualization of a commit graph.
76   */
77  public class PlotWalk extends RevWalk {
78  
79  	private Map<AnyObjectId, Set<Ref>> additionalRefMap;
80  
81  	private Map<AnyObjectId, Set<Ref>> reverseRefMap;
82  
83  	private Repository repository;
84  
85  	/** {@inheritDoc} */
86  	@Override
87  	public void dispose() {
88  		super.dispose();
89  		if (reverseRefMap != null) {
90  			reverseRefMap.clear();
91  			reverseRefMap = null;
92  		}
93  		if (additionalRefMap != null) {
94  			additionalRefMap.clear();
95  			additionalRefMap = null;
96  		}
97  		repository = null;
98  	}
99  
100 	/**
101 	 * Create a new revision walker for a given repository.
102 	 *
103 	 * @param repo
104 	 *            the repository the walker will obtain data from.
105 	 */
106 	public PlotWalk(Repository repo) {
107 		super(repo);
108 		super.sort(RevSort.TOPO, true);
109 		additionalRefMap = new HashMap<>();
110 		repository = repo;
111 	}
112 
113 	/**
114 	 * Add additional refs to the walk
115 	 *
116 	 * @param refs
117 	 *            additional refs
118 	 * @throws java.io.IOException
119 	 */
120 	public void addAdditionalRefs(Iterable<Ref> refs) throws IOException {
121 		for (Ref ref : refs) {
122 			Set<Ref> set = additionalRefMap.get(ref.getObjectId());
123 			if (set == null)
124 				set = Collections.singleton(ref);
125 			else {
126 				set = new HashSet<>(set);
127 				set.add(ref);
128 			}
129 			additionalRefMap.put(ref.getObjectId(), set);
130 		}
131 	}
132 
133 	/** {@inheritDoc} */
134 	@Override
135 	public void sort(RevSort s, boolean use) {
136 		if (s == RevSort.TOPO && !use)
137 			throw new IllegalArgumentException(JGitText.get().topologicalSortRequired);
138 		super.sort(s, use);
139 	}
140 
141 	/** {@inheritDoc} */
142 	@Override
143 	protected RevCommit createCommit(AnyObjectId id) {
144 		return new PlotCommit(id);
145 	}
146 
147 	/** {@inheritDoc} */
148 	@Override
149 	public RevCommit next() throws MissingObjectException,
150 			IncorrectObjectTypeException, IOException {
151 		PlotCommit<?> pc = (PlotCommit) super.next();
152 		if (pc != null)
153 			pc.refs = getRefs(pc);
154 		return pc;
155 	}
156 
157 	private Ref[] getRefs(AnyObjectId commitId) {
158 		if (reverseRefMap == null) {
159 			reverseRefMap = repository.getAllRefsByPeeledObjectId();
160 			for (Map.Entry<AnyObjectId, Set<Ref>> entry : additionalRefMap
161 					.entrySet()) {
162 				Set<Ref> set = reverseRefMap.get(entry.getKey());
163 				Set<Ref> additional = entry.getValue();
164 				if (set != null) {
165 					if (additional.size() == 1) {
166 						// It's an unmodifiable singleton set...
167 						additional = new HashSet<>(additional);
168 					}
169 					additional.addAll(set);
170 				}
171 				reverseRefMap.put(entry.getKey(), additional);
172 			}
173 			additionalRefMap.clear();
174 			additionalRefMap = null;
175 		}
176 		Collection<Ref> list = reverseRefMap.get(commitId);
177 		if (list == null) {
178 			return PlotCommit.NO_REFS;
179 		} else {
180 			Ref[] tags = list.toArray(new Ref[0]);
181 			Arrays.sort(tags, new PlotRefComparator());
182 			return tags;
183 		}
184 	}
185 
186 	class PlotRefComparator implements Comparator<Ref> {
187 		@Override
188 		public int compare(Reff" href="../../../../org/eclipse/jgit/lib/Ref.html#Ref">Ref o1, Ref o2) {
189 			try {
190 				RevObject obj1 = parseAny(o1.getObjectId());
191 				RevObject obj2 = parseAny(o2.getObjectId());
192 				long t1 = timeof(obj1);
193 				long t2 = timeof(obj2);
194 				if (t1 > t2)
195 					return -1;
196 				if (t1 < t2)
197 					return 1;
198 			} catch (IOException e) {
199 				// ignore
200 			}
201 
202 			int cmp = kind(o1) - kind(o2);
203 			if (cmp == 0)
204 				cmp = o1.getName().compareTo(o2.getName());
205 			return cmp;
206 		}
207 
208 		long timeof(RevObject o) {
209 			if (o instanceof RevCommit)
210 				return ((RevCommit) o).getCommitTime();
211 			if (o instanceof RevTag) {
212 				RevTag tag = (RevTag) o;
213 				try {
214 					parseBody(tag);
215 				} catch (IOException e) {
216 					return 0;
217 				}
218 				PersonIdent who = tag.getTaggerIdent();
219 				return who != null ? who.getWhen().getTime() : 0;
220 			}
221 			return 0;
222 		}
223 
224 		int kind(Ref r) {
225 			if (r.getName().startsWith(R_TAGS))
226 				return 0;
227 			if (r.getName().startsWith(R_HEADS))
228 				return 1;
229 			if (r.getName().startsWith(R_REMOTES))
230 				return 2;
231 			return 3;
232 		}
233 	}
234 }