Skip to content

Commit 52ef487

Browse files
authored
make sure getHistoryGet() implementations for CVSRepository are aligned (#3942)
fixes #3929
1 parent 14e3630 commit 52ef487

File tree

4 files changed

+53
-33
lines changed

4 files changed

+53
-33
lines changed

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

+6-11
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@
2424
package org.opengrok.indexer.history;
2525

2626
import java.io.BufferedReader;
27-
import java.io.ByteArrayInputStream;
28-
import java.io.ByteArrayOutputStream;
2927
import java.io.File;
3028
import java.io.FileReader;
3129
import java.io.IOException;
3230
import java.io.InputStream;
31+
import java.io.OutputStream;
3332
import java.util.ArrayList;
3433
import java.util.Arrays;
3534
import java.util.List;
@@ -65,7 +64,7 @@ public CVSRepository() {
6564
* that this repository is always marked as working even though it does
6665
* not have the binary available on the system.
6766
*
68-
* Setting this to null does restores the default behavior (as java
67+
* Setting this to null restores the default behavior (as java
6968
* default for reference is null) for this repository - detecting the
7069
* binary and act upon that.
7170
*
@@ -194,22 +193,19 @@ Executor getHistoryLogExecutor(final File file) throws IOException {
194193
}
195194

196195
@Override
197-
public InputStream getHistoryGet(String parent, String basename, String rev) {
198-
InputStream ret = null;
199-
196+
boolean getHistoryGet(OutputStream out, String parent, String basename, String rev) {
200197
String revision = rev;
201-
202198
if (rev.indexOf(':') != -1) {
203199
revision = rev.substring(0, rev.indexOf(':'));
204200
}
201+
205202
try {
206203
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
207204
String[] argv = {RepoCommand, "up", "-p", "-r", revision, basename};
208205
Executor executor = new Executor(Arrays.asList(argv), new File(parent),
209206
RuntimeEnvironment.getInstance().getInteractiveCommandTimeout());
210207
executor.exec();
211208

212-
ByteArrayOutputStream out = new ByteArrayOutputStream();
213209
byte[] buffer = new byte[32 * 1024];
214210
try (InputStream in = executor.getOutputStream()) {
215211
int len;
@@ -220,13 +216,12 @@ public InputStream getHistoryGet(String parent, String basename, String rev) {
220216
}
221217
}
222218
}
223-
224-
ret = new ByteArrayInputStream(out.toByteArray());
225219
} catch (Exception exp) {
226220
LOGGER.log(Level.WARNING, "Failed to get history", exp);
221+
return false;
227222
}
228223

229-
return ret;
224+
return true;
230225
}
231226

232227
@Override

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

+3-6
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ public History getHistory(File file, boolean withFiles, boolean ui) throws Histo
319319
}
320320

321321
/**
322-
* Gets a named revision of the specified file into the specified target.
322+
* Gets a named revision of the specified file into the specified target file.
323323
*
324324
* @param target a require target file
325325
* @param parent The directory containing the file
@@ -328,12 +328,9 @@ public History getHistory(File file, boolean withFiles, boolean ui) throws Histo
328328
* @return {@code true} if content was found
329329
* @throws java.io.IOException if an I/O error occurs
330330
*/
331-
public boolean getRevision(File target, String parent, String basename,
332-
String rev) throws IOException {
333-
331+
public boolean getRevision(File target, String parent, String basename, String rev) throws IOException {
334332
Repository repo = getRepository(new File(parent));
335-
return repo != null && repo.getHistoryGet(target, parent,
336-
basename, rev);
333+
return repo != null && repo.getHistoryGet(target, parent, basename, rev);
337334
}
338335

339336
/**

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import java.util.logging.Level;
4545
import java.util.logging.Logger;
4646

47+
import org.jetbrains.annotations.Nullable;
4748
import org.opengrok.indexer.configuration.CommandTimeoutType;
4849
import org.opengrok.indexer.configuration.RuntimeEnvironment;
4950
import org.opengrok.indexer.logger.LoggerFactory;
@@ -153,7 +154,7 @@ public HistoryEntry getLastHistoryEntry(File file, boolean ui) throws HistoryExc
153154
}
154155
}
155156

156-
public Repository() {
157+
Repository() {
157158
super();
158159
ignoredFiles = new ArrayList<>();
159160
ignoredDirs = new ArrayList<>();
@@ -225,9 +226,8 @@ History getHistory(File file, String sinceRevision) throws HistoryException {
225226
* @param revision the revision we expect the oldest entry to have
226227
* @throws HistoryException if the oldest entry was not the one we expected
227228
*/
228-
void removeAndVerifyOldestChangeset(List<HistoryEntry> entries,
229-
String revision)
230-
throws HistoryException {
229+
void removeAndVerifyOldestChangeset(List<HistoryEntry> entries, String revision) throws HistoryException {
230+
231231
HistoryEntry entry = entries.isEmpty() ? null : entries.remove(entries.size() - 1);
232232

233233
// TODO We should check more thoroughly that the changeset is the one
@@ -244,7 +244,7 @@ void removeAndVerifyOldestChangeset(List<HistoryEntry> entries,
244244

245245
/**
246246
* Gets the contents of a specific version of a named file, and copies
247-
* into the specified target.
247+
* into the specified target file.
248248
*
249249
* @param target a required target file which will be overwritten
250250
* @param parent the name of the directory containing the file
@@ -267,6 +267,7 @@ public boolean getHistoryGet(File target, String parent, String basename, String
267267
* @param rev the revision to get
268268
* @return a defined instance if contents were found; or else {@code null}
269269
*/
270+
@Nullable
270271
public InputStream getHistoryGet(String parent, String basename, String rev) {
271272
ByteArrayOutputStream out = new ByteArrayOutputStream();
272273
if (getHistoryGet(out, parent, basename, rev)) {

opengrok-indexer/src/test/java/org/opengrok/indexer/history/CVSRepositoryTest.java

+38-11
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import java.io.IOException;
3131
import java.net.URISyntaxException;
3232
import java.nio.channels.FileChannel;
33+
import java.nio.file.Files;
34+
import java.nio.file.Path;
3335
import java.util.ArrayList;
3436
import java.util.Arrays;
3537
import java.util.Collections;
@@ -51,7 +53,7 @@
5153
import static org.opengrok.indexer.condition.RepositoryInstalled.Type.CVS;
5254

5355
/**
54-
*
56+
* Tests for {@link CVSRepository} functionality.
5557
* @author austvik
5658
*/
5759
@EnabledForRepository(CVS)
@@ -120,7 +122,7 @@ public static void runCvsCommand(File reposRoot, String... args) {
120122
@Test
121123
void testGetBranchNoBranch() throws Exception {
122124
setUpTestRepository();
123-
File root = new File(repository.getSourceRoot(), "cvs_test/cvsrepo");
125+
File root = Path.of(repository.getSourceRoot(), "cvs_test", "cvsrepo").toFile();
124126
CVSRepository cvsrepo = (CVSRepository) RepositoryFactory.getRepository(root);
125127
assertNull(cvsrepo.getBranch());
126128
}
@@ -135,27 +137,26 @@ void testGetBranchNoBranch() throws Exception {
135137
@Test
136138
void testNewBranch() throws Exception {
137139
setUpTestRepository();
138-
File root = new File(repository.getSourceRoot(), "cvs_test/cvsrepo");
140+
File root = Path.of(repository.getSourceRoot(), "cvs_test", "cvsrepo").toFile();
139141

140142
// Create new branch and switch to it.
141143
runCvsCommand(root, "tag", "-b", "mybranch");
142144
// Note that the 'update' command will change the entries in 'cvsroot' directory.
143145
runCvsCommand(root, "update", "-r", "mybranch");
144146

145-
// Now the repository object can be instantiated so that determineBranch()
146-
// will be called.
147+
// Now the repository object can be instantiated and determineBranch() will be called.
147148
CVSRepository cvsrepo = (CVSRepository) RepositoryFactory.getRepository(root);
148149

149150
assertEquals("mybranch", cvsrepo.getBranch());
150151

151152
// Change the content and commit.
152153
File mainC = new File(root, "main.c");
153-
FileChannel outChan = new FileOutputStream(mainC, true).getChannel();
154-
outChan.truncate(0);
155-
outChan.close();
156-
FileWriter fw = new FileWriter(mainC);
157-
fw.write("#include <foo.h>\n");
158-
fw.close();
154+
try (FileChannel outChan = new FileOutputStream(mainC, true).getChannel()) {
155+
outChan.truncate(0);
156+
}
157+
try (FileWriter fw = new FileWriter(mainC)) {
158+
fw.write("#include <foo.h>\n");
159+
}
159160
runCvsCommand(root, "commit", "-m", "change on a branch", "main.c");
160161

161162
// Check that annotation for the changed line has branch revision.
@@ -169,6 +170,32 @@ void testNewBranch() throws Exception {
169170
assertEquals("1.1", mainCHistory.getHistoryEntries().get(2).getRevision());
170171
}
171172

173+
@Test
174+
void testGetHistoryGet() throws Exception {
175+
setUpTestRepository();
176+
File root = Path.of(repository.getSourceRoot(), "cvs_test", "cvsrepo").toFile();
177+
CVSRepository cvsRepo = (CVSRepository) RepositoryFactory.getRepository(root);
178+
179+
Path repoRoot = Path.of(repository.getSourceRoot(), "cvs_test", "cvsrepo");
180+
assertTrue(repoRoot.toFile().exists());
181+
String repoFile = "main.c";
182+
String revision = "1.2";
183+
184+
File tmpFile = File.createTempFile("cvsGetHistoryGetTest", "");
185+
assertTrue(tmpFile.exists());
186+
try (FileOutputStream out = new FileOutputStream(tmpFile)) {
187+
assertTrue(cvsRepo.getHistoryGet(out, repoRoot.toString(), repoFile, revision));
188+
}
189+
190+
String revisionContents1 = new String(Files.readAllBytes(tmpFile.toPath()));
191+
tmpFile.delete();
192+
assertTrue(revisionContents1.length() > 0);
193+
194+
String revisionContents2 = new String(cvsRepo.getHistoryGet(repoRoot.toString(), repoFile, revision).
195+
readAllBytes());
196+
assertEquals(revisionContents1, revisionContents2);
197+
}
198+
172199
/**
173200
* Assert that revision strings in history entries are sorted semantically.
174201
* This is necessary for displaying revisions on a branch in correct order.

0 commit comments

Comments
 (0)