View Javadoc
1   /*
2    * Copyright (C) 2009, Mark Struberg <struberg@yahoo.de>
3    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v. 1.0 which is available at
7    * https://www.eclipse.org/org/documents/edl-v10.php.
8    *
9    * SPDX-License-Identifier: BSD-3-Clause
10   */
11  
12  package org.eclipse.jgit.revwalk.filter;
13  
14  import java.io.IOException;
15  import java.util.Date;
16  
17  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
18  import org.eclipse.jgit.errors.MissingObjectException;
19  import org.eclipse.jgit.errors.StopWalkException;
20  import org.eclipse.jgit.revwalk.RevCommit;
21  import org.eclipse.jgit.revwalk.RevWalk;
22  
23  /**
24   * Selects commits based upon the commit time field.
25   */
26  public abstract class CommitTimeRevFilter extends RevFilter {
27  	/**
28  	 * Create a new filter to select commits before a given date/time.
29  	 *
30  	 * @param ts
31  	 *            the point in time to cut on.
32  	 * @return a new filter to select commits on or before <code>ts</code>.
33  	 */
34  	public static final RevFilter before(Date ts) {
35  		return before(ts.getTime());
36  	}
37  
38  	/**
39  	 * Create a new filter to select commits before a given date/time.
40  	 *
41  	 * @param ts
42  	 *            the point in time to cut on, in milliseconds
43  	 * @return a new filter to select commits on or before <code>ts</code>.
44  	 */
45  	public static final RevFilter before(long ts) {
46  		return new Before(ts);
47  	}
48  
49  	/**
50  	 * Create a new filter to select commits after a given date/time.
51  	 *
52  	 * @param ts
53  	 *            the point in time to cut on.
54  	 * @return a new filter to select commits on or after <code>ts</code>.
55  	 */
56  	public static final RevFilter after(Date ts) {
57  		return after(ts.getTime());
58  	}
59  
60  	/**
61  	 * Create a new filter to select commits after a given date/time.
62  	 *
63  	 * @param ts
64  	 *            the point in time to cut on, in milliseconds.
65  	 * @return a new filter to select commits on or after <code>ts</code>.
66  	 */
67  	public static final RevFilter after(long ts) {
68  		return new After(ts);
69  	}
70  
71  	/**
72  	 * Create a new filter to select commits after or equal a given date/time <code>since</code>
73  	 * and before or equal a given date/time <code>until</code>.
74  	 *
75  	 * @param since the point in time to cut on.
76  	 * @param until the point in time to cut off.
77  	 * @return a new filter to select commits between the given date/times.
78  	 */
79  	public static final RevFilter between(Date since, Date until) {
80  		return between(since.getTime(), until.getTime());
81  	}
82  
83  	/**
84  	 * Create a new filter to select commits after or equal a given date/time <code>since</code>
85  	 * and before or equal a given date/time <code>until</code>.
86  	 *
87  	 * @param since the point in time to cut on, in milliseconds.
88  	 * @param until the point in time to cut off, in millisconds.
89  	 * @return a new filter to select commits between the given date/times.
90  	 */
91  	public static final RevFilter between(long since, long until) {
92  		return new Between(since, until);
93  	}
94  
95  	final int when;
96  
97  	CommitTimeRevFilter(long ts) {
98  		when = (int) (ts / 1000);
99  	}
100 
101 	/** {@inheritDoc} */
102 	@Override
103 	public RevFilter clone() {
104 		return this;
105 	}
106 
107 	/** {@inheritDoc} */
108 	@Override
109 	public boolean requiresCommitBody() {
110 		return false;
111 	}
112 
113 	private static class Before extends CommitTimeRevFilter {
114 		Before(long ts) {
115 			super(ts);
116 		}
117 
118 		@Override
119 		public boolean include(RevWalk walker, RevCommit cmit)
120 				throws StopWalkException, MissingObjectException,
121 				IncorrectObjectTypeException, IOException {
122 			return cmit.getCommitTime() <= when;
123 		}
124 
125 		@SuppressWarnings("nls")
126 		@Override
127 		public String toString() {
128 			return super.toString() + "(" + new Date(when * 1000L) + ")";
129 		}
130 	}
131 
132 	private static class After extends CommitTimeRevFilter {
133 		After(long ts) {
134 			super(ts);
135 		}
136 
137 		@Override
138 		public boolean include(RevWalk walker, RevCommit cmit)
139 				throws StopWalkException, MissingObjectException,
140 				IncorrectObjectTypeException, IOException {
141 			// Since the walker sorts commits by commit time we can be
142 			// reasonably certain there is nothing remaining worth our
143 			// scanning if this commit is before the point in question.
144 			//
145 			if (cmit.getCommitTime() < when)
146 				throw StopWalkException.INSTANCE;
147 			return true;
148 		}
149 
150 		@SuppressWarnings("nls")
151 		@Override
152 		public String toString() {
153 			return super.toString() + "(" + new Date(when * 1000L) + ")";
154 		}
155 	}
156 
157 	private static class Between extends CommitTimeRevFilter {
158 		private final int until;
159 
160 		Between(long since, long until) {
161 			super(since);
162 			this.until = (int) (until / 1000);
163 		}
164 
165 		@Override
166 		public boolean include(RevWalk walker, RevCommit cmit)
167 				throws StopWalkException, MissingObjectException,
168 				IncorrectObjectTypeException, IOException {
169 			return cmit.getCommitTime() <= until && cmit.getCommitTime() >= when;
170 		}
171 
172 		@SuppressWarnings("nls")
173 		@Override
174 		public String toString() {
175 			return super.toString() + "(" + new Date(when * 1000L) + " - "
176 					+ new Date(until * 1000L) + ")";
177 		}
178 
179 	}
180 
181 }