Skip to content

Commit 539fec5

Browse files
committed
make CVSRepository override determineBranch()
add basic branch test
1 parent b537560 commit 539fec5

File tree

3 files changed

+137
-33
lines changed

3 files changed

+137
-33
lines changed

src/org/opensolaris/opengrok/history/CVSRepository.java

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,8 @@ public class CVSRepository extends RCSRepository {
5757
*/
5858
public static final String CMD_FALLBACK = "cvs";
5959

60-
private Boolean isBranch = null;
61-
private String branch = null;
62-
6360
/**
64-
* Pattern used to extract author/revision from cvs annotate.
61+
* Pattern used to extract author/revision from 'cvs annotate'.
6562
*/
6663
private static final Pattern ANNOTATE_PATTERN
6764
= Pattern.compile("([\\.\\d]+)\\W+\\((\\w+)");
@@ -165,6 +162,31 @@ public void update() throws IOException {
165162
}
166163
}
167164

165+
@Override
166+
String determineBranch() throws IOException {
167+
String branch = null;
168+
169+
File tagFile = new File(getDirectoryName(), "CVS/Tag");
170+
if (tagFile.isFile()) {
171+
try (BufferedReader br = new BufferedReader(new FileReader(tagFile))) {
172+
String line = br.readLine();
173+
if (line != null) {
174+
branch = line.substring(1);
175+
}
176+
} catch (IOException ex) {
177+
LOGGER.log(Level.WARNING,
178+
"Failed to work with CVS/Tag file of {0}",
179+
getDirectoryName() + ": " + ex.getClass().toString());
180+
} catch (Exception exp) {
181+
LOGGER.log(Level.WARNING,
182+
"Failed to get revision tag of {0}",
183+
getDirectoryName() + ": " + exp.getClass().toString());
184+
}
185+
}
186+
187+
return branch;
188+
}
189+
168190
/**
169191
* Get an executor to be used for retrieving the history log for the named
170192
* file.
@@ -184,30 +206,7 @@ Executor getHistoryLogExecutor(final File file) throws IOException {
184206
cmd.add(RepoCommand);
185207
cmd.add("log");
186208

187-
if (isBranch == null) {
188-
File tagFile = new File(getDirectoryName(), "CVS/Tag");
189-
if (tagFile.isFile()) {
190-
isBranch = Boolean.TRUE;
191-
try (BufferedReader br = new BufferedReader(new FileReader(tagFile))) {
192-
String line = br.readLine();
193-
if (line != null) {
194-
branch = line.substring(1);
195-
}
196-
} catch (IOException ex) {
197-
LOGGER.log(Level.WARNING,
198-
"Failed to work with CVS/Tag file of {0}",
199-
getDirectoryName() + ": " + ex.getClass().toString());
200-
} catch (Exception exp) {
201-
LOGGER.log(Level.WARNING,
202-
"Failed to get revision tag of {0}",
203-
getDirectoryName() + ": " + exp.getClass().toString());
204-
}
205-
} else {
206-
isBranch = Boolean.FALSE;
207-
}
208-
}
209-
210-
if (isBranch.equals(Boolean.TRUE) && branch != null && !branch.isEmpty()) {
209+
if (getBranch() != null && !getBranch().isEmpty()) {
211210
// Just generate THIS branch history, we don't care about the other
212211
// branches which are not checked out.
213212
cmd.add("-r" + branch);
@@ -220,6 +219,7 @@ Executor getHistoryLogExecutor(final File file) throws IOException {
220219
if (filename.length() > 0) {
221220
cmd.add(filename);
222221
}
222+
223223
return new Executor(cmd, new File(getDirectoryName()));
224224
}
225225

src/org/opensolaris/opengrok/history/RCSRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ String determineParent() throws IOException {
180180
}
181181

182182
@Override
183-
String determineBranch() {
183+
String determineBranch() throws IOException {
184184
return null;
185185
}
186186
}

test/org/opensolaris/opengrok/history/CVSRepositoryTest.java

Lines changed: 108 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,19 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opensolaris.opengrok.history;
2424

25+
import java.io.File;
26+
import java.io.FileOutputStream;
27+
import java.io.FileWriter;
28+
import java.io.IOException;
2529
import java.io.Reader;
2630
import java.io.StringReader;
31+
import java.nio.channels.FileChannel;
32+
import java.util.ArrayList;
33+
import java.util.List;
2734
import org.junit.After;
2835
import org.junit.AfterClass;
2936
import org.junit.Before;
@@ -35,6 +42,8 @@
3542
import org.opensolaris.opengrok.condition.RepositoryInstalled;
3643

3744
import static org.junit.Assert.*;
45+
import org.opensolaris.opengrok.util.Executor;
46+
import org.opensolaris.opengrok.util.TestRepository;
3847

3948
/**
4049
*
@@ -59,14 +68,109 @@ public static void setUpClass() throws Exception {
5968
public static void tearDownClass() throws Exception {
6069
}
6170

62-
@Before
63-
public void setUp() {
64-
instance = new CVSRepository();
71+
private TestRepository repository;
72+
73+
/**
74+
* Set up a test repository. Should be called by the tests that need it. The
75+
* test repository will be destroyed automatically when the test finishes.
76+
*/
77+
private void setUpTestRepository() throws IOException {
78+
repository = new TestRepository();
79+
repository.create(getClass().getResourceAsStream("repositories.zip"));
80+
81+
// Fix the CVS/Root so that it points to the temporary directory
82+
// rather than the workspace directory.
83+
// Normally this would be bad idea since every subdirectory includes
84+
// CVS/Root file however in this case there is just one such file
85+
// as the repository is flat in terms of directory structure.
86+
// This is done so that 'cvs update' does not change the "upstream"
87+
// cvsroot directory entries.
88+
// The alternative would be to checkout cvsrepo from cvsroot.
89+
// XXX no proper path separators + newline char below
90+
File root = new File(repository.getSourceRoot(), "cvs_test/cvsrepo/CVS/Root");
91+
if (root.isFile()) {
92+
FileChannel outChan = new FileOutputStream(root, true).getChannel();
93+
outChan.truncate(0);
94+
outChan.close();
95+
}
96+
FileWriter fw = new FileWriter(root);
97+
fw.write(repository.getSourceRoot() + "/cvs_test/cvsroot\n");
98+
fw.close();
6599
}
66100

