Introduce a constant for the length of an abbreviated hash string

Signed-off-by: Sebastian Schuberth <sebastian.schuberth@bosch.io>
Change-Id: I196d58a813f7caa1965af4cf8e2f977ed4cdc350
This commit is contained in:
Sebastian Schuberth 2022-01-05 18:21:33 +01:00 committed by Matthias Sohn
parent f7707e402a
commit 20bdcf9ea8
11 changed files with 57 additions and 15 deletions

View File

@ -15,6 +15,7 @@
import static java.lang.Integer.valueOf; import static java.lang.Integer.valueOf;
import static java.lang.Long.valueOf; import static java.lang.Long.valueOf;
import static org.eclipse.jgit.lib.Constants.OBJECT_ID_ABBREV_STRING_LENGTH;
import static org.eclipse.jgit.lib.Constants.OBJECT_ID_STRING_LENGTH; import static org.eclipse.jgit.lib.Constants.OBJECT_ID_STRING_LENGTH;
import java.io.IOException; import java.io.IOException;
@ -116,7 +117,8 @@ protected void run() {
boolean autoAbbrev = abbrev == 0; boolean autoAbbrev = abbrev == 0;
if (abbrev == 0) { if (abbrev == 0) {
abbrev = db.getConfig().getInt("core", "abbrev", 7); //$NON-NLS-1$ //$NON-NLS-2$ abbrev = db.getConfig().getInt("core", "abbrev", //$NON-NLS-1$ //$NON-NLS-2$
OBJECT_ID_ABBREV_STRING_LENGTH);
} }
if (!showBlankBoundary) { if (!showBlankBoundary) {
root = db.getConfig().getBoolean("blame", "blankboundary", false); //$NON-NLS-1$ //$NON-NLS-2$ root = db.getConfig().getBoolean("blame", "blankboundary", false); //$NON-NLS-1$ //$NON-NLS-2$

View File

@ -10,6 +10,8 @@
package org.eclipse.jgit.pgm; package org.eclipse.jgit.pgm;
import static org.eclipse.jgit.lib.Constants.OBJECT_ID_ABBREV_STRING_LENGTH;
import java.io.IOException; import java.io.IOException;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.Map; import java.util.Map;
@ -144,8 +146,10 @@ protected void run() {
case FAST_FORWARD: case FAST_FORWARD:
ObjectId oldHeadId = oldHead.getObjectId(); ObjectId oldHeadId = oldHead.getObjectId();
if (oldHeadId != null) { if (oldHeadId != null) {
String oldId = oldHeadId.abbreviate(7).name(); String oldId = oldHeadId
String newId = result.getNewHead().abbreviate(7).name(); .abbreviate(OBJECT_ID_ABBREV_STRING_LENGTH).name();
String newId = result.getNewHead()
.abbreviate(OBJECT_ID_ABBREV_STRING_LENGTH).name();
outw.println(MessageFormat.format(CLIText.get().updating, outw.println(MessageFormat.format(CLIText.get().updating,
oldId, newId)); oldId, newId));
} }

View File

@ -9,6 +9,8 @@
*/ */
package org.eclipse.jgit.pgm; package org.eclipse.jgit.pgm;
import static org.eclipse.jgit.lib.Constants.OBJECT_ID_ABBREV_STRING_LENGTH;
import java.io.IOException; import java.io.IOException;
import java.util.Collection; import java.util.Collection;
@ -45,7 +47,8 @@ protected void run() {
private String toString(ReflogEntry entry, int i) { private String toString(ReflogEntry entry, int i) {
final StringBuilder s = new StringBuilder(); final StringBuilder s = new StringBuilder();
s.append(entry.getNewId().abbreviate(7).name()); s.append(entry.getNewId().abbreviate(OBJECT_ID_ABBREV_STRING_LENGTH)
.name());
s.append(" "); //$NON-NLS-1$ s.append(" "); //$NON-NLS-1$
s.append(ref == null ? Constants.HEAD : Repository.shortenRefName(ref)); s.append(ref == null ? Constants.HEAD : Repository.shortenRefName(ref));
s.append("@{" + i + "}:"); //$NON-NLS-1$ //$NON-NLS-2$ s.append("@{" + i + "}:"); //$NON-NLS-1$ //$NON-NLS-2$

View File

@ -9,6 +9,7 @@
*/ */
package org.eclipse.jgit.api; package org.eclipse.jgit.api;
import static org.eclipse.jgit.lib.Constants.OBJECT_ID_ABBREV_STRING_LENGTH;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
@ -166,7 +167,9 @@ public void testRevertMultipleWithFail() throws IOException,
checkFile(new File(db.getWorkTree(), "a"), "first\n" checkFile(new File(db.getWorkTree(), "a"), "first\n"
+ "<<<<<<< master\n" + "second\n" + "third\n" + "=======\n" + "<<<<<<< master\n" + "second\n" + "third\n" + "=======\n"
+ ">>>>>>> " + secondCommit.getId().abbreviate(7).name() + ">>>>>>> "
+ secondCommit.getId()
.abbreviate(OBJECT_ID_ABBREV_STRING_LENGTH).name()
+ " add second\n"); + " add second\n");
Iterator<RevCommit> history = git.log().call().iterator(); Iterator<RevCommit> history = git.log().call().iterator();
RevCommit revertCommit = history.next(); RevCommit revertCommit = history.next();

View File

@ -9,6 +9,8 @@
*/ */
package org.eclipse.jgit.api; package org.eclipse.jgit.api;
import static org.eclipse.jgit.lib.Constants.OBJECT_ID_ABBREV_STRING_LENGTH;
import java.io.IOException; import java.io.IOException;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.LinkedList; import java.util.LinkedList;
@ -124,7 +126,7 @@ public CherryPickResult call() throws GitAPIException, NoMessageException,
final RevCommit srcParent = getParentCommit(srcCommit, revWalk); final RevCommit srcParent = getParentCommit(srcCommit, revWalk);
String ourName = calculateOurName(headRef); String ourName = calculateOurName(headRef);
String cherryPickName = srcCommit.getId().abbreviate(7).name() String cherryPickName = srcCommit.getId().abbreviate(OBJECT_ID_ABBREV_STRING_LENGTH).name()
+ " " + srcCommit.getShortMessage(); //$NON-NLS-1$ + " " + srcCommit.getShortMessage(); //$NON-NLS-1$
Merger merger = strategy.newMerger(repo); Merger merger = strategy.newMerger(repo);

View File

@ -9,6 +9,8 @@
*/ */
package org.eclipse.jgit.api; package org.eclipse.jgit.api;
import static org.eclipse.jgit.lib.Constants.OBJECT_ID_ABBREV_STRING_LENGTH;
import java.io.IOException; import java.io.IOException;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.LinkedList; import java.util.LinkedList;
@ -128,8 +130,9 @@ public RevCommit call() throws NoMessageException, UnmergedPathsException,
revWalk.parseHeaders(srcParent); revWalk.parseHeaders(srcParent);
String ourName = calculateOurName(headRef); String ourName = calculateOurName(headRef);
String revertName = srcCommit.getId().abbreviate(7).name() String revertName = srcCommit.getId()
+ " " + srcCommit.getShortMessage(); //$NON-NLS-1$ .abbreviate(OBJECT_ID_ABBREV_STRING_LENGTH).name() + " " //$NON-NLS-1$
+ srcCommit.getShortMessage();
ResolveMerger merger = (ResolveMerger) strategy.newMerger(repo); ResolveMerger merger = (ResolveMerger) strategy.newMerger(repo);
merger.setWorkingTreeIterator(new FileTreeIterator(repo)); merger.setWorkingTreeIterator(new FileTreeIterator(repo));

View File

@ -9,6 +9,8 @@
*/ */
package org.eclipse.jgit.api; package org.eclipse.jgit.api;
import static org.eclipse.jgit.lib.Constants.OBJECT_ID_ABBREV_STRING_LENGTH;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -302,7 +304,8 @@ public void apply(DirCacheEntry ent) {
builder.setParentId(headCommit); builder.setParentId(headCommit);
builder.setTreeId(cache.writeTree(inserter)); builder.setTreeId(cache.writeTree(inserter));
builder.setMessage(MessageFormat.format(indexMessage, branch, builder.setMessage(MessageFormat.format(indexMessage, branch,
headCommit.abbreviate(7).name(), headCommit.abbreviate(OBJECT_ID_ABBREV_STRING_LENGTH)
.name(),
headCommit.getShortMessage())); headCommit.getShortMessage()));
ObjectId indexCommit = inserter.insert(builder); ObjectId indexCommit = inserter.insert(builder);
@ -319,7 +322,10 @@ public void apply(DirCacheEntry ent) {
builder.setParentIds(new ObjectId[0]); builder.setParentIds(new ObjectId[0]);
builder.setTreeId(untrackedDirCache.writeTree(inserter)); builder.setTreeId(untrackedDirCache.writeTree(inserter));
builder.setMessage(MessageFormat.format(MSG_UNTRACKED, builder.setMessage(MessageFormat.format(MSG_UNTRACKED,
branch, headCommit.abbreviate(7).name(), branch,
headCommit
.abbreviate(OBJECT_ID_ABBREV_STRING_LENGTH)
.name(),
headCommit.getShortMessage())); headCommit.getShortMessage()));
untrackedCommit = inserter.insert(builder); untrackedCommit = inserter.insert(builder);
} }
@ -339,7 +345,8 @@ public void apply(DirCacheEntry ent) {
builder.addParentId(untrackedCommit); builder.addParentId(untrackedCommit);
builder.setMessage(MessageFormat.format( builder.setMessage(MessageFormat.format(
workingDirectoryMessage, branch, workingDirectoryMessage, branch,
headCommit.abbreviate(7).name(), headCommit.abbreviate(OBJECT_ID_ABBREV_STRING_LENGTH)
.name(),
headCommit.getShortMessage())); headCommit.getShortMessage()));
builder.setTreeId(cache.writeTree(inserter)); builder.setTreeId(cache.writeTree(inserter));
commitId = inserter.insert(builder); commitId = inserter.insert(builder);

View File

@ -18,6 +18,7 @@
import static org.eclipse.jgit.diff.DiffEntry.ChangeType.RENAME; import static org.eclipse.jgit.diff.DiffEntry.ChangeType.RENAME;
import static org.eclipse.jgit.diff.DiffEntry.Side.NEW; import static org.eclipse.jgit.diff.DiffEntry.Side.NEW;
import static org.eclipse.jgit.diff.DiffEntry.Side.OLD; import static org.eclipse.jgit.diff.DiffEntry.Side.OLD;
import static org.eclipse.jgit.lib.Constants.OBJECT_ID_ABBREV_STRING_LENGTH;
import static org.eclipse.jgit.lib.Constants.encode; import static org.eclipse.jgit.lib.Constants.encode;
import static org.eclipse.jgit.lib.Constants.encodeASCII; import static org.eclipse.jgit.lib.Constants.encodeASCII;
import static org.eclipse.jgit.lib.FileMode.GITLINK; import static org.eclipse.jgit.lib.FileMode.GITLINK;
@ -90,7 +91,7 @@ public class DiffFormatter implements AutoCloseable {
private int context = 3; private int context = 3;
private int abbreviationLength = 7; private int abbreviationLength = OBJECT_ID_ABBREV_STRING_LENGTH;
private DiffAlgorithm diffAlgorithm; private DiffAlgorithm diffAlgorithm;

View File

@ -48,6 +48,15 @@ public final class Constants {
*/ */
public static final int OBJECT_ID_STRING_LENGTH = OBJECT_ID_LENGTH * 2; public static final int OBJECT_ID_STRING_LENGTH = OBJECT_ID_LENGTH * 2;
/**
* The historic length of an abbreviated Git object hash string. Git 2.11
* changed this static number to a dynamically calculated one that scales
* as the repository grows.
*
* @since 6.1
*/
public static final int OBJECT_ID_ABBREV_STRING_LENGTH = 7;
/** Special name for the "HEAD" symbolic-ref. */ /** Special name for the "HEAD" symbolic-ref. */
public static final String HEAD = "HEAD"; public static final String HEAD = "HEAD";

View File

@ -10,6 +10,8 @@
package org.eclipse.jgit.lib; package org.eclipse.jgit.lib;
import static org.eclipse.jgit.lib.Constants.OBJECT_ID_ABBREV_STRING_LENGTH;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@ -76,7 +78,7 @@ public abstract class ObjectReader implements AutoCloseable {
*/ */
public AbbreviatedObjectId abbreviate(AnyObjectId objectId) public AbbreviatedObjectId abbreviate(AnyObjectId objectId)
throws IOException { throws IOException {
return abbreviate(objectId, 7); return abbreviate(objectId, OBJECT_ID_ABBREV_STRING_LENGTH);
} }
/** /**

View File

@ -12,6 +12,8 @@
package org.eclipse.jgit.transport; package org.eclipse.jgit.transport;
import static org.eclipse.jgit.lib.Constants.OBJECT_ID_ABBREV_STRING_LENGTH;
import org.eclipse.jgit.lib.AnyObjectId; import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.RefUpdate; import org.eclipse.jgit.lib.RefUpdate;
@ -184,9 +186,13 @@ public String toString() {
if (forceUpdate) if (forceUpdate)
sb.append(" (forced)"); sb.append(" (forced)");
sb.append(" "); sb.append(" ");
sb.append(oldObjectId == null ? "" : oldObjectId.abbreviate(7).name()); sb.append(oldObjectId == null ? ""
: oldObjectId.abbreviate(OBJECT_ID_ABBREV_STRING_LENGTH)
.name());
sb.append(".."); sb.append("..");
sb.append(newObjectId == null ? "" : newObjectId.abbreviate(7).name()); sb.append(newObjectId == null ? ""
: newObjectId.abbreviate(OBJECT_ID_ABBREV_STRING_LENGTH)
.name());
sb.append("]"); sb.append("]");
return sb.toString(); return sb.toString();
} }