Skip to content

Commit

Permalink
feat: add code syntatic normalization with spork
Browse files Browse the repository at this point in the history
  • Loading branch information
jpedroh committed Nov 19, 2024
1 parent f875e68 commit 88f21c7
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 22 deletions.
49 changes: 27 additions & 22 deletions src/main/injectors/GenericMergeModule.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,8 @@ import interfaces.DataCollector
import interfaces.OutputProcessor
import interfaces.ProjectProcessor
import services.commitFilters.MutuallyModifiedFilesCommitFilter
import services.dataCollectors.common.CompareScenarioMergeConflictsDataCollector
import services.dataCollectors.common.RunNormalizationOnScenarioFilesDataCollector
import services.dataCollectors.common.RunNormalizationOnScenarioFilesWithSporkDataCollector
import services.dataCollectors.common.SyntacticallyCompareScenarioFilesDataCollector
import services.dataCollectors.mergeToolExecutors.GitMergeFileMergeToolDataCollector
import services.dataCollectors.mergeToolExecutors.JDimeMergeToolExecutorDataCollector
import services.dataCollectors.mergeToolExecutors.LastMergeMergeToolExecutorDataCollector
import services.dataCollectors.mergeToolExecutors.SporkMergeToolExecutorDataCollector
import services.outputProcessors.EmptyOutputProcessor
import services.projectProcessors.DummyProjectProcessor

