Skip to content

Commit ffb82b4

Browse files
harrisricvladak
authored andcommitted
Initial fix for ambiguous git hashes in index. Keeps the compact revision format for the annotate screen but uses full hash on the history view.
1 parent c5aba8e commit ffb82b4

File tree

11 files changed

+188
-101
lines changed

11 files changed

+188
-101
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* Copyright (c) 2007, 2022, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2019, Krystof Tulinger <[email protected]>.
2323
* Portions Copyright (c) 2020, Chris Fraire <[email protected]>.
24+
* Portions Copyright (c) 2023, Ric Harris <[email protected]>.
2425
*/
2526
package org.opengrok.indexer.history;
2627

@@ -66,7 +67,11 @@ public Annotation(AnnotationData annotationData) {
6667
}
6768

6869
void addLine(String revision, String author, boolean enabled) {
69-
annotationData.addLine(revision, author, enabled);
70+
annotationData.addLine(revision, author, enabled, null);
71+
}
72+
73+
void addLine(String revision, String author, boolean enabled, String displayRevision) {
74+
annotationData.addLine(revision, author, enabled, displayRevision);
7075
}
7176

7277
public String getFilename() {
@@ -104,6 +109,17 @@ public String getRevision(int line) {
104109
return annotationData.getRevision(line);
105110
}
106111

112+
/**
113+
* Gets the revision for the last change to the specified line in a format that may be specifically for display purposes.
114+
*
115+
* @param line line number (counting from 1)
116+
* @return revision string, or an empty string if there is no information
117+
* about the specified line
118+
*/
119+
public String getRevisionForDisplay(int line) {
120+
return annotationData.getRevisionForDisplay(line);
121+
}
122+
107123
/**
108124
* Gets all revisions that are in use, first is the lowest one (sorted using natural order).
109125
*

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

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
/*
2121
* Copyright (c) 2007, 2022, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2023, Ric Harris <[email protected]>.
2223
*/
2324
package org.opengrok.indexer.history;
2425

@@ -110,6 +111,23 @@ public String getRevision(int line) {
110111
}
111112
}
112113

114+
/**
115+
* Gets the revision for the last change to the specified line.
116+
*
117+
* @param line line number (counting from 1)
118+
* @return revision string, or an empty string if there is no information
119+
* about the specified line
120+
*/
121+
public String getRevisionForDisplay(int line) {
122+
try {
123+
AnnotationLine annotationLine = annotationLines.get(line - 1);
124+
return annotationLine.getRevision();
125+
} catch (IndexOutOfBoundsException e) {
126+
return "";
127+
}
128+
}
129+
130+
113131
/**
114132
* Gets the enabled state for the last change to the specified line.
115133
*
@@ -177,18 +195,19 @@ public int getWidestAuthor() {
177195
*/
178196
void addLine(final AnnotationLine annotationLine) {
179197
annotationLines.add(annotationLine);
180-
widestRevision = Math.max(widestRevision, annotationLine.getRevision().length());
198+
widestRevision = Math.max(widestRevision, annotationLine.getDisplayRevision().length());
181199
widestAuthor = Math.max(widestAuthor, annotationLine.getAuthor().length());
182200
}
183201

