From f1dfc23c493570e5be865664c17e368f46665609 Mon Sep 17 00:00:00 2001 From: James Moger Date: Sun, 17 Apr 2011 16:00:02 -0400 Subject: [PATCH] Implemented diff-to-current. This is for history where we specify two revs to diff, not just a commit against its parent. --- src/com/gitblit/utils/JGitUtils.java | 27 ++++++++++++++----- src/com/gitblit/wicket/WicketUtils.java | 14 ++++++++-- .../gitblit/wicket/pages/BlobDiffPage.java | 14 +++++++++- .../gitblit/wicket/panels/HistoryPanel.java | 4 +-- 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/src/com/gitblit/utils/JGitUtils.java b/src/com/gitblit/utils/JGitUtils.java index 075ad81b..9b2e37cf 100644 --- a/src/com/gitblit/utils/JGitUtils.java +++ b/src/com/gitblit/utils/JGitUtils.java @@ -288,20 +288,35 @@ public class JGitUtils { } public static String getCommitDiff(Repository r, RevCommit commit, boolean outputHtml) { - return getCommitDiff(r, commit, null, outputHtml); + return getCommitDiff(r, null, commit, null, outputHtml); } public static String getCommitDiff(Repository r, RevCommit commit, String path, boolean outputHtml) { + return getCommitDiff(r, null, commit, path, outputHtml); + } + + public static String getCommitDiff(Repository r, RevCommit baseCommit, RevCommit commit, boolean outputHtml) { + return getCommitDiff(r, baseCommit, commit, null, outputHtml); + } + + public static String getCommitDiff(Repository r, RevCommit baseCommit, RevCommit commit, String path, boolean outputHtml) { try { - final RevWalk rw = new RevWalk(r); - RevCommit parent = rw.parseCommit(commit.getParent(0).getId()); - RevTree parentTree = parent.getTree(); + RevTree baseTree; + if (baseCommit == null) { + final RevWalk rw = new RevWalk(r); + RevCommit parent = rw.parseCommit(commit.getParent(0).getId()); + rw.dispose(); + baseTree = parent.getTree(); + } else { + baseTree = baseCommit.getTree(); + } + RevTree commitTree = commit.getTree(); final TreeWalk walk = new TreeWalk(r); walk.reset(); walk.setRecursive(true); - walk.addTree(parentTree); + walk.addTree(baseTree); walk.addTree(commitTree); walk.setFilter(TreeFilter.ANY_DIFF); @@ -316,7 +331,7 @@ public class JGitUtils { df.setRepository(r); df.setDiffComparator(cmp); df.setDetectRenames(true); - List diffs = df.scan(parentTree, commitTree); + List diffs = df.scan(baseTree, commitTree); if (path != null && path.length() > 0) { for (DiffEntry diff : diffs) { if (diff.getNewPath().equalsIgnoreCase(path)) { diff --git a/src/com/gitblit/wicket/WicketUtils.java b/src/com/gitblit/wicket/WicketUtils.java index 3bc55f1b..0a1c3a03 100644 --- a/src/com/gitblit/wicket/WicketUtils.java +++ b/src/com/gitblit/wicket/WicketUtils.java @@ -13,6 +13,7 @@ import org.eclipse.jgit.lib.Constants; import com.gitblit.GitBlit; import com.gitblit.Keys; +import com.gitblit.utils.StringUtils; import com.gitblit.utils.TimeUtils; public class WicketUtils { @@ -61,14 +62,14 @@ public class WicketUtils { } public static PageParameters newObjectParameter(String repositoryName, String objectId) { - if (objectId == null || objectId.trim().length() == 0) { + if (StringUtils.isEmpty(objectId)) { return newRepositoryParameter(repositoryName); } return new PageParameters("r=" + repositoryName + ",h=" + objectId); } public static PageParameters newPathParameter(String repositoryName, String objectId, String path) { - if (path == null || path.trim().length() == 0) { + if (StringUtils.isEmpty(path)) { return newObjectParameter(repositoryName, objectId); } return new PageParameters("r=" + repositoryName + ",h=" + objectId + ",f=" + path); @@ -88,6 +89,11 @@ public class WicketUtils { return new PageParameters("r=" + repositoryName + ",h=" + objectId + ",f=" + path + ",page=" + pageNumber); } + public static PageParameters newBlobDiffParameter(String repositoryName, String baseCommitId, String commitId, String path) { + return new PageParameters("r=" + repositoryName + ",h=" + commitId + ",f=" + path + ",hb=" + baseCommitId); + } + + public static String getRepositoryName(PageParameters params) { return params.getString("r", ""); } @@ -99,6 +105,10 @@ public class WicketUtils { public static String getPath(PageParameters params) { return params.getString("f", null); } + + public static String getBaseObjectId(PageParameters params) { + return params.getString("hb", null); + } public static int getPage(PageParameters params) { return params.getInt("page", 1); // index from 1 diff --git a/src/com/gitblit/wicket/pages/BlobDiffPage.java b/src/com/gitblit/wicket/pages/BlobDiffPage.java index de49b3cf..bff46b38 100644 --- a/src/com/gitblit/wicket/pages/BlobDiffPage.java +++ b/src/com/gitblit/wicket/pages/BlobDiffPage.java @@ -7,6 +7,7 @@ import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevCommit; import com.gitblit.utils.JGitUtils; +import com.gitblit.utils.StringUtils; import com.gitblit.wicket.LinkPanel; import com.gitblit.wicket.RepositoryPage; import com.gitblit.wicket.WicketUtils; @@ -18,10 +19,21 @@ public class BlobDiffPage extends RepositoryPage { super(params); final String blobPath = WicketUtils.getPath(params); + final String baseObjectId = WicketUtils.getBaseObjectId(params); Repository r = getRepository(); RevCommit commit = JGitUtils.getCommit(r, objectId); - String diff = JGitUtils.getCommitDiff(r, commit, blobPath, true); + + String diff; + if (StringUtils.isEmpty(baseObjectId)) { + // use first parent + diff = JGitUtils.getCommitDiff(r, commit, blobPath, true); + } else { + // base commit specified + RevCommit baseCommit = JGitUtils.getCommit(r, baseObjectId); + diff = JGitUtils.getCommitDiff(r, baseCommit, commit, blobPath, true); + } + add(new BookmarkablePageLink("patchLink", PatchPage.class, WicketUtils.newPathParameter(repositoryName, objectId, blobPath))); add(new BookmarkablePageLink("commitLink", CommitPage.class, WicketUtils.newObjectParameter(repositoryName, objectId))); add(new BookmarkablePageLink("commitDiffLink", CommitDiffPage.class, WicketUtils.newObjectParameter(repositoryName, objectId))); diff --git a/src/com/gitblit/wicket/panels/HistoryPanel.java b/src/com/gitblit/wicket/panels/HistoryPanel.java index e7878838..855128b0 100644 --- a/src/com/gitblit/wicket/panels/HistoryPanel.java +++ b/src/com/gitblit/wicket/panels/HistoryPanel.java @@ -32,7 +32,7 @@ public class HistoryPanel extends BasePanel { private boolean hasMore = false; - public HistoryPanel(String wicketId, final String repositoryName, String objectId, final String path, Repository r, int limit, int pageOffset) { + public HistoryPanel(String wicketId, final String repositoryName, final String objectId, final String path, Repository r, int limit, int pageOffset) { super(wicketId); boolean pageResults = limit <= 0; int itemsPerPage = GitBlit.self().settings().getInteger(Keys.web.logPageCommitCount, 50); @@ -96,7 +96,7 @@ public class HistoryPanel extends BasePanel { // TODO links for folder item.add(new BookmarkablePageLink("view", CommitPage.class, WicketUtils.newObjectParameter(repositoryName, entry.getName()))); item.add(new BookmarkablePageLink("commitdiff", CommitDiffPage.class, WicketUtils.newObjectParameter(repositoryName, entry.getName()))); - item.add(new BookmarkablePageLink("difftocurrent", BlobDiffPage.class, WicketUtils.newPathParameter(repositoryName, entry.getName(), path)).setEnabled(counter > 0)); + item.add(new BookmarkablePageLink("difftocurrent", BlobDiffPage.class, WicketUtils.newBlobDiffParameter(repositoryName, entry.getName(), objectId, path)).setEnabled(counter > 0)); WicketUtils.setAlternatingBackground(item, counter); counter++; -- 2.39.5