Skip to content

Commit 338dead

Browse files
tulinkryVladimir Kotal
authored andcommitted
Fixing git viewing renamed revision (#2726)
1 parent d85d62d commit 338dead

File tree

2 files changed

+110
-74
lines changed

2 files changed

+110
-74
lines changed

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

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
/*
2121
* Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2017-2019, Chris Fraire <[email protected]>.
23+
* Portions Copyright (c) 2019, Krystof Tulinger <[email protected]>.
2324
*/
2425
package org.opengrok.indexer.history;
2526

@@ -196,8 +197,9 @@ private HistoryRevResult getHistoryRev(
196197
* Be careful, git uses only forward slashes in its command and output (not in file path).
197198
* Using backslashes together with git show will get empty output and 0 status code.
198199
*/
199-
String filename = fullpath.substring(getDirectoryName().length() + 1)
200-
.replace(File.separatorChar, '/');
200+
String filename = Paths.get(getDirectoryName()).relativize(Paths.get(fullpath))
201+
.toString()
202+
.replace(File.separatorChar, '/');
201203
ensureCommand(CMD_PROPERTY_KEY, CMD_FALLBACK);
202204
String[] argv = {
203205
RepoCommand,
@@ -215,10 +217,14 @@ private HistoryRevResult getHistoryRev(
215217
* not exist or internal git error occured.
216218
*/
217219
result.success = (status == 0);
218-
} catch (Exception exp) {
220+
} catch (Exception exception) {
219221
LOGGER.log(Level.SEVERE,
220-
"Failed to get history for file {0} in revision {1}: ",
221-
new Object[]{fullpath, rev, exp.getClass().toString(), exp});
222+
String.format(
223+
"Failed to get history for file %s in revision %s:",
224+
fullpath, rev
225+
),
226+
exception
227+
);
222228
}
223229
return result;
224230
}
@@ -259,8 +265,12 @@ public String get() {
259265
});
260266
return false;
261267
}
262-
if (origpath != null && !origpath.equals(fullpath)) {
263-
result = getHistoryRev(sink, origpath, rev);
268+
269+
if (origpath != null) {
270+
final String fullRenamedPath = Paths.get(getDirectoryName(), origpath).toString();
271+
if (!fullRenamedPath.equals(fullpath)) {
272+
result = getHistoryRev(sink, fullRenamedPath, rev);
273+
}
264274
}
265275
}
266276

@@ -289,12 +299,14 @@ private String getPathRelativeToRepositoryRoot(String fullpath) {
289299
}
290300

291301
/**
292-
* Get the name of file in given revision.
302+
* Get the name of file in given revision. The returned file name is relative
303+
* to the repository root.
293304
*
294-
* @param fullpath file path
305+
* @param fullpath file path
295306
* @param changeset changeset
296307
* @return original filename relative to the repository root
297308
* @throws java.io.IOException if I/O exception occurred
309+
* @see #getPathRelativeToRepositoryRoot(String)
298310
*/
299311
protected String findOriginalName(String fullpath, String changeset)
300312
throws IOException {
@@ -321,7 +333,7 @@ protected String findOriginalName(String fullpath, String changeset)
321333
ABBREV_LOG,
322334
"--abbrev-commit",
323335
"--name-status",
324-
"--pretty=format:commit %h%b",
336+
"--pretty=format:commit %h%n%d",
325337
"--",
326338
fileInRepo
327339
};
@@ -363,7 +375,7 @@ protected String findOriginalName(String fullpath, String changeset)
363375
new Object[]{fullpath, String.valueOf(status), changeset});
364376
return null;
365377
}
366-
378+
367379
return originalFile;
368380
}
369381

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

Lines changed: 87 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
*/
1919

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

@@ -32,10 +33,11 @@
3233
import java.io.IOException;
3334
import java.io.InputStream;
3435
import java.nio.file.Paths;
35-
import java.text.DateFormat;
3636
import java.text.ParseException;
37+
import java.util.Arrays;
3738
import java.util.Collections;
3839
import java.util.HashSet;
40+
import java.util.List;
3941
import java.util.Set;
4042
import org.junit.After;
4143
import org.junit.AfterClass;
@@ -174,6 +176,16 @@ public void testDateFormats() {
174176
}
175177
}
176178

