Skip to content

Commit c199a91

Browse files
idodeclareVladimir Kotal
authored andcommitted
Revise per review
1 parent 8595159 commit c199a91

File tree

4 files changed

+64
-54
lines changed

4 files changed

+64
-54
lines changed

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

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,37 @@ boolean hasFileBasedTags() {
566566
return true;
567567
}
568568

569+
/**
570+
* Builds a Git tag list by querying Git commit hash, commit time, and tag
571+
* names.
572+
* <p>Repository technically relies on the tag list to be ancestor ordered.
573+
* <p>For a version control system that uses "linear revision numbering"
574+
* (e.g. Subversion or Mercurial), the natural ordering in the
575+
* {@link TreeSet} is by ancestor order and so
576+
* {@link TagEntry#compareTo(HistoryEntry)} always determines the correct
577+
* tag.
578+
* <p>For {@link GitTagEntry} that does not use linear revision numbering,
579+
* the {@link TreeSet} will be ordered by date. That does not necessarily
580+
* align with ancestor order. In that case,
581+
* {@link GitTagEntry#compareTo(HistoryEntry)} that compares by date can
582+
* find the wrong tag.
583+
* <p>Linus Torvalds: [When asking] "'can commit X be an ancestor of commit
584+
* Y' (as a way to basically limit certain algorithms from having to walk
585+
* all the way down). We've used commit dates for it, and realistically it
586+
* really has worked very well. But it was always a broken heuristic."
587+
* <p>"I think the lack of [generation numbers] is literally the only real
588+
* design mistake we have [in Git]."
589+
* <p>"We discussed adding generation numbers about 6 years ago [in 2005].
590+
* We clearly *should* have done it. Instead, we went with the hacky `let's
591+
* use commit time', that everybody really knew was technically wrong, and
592+
* was a hack, but avoided the need."
593+
* <p>If Git ever gets standard generation numbers,
594+
* {@link GitTagEntry#compareTo(HistoryEntry)} should be revised to work
595+
* reliably in all cases akin to a version control system that uses "linear
596+
* revision numbering."
597+
* @param directory a defined directory of the repository
598+
* @param interactive true if in interactive mode
599+
*/
569600
@Override
570601
protected void buildTagList(File directory, boolean interactive) {
571602
this.tagList = new TreeSet<>();
@@ -596,41 +627,6 @@ protected void buildTagList(File directory, boolean interactive) {
596627
new Object[]{directory.getAbsolutePath(), String.valueOf(status)});
597628
// In case of partial success, do not null-out tagList here.
598629
}
599-
600-
/*
601-
* Repository technically relies on the tag list to be ancestor
602-
* ordered.
603-
*
604-
* For an SCM that uses "linear revision numbering" (e.g. Subversion or
605-
* Mercurial), the natural ordering in the TreeSet is by ancestor order
606-
* and so TagEntry.compareTo(HistoryEntry) always determines the
607-
* correct tag.
608-
*
609-
* For GitTagEntry that does not use linear revision numbering, the
610-
* TreeSet will be ordered by date. That does not necessarily align
611-
* with ancestor order. In that case,
612-
* GitTagEntry.compareTo(HistoryEntry) that compares by date can find
613-
* the wrong tag.
614-
*
615-
* Linus Torvalds: [When asking] "'can commit X be an ancestor of
616-
* commit Y' (as a way to basically limit certain algorithms from
617-
* having to walk all the way down). We've used commit dates for it,
618-
* and realistically it really has worked very well. But it was always
619-
* a broken heuristic."
620-
*
621-
* "I think the lack of [generation numbers] is literally the only real
622-
* design mistake we have [in Git]."
623-
*
624-
* "We discussed adding generation numbers about 6 years ago [in 2005].
625-
* We clearly *should* have done it. Instead, we went with the hacky
626-
* `let's use commit time', that everybody really knew was technically
627-
* wrong, and was a hack, but avoided the need."
628-
*
629-
* If Git ever gets standard generation numbers,
630-
* GitTagEntry.compareTo(HistoryEntry) should be revised to work
631-
* reliably in all cases akin to an SCM that uses "linear revision
632-
* numbering."
633-
*/
634630
}
635631

