We are using the git plugin for "pre-build branch merging" as described in 
the Advanced-section in the git plugin wiki page. This works, but not 
quite, as we want jenkins to send an email for broken builds (ie not 
integrated), and jenkins/git-plugin does not do this for branches it has 
never seen before, which is actually the typical case for the way we use it.

I have tried to work out why this is and have traced it (correctly, I hope) 
back the changelog computation. I wrote about it on the user mailing list 
(https://groups.google.com/d/topic/jenkinsci-users/sbv6LAGfwo4/discussion) 
but didn't get any replies. Now I have tried fixing it myself, and would 
like to ask for your comments.

First, I'm using the "merge before build" option and has a pattern 
(origin/for-master/*) in the "branches to build" field. In this scenario I 
think that the changelog for a built should reflect the difference between 
the branch to build and the "branch to merge to". I am not sure if this is 
always the way to do it when "merge before build" is checked, but this is 
what I have done in the attached patch (or what I intended to do). What do 
you think?

Btw, I have also written about this issue on this bug. 
https://issues.jenkins-ci.org/browse/JENKINS-14138

Thanks,

Thomas
diff --git a/src/main/java/hudson/plugins/git/GitSCM.java b/src/main/java/hudson/plugins/git/GitSCM.java
index 4e9c4bf..33259ed 100644
--- a/src/main/java/hudson/plugins/git/GitSCM.java
+++ b/src/main/java/hudson/plugins/git/GitSCM.java
@@ -1246,7 +1246,7 @@ public class GitSCM extends SCM implements Serializable {
                         git.tag(buildnumber, "Jenkins Build #" + buildNumber);
                     }
 
-                    computeChangeLog(git, revToBuild, listener, buildData, changelogFile);
+                    computeMergeChangeLog(git, revToBuild, mergeOptions.getRemoteBranchName(), listener, changelogFile);
 
                     Build build = new Build(revToBuild, buildNumber, null);
                     buildData.saveBuild(build);
@@ -1380,6 +1380,31 @@ public class GitSCM extends SCM implements Serializable {
         }
     }
 
+    private void computeMergeChangeLog(IGitAPI git, Revision revToBuild, String revFrom, BuildListener listener, FilePath changelogFile) throws IOException, InterruptedException {
+        if (!git.isCommitInRepo(revFrom)) {
+            listener.getLogger().println("Could not record history. Previous build's commit, " + revFrom
+                                         + ", does not exist in the current repository.");
+        } else {
+            int histories = 0;
+
+            PrintStream out = new PrintStream(changelogFile.write());
+            try {
+                for (Branch b : revToBuild.getBranches()) {
+                    putChangelogDiffs(git, b.name, revFrom, revToBuild.getSha1().name(), out);
+                    histories++;
+                }
+            } catch (GitException ge) {
+                out.println("Unable to retrieve changeset");
+            } finally {
+                IOUtils.closeQuietly(out);
+            }
+
+            if (histories > 1) {
+                listener.getLogger().println("Warning : There are multiple branch changesets here");
+            }
+        }
+    }
+
     public void buildEnvVars(AbstractBuild<?, ?> build, java.util.Map<String, String> env) {
         super.buildEnvVars(build, env);
         Revision rev = fixNull(getBuildData(build, false)).getLastBuiltRevision();

Reply via email to