Expand All @@ -26,28 +21,38 @@ class GenericMergeModule extends AbstractModule {
Multibinder<DataCollector> dataCollectorBinder = Multibinder.newSetBinder(binder(), DataCollector.class)

// Run the merge tools on the scenarios
dataCollectorBinder.addBinding().to(JDimeMergeToolExecutorDataCollector.class)
dataCollectorBinder.addBinding().to(LastMergeMergeToolExecutorDataCollector.class)
dataCollectorBinder.addBinding().to(SporkMergeToolExecutorDataCollector.class)
dataCollectorBinder.addBinding().to(GitMergeFileMergeToolDataCollector.class)
// dataCollectorBinder.addBinding().to(JDimeMergeToolExecutorDataCollector.class)
// dataCollectorBinder.addBinding().to(LastMergeMergeToolExecutorDataCollector.class)
// dataCollectorBinder.addBinding().to(SporkMergeToolExecutorDataCollector.class)
// dataCollectorBinder.addBinding().to(GitMergeFileMergeToolDataCollector.class)

// Normalize the output files formatting due to jDime pretty-printing
dataCollectorBinder.addBinding().toInstance(new RunNormalizationOnScenarioFilesDataCollector(["merge.java", "merge.spork.java", "merge.last_merge.java"]))
// dataCollectorBinder.addBinding().toInstance(new RunNormalizationOnScenarioFilesDataCollector(["merge.java", "merge.spork.java", "merge.last_merge.java"]))

// Run comparisons between tools themselves
dataCollectorBinder.addBinding().toInstance(new SyntacticallyCompareScenarioFilesDataCollector("merge.last_merge.java", "merge.spork.java"))
dataCollectorBinder.addBinding().toInstance(new SyntacticallyCompareScenarioFilesDataCollector("merge.last_merge.java", "merge.jdime.java"))
dataCollectorBinder.addBinding().toInstance(new SyntacticallyCompareScenarioFilesDataCollector("merge.jdime.java", "merge.spork.java"))

// Run comparisons between tools and the original merge file
dataCollectorBinder.addBinding().toInstance(new SyntacticallyCompareScenarioFilesDataCollector("merge.jdime.java", "merge.java"))
dataCollectorBinder.addBinding().toInstance(new SyntacticallyCompareScenarioFilesDataCollector("merge.last_merge.java", "merge.java"))
dataCollectorBinder.addBinding().toInstance(new SyntacticallyCompareScenarioFilesDataCollector("merge.spork.java", "merge.java"))
// Normalize the output files formatting due to Spork pretty-printing
dataCollectorBinder.addBinding().toInstance(new RunNormalizationOnScenarioFilesWithSporkDataCollector("merge.java", "merge.spork_normalized.java"))
dataCollectorBinder.addBinding().toInstance(new RunNormalizationOnScenarioFilesWithSporkDataCollector("merge.last_merge.java", "merge.last_merge.spork_normalized.java"))

dataCollectorBinder.addBinding().toInstance(new SyntacticallyCompareScenarioFilesDataCollector("merge.last_merge.spork_normalized.java", "merge.spork.java"))

dataCollectorBinder.addBinding().toInstance(new SyntacticallyCompareScenarioFilesDataCollector("merge.spork.java", "merge.spork_normalized.java"))


// Run comparisons between tools themselves
// dataCollectorBinder.addBinding().toInstance(new SyntacticallyCompareScenarioFilesDataCollector("merge.last_merge.java", "merge.spork.java"))
// dataCollectorBinder.addBinding().toInstance(new SyntacticallyCompareScenarioFilesDataCollector("merge.last_merge.java", "merge.jdime.java"))
// dataCollectorBinder.addBinding().toInstance(new SyntacticallyCompareScenarioFilesDataCollector("merge.jdime.java", "merge.spork.java"))
//
// // Run comparisons between tools and the original merge file
// dataCollectorBinder.addBinding().toInstance(new SyntacticallyCompareScenarioFilesDataCollector("merge.jdime.java", "merge.java"))
// dataCollectorBinder.addBinding().toInstance(new SyntacticallyCompareScenarioFilesDataCollector("merge.last_merge.java", "merge.java"))
// dataCollectorBinder.addBinding().toInstance(new SyntacticallyCompareScenarioFilesDataCollector("merge.spork.java", "merge.java"))

// Run comparisons between conflicts themselves
dataCollectorBinder.addBinding().toInstance(new CompareScenarioMergeConflictsDataCollector("merge.last_merge.java", "merge.spork.java"))
dataCollectorBinder.addBinding().toInstance(new CompareScenarioMergeConflictsDataCollector("merge.last_merge.java", "merge.jdime.java"))
dataCollectorBinder.addBinding().toInstance(new CompareScenarioMergeConflictsDataCollector("merge.jdime.java", "merge.spork.java"))
// dataCollectorBinder.addBinding().toInstance(new CompareScenarioMergeConflictsDataCollector("merge.last_merge.java", "merge.spork.java"))
// dataCollectorBinder.addBinding().toInstance(new CompareScenarioMergeConflictsDataCollector("merge.last_merge.java", "merge.jdime.java"))
// dataCollectorBinder.addBinding().toInstance(new CompareScenarioMergeConflictsDataCollector("merge.jdime.java", "merge.spork.java"))

Multibinder<OutputProcessor> outputProcessorBinder = Multibinder.newSetBinder(binder(), OutputProcessor.class)
outputProcessorBinder.addBinding().to(EmptyOutputProcessor.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package services.dataCollectors.common

import interfaces.DataCollector
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger
import project.MergeCommit
import project.Project
import services.dataCollectors.S3MMergesCollector.MergeScenarioCollector
import util.ProcessRunner

import java.nio.file.Path

class RunNormalizationOnScenarioFilesWithSporkDataCollector implements DataCollector {
private static Logger LOG = LogManager.getLogger(RunNormalizationOnScenarioFilesWithSporkDataCollector.class)

private static final String SPORK_JAR_PATH = "${System.getProperty("user.dir")}/dependencies/spork.jar"

private String inputFileName
private String outputFileName

RunNormalizationOnScenarioFilesWithSporkDataCollector(String inputFileName, String outputFileName) {
this.inputFileName = inputFileName
this.outputFileName = outputFileName
}

@Override
void collectData(Project project, MergeCommit mergeCommit) {
def scenarioFiles = MergeScenarioCollector.collectNonFastForwardMergeScenarios(project, mergeCommit)

LOG.trace("Starting normalization of scenario files")
scenarioFiles.parallelStream().forEach(scenarioFile -> {
normalizeFile(scenarioFile)
})
LOG.trace("Finished normalization of scenario files")
}

private void normalizeFile(Path scenarioDirectory) {
def processBuilder = ProcessRunner.buildProcess(System.getProperty("user.dir"))
processBuilder.command().addAll(getBuildParameters(scenarioDirectory))

LOG.trace("Calling spork with command \"${processBuilder.command().join(' ')}\"")
def exitCode = ProcessRunner.startProcess(processBuilder).waitFor()
if (exitCode != 0) {
LOG.warn("File normalization failed with exit code ${exitCode}")
}
}

private List<String> getBuildParameters(Path scenarioDirectory) {
def list = new ArrayList<String>()
list.add("java")
list.add("-jar")
list.add(SPORK_JAR_PATH)
list.add(scenarioDirectory.resolve(inputFileName).toString())
list.add(scenarioDirectory.resolve(inputFileName).toString())
list.add(scenarioDirectory.resolve(inputFileName).toString())
list.add("--output=${scenarioDirectory.resolve(outputFileName).toString()}".toString())
return list
}
}

0 comments on commit 88f21c7

Please sign in to comment.