Skip to content

Commit 15e55fe

Browse files
authored
Merge pull request #1757 from vladak/latest_history_fix
getLatestRevision() fix
2 parents 9717060 + a04d659 commit 15e55fe

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

src/org/opensolaris/opengrok/web/PageConfig.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,15 +1193,15 @@ public File findDataFile() {
11931193
path, env.isCompressXref());
11941194
}
11951195

1196-
private String getLatestRevision() {
1197-
if (!env.isHistoryEnabled()) {
1196+
protected String getLatestRevision() {
1197+
if (!getEnv().isHistoryEnabled()) {
11981198
return null;
11991199
}
12001200

12011201
History hist;
12021202
try {
12031203
hist = HistoryGuru.getInstance().
1204-
getHistory(new File(env.getSourceRootFile(), path));
1204+
getHistory(new File(getEnv().getSourceRootFile(), getPath()));
12051205
} catch (HistoryException ex) {
12061206
return null;
12071207
}
@@ -1210,7 +1210,16 @@ private String getLatestRevision() {
12101210
return null;
12111211
}
12121212

1213-
HistoryEntry he = hist.getHistoryEntries().get(0);
1213+
List<HistoryEntry> hlist = hist.getHistoryEntries();
1214+
if (hlist == null) {
1215+
return null;
1216+
}
1217+
1218+
if (hlist.size() == 0) {
1219+
return null;
1220+
}
1221+
1222+
HistoryEntry he = hlist.get(0);
12141223
if (he == null) {
12151224
return null;
12161225
}
@@ -1234,12 +1243,17 @@ public boolean isLatestRevision(String rev) {
12341243
*/
12351244
public String getLatestRevisionLocation() {
12361245
StringBuilder sb = new StringBuilder();
1246+
String revStr;
1247+
1248+
if ((revStr = getLatestRevision()) == null) {
1249+
return null;
1250+
}
12371251

12381252
sb.append(req.getContextPath());
12391253
sb.append(Prefix.XREF_P);
12401254
sb.append(Util.URIEncodePath(path));
12411255
sb.append("?r=");
1242-
sb.append(Util.URIEncode(getLatestRevision()));
1256+
sb.append(Util.URIEncode(revStr));
12431257

12441258
return sb.toString();
12451259
}

test/org/opensolaris/opengrok/web/PageConfigTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,38 @@ public String getParameter(String name) {
241241
}
242242
}
243243

244+
@Test
245+
public void testGetLatestRevisionValid() {
246+
DummyHttpServletRequest req1 = new DummyHttpServletRequest() {
247+
@Override
248+
public String getPathInfo() {
249+
return "/git/main.c";
250+
}
251+
};
252+
253+
PageConfig cfg = PageConfig.get(req1);
254+
String rev = cfg.getLatestRevision();
255+
256+
assertEquals("aa35c258", rev);
257+
}
258+
259+
@Test
260+
public void testGetLatestRevisionNotValid() {
261+
DummyHttpServletRequest req2 = new DummyHttpServletRequest() {
262+
@Override
263+
public String getPathInfo() {
264+
return "/git/nonexistent_file";
265+
}
266+
};
267+
268+
PageConfig cfg = PageConfig.get(req2);
269+
String rev = cfg.getLatestRevision();
270+
assertNull(rev);
271+
272+
String location = cfg.getLatestRevisionLocation();
273+
assertNull(location);
274+
}
275+
244276
@Test
245277
public void testGetRequestedRevision() {
246278
final String[] params = {"r", "h", "r", "r", "r"};

0 commit comments

Comments
 (0)