diff --git a/org.fortiss.tooling.ext.quality/src/org/fortiss/tooling/ext/quality/storage/.ratings b/org.fortiss.tooling.ext.quality/src/org/fortiss/tooling/ext/quality/storage/.ratings index 1af854e96f300936f6e33d7f1b817a581a429563..a7cf9548b4d9bf548e8e5e8c421052006cc6af96 100644 --- a/org.fortiss.tooling.ext.quality/src/org/fortiss/tooling/ext/quality/storage/.ratings +++ b/org.fortiss.tooling.ext.quality/src/org/fortiss/tooling/ext/quality/storage/.ratings @@ -1,2 +1,2 @@ -CSVFileWriter.java a779fce240c25aae1b72dfd11d27b74b0b21d599 RED +CSVFileWriter.java 0cae6203edc159cec0d3f68c61f4b04a3c61eecd RED ModelQualityStorageManager.java 8293f17743bdc85e2595eae99b978ed868bd029b RED diff --git a/org.fortiss.tooling.ext.quality/src/org/fortiss/tooling/ext/quality/storage/CSVFileWriter.java b/org.fortiss.tooling.ext.quality/src/org/fortiss/tooling/ext/quality/storage/CSVFileWriter.java index a779fce240c25aae1b72dfd11d27b74b0b21d599..0cae6203edc159cec0d3f68c61f4b04a3c61eecd 100644 --- a/org.fortiss.tooling.ext.quality/src/org/fortiss/tooling/ext/quality/storage/CSVFileWriter.java +++ b/org.fortiss.tooling.ext.quality/src/org/fortiss/tooling/ext/quality/storage/CSVFileWriter.java @@ -15,18 +15,14 @@ +--------------------------------------------------------------------------*/ package org.fortiss.tooling.ext.quality.storage; -import static java.lang.Integer.toHexString; - import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.time.LocalDateTime; @@ -102,7 +98,7 @@ public class CSVFileWriter { // Add topLevelElement.doSave(new NullProgressMonitor()); Path modelFile = new File(ModelQualityStorageManager.MODEL_PROJECT_DIR, topLevelElement.getSaveableName()).toPath(); - String projectHash = computeGitHash(modelFile); + String projectHash = computeGitObjectHash(modelFile); List<String> allKeys = new ArrayList<>(); boolean createNewIndex = true; @@ -216,27 +212,39 @@ public class CSVFileWriter { } /** - * Function to generate the hash for the model in order to later on match versions of the file - * project with saved metrics (assuming both are under version control using Git). + * Return the Git object hash for the contents of the given file: + * <p> + * {@code SHA-1("blob <size>\0"+<contents>).asHexString()}. */ - private static String computeGitHash(Path filePath) + private static String computeGitObjectHash(Path filePath) throws NoSuchAlgorithmException, IOException { - String fileAsString = new String(Files.readAllBytes(Paths.get(filePath.toString()))); - MessageDigest digest = MessageDigest.getInstance("SHA-1"); - byte[] hash = digest.digest(fileAsString.getBytes(StandardCharsets.UTF_8)); - return bytesToHex(hash); - } - /** Switching from bytes to Hex notation. */ - private static String bytesToHex(byte[] hash) { - StringBuilder hexString = new StringBuilder(2 * hash.length); - for(byte b : hash) { - String hex = toHexString(0xff & b); - if(hex.length() == 1) { - hexString.append('0'); + try(BufferedReader reader = new BufferedReader(new FileReader(filePath.toString()))) { + // Read file line-wise with UNIX line endings + StringBuilder inputBuilder = new StringBuilder(); + do { + String line = reader.readLine(); + if(line == null) { + break; + } + inputBuilder.append(line); + inputBuilder.append("\n"); + } while(true); + + // Construct pre-image (input to hash function) according to Git specification + String fileContents = inputBuilder.toString(); + int n = fileContents.length(); + String preImage = "blob " + n + "\0" + fileContents; + + // Compute hash and convert it to a hex string + MessageDigest sha1 = MessageDigest.getInstance("SHA-1"); + byte[] digest = sha1.digest(preImage.getBytes()); + + StringBuilder resultBuilder = new StringBuilder(); + for(byte b : digest) { + resultBuilder.append(String.format("%02x", b & 0xff)); } - hexString.append(hex); + return resultBuilder.toString(); } - return hexString.toString(); } }