Skip to content

Commit 4285cca

Browse files
author
Vladimir Kotal
committed
convert GitRepository#GetHistoryGet() to JGit
1 parent dadacd3 commit 4285cca

16 files changed

+126
-119
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2018, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opengrok.indexer.history;
2525

2626
import java.io.BufferedReader;
2727
import java.io.File;
2828
import java.io.IOException;
29+
import java.io.OutputStream;
2930
import java.nio.file.Files;
3031
import java.nio.file.Paths;
3132
import java.nio.file.Path;
@@ -38,7 +39,6 @@
3839
import org.opengrok.indexer.configuration.CommandTimeoutType;
3940
import org.opengrok.indexer.configuration.RuntimeEnvironment;
4041
import org.opengrok.indexer.logger.LoggerFactory;
41-
import org.opengrok.indexer.util.BufferSink;
4242
import org.opengrok.indexer.util.Executor;
4343

4444
/**
@@ -164,8 +164,7 @@ Executor getHistoryLogExecutor(File file) throws IOException {
164164
}
165165

166166
@Override
167-
boolean getHistoryGet(
168-
BufferSink sink, String parent, String basename, String rev) {
167+
boolean getHistoryGet(OutputStream out, String parent, String basename, String rev) {
169168

170169
ArrayList<String> cmd = new ArrayList<>();
171170
File directory = new File(parent);
@@ -216,7 +215,7 @@ boolean getHistoryGet(
216215
executor = new Executor(cmd, directory);
217216
executor.exec();
218217
try {
219-
copyBytes(sink, executor.getOutputStream());
218+
copyBytes(out::write, executor.getOutputStream());
220219
return true;
221220
} catch (IOException e) {
222221
LOGGER.log(Level.SEVERE, "Failed to obtain content for {0}",

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2017, 2018, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opengrok.indexer.history;
2525

2626
import java.io.File;
2727
import java.io.IOException;
28+
import java.io.OutputStream;
2829
import java.util.ArrayList;
2930
import java.util.List;
3031
import java.util.TreeSet;
@@ -34,7 +35,6 @@
3435
import org.opengrok.indexer.configuration.CommandTimeoutType;
3536
import org.opengrok.indexer.configuration.RuntimeEnvironment;
3637
import org.opengrok.indexer.logger.LoggerFactory;
37-
import org.opengrok.indexer.util.BufferSink;
3838
import org.opengrok.indexer.util.Executor;
3939

4040
/**
@@ -96,8 +96,7 @@ Executor getHistoryLogExecutor(final File file, final String sinceRevision)
9696
}
9797

9898
@Override
99-
boolean getHistoryGet(
100-
BufferSink sink, String parent, String basename, String rev) {
99+
boolean getHistoryGet(OutputStream out, String parent, String basename, String rev) {
101100

102101
File directory = new File(getDirectoryName());
103102
Process process = null;
@@ -107,7 +106,7 @@ boolean getHistoryGet(
107106
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
108107
String[] argv = {RepoCommand, "cat", "-r", rev, filename};
109108
process = Runtime.getRuntime().exec(argv, null, directory);
110-
copyBytes(sink, process.getInputStream());
109+
copyBytes(out::write, process.getInputStream());
111110
return true;
112111
} catch (Exception exp) {
113112
LOGGER.log(Level.SEVERE,

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,21 @@
1919

2020
/*
2121
* Copyright (c) 2017, James Service <[email protected]>
22-
* Portions Copyright (c) 2017, 2020, Oracle and/or its affiliates.
22+
* Portions Copyright (c) 2017, 2021, Oracle and/or its affiliates.
2323
* Portions Copyright (c) 2018, Chris Fraire <[email protected]>.
2424
*/
2525
package org.opengrok.indexer.history;
2626

2727
import java.io.File;
2828
import java.io.IOException;
29+
import java.io.OutputStream;
2930
import java.util.ArrayList;
3031
import java.util.logging.Level;
3132
import java.util.logging.Logger;
3233
import java.util.regex.Matcher;
3334
import java.util.regex.Pattern;
3435

3536
import org.opengrok.indexer.configuration.CommandTimeoutType;
36-
import org.opengrok.indexer.util.BufferSink;
3737
import org.suigeneris.jrcs.rcs.InvalidVersionNumberException;
3838
import org.suigeneris.jrcs.rcs.Version;
3939
import org.opengrok.indexer.configuration.RuntimeEnvironment;
@@ -300,8 +300,7 @@ History getHistory(File file, String sinceRevision) throws HistoryException {
300300
}
301301

