Skip to content

Commit

Permalink
Use a digest to generate the API baseline target location
Browse files Browse the repository at this point in the history
Currently the ApiModelFactory#generateTargetLocation only uses the
sequence number for its location what is unreliable as many aspects can
influence the actual "change" in a target.

This now uses a SHA-1 hash over the actual target content to produce a
location that is unique for a given target content.
  • Loading branch information
laeubi committed Dec 31, 2024
1 parent 177fe6b commit 12198a6
Showing 1 changed file with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
package org.eclipse.pde.api.tools.internal.model;

import java.io.File;
import java.io.OutputStream;
import java.net.URI;
import java.security.DigestOutputStream;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -42,7 +45,7 @@
import org.eclipse.pde.core.target.ITargetPlatformService;
import org.eclipse.pde.core.target.TargetBundle;
import org.eclipse.pde.internal.core.target.ExternalFileTargetHandle;
import org.eclipse.pde.internal.core.target.TargetDefinition;
import org.eclipse.pde.internal.core.target.TargetDefinitionPersistenceHelper;
import org.eclipse.pde.internal.core.target.WorkspaceFileTargetHandle;

/**
Expand Down Expand Up @@ -340,7 +343,7 @@ public static IApiBaseline newApiBaselineFromTarget(String name, ITargetDefiniti

/**
* Create predictable location description for a target definition. Form is
* <code>target:/targetSeq/definitionLocation</code>. A location must be
* <code>target:/[target hashcode}/definitionLocation</code>. A location must be
* compatible with {@link IPath#fromPortableString(String)}.
*
* @param definition the target platform definition
Expand All @@ -351,8 +354,16 @@ public static IApiBaseline newApiBaselineFromTarget(String name, ITargetDefiniti
private static String generateTargetLocation(ITargetDefinition definition) {
StringBuilder sb = new StringBuilder(TARGET_PREFIX);
sb.append(IPath.SEPARATOR);
if (definition instanceof TargetDefinition target) {
sb.append(target.getSequenceNumber());
try {
MessageDigest digest = MessageDigest.getInstance("SHA-1"); //$NON-NLS-1$
try (DigestOutputStream output = new DigestOutputStream(OutputStream.nullOutputStream(), digest)) {
TargetDefinitionPersistenceHelper.persistXML(definition, output);
}
for (byte b : digest.digest()) {
sb.append(Integer.toHexString(b & 0xFF));
}
} catch (Exception e) {
// can't record a hashcode then...
}
sb.append(IPath.SEPARATOR);
sb.append(getDefinitionIdentifier(definition));
Expand Down

0 comments on commit 12198a6

Please sign in to comment.