Skip to content

Commit e96415d

Browse files
Knut Anders Hatlentrondn
authored andcommitted
Centralize verification of partial history
1 parent 079786e commit e96415d

File tree

5 files changed

+41
-27
lines changed

5 files changed

+41
-27
lines changed

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
21+
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
2222
* Use is subject to license terms.
2323
*/
2424
package org.opensolaris.opengrok.history;
@@ -70,11 +70,7 @@ History parse(File file, String sinceRevision) throws HistoryException {
7070
// Also check that the specified changeset was found, otherwise throw
7171
// an exception.
7272
if (sinceRevision != null) {
73-
HistoryEntry entry = entries.isEmpty() ?
74-
null : entries.remove(entries.size() - 1);
75-
if (entry == null || !sinceRevision.equals(entry.getRevision())) {
76-
throw new HistoryException("No such revision: " + sinceRevision);
77-
}
73+
repository.removeAndVerifyOldestChangeset(entries, sinceRevision);
7874
}
7975

8076
return new History(entries);

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
21+
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
2222
* Use is subject to license terms.
2323
*/
2424
package org.opensolaris.opengrok.history;
@@ -82,11 +82,7 @@ History parse(File file, String changeset) throws HistoryException {
8282
// Also check that the specified changeset was found, otherwise throw
8383
// an exception.
8484
if (changeset != null) {
85-
HistoryEntry entry = entries.isEmpty() ?
86-
null : entries.remove(entries.size() - 1);
87-
if (entry == null || !changeset.equals(entry.getRevision())) {
88-
throw new HistoryException("No such revision: " + changeset);
89-
}
85+
repository.removeAndVerifyOldestChangeset(entries, changeset);
9086
}
9187

9288
return new History(entries);

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

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
21+
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
2222
* Use is subject to license terms.
2323
*/
2424
package org.opensolaris.opengrok.history;
@@ -102,19 +102,42 @@ History getHistory(File file, String sinceRevision)
102102

103103
List<HistoryEntry> partial = new ArrayList<HistoryEntry>();
104104
for (HistoryEntry entry : history.getHistoryEntries()) {
105+
partial.add(entry);
105106
if (sinceRevision.equals(entry.getRevision())) {
106-
// Found revision right before the first one to return. Skip
107-
// this one and the rest (older revisions), and return those
108-
// entries we have seen so far.
109-
history.setHistoryEntries(partial);
110-
return history;
107+
// Found revision right before the first one to return.
108+
break;
111109
}
112-
partial.add(entry);
113110
}
114111

115-
// If we got here, we never saw sinceRevision in the history, hence
116-
// it doesn't exist. Raise an error.
117-
throw new HistoryException("No such revision: " + sinceRevision);
112+
removeAndVerifyOldestChangeset(partial, sinceRevision);
113+
history.setHistoryEntries(partial);
114+
return history;
115+
}
116+
117+
/**
118+
* Remove the oldest changeset from a list (assuming sorted with most
119+
* recent changeset first) and verify that it is the changeset we expected
120+
* to find there.
121+
*
122+
* @param entries a list of {@code HistoryEntry} objects
123+
* @param revision the revision we expect the oldest entry to have
124+
* @throws HistoryException if the oldest entry was not the one we expected
125+
*/
126+
void removeAndVerifyOldestChangeset(List<HistoryEntry> entries,
127+
String revision)
128+
throws HistoryException {
129+
HistoryEntry entry =
130+
entries.isEmpty() ? null : entries.remove(entries.size() - 1);
131+
132+
// TODO We should check more thoroughly that the changeset is the one
133+
// we expected it to be, since some SCMs may change the revision
134+
// numbers so that identical revision numbers does not always mean
135+
// identical changesets. We could for example get the cached changeset
136+
// and compare more fields, like author and date.
137+
if (entry == null || !revision.equals(entry.getRevision())) {
138+
throw new HistoryException("Cached revision '" + revision +
139+
"' not found in the repository");
140+
}
118141
}
119142

120143
/**

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
21+
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
2222
* Use is subject to license terms.
2323
*/
2424
package org.opensolaris.opengrok.history;
@@ -164,8 +164,7 @@ History parse(File file, SubversionRepository repos, String sinceRevision)
164164
// If we only fetch parts of the history, we're not interested in
165165
// sinceRevision. Remove it.
166166
if (sinceRevision != null) {
167-
HistoryEntry removed = entries.remove(entries.size() - 1);
168-
assert sinceRevision.equals(removed.getRevision());
167+
repos.removeAndVerifyOldestChangeset(entries, sinceRevision);
169168
}
170169

171170
return new History(entries);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
21+
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
2222
* Use is subject to license terms.
2323
*/
2424

@@ -128,7 +128,7 @@ public void testGetHistoryWithNoSuchRevision() throws Exception {
128128
fail("getHistory() should have failed");
129129
} catch (HistoryException he) {
130130
String msg = he.getMessage();
131-
if (msg != null && msg.startsWith("No such revision")) {
131+
if (msg != null && msg.contains("not found in the repository")) {
132132
// expected exception, do nothing
133133
} else {
134134
// unexpected exception, rethrow it

0 commit comments

Comments
 (0)