Skip to content

Commit d04d1c6

Browse files
tulinkryVladimir Kotal
authored andcommitted
fixing git repository tests - path issue
1 parent bf37f36 commit d04d1c6

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.io.InputStream;
3232
import java.io.InputStreamReader;
3333
import java.io.Reader;
34+
import java.nio.file.Paths;
3435
import java.text.ParseException;
3536
import java.util.ArrayList;
3637
import java.util.Arrays;
@@ -179,14 +180,19 @@ private InputStream getHistoryRev(String fullpath, String rev) {
179180
File directory = new File(getDirectoryName());
180181

181182
try {
182-
String filename = fullpath.substring(getDirectoryName().length() + 1);
183+
/*
184+
* Be careful, git uses only forward slashes in its command and output (not in file path).
185+
* Using backslashes together with git show will get empty output and 0 status code.
186+
*/
187+
String filename = fullpath.substring(getDirectoryName().length() + 1)
188+
.replace(File.separatorChar, '/');
183189
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
184190
String argv[] = {
185191
RepoCommand,
186192
"show",
187193
rev + ":" + filename
188194
};
189-
195+
190196
Executor executor = new Executor(Arrays.asList(argv), directory,
191197
RuntimeEnvironment.getInstance().getInteractiveCommandTimeout());
192198
int status = executor.exec();
@@ -236,6 +242,7 @@ public String get() {
236242
}
237243

238244
InputStream ret = getHistoryRev(fullpath, rev);
245+
239246
if (ret == null) {
240247
/*
241248
* If we failed to get the contents it might be that the file was
@@ -303,6 +310,7 @@ protected String findOriginalName(String fullpath, String changeset)
303310
}
304311

305312
String fileInRepo = getPathRelativeToRepositoryRoot(fullpath);
313+
306314
/*
307315
* Get the list of file renames for given file to the specified
308316
* revision.
@@ -345,7 +353,8 @@ protected String findOriginalName(String fullpath, String changeset)
345353
}
346354

347355
if ((m = pattern.matcher(line)).find()) {
348-
originalFile = m.group(1);
356+
// git output paths with forward slashes so transform it if needed
357+
originalFile = Paths.get(m.group(1)).toString();
349358
}
350359
}
351360
}
@@ -362,7 +371,7 @@ protected String findOriginalName(String fullpath, String changeset)
362371

363372
/**
364373
* Get first revision of given file without following renames.
365-
* @param file file to get first revision of
374+
* @param fullpath file path to get first revision of
366375
*/
367376
private String getFirstRevision(String fullpath) throws IOException {
368377
String[] argv = {

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

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,20 @@
2323
*/
2424
package org.opengrok.indexer.history;
2525

26+
import static org.junit.Assert.assertEquals;
27+
import static org.junit.Assert.assertNotNull;
28+
import static org.junit.Assert.assertTrue;
29+
2630
import java.io.ByteArrayInputStream;
2731
import java.io.File;
2832
import java.io.IOException;
2933
import java.io.InputStream;
30-
import java.io.Reader;
31-
import java.io.StringReader;
3234
import java.nio.file.Paths;
3335
import java.text.DateFormat;
3436
import java.text.ParseException;
3537
import java.util.Collections;
3638
import java.util.HashSet;
3739
import java.util.Set;
38-
3940
import org.junit.After;
4041
import org.junit.AfterClass;
4142
import org.junit.Assert;
@@ -46,10 +47,8 @@
4647
import org.opengrok.indexer.condition.ConditionalRun;
4748
import org.opengrok.indexer.condition.ConditionalRunRule;
4849
import org.opengrok.indexer.condition.RepositoryInstalled;
49-
import org.opengrok.indexer.util.TestRepository;
50-
51-
import static org.junit.Assert.*;
5250
import org.opengrok.indexer.configuration.RuntimeEnvironment;
51+
import org.opengrok.indexer.util.TestRepository;
5352

5453
/**
5554
*
@@ -178,13 +177,13 @@ public void testDateFormats() {
178177
@Test
179178
public void testRenamedFiles() throws Exception {
180179
String[][] tests = new String[][]{
181-
{"moved2/renamed2.c", "84599b3c", "moved2/renamed2.c"},
182-
{"moved2/renamed2.c", "67dfbe26", "moved/renamed2.c"},
183-
{"moved2/renamed2.c", "67dfbe26", "moved/renamed2.c"},
184-
{"moved2/renamed2.c", "1086eaf5", "moved/renamed.c"},
185-
{"moved2/renamed2.c", "b6413947", "moved/renamed.c"},
186-
{"moved2/renamed2.c", "ce4c98ec", "renamed.c"},
187-
{"moved2/renamed2.c", "bb74b7e8", "renamed.c"}
180+
{Paths.get("moved2", "renamed2.c").toString(), "84599b3c", Paths.get("moved2", "renamed2.c").toString()},
181+
{Paths.get("moved2", "renamed2.c").toString(), "67dfbe26", Paths.get("moved", "renamed2.c").toString()},
182+
{Paths.get("moved2", "renamed2.c").toString(), "67dfbe26", Paths.get("moved", "renamed2.c").toString()},
183+
{Paths.get("moved2", "renamed2.c").toString(), "1086eaf5", Paths.get("moved", "renamed.c").toString()},
184+
{Paths.get("moved2", "renamed2.c").toString(), "b6413947", Paths.get("moved", "renamed.c").toString()},
185+
{Paths.get("moved2", "renamed2.c").toString(), "ce4c98ec", "renamed.c"},
186+
{Paths.get("moved2", "renamed2.c").toString(), "bb74b7e8", "renamed.c"}
188187
};
189188

190189
File root = new File(repository.getSourceRoot(), "git");
@@ -312,8 +311,8 @@ public void testGetHistoryForOnceRenamed() throws Exception {
312311
+ "\treturn 0;\n"
313312
+ "}\n";
314313

315-
runRenamedTest(Paths.get("moved2", "renamed2.c").toString(), "84599b3", exp_str);
316-
runRenamedTest(Paths.get("moved", "renamed2.c").toString(), "67dfbe2", exp_str);
314+
runRenamedTest("moved2/renamed2.c", "84599b3", exp_str);
315+
runRenamedTest("moved/renamed2.c", "67dfbe2", exp_str);
317316
}
318317

319318
/**
@@ -379,7 +378,7 @@ public void testGetHistoryForThreeTimesRenamed() throws Exception {
379378
+ "}\n";
380379

381380
runRenamedTest(Paths.get("moved", "renamed.c").toString(), "b641394", exp_str);
382-
runRenamedTest("renamed.c", "ce4c98e", exp_str);
381+
runRenamedTest(Paths.get("renamed.c").toString(), "ce4c98e", exp_str);
383382
}
384383

385384
/**
@@ -391,7 +390,7 @@ public void testGetHistoryForThreeTimesRenamed() throws Exception {
391390
@Test
392391
public void testGetHistoryForNonExistentRenamed() throws Exception {
393392
runRenamedTest(Paths.get("moved", "renamed.c").toString(), "67dfbe2", null);
394-
runRenamedTest("renamed.c", "67dfbe2", null);
393+
runRenamedTest(Paths.get("renamed.c").toString(), "67dfbe2", null);
395394
}
396395

397396
private void runRenamedTest(String fname, String cset, String content) throws Exception {

0 commit comments

Comments
 (0)