1 /*
2 * Copyright (C) 2011-2012, Robin Stocker <robin@nibor.org>
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.revwalk;
45
46 import java.io.IOException;
47 import java.util.ArrayList;
48 import java.util.Collection;
49 import java.util.List;
50
51 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
52 import org.eclipse.jgit.errors.MissingObjectException;
53 import org.eclipse.jgit.lib.Ref;
54
55 /**
56 * Utility methods for {@link RevWalk}.
57 */
58 public final class RevWalkUtils {
59
60 private RevWalkUtils() {
61 // Utility class
62 }
63
64 /**
65 * Count the number of commits that are reachable from <code>start</code>
66 * until a commit that is reachable from <code>end</code> is encountered. In
67 * other words, count the number of commits that are in <code>start</code>,
68 * but not in <code>end</code>.
69 * <p>
70 * Note that this method calls {@link RevWalk#reset()} at the beginning.
71 * Also note that the existing rev filter on the walk is left as-is, so be
72 * sure to set the right rev filter before calling this method.
73 *
74 * @param walk
75 * the rev walk to use
76 * @param start
77 * the commit to start counting from
78 * @param end
79 * the commit where counting should end, or null if counting
80 * should be done until there are no more commits
81 *
82 * @return the number of commits
83 * @throws MissingObjectException
84 * @throws IncorrectObjectTypeException
85 * @throws IOException
86 */
87 public static int count(final RevWalk walk, final RevCommit start,
88 final RevCommit end) throws MissingObjectException,
89 IncorrectObjectTypeException, IOException {
90 return find(walk, start, end).size();
91 }
92
93 /**
94 * Find commits that are reachable from <code>start</code> until a commit
95 * that is reachable from <code>end</code> is encountered. In other words,
96 * Find of commits that are in <code>start</code>, but not in
97 * <code>end</code>.
98 * <p>
99 * Note that this method calls {@link RevWalk#reset()} at the beginning.
100 * Also note that the existing rev filter on the walk is left as-is, so be
101 * sure to set the right rev filter before calling this method.
102 *
103 * @param walk
104 * the rev walk to use
105 * @param start
106 * the commit to start counting from
107 * @param end
108 * the commit where counting should end, or null if counting
109 * should be done until there are no more commits
110 * @return the commits found
111 * @throws MissingObjectException
112 * @throws IncorrectObjectTypeException
113 * @throws IOException
114 */
115 public static List<RevCommit> find(final RevWalk walk,
116 final RevCommit start, final RevCommit end)
117 throws MissingObjectException, IncorrectObjectTypeException,
118 IOException {
119 walk.reset();
120 walk.markStart(start);
121 if (end != null)
122 walk.markUninteresting(end);
123
124 List<RevCommit> commits = new ArrayList<>();
125 for (RevCommit c : walk)
126 commits.add(c);
127 return commits;
128 }
129
130 /**
131 * Find the list of branches a given commit is reachable from when following
132 * parent.s
133 * <p>
134 * Note that this method calls {@link RevWalk#reset()} at the beginning.
135 * <p>
136 * In order to improve performance this method assumes clock skew among
137 * committers is never larger than 24 hours.
138 *
139 * @param commit
140 * the commit we are looking at
141 * @param revWalk
142 * The RevWalk to be used.
143 * @param refs
144 * the set of branches we want to see reachability from
145 * @return the list of branches a given commit is reachable from
146 * @throws MissingObjectException
147 * @throws IncorrectObjectTypeException
148 * @throws IOException
149 */
150 public static List<Ref> findBranchesReachableFrom(RevCommit commit,
151 RevWalk revWalk, Collection<Ref> refs)
152 throws MissingObjectException, IncorrectObjectTypeException,
153 IOException {
154
155 // Make sure commit is from the same RevWalk
156 commit = revWalk.parseCommit(commit.getId());
157 revWalk.reset();
158 List<Ref> result = new ArrayList<>();
159
160 final int SKEW = 24*3600; // one day clock skew
161
162 for (Ref ref : refs) {
163 RevObject maybehead = revWalk.parseAny(ref.getObjectId());
164 if (!(maybehead instanceof RevCommit))
165 continue;
166 RevCommit headCommit = (RevCommit) maybehead;
167
168 // if commit is in the ref branch, then the tip of ref should be
169 // newer than the commit we are looking for. Allow for a large
170 // clock skew.
171 if (headCommit.getCommitTime() + SKEW < commit.getCommitTime())
172 continue;
173
174 if (revWalk.isMergedInto(commit, headCommit))
175 result.add(ref);
176 }
177 return result;
178 }
179
180 }