diff --git a/README.md b/README.md index 8a905ad2..7a5d35da 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ Direct dependencies are specified in a top level `versions.props` file and then 4. Run **`./gradlew --write-locks`** and see your versions.lock file be automatically created. This file should be checked into your repo: ```bash - # Run ./gradlew --write-locks to regenerate this file + # Run ./gradlew --write-locks to regenerate this file. Blank lines are to minimize merge conflicts. com.squareup.okhttp3:okhttp:3.12.0 (1 constraints: 38053b3b) com.squareup.okio:okio:1.15.0 (1 constraints: 810cbb09) ``` diff --git a/changelog/@unreleased/pr-1001.v2.yml b/changelog/@unreleased/pr-1001.v2.yml new file mode 100644 index 00000000..44fd1f07 --- /dev/null +++ b/changelog/@unreleased/pr-1001.v2.yml @@ -0,0 +1,5 @@ +type: improvement +improvement: + description: Add 3 blank lines between lock entries to minimize merge conflicts + links: + - https://github.com/palantir/gradle-consistent-versions/pull/1001 diff --git a/src/main/java/com/palantir/gradle/versions/ConflictSafeLockFile.java b/src/main/java/com/palantir/gradle/versions/ConflictSafeLockFile.java index e78d8d6f..0b85d0fa 100644 --- a/src/main/java/com/palantir/gradle/versions/ConflictSafeLockFile.java +++ b/src/main/java/com/palantir/gradle/versions/ConflictSafeLockFile.java @@ -35,7 +35,8 @@ import org.gradle.api.GradleException; final class ConflictSafeLockFile { - private static final String HEADER_COMMENT = "# Run ./gradlew --write-locks to regenerate this file"; + private static final String HEADER_COMMENT = + "# Run ./gradlew --write-locks to regenerate this file. Blank lines are to minimize merge conflicts."; private static final Pattern LINE_PATTERN = Pattern.compile("(?[^(:]+):(?[^(:]+):(?[^(:\\s]+)" + "\\s+\\((?\\d+) constraints: (?\\w+)\\)"); @@ -49,16 +50,14 @@ final class ConflictSafeLockFile { /** Reads and returns the {@link LockState}. */ public LockState readLocks() { - try (Stream linesStream = Files.lines(lockfile)) { + try (Stream linesStream = Files.lines(lockfile).filter(line -> !line.isBlank())) { List lines = linesStream.filter(line -> !line.trim().startsWith("#")).collect(Collectors.toList()); int testDependenciesPosition = lines.indexOf(TEST_DEPENDENCIES_MARKER); Stream productionDeps; Stream testDeps; - if (testDependenciesPosition >= 0) { - productionDeps = lines - .subList(0, testDependenciesPosition - 1) // skip blank line before marker - .stream(); + if (testDependenciesPosition >= 0 && testDependenciesPosition + 1 < lines.size()) { + productionDeps = lines.subList(0, testDependenciesPosition).stream(); testDeps = lines.subList(testDependenciesPosition + 1, lines.size()).stream(); } else { productionDeps = lines.stream().filter(line -> !line.trim().startsWith("#")); @@ -74,6 +73,7 @@ public LockState readLocks() { public Stream parseLines(Stream stringStream) { return stringStream + .filter(line -> !line.isBlank()) .map(line -> { Matcher matcher = LINE_PATTERN.matcher(line); Preconditions.checkState( diff --git a/src/main/java/com/palantir/gradle/versions/VerifyLocksTask.java b/src/main/java/com/palantir/gradle/versions/VerifyLocksTask.java index bbe47deb..d67146f2 100644 --- a/src/main/java/com/palantir/gradle/versions/VerifyLocksTask.java +++ b/src/main/java/com/palantir/gradle/versions/VerifyLocksTask.java @@ -107,8 +107,8 @@ private static String formatDependencyDifferences(Map