Skip to content
Open
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 @@ -1342,12 +1342,23 @@ public List<String> showRevision(ObjectId from, ObjectId to) throws GitException
return showRevision(from, to, true);
}

/** {@inheritDoc} */
@Override
public List<String> showRevision(ObjectId from, ObjectId to, Boolean useRawOutput)
throws GitException, InterruptedException {
return showRevision(from, to, useRawOutput, false);
}

/** {@inheritDoc} */
@Override
public List<String> showRevision(ObjectId from, ObjectId to, Boolean useRawOutput, Boolean suppressMergeCommitDiff)
throws GitException, InterruptedException {
ArgumentListBuilder args =
new ArgumentListBuilder("log", "--full-history", "--no-abbrev", "--format=raw", "-M", "-m");
new ArgumentListBuilder("log", "--full-history", "--no-abbrev", "--format=raw", "-M");

if (!suppressMergeCommitDiff) {
args.add("-m");
}

if (useRawOutput) {
args.add("--raw");
}
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/org/jenkinsci/plugins/gitclient/GitClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,31 @@ void submoduleUpdate(boolean recursive, boolean remoteTracking, String reference
List<String> showRevision(ObjectId from, ObjectId to, Boolean useRawOutput)
throws GitException, InterruptedException;

/**
* Given a Revision, show it as if it were an entry from git whatchanged, so that it
* can be parsed by GitChangeLogParser.
*
* <p>
* If useRawOutput is true, the '--raw' option will include commit file information to be passed to the
* GitChangeLogParser.
*
* <p>
* Changes are computed on the [from..to] range. If {@code from} is null, this prints
* just one commit that {@code to} represents.
*
* <p>
* If suppressMergeCommitDiff is false, for merge commit this method reports one diff per each parent.
*
* @param from a {@link org.eclipse.jgit.lib.ObjectId} object.
* @param to a {@link org.eclipse.jgit.lib.ObjectId} object.
* @param useRawOutput a {java.lang.Boolean} object.
* @param suppressMergeCommitDiff a {java.lang.Boolean} object.
* @return The git whatchanged output, in <code>raw</code> format.
* @throws hudson.plugins.git.GitException if underlying git operation fails.
* @throws java.lang.InterruptedException if interrupted.
*/
List<String> showRevision(ObjectId from, ObjectId to, Boolean useRawOutput, Boolean suppressMergeCommitDiff)
throws GitException, InterruptedException;
/**
* Equivalent of "git-describe --tags".
*
Expand Down
14 changes: 11 additions & 3 deletions src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2438,6 +2438,13 @@ public List<String> showRevision(ObjectId from, ObjectId to) throws GitException
/** {@inheritDoc} */
@Override
public List<String> showRevision(ObjectId from, ObjectId to, Boolean useRawOutput) throws GitException {
return showRevision(from, to, useRawOutput, false);
}

/** {@inheritDoc} */
@Override
public List<String> showRevision(ObjectId from, ObjectId to, Boolean useRawOutput, Boolean suppressMergeCommitDiff)
throws GitException {
try (Repository repo = getRepository();
ObjectReader or = repo.newObjectReader();
RevWalk w = new RevWalk(or)) {
Expand All @@ -2457,9 +2464,10 @@ public List<String> showRevision(ObjectId from, ObjectId to, Boolean useRawOutpu
if (c.getParentCount() <= 1 || !useRawOutput) {
f.format(c, null, pw, useRawOutput);
} else {
// the effect of the -m option, which makes the diff produce for each parent of a merge commit
for (RevCommit p : c.getParents()) {
f.format(c, p, pw, useRawOutput);
if (!suppressMergeCommitDiff) {
for (RevCommit p : c.getParents()) {
f.format(c, p, pw, useRawOutput);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,12 @@
return proxy.showRevision(from, to, useRawOutput);
}

@Override
public List<String> showRevision(ObjectId from, ObjectId to, Boolean useRawOutput, Boolean suppressMergeCommitDiff)
throws GitException, InterruptedException {
return proxy.showRevision(from, to, useRawOutput);

Check warning on line 858 in src/main/java/org/jenkinsci/plugins/gitclient/RemoteGitImpl.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 858 is not covered by tests
}

/** {@inheritDoc} */
@Override
public boolean hasGitModules(String treeIsh) throws GitException, InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2046,6 +2046,49 @@ public void testChangelogWithMergeCommitAndMaxLogHistory() throws Exception {
assertThat(writer.toString(), is(not("")));
}

@Issue("JENKINS-23606")
@Test
public void testSupressMergeCommitDiffInShowRevision() throws Exception {
w.init();
w.commitEmpty("init");

final String branchBeforeCommit = "branchBeforeCommit";
w.git.branch(branchBeforeCommit);

final String fileFromBranch = "fileFromBranch";
w.git.checkout().ref(branchBeforeCommit).execute();
w.touch(fileFromBranch, "content-1");
w.git.add(fileFromBranch);
w.git.commit("commit-in-branch-before-commit");

final String fileInMain = "fileInMain";
w.git.checkout().ref(defaultBranchName).execute();
w.touch(fileInMain, "content-2");
w.git.add(fileInMain);
w.git.commit("commit-in-main-after-branch-created");
String commitSha1 = w.git.revParse("HEAD").name();

w.git.merge()
.setGitPluginFastForwardMode(MergeCommand.GitPluginFastForwardMode.NO_FF)
.setRevisionToMerge(w.git.getHeadRev(w.repoPath(), branchBeforeCommit))
.execute();
String commitSha2 = w.git.revParse("HEAD").name();

final List<String> defaultBehaviour =
w.git.showRevision(ObjectId.fromString(commitSha1), ObjectId.fromString(commitSha2), true);
assertTrue(
"Default behaviour shows merge commits diffs",
defaultBehaviour.stream().anyMatch(e -> e.contains(fileInMain)));
assertTrue(
"Shows file from merged branch", defaultBehaviour.stream().anyMatch(e -> e.contains(fileFromBranch)));

final List<String> notShowDiffMerge =
w.git.showRevision(ObjectId.fromString(commitSha1), ObjectId.fromString(commitSha2), true, true);
assertTrue("Merge commit diffs aren't shown", !notShowDiffMerge.stream().anyMatch(e -> e.contains(fileInMain)));
assertTrue(
"Shows file from merged branch", notShowDiffMerge.stream().anyMatch(e -> e.contains(fileFromBranch)));
}

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