636632
@Override

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,20 @@
3333
*/
3434
class GitTagEntry extends TagEntry {
3535

36-
final String hash;
36+
private final String hash;
3737

3838
GitTagEntry(String hash, Date date, String tags) {
3939
super(date, tags);
4040
this.hash = hash;
4141
}
4242

43+
/**
44+
* Gets the immutable, initialized Git hash value.
45+
*/
46+
String getHash() {
47+
return hash;
48+
}
49+
4350
@Override
4451
public int compareTo(HistoryEntry that) {
4552
assert this.date != null : "Git TagEntry created without date specified";

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

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,34 @@
2626
import java.util.Date;
2727

2828
/**
29-
* Class representing tag as Pair&lt;revision, tag string&gt;. Has overloaded
30-
* equals() using only revision string.
29+
* Represents an identifier for a version control "commit" (also known as a
30+
* "changeset") where the version control system uses either monotonic, linear
31+
* revision numbering; or alternatively where OpenGrok uses "commit time" as a
32+
* proxy for ancestry.
3133
*
3234
* @author Stanislav Kozina
3335
*/
3436
public abstract class TagEntry implements Comparable<TagEntry> {
3537

36-
protected int revision;
3738
/**
3839
* If repo uses linear revision numbering.
3940
*/
40-
protected Date date;
41+
protected final int revision;
4142
/**
4243
* If repo does not use linear numbering.
4344
*/
44-
protected String tags;
45+
protected final Date date;
4546
/**
4647
* Tag of the revision.
4748
*/
49+
protected String tags;
50+
/**
51+
* Sentinel value for a repo that does not use linear numbering.
52+
*/
4853
protected static final int NOREV = -1;
4954

5055
/**
51-
* Revision number not present.
56+
* Initializes an instance for a repo where revision number is present.
5257
*
5358
* @param revision revision number
5459
* @param tags string representing tags
@@ -59,6 +64,14 @@ public TagEntry(int revision, String tags) {
5964
this.tags = tags;
6065
}
6166

67+
/**
68+
* Initializes an instance for a repo where revision number is not present
69+
* and {@code date} is used instead.
70+
*
71+
* @param date revision date
72+
* @param tags string representing tags
73+
* @throws IllegalArgumentException if {@code date} is null
74+
*/
6275
public TagEntry(Date date, String tags) {
6376
if (date == null) {
6477
throw new IllegalArgumentException("`date' is null");

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public class GitTagParserTest {
191191
"61172cb46e108f65a356772860ac577141fdf8e8:1385651730:tag: untagged-2d067cc3eab919a1b8d1:\n" +
192192
"17d2cbd8d550eac92cd13955d63de4298303e4e0:1382628329:tag: 0.12-rc2:\n" +
193193
"c6963a7ea2753672325502d342e653700be550a8:1377876557:tag: 0.12-rc1:\n" +
194-
"c23e82b612acd5e947c164114377578116f6d298:1163621273::";
194+
"c23e82b612acd5e947c164114377578116f6d298:1163621273::\n";
195195

196196
@Test
197197
public void shouldParseOpenGrokTagsLog() throws IOException {
@@ -202,13 +202,8 @@ public void shouldParseOpenGrokTagsLog() throws IOException {
202202
GitTagParser parser = new GitTagParser(tagList);
203203
parser.processStream(bytesIn);
204204

205-
/*
206-
* The following comparison works because there is no trailing space in
207-
* the sample, and the last line does not have a tag name. So the
208-
* expected number of tag entries should match the number of LFs.
209-
*/
210205
long countLF = OGK_LOG.chars().filter(ch -> ch == '\n').count();
211-
assertEquals("size should == LF count", countLF, tagList.size());
206+
assertEquals("size should == LF count - 1", countLF - 1, tagList.size());
212207

213208
// c6963a7ea2753672325502d342e653700be550a8:1377876557:tag: 0.12-rc1:
214209
GitTagEntry t1 = new GitTagEntry("c6963a7ea2753672325502d342e653700be550a8",
@@ -218,7 +213,7 @@ public void shouldParseOpenGrokTagsLog() throws IOException {
218213
TagEntry floor1 = tagList.floor(t1);
219214
assertNotNull("should find floor() given contains() is true", floor1);
220215
assertEquals("tags should equal", t1.tags, floor1.tags);
221-
assertEquals("hashes should equal", t1.hash, ((GitTagEntry) floor1).hash);
216+
assertEquals("hashes should equal", t1.getHash(), ((GitTagEntry) floor1).getHash());
222217

223218
// 6d454cfc137241865a8639a2b1ed64dea81a448b:1538750353:tag: 1.1-rc45, tag: 1.1-rc44:
224219
GitTagEntry t2 = new GitTagEntry("6d454cfc137241865a8639a2b1ed64dea81a448b",
@@ -228,23 +223,22 @@ public void shouldParseOpenGrokTagsLog() throws IOException {
228223
TagEntry floor2 = tagList.floor(t2);
229224
assertNotNull("should find floor() given contains() is true", floor2);
230225
assertEquals("tags should equal", t2.tags, floor2.tags);
231-
assertEquals("hashes should equal", t2.hash, ((GitTagEntry) floor2).hash);
226+
assertEquals("hashes should equal", t2.getHash(), ((GitTagEntry) floor2).getHash());
232227
}
233228

234229
@Test
235230
public void shouldParseOneCharacterTags() throws IOException {
236231
final String LOG =
237232
"c6963a7ea2753672325502d342e653700be550a8:1377876557:tag: z:\n" +
238-
"c23e82b612acd5e947c164114377578116f6d298:1163621273::";
233+
"c23e82b612acd5e947c164114377578116f6d298:1163621273::\n";
239234
final byte[] LOG_BYTES = LOG.getBytes(StandardCharsets.UTF_8);
240235
ByteArrayInputStream bytesIn = new ByteArrayInputStream(LOG_BYTES);
241236

242237
TreeSet<TagEntry> tagList = new TreeSet<>();
243238
GitTagParser parser = new GitTagParser(tagList);
244239
parser.processStream(bytesIn);
245240

246-
// See comment about LF count in shouldParseOpenGrokTagsLog().
247241
long countLF = LOG.chars().filter(ch -> ch == '\n').count();
248-
assertEquals("size should == LF count", countLF, tagList.size());
242+
assertEquals("size should == LF count - 1", countLF - 1, tagList.size());
249243
}
250244
}

0 commit comments

Comments
 (0)