302302
@Override
303-
boolean getHistoryGet(
304-
BufferSink sink, String parent, String basename, String revision) {
303+
boolean getHistoryGet(OutputStream out, String parent, String basename, String revision) {
305304

306305
final File directory = new File(parent).getAbsoluteFile();
307306
final ArrayList<String> argv = new ArrayList<String>();
@@ -322,7 +321,7 @@ boolean getHistoryGet(
322321
}
323322

324323
try {
325-
copyBytes(sink, executor.getOutputStream());
324+
copyBytes(out::write, executor.getOutputStream());
326325
return true;
327326
} catch (IOException e) {
328327
LOGGER.log(Level.SEVERE, "Failed to get content for {0}",

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2017, 2020, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opengrok.indexer.history;
2525

2626
import java.io.File;
2727
import java.io.FileInputStream;
2828
import java.io.IOException;
29+
import java.io.OutputStream;
2930
import java.util.ArrayList;
3031
import java.util.Arrays;
3132
import java.util.List;
@@ -36,7 +37,6 @@
3637
import org.opengrok.indexer.configuration.CommandTimeoutType;
3738
import org.opengrok.indexer.configuration.RuntimeEnvironment;
3839
import org.opengrok.indexer.logger.LoggerFactory;
39-
import org.opengrok.indexer.util.BufferSink;
4040
import org.opengrok.indexer.util.Executor;
4141

4242
/**
@@ -89,8 +89,7 @@ Executor getHistoryLogExecutor(final File file) throws IOException {
8989
}
9090

9191
@Override
92-
boolean getHistoryGet(
93-
BufferSink sink, String parent, String basename, String rev) {
92+
boolean getHistoryGet(OutputStream out, String parent, String basename, String rev) {
9493

9594
File directory = new File(getDirectoryName());
9695

@@ -119,7 +118,7 @@ boolean getHistoryGet(
119118
}
120119

121120
try (FileInputStream in = new FileInputStream(tmp)) {
122-
copyBytes(sink, in);
121+
copyBytes(out::write, in);
123122
} finally {
124123
// delete the temporary file on close
125124
if (!tmp.delete()) {

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

Lines changed: 62 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.io.IOException;
3030
import java.io.InputStream;
3131
import java.io.InputStreamReader;
32+
import java.io.OutputStream;
3233
import java.io.Reader;
3334
import java.nio.charset.StandardCharsets;
3435
import java.nio.file.Paths;
@@ -53,16 +54,21 @@
5354
import org.eclipse.jgit.api.Git;
5455
import org.eclipse.jgit.api.errors.GitAPIException;
5556
import org.eclipse.jgit.lib.Constants;
57+
import org.eclipse.jgit.lib.ObjectId;
58+
import org.eclipse.jgit.lib.ObjectLoader;
5659
import org.eclipse.jgit.lib.ObjectReader;
5760
import org.eclipse.jgit.lib.Ref;
5861
import org.eclipse.jgit.revwalk.RevCommit;
62+
import org.eclipse.jgit.revwalk.RevTree;
5963
import org.eclipse.jgit.revwalk.RevWalk;
6064
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
65+
import org.eclipse.jgit.treewalk.TreeWalk;
66+
import org.eclipse.jgit.treewalk.filter.PathFilter;
67+
import org.eclipse.jgit.util.io.CountingOutputStream;
6168
import org.jetbrains.annotations.NotNull;
6269
import org.opengrok.indexer.configuration.CommandTimeoutType;
6370
import org.opengrok.indexer.configuration.RuntimeEnvironment;
6471
import org.opengrok.indexer.logger.LoggerFactory;
65-
import org.opengrok.indexer.util.BufferSink;
6672
import org.opengrok.indexer.util.Executor;
6773
import org.opengrok.indexer.util.HeadHandler;
6874
import org.opengrok.indexer.util.LazilyInstantiate;
@@ -224,57 +230,74 @@ Executor getRenamedFilesExecutor(final File file, String sinceRevision) throws I
224230
/**
225231
* Try to get file contents for given revision.
226232
*
227-
* @param sink a required target sink
233+
* @param out a required OutputStream
228234
* @param fullpath full pathname of the file
229-
* @param rev revision
235+
* @param rev revision string
230236
* @return a defined instance with {@code success} == {@code true} if no
231-
* error occurred and with non-zero {@code iterations} if some data was
232-
* transferred
237+
* error occurred and with non-zero {@code iterations} if some data was transferred
233238
*/
234-
private HistoryRevResult getHistoryRev(
235-
BufferSink sink, String fullpath, String rev) {
239+
private HistoryRevResult getHistoryRev(OutputStream out, String fullpath, String rev) {
236240

237241
HistoryRevResult result = new HistoryRevResult();
238242
File directory = new File(getDirectoryName());
243+
244+
/*
245+
* Be careful, git uses only forward slashes in its command and output (not in file path).
246+
* Using backslashes together with git show will get empty output and 0 status code.
247+
*/
248+
String filename;
249+
result.success = false;
239250
try {
240-
/*
241-
* Be careful, git uses only forward slashes in its command and output (not in file path).
242-
* Using backslashes together with git show will get empty output and 0 status code.
243-
*/
244-
String filename = Paths.get(getCanonicalDirectoryName()).relativize(
251+
filename = Paths.get(getCanonicalDirectoryName()).relativize(
245252
Paths.get(fullpath)).toString().replace(File.separatorChar, '/');
246-
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
247-
String[] argv = {
248-
RepoCommand,
249-
"show",
250-
rev + ":" + filename
251-
};
253+
} catch (IOException e) {
254+
LOGGER.log(Level.WARNING, String.format("Failed to relativize '%s' in for repository '%s'",
255+
fullpath, directory), e);
256+
return result;
257+
}
252258

253-
Executor executor = new Executor(Arrays.asList(argv), directory,
254-
RuntimeEnvironment.getInstance().getInteractiveCommandTimeout());
255-
int status = executor.exec();
256-
result.iterations = copyBytes(sink, executor.getOutputStream());
259+
try (org.eclipse.jgit.lib.Repository repository = getJGitRepository(directory.getAbsolutePath())) {
260+
ObjectId commitId = repository.resolve(rev);
261+
262+
// a RevWalk allows to walk over commits based on some filtering that is defined
263+
try (RevWalk revWalk = new RevWalk(repository)) {
264+
RevCommit commit = revWalk.parseCommit(commitId);
265+
// and using commit's tree find the path
266+
RevTree tree = commit.getTree();
267+
268+
// now try to find a specific file
269+
try (TreeWalk treeWalk = new TreeWalk(repository)) {
270+
treeWalk.addTree(tree);
271+
treeWalk.setRecursive(true);
272+
treeWalk.setFilter(PathFilter.create(filename));
273+
if (!treeWalk.next()) {
274+
LOGGER.log(Level.FINEST,
275+
String.format("Did not find expected file '%s' in revision %s for repository '%s'",
276+
filename, rev, directory));
277+
return result;
278+
}
257279

258-
/*
259-
* If exit value of the process was not 0 then the file did
260-
* not exist or internal git error occured.
261-
*/
262-
result.success = (status == 0);
263-
} catch (Exception exception) {
264-
LOGGER.log(Level.SEVERE,
265-
String.format(
266-
"Failed to get history for file %s in revision %s:",
267-
fullpath, rev
268-
),
269-
exception
270-
);
280+
ObjectId objectId = treeWalk.getObjectId(0);
281+
ObjectLoader loader = repository.open(objectId);
282+
283+
CountingOutputStream countingOutputStream = new CountingOutputStream(out);
284+
loader.copyTo(countingOutputStream);
285+
result.iterations = countingOutputStream.getCount();
286+
result.success = true;
287+
}
288+
289+
revWalk.dispose();
290+
}
291+
} catch (IOException e) {
292+
LOGGER.log(Level.WARNING, String.format("Failed to get file '%s' in revision %s for repository '%s'",
293+
filename, rev, directory), e);
271294
}
295+
272296
return result;
273297
}
274298

275299
@Override
276-
boolean getHistoryGet(
277-
BufferSink sink, String parent, String basename, String rev) {
300+
boolean getHistoryGet(OutputStream out, String parent, String basename, String rev) {
278301

279302
String fullpath;
280303
try {
@@ -285,7 +308,7 @@ boolean getHistoryGet(
285308
return false;
286309
}
287310

288-
HistoryRevResult result = getHistoryRev(sink::write, fullpath, rev);
311+
HistoryRevResult result = getHistoryRev(out, fullpath, rev);
289312
if (!result.success && result.iterations < 1) {
290313
/*
291314
* If we failed to get the contents it might be that the file was
@@ -312,7 +335,7 @@ boolean getHistoryGet(
312335
return false;
313336
}
314337
if (!fullRenamedPath.equals(fullpath)) {
315-
result = getHistoryRev(sink, fullRenamedPath, rev);
338+
result = getHistoryRev(out, fullRenamedPath, rev);
316339
}
317340
}
318341
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.File;
2828
import java.io.IOException;
2929
import java.io.InputStreamReader;
30+
import java.io.OutputStream;
3031
import java.util.ArrayList;
3132
import java.util.Arrays;
3233
import java.util.HashMap;
@@ -350,8 +351,7 @@ private String findOriginalName(String fullpath, String full_rev_to_find)
350351
}
351352

352353
@Override
353-
boolean getHistoryGet(
354-
BufferSink sink, String parent, String basename, String rev) {
354+
boolean getHistoryGet(OutputStream out, String parent, String basename, String rev) {
355355

356356
String fullpath;
357357
try {
@@ -362,7 +362,7 @@ boolean getHistoryGet(
362362
return false;
363363
}
364364

365-
HistoryRevResult result = getHistoryRev(sink::write, fullpath, rev);
365+
HistoryRevResult result = getHistoryRev(out::write, fullpath, rev);
366366
if (!result.success && result.iterations < 1) {
367367
/*
368368
* If we failed to get the contents it might be that the file was
@@ -379,7 +379,7 @@ boolean getHistoryGet(
379379
return false;
380380
}
381381
if (origpath != null && !origpath.equals(fullpath)) {
382-
result = getHistoryRev(sink, origpath, rev);
382+
result = getHistoryRev(out::write, origpath, rev);
383383
}
384384
}
385385

0 commit comments

Comments
 (0)