Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Command builder for generating changelog in the format {@code GitSCM} expects.
*
* <p>
* The output format is that of <code>git log --raw</code>, which looks something like this:
* The output format is that of <code>git log --raw --no-merges</code>, which looks something like this:
*
* <pre>
* commit dadaf808d99c4c23c53476b0c48e25a181016300
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1300,7 +1300,8 @@ public void abort() {

@Override
public void execute() throws GitException, InterruptedException {
ArgumentListBuilder args = new ArgumentListBuilder(gitExe, "log", "--raw", "--no-abbrev", "-M");
ArgumentListBuilder args =
new ArgumentListBuilder(gitExe, "log", "--raw", "--no-merges", "--no-abbrev", "-M");
if (isAtLeastVersion(1, 8, 3, 0)) {
args.add("--format=" + RAW);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1394,15 +1394,16 @@
this.includes("HEAD");
}
for (RevCommit commit : walk) {
// git log --raw doesn't show the merge commits unless -m is given
// git log --raw --no-merges doesn't show merge commits
if (commit.getParentCount() > 1) {
continue;
}

formatter.format(commit, null, pw, true);
}
} catch (IOException e) {
throw new GitException("Error: jgit log --raw in " + workspace + " " + e.getMessage(), e);
throw new GitException(
"Error: jgit log --raw --no-merges in " + workspace + " " + e.getMessage(), e);

Check warning on line 1406 in src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 1405-1406 are not covered by tests
} finally {
closeResources();
}
Expand Down Expand Up @@ -1444,7 +1445,7 @@
* Commit to format.
* @param parent
* Optional parent commit to produce the diff against. This only matters
* for merge commits, and git-log behaves differently with respect to this.
* for merge commits and git-log behaves differently with respect to this.
*/
@SuppressFBWarnings(
value = "VA_FORMAT_STRING_USES_NEWLINE",
Expand Down
58 changes: 58 additions & 0 deletions src/test/java/org/jenkinsci/plugins/gitclient/GitAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1710,6 +1710,64 @@ public void testChangeLogAbort() throws Exception {
assertTrue("No SHA1 in " + writer, writer.toString().contains(sha1));
}

@Test
public void testChangelogSkipsMerges() throws Exception {
int counter = 0;
workspace.touch(testGitDir, "file-skips-merges-" + counter, "changelog skips merges " + counter);
testGitClient.add("file-skips-merges-" + counter);
testGitClient.commit("skips-merges-" + counter++);
String rootCommit = testGitClient.revParse("HEAD").name();

// Create branches a, b, and common that will merge a and b
testGitClient.branch("branch-A"); // Create branch-A without switching to it
testGitClient.branch("branch-B"); // Create branch-B without switching to it
testGitClient.branch("common"); // common branch that will merge branch-A and branch-B

testGitClient.checkoutBranch("branch-A", rootCommit); // Switch to branch-A
workspace.touch(testGitDir, "file-branch-A", "branch-A file " + counter++);
testGitClient.add("file-branch-A");
testGitClient.commit("file-branch-A on branch-A");
String branchACommit = testGitClient.revParse("HEAD").name();

testGitClient.checkoutBranch("branch-B", rootCommit); // Switch to branch-B
workspace.touch(testGitDir, "file-branch-B", "branch-B file " + counter++);
testGitClient.add("file-branch-B");
testGitClient.commit("file-branch-B on branch-B");
String branchBCommit = testGitClient.revParse("HEAD").name();

String mergeMessage = "Merged branch-B into common";
testGitClient.checkoutBranch("common", rootCommit); // Switch to common branch
testGitClient
.merge()
.setRevisionToMerge(ObjectId.fromString(branchACommit))
.execute();
testGitClient
.merge()
.setRevisionToMerge(ObjectId.fromString(branchBCommit))
.setMessage(mergeMessage)
.execute();
String mergedCommit = testGitClient.revParse("HEAD").name();

workspace.touch(testGitDir, "file-skips-merges-" + counter, "changelog skips merges " + counter);
testGitClient.add("file-skips-merges-" + counter);
testGitClient.commit("skips-merges-" + counter++);
String finalCommit = testGitClient.revParse("HEAD").name();

// Calculate the changelog
StringWriter writer = new StringWriter();
testGitClient.changelog().to(writer).execute();
String changelog = writer.toString();

// Confirm the changelog includes expected commits
assertThat(changelog, containsString("commit " + branchACommit));
assertThat(changelog, containsString("commit " + branchBCommit));
assertThat(changelog, containsString("commit " + finalCommit));

// Confirm the changelog does not include the merge commit
assertThat(changelog, not(containsString("commit " + mergedCommit)));
assertThat(changelog, not(containsString(mergeMessage)));
}

/**
* inline ${@link hudson.Functions#isWindows()} to prevent a transient remote classloader issue
*/
Expand Down
Loading