Skip to content

Commit 788db72

Browse files
committed
Use a digest to generate the API baseline target location
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.
1 parent c220b37 commit 788db72

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

apitools/org.eclipse.pde.api.tools/src/org/eclipse/pde/api/tools/internal/model/ApiModelFactory.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
package org.eclipse.pde.api.tools.internal.model;
1616

1717
import java.io.File;
18+
import java.io.OutputStream;
1819
import java.net.URI;
20+
import java.security.DigestOutputStream;
21+
import java.security.MessageDigest;
1922
import java.util.ArrayList;
2023
import java.util.List;
2124

@@ -42,7 +45,7 @@
4245
import org.eclipse.pde.core.target.ITargetPlatformService;
4346
import org.eclipse.pde.core.target.TargetBundle;
4447
import org.eclipse.pde.internal.core.target.ExternalFileTargetHandle;
45-
import org.eclipse.pde.internal.core.target.TargetDefinition;
48+
import org.eclipse.pde.internal.core.target.TargetDefinitionPersistenceHelper;
4649
import org.eclipse.pde.internal.core.target.WorkspaceFileTargetHandle;
4750

4851
/**
@@ -340,7 +343,7 @@ public static IApiBaseline newApiBaselineFromTarget(String name, ITargetDefiniti
340343

341344
/**
342345
* Create predictable location description for a target definition. Form is
343-
* <code>target:/targetSeq/definitionLocation</code>. A location must be
346+
* <code>target:/[target hashcode}/definitionLocation</code>. A location must be
344347
* compatible with {@link IPath#fromPortableString(String)}.
345348
*
346349
* @param definition the target platform definition
@@ -351,8 +354,16 @@ public static IApiBaseline newApiBaselineFromTarget(String name, ITargetDefiniti
351354
private static String generateTargetLocation(ITargetDefinition definition) {
352355
StringBuilder sb = new StringBuilder(TARGET_PREFIX);
353356
sb.append(IPath.SEPARATOR);
354-
if (definition instanceof TargetDefinition target) {
355-
sb.append(target.getSequenceNumber());
357+
try {
358+
MessageDigest digest = MessageDigest.getInstance("SHA-1"); //$NON-NLS-1$
359+
try (DigestOutputStream output = new DigestOutputStream(OutputStream.nullOutputStream(), digest)) {
360+
TargetDefinitionPersistenceHelper.persistXML(definition, output);
361+
}
362+
for (byte b : digest.digest()) {
363+
sb.append(Integer.toHexString(b & 0xFF));
364+
}
365+
} catch (Exception e) {
366+
// can't record a hashcode then...
356367
}
357368
sb.append(IPath.SEPARATOR);
358369
sb.append(getDefinitionIdentifier(definition));

0 commit comments

Comments
 (0)