Skip to content

Commit d071f6f

Browse files
author
Vladimir Kotal
authored
always provide revision number for xrefs (#1743)
fixes #1736
1 parent 12ed03e commit d071f6f

File tree

2 files changed

+150
-75
lines changed

2 files changed

+150
-75
lines changed

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

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@
6060
import org.opensolaris.opengrok.configuration.RuntimeEnvironment;
6161
import org.opensolaris.opengrok.configuration.messages.Message;
6262
import org.opensolaris.opengrok.history.Annotation;
63+
import org.opensolaris.opengrok.history.History;
64+
import org.opensolaris.opengrok.history.HistoryEntry;
65+
import org.opensolaris.opengrok.history.HistoryException;
6366
import org.opensolaris.opengrok.history.HistoryGuru;
6467
import org.opensolaris.opengrok.index.IgnoredNames;
6568
import org.opensolaris.opengrok.logger.LoggerFactory;
@@ -1190,6 +1193,57 @@ public File findDataFile() {
11901193
path, env.isCompressXref());
11911194
}
11921195

1196+
private String getLatestRevision() {
1197+
if (!env.isHistoryEnabled()) {
1198+
return null;
1199+
}
1200+
1201+
History hist;
1202+
try {
1203+
hist = HistoryGuru.getInstance().
1204+
getHistory(new File(env.getSourceRootFile(), path));
1205+
} catch (HistoryException ex) {
1206+
return null;
1207+
}
1208+
1209+
if (hist == null) {
1210+
return null;
1211+
}
1212+
1213+
HistoryEntry he = hist.getHistoryEntries().get(0);
1214+
if (he == null) {
1215+
return null;
1216+
}
1217+
1218+
return he.getRevision();
1219+
}
1220+
1221+
/**
1222+
* Is revision the latest revision ?
1223+
* @param rev revision string
1224+
* @return true if latest revision, false otherwise
1225+
*/
1226+
public boolean isLatestRevision(String rev) {
1227+
return rev.equals(getLatestRevision());
1228+
}
1229+
1230+
/**
1231+
* Get the location of cross reference for given file containing the current
1232+
* revision.
1233+
* @return location to redirect to or null if failed
1234+
*/
1235+
public String getLatestRevisionLocation() {
1236+
StringBuilder sb = new StringBuilder();
1237+
1238+
sb.append(req.getContextPath());
1239+
sb.append(Prefix.XREF_P);
1240+
sb.append(path);
1241+
sb.append("?r=");
1242+
sb.append(Util.URIEncode(getLatestRevision()));
1243+
1244+
return sb.toString();
1245+
}
1246+
11931247
/**
11941248
* Get the path the request should be redirected (if any).
11951249
*
@@ -1331,7 +1385,7 @@ public SearchHelper prepareSearch() {
13311385
}
13321386

13331387
/**
1334-
* Get the config wrt. the given request. If there is none yet, a new config
1388+
* Get the config w.r.t. the given request. If there is none yet, a new config
13351389
* gets created, attached to the request and returned.
13361390
* <p>
13371391
*

web/list.jsp

Lines changed: 95 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ java.util.List,
3838
java.util.Set,
3939
java.util.logging.Level,
4040
java.util.zip.GZIPInputStream,
41+
javax.servlet.http.HttpServletResponse,
4142

4243
org.opensolaris.opengrok.analysis.AnalyzerGuru,
4344
org.opensolaris.opengrok.analysis.Definitions,
@@ -147,95 +148,115 @@ document.pageReady.push(function() { pageReadyList();});
147148
}
148149
}
149150
} else if (rev.length() != 0) {
150-
// requesting a previous revision
151-
FileAnalyzerFactory a = AnalyzerGuru.find(basename);
152-
Genre g = AnalyzerGuru.getGenre(a);
153-
String error = null;
154-
if (g == Genre.PLAIN|| g == Genre.HTML || g == null) {
155-
InputStream in = null;
156-
try {
157-
in = HistoryGuru.getInstance()
158-
.getRevision(resourceFile.getParent(), basename, rev);
159-
} catch (Exception e) {
160-
// fall through to error message
161-
error = e.getMessage();
162-
}
163-
if (in != null) {
164-
try {
165-
if (g == null) {
166-
a = AnalyzerGuru.find(in);
167-
g = AnalyzerGuru.getGenre(a);
168-
}
169-
if (g == Genre.DATA || g == Genre.XREFABLE
170-
|| g == null)
171-
{
172-
%>
173-
<div id="src">
174-
Binary file [Click <a href="<%= rawPath %>?r=<%= Util.URIEncode(rev) %>">here</a> to download]
175-
</div><%
176-
} else {
151+
// requesting a revision
152+
if (cfg.isLatestRevision(rev)) {
153+
File xrefFile = cfg.findDataFile();
154+
if (xrefFile != null) {
177155
%>
178-
<div id="src">
156+
<div id="src" data-navigate-window-enabled="<%= navigateWindowEnabled %>">
179157
<pre><%
180-
if (g == Genre.PLAIN) {
181-
// We don't have any way to get definitions
182-
// for old revisions currently.
183-
Definitions defs = null;
184-
Annotation annotation = cfg.getAnnotation();
185-
//not needed yet
186-
//annotation.writeTooltipMap(out);
187-
r = IOUtils.createBOMStrippedReader(in);
188-
AnalyzerGuru.writeXref(a, r, out, defs,
189-
annotation, Project.getProject(resourceFile));
190-
} else if (g == Genre.IMAGE) {
158+
Util.dump(out, xrefFile, xrefFile.getName().endsWith(".gz"));
191159
%></pre>
192-
<img src="<%= rawPath %>?r=<%= Util.URIEncode(rev) %>"/>
193-
<pre><%
194-
} else if (g == Genre.HTML) {
195-
r = new InputStreamReader(in);
196-
Util.dump(out, r);
160+
</div><%
161+
}
162+
} else {
163+
// requesting a previous revision
164+
FileAnalyzerFactory a = AnalyzerGuru.find(basename);
165+
Genre g = AnalyzerGuru.getGenre(a);
166+
String error = null;
167+
if (g == Genre.PLAIN|| g == Genre.HTML || g == null) {
168+
InputStream in = null;
169+
try {
170+
in = HistoryGuru.getInstance()
171+
.getRevision(resourceFile.getParent(), basename, rev);
172+
} catch (Exception e) {
173+
// fall through to error message
174+
error = e.getMessage();
175+
}
176+
if (in != null) {
177+
try {
178+
if (g == null) {
179+
a = AnalyzerGuru.find(in);
180+
g = AnalyzerGuru.getGenre(a);
181+
}
182+
if (g == Genre.DATA || g == Genre.XREFABLE
183+
|| g == null)
184+
{
185+
%>
186+
<div id="src">
187+
Binary file [Click <a href="<%= rawPath %>?r=<%= Util.URIEncode(rev) %>">here</a> to download]
188+
</div><%
197189
} else {
198-
%> Click <a href="<%= rawPath %>?r=<%= Util.URIEncode(rev) %>">download <%= basename %></a><%
190+
%>
191+
<div id="src">
192+
<pre><%
193+
if (g == Genre.PLAIN) {
194+
// We don't have any way to get definitions
195+
// for old revisions currently.
196+
Definitions defs = null;
197+
Annotation annotation = cfg.getAnnotation();
198+
//not needed yet
199+
//annotation.writeTooltipMap(out);
200+
r = IOUtils.createBOMStrippedReader(in);
201+
AnalyzerGuru.writeXref(a, r, out, defs,
202+
annotation, Project.getProject(resourceFile));
203+
} else if (g == Genre.IMAGE) {
204+
%></pre>
205+
<img src="<%= rawPath %>?r=<%= Util.URIEncode(rev) %>"/>
206+
<pre><%
207+
} else if (g == Genre.HTML) {
208+
r = new InputStreamReader(in);
209+
Util.dump(out, r);
210+
} else {
211+
%> Click <a href="<%= rawPath %>?r=<%= Util.URIEncode(rev) %>">download <%= basename %></a><%
212+
}
213+
}
214+
} catch (IOException e) {
215+
error = e.getMessage();
216+
} finally {
217+
if (r != null) {
218+
try { r.close(); in = null;}
219+
catch (Exception e) { /* ignore */ }
220+
}
221+
if (in != null) {
222+
try { in.close(); }
223+
catch (Exception e) { /* ignore */ }
199224
}
200225
}
201-
} catch (IOException e) {
202-
error = e.getMessage();
203-
} finally {
204-
if (r != null) {
205-
try { r.close(); in = null;}
206-
catch (Exception e) { /* ignore */ }
207-
}
208-
if (in != null) {
209-
try { in.close(); }
210-
catch (Exception e) { /* ignore */ }
226+
%></pre>
227+
</div><%
228+
} else {
229+
%>
230+
<h3 class="error">Error reading file</h3><%
231+
if (error != null) {
232+
%>
233+
<p class="error"><%= error %></p><%
211234
}
212235
}
213-
%></pre>
214-
</div><%
236+
} else if (g == Genre.IMAGE) {
237+
%>
238+
<div id="src">
239+
<img src="<%= rawPath %>?r=<%= Util.URIEncode(rev) %>"/>
240+
</div><%
215241
} else {
216-
%>
217-
<h3 class="error">Error reading file</h3><%
218-
if (error != null) {
219-
%>
220-
<p class="error"><%= error %></p><%
221-
}
242+
%>
243+
<div id="src">
244+
Binary file [Click <a href="<%= rawPath %>?r=<%= Util.URIEncode(rev) %>">here</a> to download]
245+
</div><%
222246
}
223-
} else if (g == Genre.IMAGE) {
224-
%>
225-
<div id="src">
226-
<img src="<%= rawPath %>?r=<%= Util.URIEncode(rev) %>"/>
227-
</div><%
228-
} else {
229-
%>
230-
<div id="src">
231-
Binary file [Click <a href="<%= rawPath %>?r=<%= Util.URIEncode(rev) %>">here</a> to download]
232-
</div><%
233247
}
234248
} else {
235249
// requesting cross referenced file
236250
File xrefFile = null;
237251
if (!cfg.annotate()) {
238-
xrefFile = cfg.findDataFile();
252+
// Get the latest revision and redirect so that the revision number
253+
// appears in the URL.
254+
String location = cfg.getLatestRevisionLocation();
255+
if (location != null) {
256+
response.sendRedirect(location);
257+
} else {
258+
xrefFile = cfg.findDataFile();
259+
}
239260
}
240261
if (xrefFile != null) {
241262
%>

0 commit comments

Comments
 (0)