Skip to content

Commit

Permalink
Merge pull request #190 from bjacklyn/user/bjacklyn/git-polling-cause…
Browse files Browse the repository at this point in the history
…s-outofmemoryexception-JENKINS-31326

Add showRevision(from,to,excludeCommitFiles) to support JENKINS-31326
  • Loading branch information
MarkEWaite committed Mar 6, 2016
2 parents a9f57aa + e24ba9d commit 3f70409
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 20 deletions.
11 changes: 10 additions & 1 deletion src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java
Expand Up @@ -850,7 +850,16 @@ public void execute() throws GitException, InterruptedException {

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

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

if (from != null){
args.add(from.name() + ".." + to.name());
} else {
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/org/jenkinsci/plugins/gitclient/GitClient.java
Expand Up @@ -870,6 +870,32 @@ public interface GitClient {
*/
List<String> showRevision(ObjectId from, ObjectId to) 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>
* For merge commit, this method reports one diff per each parent. This makes this method
* behave differently from {@link #changelog()}.
*
* @return The git show output, in <tt>raw</tt> format.
* @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.
* @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) throws GitException, InterruptedException;


/**
* Equivalent of "git-describe --tags".
*
Expand Down
46 changes: 27 additions & 19 deletions src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java
Expand Up @@ -1167,7 +1167,7 @@ public void execute() throws GitException, InterruptedException {
// git whatachanged doesn't show the merge commits unless -m is given
if (commit.getParentCount()>1) continue;

formatter.format(commit, null, pw);
formatter.format(commit, null, pw, true);
}
} catch (IOException e) {
throw new GitException(e);
Expand Down Expand Up @@ -1210,7 +1210,7 @@ private String statusOf(DiffEntry d) {
* Optional parent commit to produce the diff against. This only matters
* for merge commits, and git-log/git-whatchanged/etc behaves differently with respect to this.
*/
void format(RevCommit commit, @Nullable RevCommit parent, PrintWriter pw) throws IOException {
void format(RevCommit commit, @Nullable RevCommit parent, PrintWriter pw, Boolean useRawOutput) throws IOException {
if (parent!=null)
pw.printf("commit %s (from %s)\n", commit.name(), parent.name());
else
Expand Down Expand Up @@ -1266,20 +1266,22 @@ void format(RevCommit commit, @Nullable RevCommit parent, PrintWriter pw) throws
tw.release();
or.release();
repo.close();
for (DiffEntry diff : diffs) {
pw.printf(":%06o %06o %s %s %s\t%s",
diff.getOldMode().getBits(),
diff.getNewMode().getBits(),
diff.getOldId().name(),
diff.getNewId().name(),
statusOf(diff),
diff.getChangeType()==ChangeType.ADD ? diff.getNewPath() : diff.getOldPath());

if (hasNewPath(diff)) {
pw.printf(" %s",diff.getNewPath()); // copied to
}
pw.println();
pw.println();
if (useRawOutput) {
for (DiffEntry diff : diffs) {
pw.printf(":%06o %06o %s %s %s\t%s",
diff.getOldMode().getBits(),
diff.getNewMode().getBits(),
diff.getOldId().name(),
diff.getNewId().name(),
statusOf(diff),
diff.getChangeType()==ChangeType.ADD ? diff.getNewPath() : diff.getOldPath());

if (hasNewPath(diff)) {
pw.printf(" %s",diff.getNewPath()); // copied to
}
pw.println();
pw.println();
}
}
}
}
Expand Down Expand Up @@ -2053,6 +2055,11 @@ public ObjectId revParse(String revName) throws GitException {

/** {@inheritDoc} */
public List<String> showRevision(ObjectId from, ObjectId to) throws GitException {
return showRevision(from, to, true);
}

/** {@inheritDoc} */
public List<String> showRevision(ObjectId from, ObjectId to, Boolean useRawOutput) throws GitException {
Repository repo = null;
ObjectReader or = null;
RevWalk w = null;
Expand All @@ -2071,12 +2078,13 @@ public List<String> showRevision(ObjectId from, ObjectId to) throws GitException
PrintWriter pw = new PrintWriter(sw);
RawFormatter f = new RawFormatter();
for (RevCommit c : w) {
if (c.getParentCount()<=1) {
f.format(c,null,pw);
// do not duplicate merge commits unless using raw output
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);
f.format(c,p,pw,useRawOutput);
}
}

Expand Down
Expand Up @@ -699,6 +699,11 @@ public List<String> showRevision(ObjectId from, ObjectId to) throws GitException
return proxy.showRevision(from, to);
}

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

/** {@inheritDoc} */
public boolean hasGitModules(String treeIsh) throws GitException, InterruptedException {
return getGitAPI().hasGitModules(treeIsh);
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/org/jenkinsci/plugins/gitclient/GitAPITestCase.java
Expand Up @@ -3105,6 +3105,32 @@ public String apply(String diff) {
assertFalse(paths.contains("README.md"));
}

public void test_show_revision_for_merge_exclude_files() throws Exception {
w = clone(localMirror());
ObjectId from = ObjectId.fromString("45e76942914664ee19f31d90e6f2edbfe0d13a46");
ObjectId to = ObjectId.fromString("b53374617e85537ec46f86911b5efe3e4e2fa54b");
Boolean useRawOutput = false;

List<String> revisionDetails = w.git.showRevision(from, to, useRawOutput);

Collection<String> commits = Collections2.filter(revisionDetails, new Predicate<String>() {
public boolean apply(String detail) {
return detail.startsWith("commit ");
}
});
assertEquals(2, commits.size());
assertTrue(commits.contains("commit 4f2964e476776cf59be3e033310f9177bedbf6a8"));
assertTrue(commits.contains("commit b53374617e85537ec46f86911b5efe3e4e2fa54b"));

Collection<String> diffs = Collections2.filter(revisionDetails, new Predicate<String>() {
public boolean apply(String detail) {
return detail.startsWith(":");
}
});

assertTrue(diffs.isEmpty());
}

private void check_bounded_changelog_sha1(final String sha1Begin, final String sha1End, final String branchName) throws InterruptedException
{
StringWriter writer = new StringWriter();
Expand Down

0 comments on commit 3f70409

Please sign in to comment.