67101
@After
68102
public void tearDown() {
69103
instance = null;
104+
105+
if (repository != null) {
106+
repository.destroy();
107+
repository = null;
108+
}
109+
}
110+
111+
@Before
112+
public void setUp() {
113+
instance = new CVSRepository();
114+
}
115+
116+
/**
117+
* Run the 'cvs' command.
118+
*
119+
* @param reposRoot directory of the repository root
120+
* @param command command to run
121+
* @param arg argument to use for the command
122+
*/
123+
private static void runCvsCommand(File reposRoot, String command, String ... args) {
124+
List<String> cmdargs = new ArrayList<>();
125+
cmdargs.add(CVSRepository.CMD_FALLBACK);
126+
cmdargs.add(command);
127+
for (String arg: args) {
128+
cmdargs.add(arg);
129+
}
130+
Executor exec = new Executor(cmdargs, reposRoot);
131+
int exitCode = exec.exec();
132+
if (exitCode != 0) {
133+
fail("cvs " + command + " failed."
134+
+ "\nexit code: " + exitCode
135+
+ "\nstdout:\n" + exec.getOutputString()
136+
+ "\nstderr:\n" + exec.getErrorString());
137+
}
138+
}
139+
140+
/**
141+
* Get the CVS repository, test that getBranch() returns null if there is
142+
* no branch.
143+
* @throws Exception
144+
*/
145+
@Test
146+
public void testGetBranchNoBranch() throws Exception {
147+
setUpTestRepository();
148+
File root = new File(repository.getSourceRoot(), "cvs_test/cvsrepo");
149+
CVSRepository cvsrepo
150+
= (CVSRepository) RepositoryFactory.getRepository(root);
151+
assertEquals(null, cvsrepo.getBranch());
152+
}
153+
154+
/**
155+
* Get the CVS repository, create new branch and verify getBranch() returns it.
156+
* @throws Exception
157+
*/
158+
@Test
159+
public void testGetBranchNewBranch() throws Exception {
160+
setUpTestRepository();
161+
File root = new File(repository.getSourceRoot(), "cvs_test/cvsrepo");
162+
163+
// Create new branch and switch to it.
164+
runCvsCommand(root, "tag", "-b", "mybranch");
165+
// Note that the 'update' command will change the entries in 'cvsroot' directory.
166+
runCvsCommand(root, "update", "-r", "mybranch");
167+
168+
// Now the repository object can be instantiated so that determineBranch()
169+
// will be called.
170+
CVSRepository cvsrepo
171+
= (CVSRepository) RepositoryFactory.getRepository(root);
172+
173+
assertEquals("mybranch", cvsrepo.getBranch());
70174
}
71175

72176
/**

0 commit comments

Comments
 (0)