Skip to content

Commit bddbeb5

Browse files
Vladimir Kotalahornace
authored andcommitted
initial changes for Mercurial per partes history
1 parent 5d92647 commit bddbeb5

File tree

3 files changed

+100
-19
lines changed

3 files changed

+100
-19
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/history/MercurialHistoryParser.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ class MercurialHistoryParser implements Executor.StreamHandler {
6868
* specified one.
6969
*
7070
* @param file the file or directory to get history for
71-
* @param changeset the changeset right before the first one to fetch, or
71+
* @param sinceRevision the changeset right before the first one to fetch, or
7272
* {@code null} if all changesets should be fetched
7373
* @return history for the specified file or directory
7474
* @throws HistoryException if an error happens when parsing the history
7575
*/
76-
History parse(File file, String changeset) throws HistoryException {
76+
History parse(File file, String sinceRevision, String tillRevision) throws HistoryException {
7777
isDir = file.isDirectory();
7878
try {
79-
Executor executor = repository.getHistoryLogExecutor(file, changeset);
79+
Executor executor = repository.getHistoryLogExecutor(file, sinceRevision, tillRevision,false);
8080
int status = executor.exec(true, this);
8181

8282
if (status != 0) {
@@ -93,8 +93,8 @@ History parse(File file, String changeset) throws HistoryException {
9393
// from the list, since only the ones following it should be returned.
9494
// Also check that the specified changeset was found, otherwise throw
9595
// an exception.
96-
if (changeset != null) {
97-
repository.removeAndVerifyOldestChangeset(entries, changeset);
96+
if (sinceRevision != null) {
97+
repository.removeAndVerifyOldestChangeset(entries, sinceRevision);
9898
}
9999

100100
return new History(entries, renamedFiles);
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
22+
*/
23+
package org.opengrok.indexer.history;
24+
25+
import org.opengrok.indexer.util.Executor;
26+
27+
import java.io.BufferedReader;
28+
import java.io.File;
29+
import java.io.IOException;
30+
import java.io.InputStream;
31+
import java.io.InputStreamReader;
32+
import java.util.function.Consumer;
33+
34+
class MercurialHistoryParserRevisionsOnly implements Executor.StreamHandler {
35+
private final MercurialRepository repository;
36+
private final Consumer<String> visitor;
37+
38+
MercurialHistoryParserRevisionsOnly(MercurialRepository repository, Consumer<String> visitor) {
39+
this.repository = repository;
40+
this.visitor = visitor;
41+
}
42+
43+
void parse(File file, String sinceRevision) throws HistoryException {
44+
try {
45+
Executor executor = repository.getHistoryLogExecutor(file, sinceRevision, null, true);
46+
int status = executor.exec(true, this);
47+
48+
if (status != 0) {
49+
throw new HistoryException("Failed to get revisions for: \"" +
50+
file.getAbsolutePath() +
51+
"\" Exit code: " + status); // TODO log sinceRevision
52+
}
53+
} catch (IOException e) {
54+
throw new HistoryException("Failed to get history for: \"" +
55+
file.getAbsolutePath() + "\"", e);
56+
}
57+
}
58+
59+
@Override
60+
public void processStream(InputStream input) throws IOException {
61+
BufferedReader in = new BufferedReader(new InputStreamReader(input));
62+
String s;
63+
while ((s = in.readLine()) != null) {
64+
visitor.accept(s);
65+
}
66+
}
67+
}

opengrok-indexer/src/main/java/org/opengrok/indexer/history/MercurialRepository.java

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.HashMap;
3434
import java.util.List;
3535
import java.util.TreeSet;
36+
import java.util.function.Consumer;
3637
import java.util.function.Supplier;
3738
import java.util.logging.Level;
3839
import java.util.logging.Logger;
@@ -50,7 +51,7 @@
5051
* Access to a Mercurial repository.
5152
*
5253
*/
53-
public class MercurialRepository extends Repository {
54+
public class MercurialRepository extends RepositoryWithPerPartesHistory {
5455

5556
private static final Logger LOGGER = LoggerFactory.getLogger(MercurialRepository.class);
5657

@@ -81,6 +82,7 @@ public class MercurialRepository extends Repository {
8182
static final String END_OF_ENTRY
8283
= "mercurial_history_end_of_entry";
8384

85+
private static final String TEMPLATE_REVS = "{rev}\\n";
8486
private static final String TEMPLATE_STUB
8587
= CHANGESET + "{rev}:{node|short}\\n"
8688
+ USER + "{author}\\n" + DATE + "{date|isodate}\\n"
@@ -151,12 +153,14 @@ String determineBranch(CommandTimeoutType cmdType) throws IOException {
151153
* @param sinceRevision the oldest changeset to return from the executor, or
152154
* {@code null} if all changesets should be returned.
153155
* For files this does not apply and full history is returned.
156+
* @param tillRevision end revision
157+
* @param revisionsOnly get only revision numbers
154158
* @return An Executor ready to be started
155159
*/
156-
Executor getHistoryLogExecutor(File file, String sinceRevision)
160+
Executor getHistoryLogExecutor(File file, String sinceRevision, String tillRevision, boolean revisionsOnly)
157161
throws HistoryException, IOException {
162+
158163
String filename = getRepoRelativePath(file);
159-
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
160164

161165
List<String> cmd = new ArrayList<>();
162166
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
@@ -166,15 +170,14 @@ Executor getHistoryLogExecutor(File file, String sinceRevision)
166170
if (file.isDirectory()) {
167171
// If this is non-default branch we would like to get the changesets
168172
// on that branch and also follow any changesets from the parent branch.
173+
// TODO tillRevision
169174
if (sinceRevision != null) {
170175
cmd.add("-r");
171176
String[] parts = sinceRevision.split(":");
172177
if (parts.length == 2) {
173178
cmd.add("reverse(" + parts[0] + "::'" + getBranch() + "')");
174179
} else {
175-
throw new HistoryException(
176-
"Don't know how to parse changeset identifier: "
177-
+ sinceRevision);
180+
throw new HistoryException("Don't know how to parse changeset identifier: " + sinceRevision);
178181
}
179182
} else {
180183
cmd.add("-r");
@@ -197,10 +200,14 @@ Executor getHistoryLogExecutor(File file, String sinceRevision)
197200
}
198201

199202
cmd.add("--template");
200-
if (file.isDirectory()) {
201-
cmd.add(this.isHandleRenamedFiles() ? DIR_TEMPLATE_RENAMED : DIR_TEMPLATE);
203+
if (revisionsOnly) {
204+
cmd.add(TEMPLATE_REVS);
202205
} else {
203-
cmd.add(FILE_TEMPLATE);
206+
if (file.isDirectory()) {
207+
cmd.add(this.isHandleRenamedFiles() ? DIR_TEMPLATE_RENAMED : DIR_TEMPLATE);
208+
} else {
209+
cmd.add(FILE_TEMPLATE);
210+
}
204211
}
205212

206213
if (!filename.isEmpty()) {
@@ -259,7 +266,7 @@ private HistoryRevResult getHistoryRev(
259266
* @param fullpath file path
260267
* @param full_rev_to_find revision number (in the form of
261268
* {rev}:{node|short})
262-
* @returns original filename
269+
* @return original filename
263270
*/
264271
private String findOriginalName(String fullpath, String full_rev_to_find)
265272
throws IOException {
@@ -511,9 +518,17 @@ History getHistory(File file) throws HistoryException {
511518
return getHistory(file, null);
512519
}
513520

521+
public void accept(String sinceRevision, Consumer<String> visitor) throws HistoryException {
522+
new MercurialHistoryParserRevisionsOnly(this, visitor).
523+
parse(new File(getDirectoryName()), sinceRevision);
524+
}
525+
526+
History getHistory(File file, String sinceRevision) throws HistoryException {
527+
return getHistory(file, sinceRevision, null);
528+
}
529+
514530
@Override
515-
History getHistory(File file, String sinceRevision)
516-
throws HistoryException {
531+
History getHistory(File file, String sinceRevision, String tillRevision) throws HistoryException {
517532
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
518533
// Note that the filtering of revisions based on sinceRevision is done
519534
// in the history log executor by passing appropriate options to
@@ -522,8 +537,7 @@ History getHistory(File file, String sinceRevision)
522537
// for file, the file is renamed and its complete history is fetched
523538
// so no sinceRevision filter is needed.
524539
// See findOriginalName() code for more details.
525-
History result = new MercurialHistoryParser(this).parse(file,
526-
sinceRevision);
540+
History result = new MercurialHistoryParser(this).parse(file, sinceRevision, tillRevision);
527541

528542
// Assign tags to changesets they represent.
529543
// We don't need to check if this repository supports tags,

0 commit comments

Comments
 (0)