179+
/**
180+
* For the following renamed tests the structure in the git repo is as following:
181+
* <pre>
182+
* ce4c98ec - new file renamed.c (with some content)
183+
* b6413947 - renamed.c renamed to moved/renamed.c
184+
* 1086eaf5 - modification of file moved/renamed.c (different content)
185+
* 67dfbe26 - moved/renamed.c renamed to moved/renamed2.c
186+
* 84599b3c - moved/renamed2.c renamed to moved2/renamed2.c
187+
* </pre>
188+
*/
177189
@Test
178190
public void testRenamedFiles() throws Exception {
179191
String[][] tests = new String[][]{
@@ -281,18 +293,14 @@ public void testInvalidRenamedFiles() throws Exception {
281293
* file.
282294
*
283295
* @throws java.lang.Exception
296+
* @see #testRenamedFiles for git repo structure info
284297
*/
285298
@Test
286-
public void testGetHistoryForOnceRenamed() throws Exception {
287-
String exp_str
299+
public void testGetRenamedFileContent() throws Exception {
300+
String old_content
288301
= "#include <stdio.h>\n"
289302
+ "#include <stdlib.h>\n"
290303
+ "\n"
291-
+ "int foo ( const char * path )\n"
292-
+ "{\n"
293-
+ "\treturn path && *path == 'A';\n"
294-
+ "}\n"
295-
+ "\n"
296304
+ "int main ( int argc, const char * argv[] )\n"
297305
+ "{\n"
298306
+ "\tint i;\n"
@@ -301,29 +309,10 @@ public void testGetHistoryForOnceRenamed() throws Exception {
301309
+ "\t\tprintf ( \"%s called with %d\\n\", argv [ 0 ], argv [ i ] );\n"
302310
+ "\t}\n"
303311
+ "\n"
304-
+ "\tprintf ( \"Hello, world!\\n\" );\n"
305-
+ "\n"
306-
+ "\tif ( foo ( argv [ 0 ] ) )\n"
307-
+ "\t{\n"
308-
+ "\t\tprintf ( \"Correct call\\n\" );\n"
309-
+ "\t}\n"
310-
+ "\n"
311312
+ "\treturn 0;\n"
312313
+ "}\n";
313314

314-
runRenamedTest("moved2/renamed2.c", "84599b3", exp_str);
315-
runRenamedTest("moved/renamed2.c", "67dfbe2", exp_str);
316-
}
317-
318-
/**
319-
* Test that {@code getHistoryGet()} returns historical contents of renamed
320-
* file.
321-
*
322-
* @throws java.lang.Exception
323-
*/
324-
@Test
325-
public void testGetHistoryForTwiceRenamed() throws Exception {
326-
String exp_str
315+
String new_content
327316
= "#include <stdio.h>\n"
328317
+ "#include <stdlib.h>\n"
329318
+ "\n"
@@ -350,47 +339,56 @@ public void testGetHistoryForTwiceRenamed() throws Exception {
350339
+ "\treturn 0;\n"
351340
+ "}\n";
352341

353-
runRenamedTest(Paths.get("moved", "renamed.c").toString(), "1086eaf", exp_str);
354-
runRenamedTest(Paths.get("moved", "renamed2.c").toString(), "67dfbe2", exp_str);
342+
final List<String[]> tests = Arrays.asList(
343+
// old content (after revision 1086eaf5 inclusively)
344+
new String[]{Paths.get("moved2", "renamed2.c").toString(), "84599b3c", new_content},
345+
new String[]{Paths.get("moved2", "renamed2.c").toString(), "67dfbe26", new_content},
346+
new String[]{Paths.get("moved2", "renamed2.c").toString(), "1086eaf5", new_content},
347+
348+
new String[]{Paths.get("moved", "renamed2.c").toString(), "67dfbe26", new_content},
349+
new String[]{Paths.get("moved", "renamed2.c").toString(), "1086eaf5", new_content},
350+
351+
new String[]{Paths.get("moved", "renamed.c").toString(), "1086eaf5", new_content},
352+
353+
// old content (before revision b6413947 inclusively)
354+
new String[]{Paths.get("moved2", "renamed2.c").toString(), "b6413947", old_content},
355+
new String[]{Paths.get("moved2", "renamed2.c").toString(), "ce4c98ec", old_content},
356+
new String[]{Paths.get("moved", "renamed2.c").toString(), "b6413947", old_content},
357+
new String[]{Paths.get("moved", "renamed2.c").toString(), "ce4c98ec", old_content},
358+
new String[]{Paths.get("moved", "renamed.c").toString(), "b6413947", old_content},
359+
new String[]{Paths.get("moved", "renamed.c").toString(), "ce4c98ec", old_content},
360+
new String[]{Paths.get("renamed.c").toString(), "ce4c98ec", old_content}
361+
);
362+
363+
for (String[] params : tests) {
364+
runRenamedTest(params[0], params[1], params[2]);
365+
}
355366
}
356367

357368
/**
358369
* Test that {@code getHistoryGet()} returns historical contents of renamed
359370
* file.
360371
*
361372
* @throws java.lang.Exception
373+
* @see #testRenamedFiles for git repo structure info
362374
*/
363375
@Test
364-
public void testGetHistoryForThreeTimesRenamed() throws Exception {
365-
String exp_str
366-
= "#include <stdio.h>\n"
367-
+ "#include <stdlib.h>\n"
368-
+ "\n"
369-
+ "int main ( int argc, const char * argv[] )\n"
370-
+ "{\n"
371-
+ "\tint i;\n"
372-
+ "\tfor ( i = 1; i < argc; i ++ )\n"
373-
+ "\t{\n"
374-
+ "\t\tprintf ( \"%s called with %d\\n\", argv [ 0 ], argv [ i ] );\n"
375-
+ "\t}\n"
376-
+ "\n"
377-
+ "\treturn 0;\n"
378-
+ "}\n";
376+
public void testGetHistoryForNonExistentRenamed() throws Exception {
377+
final List<String[]> tests = Arrays.asList(
378+
new String[]{Paths.get("moved", "renamed2.c").toString(), "84599b3c"},
379379

380-
runRenamedTest(Paths.get("moved", "renamed.c").toString(), "b641394", exp_str);
381-
runRenamedTest(Paths.get("renamed.c").toString(), "ce4c98e", exp_str);
382-
}
380+
new String[]{Paths.get("moved", "renamed.c").toString(), "84599b3c"},
381+
new String[]{Paths.get("moved", "renamed.c").toString(), "67dfbe26"},
383382

384-
/**
385-
* Test that {@code getHistoryGet()} returns historical contents of renamed
386-
* file.
387-
*
388-
* @throws java.lang.Exception
389-
*/
390-
@Test
391-
public void testGetHistoryForNonExistentRenamed() throws Exception {
392-
runRenamedTest(Paths.get("moved", "renamed.c").toString(), "67dfbe2", null);
393-
runRenamedTest(Paths.get("renamed.c").toString(), "67dfbe2", null);
383+
new String[]{Paths.get("renamed.c").toString(), "84599b3c"},
384+
new String[]{Paths.get("renamed.c").toString(), "67dfbe26"},
385+
new String[]{Paths.get("renamed.c").toString(), "1086eaf5"},
386+
new String[]{Paths.get("renamed.c").toString(), "b6413947"}
387+
);
388+
389+
for (String[] params : tests) {
390+
runRenamedTest(params[0], params[1], null);
391+
}
394392
}
395393

396394
private void runRenamedTest(String fname, String cset, String content) throws Exception {
@@ -402,13 +400,39 @@ private void runRenamedTest(String fname, String cset, String content) throws Ex
402400
InputStream input = gitrepo.getHistoryGet(root.getCanonicalPath(),
403401
fname, cset);
404402
if (content == null) {
405-
Assert.assertNull(input);
403+
Assert.assertNull(
404+
String.format("Expecting the revision %s for file %s does not exist",
405+
cset,
406+
fname
407+
),
408+
input
409+
);
406410
} else {
407-
Assert.assertNotNull("expecting not null", input);
411+
Assert.assertNotNull(
412+
String.format("Expecting the revision %s for file %s does exist",
413+
cset,
414+
fname
415+
),
416+
input
417+
);
408418
int len = input.read(buffer);
409-
Assert.assertNotEquals(-1, len);
419+
Assert.assertNotEquals(
420+
String.format("Expecting the revision %s for file %s does have some content",
421+
cset,
422+
fname
423+
),
424+
-1,
425+
len
426+
);
410427
String str = new String(buffer, 0, len);
411-
Assert.assertEquals(content, str);
428+
Assert.assertEquals(
429+
String.format("Expecting the revision %s for file %s does match the expected content",
430+
cset,
431+
fname
432+
),
433+
content,
434+
str
435+
);
412436
}
413437
}
414438

0 commit comments

Comments
 (0)