Skip to content

Commit

Permalink
Add showRevision(from,to,useRawOutput) to support JENKINS-31326
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Jacklyn committed Nov 2, 2015
1 parent a637509 commit 5d52c7a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 20 deletions.
11 changes: 10 additions & 1 deletion src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java
Expand Up @@ -796,7 +796,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 @@ -864,6 +864,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 @@ -1161,7 +1161,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 @@ -1204,7 +1204,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 @@ -1260,20 +1260,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 @@ -2007,6 +2009,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 @@ -2025,12 +2032,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 @@ -690,6 +690,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

0 comments on commit 5d52c7a

Please sign in to comment.