Repository: vackosar/gitflow-incremental-builder
Pull request #38 (2018-06-24 19:16:38)

Fix issue #28: Introduce gib.disableBranchComparison
Disables the comparison between base and reference branch.


Patch:
diff --git a/README.md b/README.md
index 9b75dcd..0e0b571 100644
--- a/README.md
+++ b/README.md
@@ -211,6 +211,7 @@ Maven pom properties configuration with default values is below:
 <properties>
 	<gib.enabled>true</gib.enabled>
 	<gib.repositorySshKey></gib.repositorySshKey>
+	<gib.disableBranchComparison>false</gib.disableBranchComparison>
 	<gib.referenceBranch>refs/remotes/origin/develop</gib.referenceBranch>
 	<gib.baseBranch>HEAD</gib.baseBranch>
 	<gib.uncommited>true</gib.uncommited>
@@ -226,6 +227,15 @@ Maven pom properties configuration with default values is below:
 </properties>
 ```
 
+### gib.disableBranchComparison
+
+Disables the comparison between `baseBranch` and `referenceBranch`. This property should be enabled if _only_ uncommitted and/or untracked files shall be detected to only build projects that have been changed since the last commit in the current branch (see `gib.uncommited` and `gib.untracked`).
+The following properties are _not_ evaluated when `gib.disableBranchDiff` is enabled:
+ - `gib.referenceBranch`
+ - `gib.compareToMergeBase`
+ - `gib.fetchReferenceBranch`
+ - `gib.excludePathRegex`
+
 ### gib.uncommited
 
 Detects changed files that have not yet been committed. This does **not** include _untracked_ files (see `git status` manual).
diff --git a/src/main/java/com/vackosar/gitflowincrementalbuild/boundary/Configuration.java b/src/main/java/com/vackosar/gitflowincrementalbuild/boundary/Configuration.java
index 3a4a5a4..235a726 100644
--- a/src/main/java/com/vackosar/gitflowincrementalbuild/boundary/Configuration.java
+++ b/src/main/java/com/vackosar/gitflowincrementalbuild/boundary/Configuration.java
@@ -22,6 +22,7 @@ public class Configuration {
 
     public final boolean enabled;
     public final Optional<Path> key;
+    public final boolean disableBranchComparison;
     public final String referenceBranch;
     public String baseBranch;
     public final boolean uncommited;
@@ -41,6 +42,7 @@ public class Configuration {
             checkProperties();
             enabled = Boolean.valueOf(Property.enabled.getValue());
             key = parseKey(session);
+            disableBranchComparison = Boolean.valueOf(Property.disableBranchComparison.getValue());
             referenceBranch = Property.referenceBranch.getValue();
             baseBranch = Property.baseBranch.getValue();
             uncommited = Boolean.valueOf(Property.uncommited.getValue());
diff --git a/src/main/java/com/vackosar/gitflowincrementalbuild/control/DifferentFiles.java b/src/main/java/com/vackosar/gitflowincrementalbuild/control/DifferentFiles.java
index 838d405..5a50e97 100644
--- a/src/main/java/com/vackosar/gitflowincrementalbuild/control/DifferentFiles.java
+++ b/src/main/java/com/vackosar/gitflowincrementalbuild/control/DifferentFiles.java
@@ -32,25 +32,35 @@ public class DifferentFiles {
     @Inject private Logger logger;
 
     public Set<Path> get() throws GitAPIException, IOException {
+        Set<Path> paths = new HashSet<>();
         fetch();
         checkout();
-        RevCommit base = getBranchCommit(configuration.baseBranch);
-        final TreeWalk treeWalk = new TreeWalk(git.getRepository());
-        treeWalk.addTree(base.getTree());
-        treeWalk.addTree(resolveReference(base).getTree());
-        treeWalk.setFilter(TreeFilter.ANY_DIFF);
-        treeWalk.setRecursive(true);
         final Path workTree = git.getRepository().getWorkTree().toPath().normalize().toAbsolutePath();
-        final Set<Path> paths = getDiff(treeWalk, workTree);
+        if (!configuration.disableBranchComparison) {
+            paths.addAll(getBranchDiff(workTree));
+        }
         if (configuration.uncommited || configuration.untracked) {
             paths.addAll(getChangesFromStatus(workTree));
         }
-        treeWalk.close();
         git.getRepository().close();
         git.close();
         return paths;
     }
 
+    private Set<Path> getBranchDiff(Path workTree) throws IOException {
+        RevCommit base = getBranchCommit(configuration.baseBranch);
+        final TreeWalk treeWalk = new TreeWalk(git.getRepository());
+        try {
+            treeWalk.addTree(base.getTree());
+            treeWalk.addTree(resolveReference(base).getTree());
+            treeWalk.setFilter(TreeFilter.ANY_DIFF);
+            treeWalk.setRecursive(true);
+            return getDiff(treeWalk, workTree);
+        } finally {
+            treeWalk.close();
+        }
+    }
+
     private void checkout() throws IOException, GitAPIException {
         if (! (HEAD.equals(configuration.baseBranch) || configuration.baseBranch.startsWith("worktrees/")) && ! git.getRepository().getFullBranch().equals(configuration.baseBranch)) {
             logger.info("Checking out base branch " + configuration.baseBranch + "...");
@@ -59,7 +69,7 @@ public class DifferentFiles {
     }
 
     private void fetch() throws GitAPIException {
-        if (configuration.fetchReferenceBranch) {
+        if (!configuration.disableBranchComparison && configuration.fetchReferenceBranch) {
             fetch(configuration.referenceBranch);
         }
         if (configuration.fetchBaseBranch) {
diff --git a/src/main/java/com/vackosar/gitflowincrementalbuild/control/Property.java b/src/main/java/com/vackosar/gitflowincrementalbuild/control/Property.java
index 4f01911..d05c744 100644
--- a/src/main/java/com/vackosar/gitflowincrementalbuild/control/Property.java
+++ b/src/main/java/com/vackosar/gitflowincrementalbuild/control/Property.java
@@ -3,6 +3,7 @@ package com.vackosar.gitflowincrementalbuild.control;
 public enum Property {
     enabled("true"),
     repositorySshKey(""),
+    disableBranchComparison("false"),
     referenceBranch("refs/remotes/origin/develop"),
     baseBranch("HEAD"),
     uncommited("true"),


Test patch:
diff --git a/src/test/java/com/vackosar/gitflowincrementalbuild/control/DifferentFilesTest.java b/src/test/java/com/vackosar/gitflowincrementalbuild/control/DifferentFilesTest.java
index ef120a3..98664c0 100644
--- a/src/test/java/com/vackosar/gitflowincrementalbuild/control/DifferentFilesTest.java
+++ b/src/test/java/com/vackosar/gitflowincrementalbuild/control/DifferentFilesTest.java
@@ -35,6 +35,7 @@ import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.nio.file.StandardOpenOption;
 import java.util.Arrays;
+import java.util.Collections;
 import java.util.HashSet;
 import java.util.Set;
 import java.util.stream.Collectors;
@@ -164,6 +165,15 @@ public class DifferentFilesTest extends BaseRepoTest {
         Assert.assertEquals(expected, differentFiles.get());
     }
 
+    @Test
+    public void listWithDisabledBranchComparison() throws Exception {
+        Property.disableBranchComparison.setValue(Boolean.TRUE.toString());
+
+        final DifferentFiles differentFiles = getInstance(localRepoMock.getBaseCanonicalBaseFolder().toPath());
+
+        Assert.assertEquals(Collections.emptySet(), differentFiles.get());
+    }
+
     @Test
     public void listInSubdir() throws Exception {
         Path workDir = localRepoMock.getBaseCanonicalBaseFolder().toPath().resolve("parent/child2");