184202
/**
185203
* @param revision revision number
186204
* @param author author name
187205
* @param enabled whether the line is enabled
206+
* @param displayRevision a specialised revision number of display purposes. Can be null in which case the revision number will be used.
188207
* @see #addLine(AnnotationLine)
189208
*/
190-
void addLine(String revision, String author, boolean enabled) {
191-
final AnnotationLine annotationLine = new AnnotationLine(revision, author, enabled);
209+
void addLine(String revision, String author, boolean enabled, String displayRevision) {
210+
final AnnotationLine annotationLine = new AnnotationLine(revision, author, enabled, displayRevision);
192211
addLine(annotationLine);
193212
}
194213

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
/*
2121
* Copyright (c) 2007, 2022, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2023, Ric Harris <[email protected]>.
2223
*/
2324
package org.opengrok.indexer.history;
2425

@@ -40,14 +41,17 @@ public class AnnotationLine implements Serializable {
4041
private String author;
4142
private boolean enabled;
4243

44+
private String displayRevision;
45+
4346
public AnnotationLine() {
4447
// for serialization
4548
}
4649

47-
AnnotationLine(String revision, String author, boolean enabled) {
48-
this.revision = (revision == null) ? "" : revision;
49-
this.author = (author == null) ? "" : author;
50+
AnnotationLine(String revision, String author, boolean enabled, String displayRevision) {
51+
this.revision = revision == null ? "" : revision;
52+
this.author = author == null ? "" : author;
5053
this.enabled = enabled;
54+
this.setDisplayRevision(displayRevision);
5155
}
5256

5357
public String getRevision() {
@@ -74,6 +78,19 @@ public void setEnabled(boolean enabled) {
7478
this.enabled = enabled;
7579
}
7680

81+
/**
82+
* A specialised revision for display purposes.
83+
*
84+
* @return the display revision if not null, otherwise the revision.
85+
*/
86+
public String getDisplayRevision() {
87+
return displayRevision == null ? revision : displayRevision;
88+
}
89+
90+
public void setDisplayRevision(String displayRevision) {
91+
this.displayRevision = displayRevision;
92+
}
93+
7794
@Override
7895
public boolean equals(Object obj) {
7996
if (obj == null) {

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

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

@@ -377,8 +378,10 @@ private Annotation getAnnotation(String revision, String filePath, String fileNa
377378
for (int i = 0; i < rawText.size(); i++) {
378379
final PersonIdent sourceAuthor = result.getSourceAuthor(i);
379380
final RevCommit sourceCommit = result.getSourceCommit(i);
380-
annotation.addLine(sourceCommit.getId().abbreviate(GIT_ABBREV_LEN).
381-
name(), sourceAuthor.getName(), true);
381+
annotation.addLine(
382+
sourceCommit.getId().name(),
383+
sourceAuthor.getName(), true,
384+
sourceCommit.getId().abbreviate(GIT_ABBREV_LEN).name());
382385
}
383386
}
384387
} catch (GitAPIException e) {
@@ -449,6 +452,7 @@ public int getPerPartesCount() {
449452
return MAX_CHANGESETS;
450453
}
451454

455+
@Override
452456
public void accept(String sinceRevision, Consumer<BoundaryChangesets.IdWithProgress> visitor, Progress progress)
453457
throws HistoryException {
454458

@@ -476,10 +480,12 @@ public HistoryEntry getLastHistoryEntry(File file, boolean ui) throws HistoryExc
476480
return hist.getLastHistoryEntry();
477481
}
478482

483+
@Override
479484
public History getHistory(File file, String sinceRevision, String tillRevision) throws HistoryException {
480485
return getHistory(file, sinceRevision, tillRevision, null);
481486
}
482487

488+
@Override
483489
public void traverseHistory(File file, String sinceRevision, String tillRevision,
484490
Integer numCommits, List<ChangesetVisitor> visitors) throws HistoryException {
485491

@@ -496,7 +502,7 @@ public void traverseHistory(File file, String sinceRevision, String tillRevision
496502

497503
int num = 0;
498504
for (RevCommit commit : walk) {
499-
CommitInfo commitInfo = new CommitInfo(commit.getId().abbreviate(GIT_ABBREV_LEN).name(),
505+
CommitInfo commitInfo = new CommitInfo(commit.getId().name(),
500506
commit.getAuthorIdent().getWhen(), commit.getAuthorIdent().getName(),
501507
commit.getAuthorIdent().getEmailAddress(), commit.getFullMessage());
502508

@@ -734,7 +740,7 @@ private void rebuildTagList(File directory) {
734740

735741
for (Map.Entry<RevCommit, String> entry : commit2Tags.entrySet()) {
736742
int commitTime = entry.getKey().getCommitTime();
737-
Date date = new Date((long) (commitTime) * 1000);
743+
Date date = new Date((long) commitTime * 1000);
738744
GitTagEntry tagEntry = new GitTagEntry(entry.getKey().getName(),
739745
date, entry.getValue());
740746
this.tagList.add(tagEntry);
@@ -835,7 +841,7 @@ public String determineCurrentVersion(CommandTimeoutType cmdType) throws IOExcep
835841
try (RevWalk walk = new RevWalk(repository); ObjectReader reader = repository.newObjectReader()) {
836842
RevCommit commit = walk.parseCommit(head.getObjectId());
837843
int commitTime = commit.getCommitTime();
838-
Date date = new Date((long) (commitTime) * 1000);
844+
Date date = new Date((long) commitTime * 1000);
839845
return String.format("%s %s %s %s",
840846
format(date),
841847
reader.abbreviate(head.getObjectId()).name(),

opengrok-indexer/src/main/java/org/opengrok/indexer/web/Util.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* Portions Copyright (c) 2011, Jens Elkner.
2323
* Portions Copyright (c) 2017, 2020, Chris Fraire <[email protected]>.
2424
* Portions Copyright (c) 2019, Krystof Tulinger <[email protected]>.
25+
* Portions Copyright (c) 2023, Ric Harris <[email protected]>.
2526
*/
2627
package org.opengrok.indexer.web;
2728

@@ -762,7 +763,7 @@ private static void writeAnnotation(int num, Writer out, Annotation annotation,
762763
buf.append("<span class=\"most_recent_revision\">");
763764
buf.append('*');
764765
}
765-
htmlize(r, buf);
766+
htmlize(annotation.getRevisionForDisplay(num), buf);
766767
if (most_recent_revision) {
767768
buf.append("</span>"); // recent revision span
768769
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ void testEqualsNegativeFileName() {
6666
*/
6767
@Test
6868
void testEqualsNegative() {
69-
final AnnotationLine annotationLine1 = new AnnotationLine("1.0", "Me", true);
70-
final AnnotationLine annotationLine2 = new AnnotationLine("1.1", "Me", true);
71-
final AnnotationLine annotationLine3 = new AnnotationLine("1.2", "Me", true);
69+
final AnnotationLine annotationLine1 = new AnnotationLine("1.0", "Me", true, null);
70+
final AnnotationLine annotationLine2 = new AnnotationLine("1.1", "Me", true, null);
71+
final AnnotationLine annotationLine3 = new AnnotationLine("1.2", "Me", true, null);
7272

7373
AnnotationData annotationData1 = new AnnotationData();
7474
annotationData1.addLine(annotationLine1);
@@ -88,8 +88,8 @@ void testEqualsNegative() {
8888
*/
8989
@Test
9090
void testEqualsNegativeStoredRevision() {
91-
final AnnotationLine annotationLine1 = new AnnotationLine("1.0", "Me", true);
92-
final AnnotationLine annotationLine2 = new AnnotationLine("1.1", "Me", true);
91+
final AnnotationLine annotationLine1 = new AnnotationLine("1.0", "Me", true, null);
92+
final AnnotationLine annotationLine2 = new AnnotationLine("1.1", "Me", true, null);
9393

9494
AnnotationData annotationData1 = new AnnotationData();
9595
annotationData1.addLine(annotationLine1);
@@ -111,9 +111,9 @@ void testEqualsNegativeStoredRevision() {
111111
@Test
112112
void testEqualsPositive() {
113113
final String fileName = "foo.txt";
114-
final AnnotationLine annotationLine1 = new AnnotationLine("1.0", "Me", true);
115-
final AnnotationLine annotationLine2 = new AnnotationLine("1.1", "Me", true);
116-
final AnnotationLine annotationLine3 = new AnnotationLine("1.2", "Me", true);
114+
final AnnotationLine annotationLine1 = new AnnotationLine("1.0", "Me", true, null);
115+
final AnnotationLine annotationLine2 = new AnnotationLine("1.1", "Me", true, null);
116+
final AnnotationLine annotationLine3 = new AnnotationLine("1.2", "Me", true, null);
117117

118118
AnnotationData annotationData1 = new AnnotationData(fileName);
119119
annotationData1.addLine(annotationLine1);

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

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
/*
2121
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2023, Ric Harris <[email protected]>.
2223
*/
2324
package org.opengrok.indexer.history;
2425

@@ -36,8 +37,8 @@ class AnnotationLineTest {
3637
*/
3738
@Test
3839
void testEqualsNegative1() {
39-
AnnotationLine annotationLine1 = new AnnotationLine("1.0", "bar", true);
40-
AnnotationLine annotationLine2 = new AnnotationLine("1.0", "barX", true);
40+
AnnotationLine annotationLine1 = new AnnotationLine("1.0", "bar", true, null);
41+
AnnotationLine annotationLine2 = new AnnotationLine("1.0", "barX", true, null);
4142
assertNotEquals(annotationLine1, annotationLine2);
4243
}
4344

@@ -46,8 +47,8 @@ void testEqualsNegative1() {
4647
*/
4748
@Test
4849
void testEqualsNegative2() {
49-
AnnotationLine annotationLine1 = new AnnotationLine("1.0.1", "bar", true);
50-
AnnotationLine annotationLine2 = new AnnotationLine("1.0", "bar", true);
50+
AnnotationLine annotationLine1 = new AnnotationLine("1.0.1", "bar", true, null);
51+
AnnotationLine annotationLine2 = new AnnotationLine("1.0", "bar", true, null);
5152
assertNotEquals(annotationLine1, annotationLine2);
5253
}
5354

@@ -56,8 +57,8 @@ void testEqualsNegative2() {
5657
*/
5758
@Test
5859
void testEqualsNegative3() {
59-
AnnotationLine annotationLine1 = new AnnotationLine("1.0", "bar", true);
60-
AnnotationLine annotationLine2 = new AnnotationLine("1.0", "bar", false);
60+
AnnotationLine annotationLine1 = new AnnotationLine("1.0", "bar", true, null);
61+
AnnotationLine annotationLine2 = new AnnotationLine("1.0", "bar", false, null);
6162
assertNotEquals(annotationLine1, annotationLine2);
6263
}
6364

@@ -66,8 +67,26 @@ void testEqualsNegative3() {
6667
*/
6768
@Test
6869
void testEqualsPositive() {
69-
AnnotationLine annotationLine1 = new AnnotationLine("1.0", "bar", true);
70-
AnnotationLine annotationLine2 = new AnnotationLine("1.0", "bar", true);
70+
AnnotationLine annotationLine1 = new AnnotationLine("1.0", "bar", true, null);
71+
AnnotationLine annotationLine2 = new AnnotationLine("1.0", "bar", true, null);
7172
assertEquals(annotationLine1, annotationLine2);
7273
}
74+
75+
/**
76+
* Verify that the display revision getter falls back to the revision.
77+
*/
78+
@Test
79+
void testDisplayRevisionFallBack() {
80+
AnnotationLine annotationLine1 = new AnnotationLine("1.0", "bar", true, null);
81+
assertEquals("1.0", annotationLine1.getDisplayRevision());
82+
}
83+
84+
/**
85+
* Verify that the display revision getter overrides the base revision.
86+
*/
87+
@Test
88+
void testDisplayRevisionOverrides() {
89+
AnnotationLine annotationLine1 = new AnnotationLine("1.0", "bar", true, "1.0.abcd");
90+
assertEquals("1.0.abcd", annotationLine1.getDisplayRevision());
91+
}
7392
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void testStoreAndGet() throws Exception {
7171

7272
cache.store(historyToStore, repo);
7373

74-
assertEquals("206f862b", cache.getLatestCachedRevision(repo), "latest git-octopus commit");
74+
assertEquals("206f862b18a4e1a73025e6c0c82883cb92a89b1d", cache.getLatestCachedRevision(repo), "latest git-octopus commit");
7575

7676
History dHist = cache.get(new File(reposRoot, "d"), repo, true);
7777
assertNotNull(dHist, "cache get() for git-octopus/d");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public void testOctopusHistory() throws Exception {
181181
assertEquals(4, allFiles.size(), "git-octopus files from log");
182182

183183
HistoryEntry first = entries.get(0);
184-
assertEquals("206f862b", first.getRevision(), "should be merge commit hash");
184+
assertEquals("206f862b18a4e1a73025e6c0c82883cb92a89b1d", first.getRevision(), "should be merge commit hash");
185185
assertEquals("Merge branches 'file_a', 'file_b' and 'file_c' into master, and add d",
186186
first.getMessage(),
187187
"should be merge commit message");

0 commit comments

Comments
 (0)