# [EGIT PATCH 4/4] Show only commits in the current branch.

3 messages from 2008-03-24 to 2008-03-24. Participants: Roger C. Soares, Robin Rosenberg.
Thread: https://gitlist.dev/t/12833

## Roger C. Soares, 2008-03-24 02:58

Subject: [EGIT PATCH 4/4] Show only commits in the current branch.
Message-ID: <1206327487-6543-1-git-send-email-rogersoares@intelinet.com.br>
URL: https://gitlist.dev/e/1206327487-6543-1-git-send-email-rogersoares%40intelinet.com.br

```
This is a quick hack to have the functionality. It
filters the commits returned by GitFileHistory.getHistoryFor.

Signed-off-by: Roger C. Soares <rogersoares@intelinet.com.br>
---
 org.spearce.egit.ui/icons/elcl16/cbcommits.gif     |  Bin 0 -> 354 bytes
 .../src/org/spearce/egit/ui/GitHistoryPage.java    |   80 ++++++++++++++++++++
 .../egit/ui/PluginPreferenceInitializer.java       |    1 +
 .../src/org/spearce/egit/ui/UIIcons.java           |    3 +
 .../src/org/spearce/egit/ui/UIPreferences.java     |    2 +
 5 files changed, 86 insertions(+), 0 deletions(-)
 create mode 100644 org.spearce.egit.ui/icons/elcl16/cbcommits.gif

diff --git a/org.spearce.egit.ui/icons/elcl16/cbcommits.gif b/org.spearce.egit.ui/icons/elcl16/cbcommits.gif
new file mode 100644
index 0000000000000000000000000000000000000000..21359d460ff9c3332482d503bab5d31a4db02188
GIT binary patch
literal 354
zcmZ?wbhEHb6krfwSjx!2(7+H=wQAzh<9qM*?tRd;_hIMWhh6&~cJ6!Jy6<t@ejxdz
zWj_eD?tj*>|5@XKXY~i3H5`0deeh+?!PgZB-&7oWQ-0`e$)UHUhu)SSdRKfHgh~#-
zFFO38;K+x9!ygNde9SxYG5;u#{FHa}bN12CIY+-_9sQDh^lRqPuh~G`DM5-qSs1w(
z>=|?zfB@tt2DbVG^9ww5_!wuMZ52MCV<^EOs326!!g!GV%SVs?6+&7LZZprsD?V7_
zmB_lXLhJA1F1IBNobpN+8KoQZrDa>|MMcC?7{w&}>V<`c@`a`f)$<GT2{H=I77(cC
s<lxHZ;^yV4XJcXQW@Tq*t!Lhvzjf=D9gI762JhIuqr~lqy(5D)038{o*8l(j

literal 0
HcmV?d00001

diff --git a/org.spearce.egit.ui/src/org/spearce/egit/ui/GitHistoryPage.java b/org.spearce.egit.ui/src/org/spearce/egit/ui/GitHistoryPage.java
index d0346a9..5a7e05b 100644
--- a/org.spearce.egit.ui/src/org/spearce/egit/ui/GitHistoryPage.java
+++ b/org.spearce.egit.ui/src/org/spearce/egit/ui/GitHistoryPage.java
@@ -18,6 +18,7 @@ package org.spearce.egit.ui;
 
 import java.io.IOException;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Date;
 import java.util.HashMap;
@@ -88,7 +89,9 @@ import org.spearce.egit.core.internal.mapping.GitFileHistoryProvider;
 import org.spearce.egit.core.internal.mapping.GitFileRevision;
 import org.spearce.egit.core.project.RepositoryMapping;
 import org.spearce.egit.ui.internal.actions.GitCompareRevisionAction;
+import org.spearce.jgit.lib.Commit;
 import org.spearce.jgit.lib.ObjectId;
+import org.spearce.jgit.lib.Repository;
 import org.spearce.jgit.lib.Tag;
 import org.spearce.jgit.lib.TopologicalSorter;
 import org.spearce.jgit.lib.Repository.StGitPatch;
@@ -124,6 +127,7 @@ public class GitHistoryPage extends HistoryPage implements IAdaptable,
 	/* private */IAction toggleRevCommentAction;
 	/* private */IAction toggleTooltipsAction;
 	/* private */IAction findAction;
+	private IAction currentBranchCommitsAction;
 
 	/* private */Table table;
 	private MouseMoveListener tableMouseMoveListener;
@@ -132,6 +136,8 @@ public class GitHistoryPage extends HistoryPage implements IAdaptable,
 
 	private boolean hintShowDiffNow;
 
+	private boolean showOnlyCurrentBranch;
+
 	private boolean showAllProjectVersions;
 
 	private boolean showAllFolderVersions;
@@ -146,6 +152,7 @@ public class GitHistoryPage extends HistoryPage implements IAdaptable,
 	 */
 	public GitHistoryPage(Object object) {
 		setInput(object);
+		showOnlyCurrentBranch = prefs.getBoolean(UIPreferences.RESOURCEHISTORY_SHOW_ONLY_CURRENT_BRANCH);
 		showAllProjectVersions = Activator.getDefault().getPreferenceStore()
 				.getBoolean(PREF_SHOWALLPROJECTVERSIONS);
 		showAllFolderVersions = Activator.getDefault().getPreferenceStore()
@@ -313,6 +320,27 @@ public class GitHistoryPage extends HistoryPage implements IAdaptable,
 		getSite().getActionBars().getToolBarManager()
 				.add(findAction);
 
+		getSite().getActionBars().getToolBarManager()
+				.add(new Separator());
+
+		currentBranchCommitsAction = new Action("HEAD", UIIcons.ELCL16_HEAD) {
+			public void run() {
+				showOnlyCurrentBranch = isChecked();
+				if (historyRefreshJob.cancel()) {
+					System.out.println("rescheduling");
+					historyRefreshJob.schedule();
+				} else {
+					System.out.println("failed to cancel?");
+				}
+				prefs.setValue(UIPreferences.RESOURCEHISTORY_SHOW_ONLY_CURRENT_BRANCH, currentBranchCommitsAction.isChecked());
+				Activator.getDefault().savePluginPreferences();
+			}
+		};
+		currentBranchCommitsAction.setToolTipText("Show only commits in the current branch");
+		currentBranchCommitsAction.setChecked(showOnlyCurrentBranch);
+		getSite().getActionBars().getToolBarManager()
+				.add(currentBranchCommitsAction);
+
 		Action showAllRepoVersionsAction = new Action("R") {
 			public void run() {
 				setShowAllRepoVersions(isChecked());
@@ -842,6 +870,37 @@ public class GitHistoryPage extends HistoryPage implements IAdaptable,
 								monitor,
 								isShowAllRepoVersions());
 				fileRevisions = Collections.synchronizedList(fileHistoryFor.getFileRevisionsList());
+
+				if(showOnlyCurrentBranch) {
+					// Filters the history revisions list to show only
+					// the current selected branch. This is a quick hack to have it working
+					// TODO: this should probably be retrieved from the fileHistoryProvider.getHistoryFor
+					List<IFileRevision> filteredFileRevisions = Collections.synchronizedList(new ArrayList<IFileRevision>());
+					Map<String, String> parentMap = new HashMap<String, String>();
+					List<ObjectId> commitsToWalk = new ArrayList<ObjectId>();
+					if (fileRevisions.size() > 0) {
+						try {
+							Commit initialCommit = repositoryMapping.getRepository().mapCommit(currentHead);
+							verifyCurrentHeadCommits(repositoryMapping.getRepository(), parentMap, commitsToWalk, initialCommit);
+						} catch (Exception e) {
+							// TODO Auto-generated catch block
+							e.printStackTrace();
+						}
+
+						for(IFileRevision rev : fileRevisions) {
+							if(rev instanceof GitCommitFileRevision) {
+								String revId = rev.getContentIdentifier();
+								if(parentMap.containsKey(revId)) {
+									filteredFileRevisions.add(rev);
+								}
+							} else {
+								filteredFileRevisions.add(rev);
+							}
+						}
+					}
+					fileRevisions = filteredFileRevisions;
+				}
+
 				findToolbar.setFileRevisions(fileRevisions, newtags, branches);
 				Display.getDefault().syncExec(new Runnable() {
 					public void run() {
@@ -891,6 +950,27 @@ public class GitHistoryPage extends HistoryPage implements IAdaptable,
 		}
 	}
 
+	private void verifyCurrentHeadCommits(Repository repository, 
+			Map<String, String> parentMap, List<ObjectId> commitsToWalk, Commit initialCommit) throws IOException {
+		Commit commit = initialCommit;
+		while (commit.getParentIds() != null && commit.getParentIds().length > 0 
+				&& !parentMap.containsKey(commit.getCommitId().toString())) {
+			String cid = commit.getCommitId().toString();
+			parentMap.put(cid, cid);
+			commit = repository.mapCommit(commit.getParentIds()[0]);
+			int parentCount = commit.getParentIds().length;
+			if (parentCount > 1) {
+				for (int i = 1; i < parentCount; i++) {
+					commitsToWalk.add(commit.getParentIds()[i]);
+				}
+			}
+		}
+		while (commitsToWalk.size() > 0) {
+			commit = repository.mapCommit(commitsToWalk.remove(0));
+			verifyCurrentHeadCommits(repository, parentMap, commitsToWalk, commit);
+		}
+	}
+
 	HistoryRefreshJob historyRefreshJob = new HistoryRefreshJob("Git history refresh");
 	
 	class GitHistoryContentProvider implements ILazyContentProvider {
diff --git a/org.spearce.egit.ui/src/org/spearce/egit/ui/PluginPreferenceInitializer.java b/org.spearce.egit.ui/src/org/spearce/egit/ui/PluginPreferenceInitializer.java
index c8ca116..3464fe7 100644
--- a/org.spearce.egit.ui/src/org/spearce/egit/ui/PluginPreferenceInitializer.java
+++ b/org.spearce.egit.ui/src/org/spearce/egit/ui/PluginPreferenceInitializer.java
@@ -41,6 +41,7 @@ public class PluginPreferenceInitializer extends AbstractPreferenceInitializer {
 		prefs.setDefault(UIPreferences.RESOURCEHISTORY_SHOW_REV_DETAIL, true);
 		prefs.setDefault(UIPreferences.RESOURCEHISTORY_SHOW_REV_COMMENT, true);
 		prefs.setDefault(UIPreferences.RESOURCEHISTORY_SHOW_TOOLTIPS, false);
+		prefs.setDefault(UIPreferences.RESOURCEHISTORY_SHOW_ONLY_CURRENT_BRANCH, false);
 
 		prefs.setDefault(UIPreferences.FINDTOOLBAR_IGNORE_CASE, true);
 		prefs.setDefault(UIPreferences.FINDTOOLBAR_FIND_IN, 2);
diff --git a/org.spearce.egit.ui/src/org/spearce/egit/ui/UIIcons.java b/org.spearce.egit.ui/src/org/spearce/egit/ui/UIIcons.java
index a04bb5c..4021fdc 100644
--- a/org.spearce.egit.ui/src/org/spearce/egit/ui/UIIcons.java
+++ b/org.spearce.egit.ui/src/org/spearce/egit/ui/UIIcons.java
@@ -42,6 +42,8 @@ public class UIIcons {
 
 	/** Find icon */
 	public static final ImageDescriptor ELCL16_FIND;
+	/** Show only commits in the current branch icon */
+	public static final ImageDescriptor ELCL16_HEAD;
 	/** Next arrow icon */
 	public static final ImageDescriptor ELCL16_NEXT;
 	/** Previous arrow icon */
@@ -67,6 +69,7 @@ public class UIIcons {
 		OVR_CONFLICT = map("ovr/conflict.gif");
 		OVR_ASSUMEVALID = map("ovr/assumevalid.gif");
 		ELCL16_FIND = map("elcl16/find.gif");
+		ELCL16_HEAD = map("elcl16/cbcommits.gif");
 		ELCL16_NEXT = map("elcl16/next.gif");
 		ELCL16_PREVIOUS = map("elcl16/previous.gif");
 		ELCL16_COMMIT = map("elcl16/commit.gif");
diff --git a/org.spearce.egit.ui/src/org/spearce/egit/ui/UIPreferences.java b/org.spearce.egit.ui/src/org/spearce/egit/ui/UIPreferences.java
index 2ff7589..fbc7064 100644
--- a/org.spearce.egit.ui/src/org/spearce/egit/ui/UIPreferences.java
+++ b/org.spearce.egit.ui/src/org/spearce/egit/ui/UIPreferences.java
@@ -32,6 +32,8 @@ public class UIPreferences {
 	/** */
 	public final static String RESOURCEHISTORY_SHOW_FINDTOOLBAR = "resourcehistory_show_findtoolbar";
 	/** */
+	public final static String RESOURCEHISTORY_SHOW_ONLY_CURRENT_BRANCH = "resourcehistory_show_only_current_branch";
+	/** */
 	public final static String FINDTOOLBAR_IGNORE_CASE = "findtoolbar_ignore_case";
 	/** */
 	public final static String FINDTOOLBAR_FIND_IN = "findtoolbar_find_in";
-- 
1.5.4.1

```

## Robin Rosenberg, 2008-03-24 09:47

Subject: Re: [EGIT PATCH 4/4] Show only commits in the current branch.
Message-ID: <200803240947.15130.robin.rosenberg@dewire.com>
URL: https://gitlist.dev/e/200803240947.15130.robin.rosenberg%40dewire.com
In-Reply-To: <1206327487-6543-1-git-send-email-rogersoares@intelinet.com.br>

```
Den Monday 24 March 2008 02.58.07 skrev du:
> This is a quick hack to have the functionality. It
> filters the commits returned by GitFileHistory.getHistoryFor.

Good ui, but I don't like the filter (seem you didn't either from your comment) so I suggest inserting this patch
before the 4/4 patch of yours. Then in your patch drop the filter and fix the call to getHistory. This will
improve performance since the "current branch only" will be the view I (and probably most other people)
will use the most.

@@ -868,39 +865,10 @@ public class GitHistoryPage extends HistoryPage implements IAdaptable,
 						.getHistoryFor(startingPoint,
 								-1,
 								monitor,
-								isShowAllRepoVersions());
+								isShowAllRepoVersions(),
+								!showOnlyCurrentBranch);
 				fileRevisions = Collections.synchronizedList(fileHistoryFor.getFileRevisionsList());

As a side note, I used the IFileHistory{,Provider} interfaces originally because they existed in Eclipse already.
The problem is that it really doesn't fit so the getHistoryFor method used is not part of those interfaces. I was just too lazy to
completely remove the interfaces yet.

-- robin

>From 5935318f49f8bcfdcae81ec12667605e42ea8d9e Mon Sep 17 00:00:00 2001
From: Robin Rosenberg <robin.rosenberg@dewire.com>
Date: Mon, 24 Mar 2008 08:56:35 +0000
Subject: [PATCH] Add a flag to GitFileHistory to follow only current branch

Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
---
 .../egit/core/internal/mapping/GitFileHistory.java |   17 ++++++++++++-----
 .../internal/mapping/GitFileHistoryProvider.java   |    8 +++++---
 .../src/org/spearce/egit/ui/GitHistoryPage.java    |    3 ++-
 3 files changed, 19 insertions(+), 9 deletions(-)

diff --git a/org.spearce.egit.core/src/org/spearce/egit/core/internal/mapping/GitFileHistory.java b/org.spearce.egit.core/src/org/spearce/egit/core/internal/mapping/GitFileHistory.java
index bdef5f1..ea1faa1 100644
--- a/org.spearce.egit.core/src/org/spearce/egit/core/internal/mapping/GitFileHistory.java
+++ b/org.spearce.egit.core/src/org/spearce/egit/core/internal/mapping/GitFileHistory.java
@@ -65,6 +65,8 @@ public class GitFileHistory extends FileHistory implements IAdaptable {
 
 	private final boolean returnAll;
 
+	private boolean allHeads;
+
 	/**
 	 * Construct a {@link GitFileHistory} object for a given resource (path)
 	 * with some filtering applied. The filter could reduce the number of
@@ -78,11 +80,14 @@ public class GitFileHistory extends FileHistory implements IAdaptable {
 	 * @param returnAll
 	 *            true if all versions should be collected even if the filter
 	 *            does not match.
+	 * @param allHeads
+	 *            true if all heads should be followed
 	 */
-	public GitFileHistory(IResource resource, int flags, IProgressMonitor monitor, boolean returnAll) {
+	public GitFileHistory(IResource resource, int flags, IProgressMonitor monitor, boolean returnAll, boolean allHeads) {
 		this.resource = resource;
 		this.flags = flags;
 		this.returnAll = returnAll;
+		this.allHeads = allHeads;
 		String prefix = RepositoryMapping.getMapping(resource).getSubset();
 		String[] prefixSegments = prefix!=null ? prefix.split("/") : new String[0];
 		String[] resourceSegments = resource.getProjectRelativePath().segments(); 
@@ -262,10 +267,12 @@ public class GitFileHistory extends FileHistory implements IAdaptable {
 				List<Commit> startList = new ArrayList<Commit>();
 
 				startList.add(repository.mapCommit(head));
-				for(String branch : repository.getBranches()) {
-					Commit commit = repository.mapCommit(branch);
-					if (commit != null)
-						startList.add(commit);
+				if (allHeads) {
+					for(String branch : repository.getBranches()) {
+						Commit commit = repository.mapCommit(branch);
+						if (commit != null)
+							startList.add(commit);
+					}
 				}
 				Commit[] starts = startList.toArray(new Commit[startList.size()]);
 				EclipseWalker walker = new EclipseWalker(
diff --git a/org.spearce.egit.core/src/org/spearce/egit/core/internal/mapping/GitFileHistoryProvider.java b/org.spearce.egit.core/src/org/spearce/egit/core/internal/mapping/GitFileHistoryProvider.java
index 7847fd5..cbbfaa3 100644
--- a/org.spearce.egit.core/src/org/spearce/egit/core/internal/mapping/GitFileHistoryProvider.java
+++ b/org.spearce.egit.core/src/org/spearce/egit/core/internal/mapping/GitFileHistoryProvider.java
@@ -34,7 +34,7 @@ public class GitFileHistoryProvider extends FileHistoryProvider implements
 
 	public IFileHistory getFileHistoryFor(IResource resource, int flags,
 			IProgressMonitor monitor) {
-		return new GitFileHistory(resource, flags, monitor, false);
+		return new GitFileHistory(resource, flags, monitor, false, true);
 	}
 
 	public IFileRevision getWorkspaceFileRevision(IResource resource) {
@@ -58,12 +58,14 @@ public class GitFileHistoryProvider extends FileHistoryProvider implements
 	 *            progress monitor
 	 * @param returnAll
 	 *            true if all revisions are returned and interesting revisions marked.
+	 * @param allHeads
+	 *            true if all heads should be followed
 	 *
 	 * @return a {@link GitFileHistory} object.
 	 *
 	 */
-	public GitFileHistory getHistoryFor(IResource resource, int flags, IProgressMonitor monitor, boolean returnAll) {
-		return new GitFileHistory(resource, flags, monitor, returnAll); // TODO: implement flags
+	public GitFileHistory getHistoryFor(IResource resource, int flags, IProgressMonitor monitor, boolean returnAll, boolean allHeads) {
+		return new GitFileHistory(resource, flags, monitor, returnAll, allHeads);
 	}
 
 }
diff --git a/org.spearce.egit.ui/src/org/spearce/egit/ui/GitHistoryPage.java b/org.spearce.egit.ui/src/org/spearce/egit/ui/GitHistoryPage.java
index d0346a9..984e54e 100644
--- a/org.spearce.egit.ui/src/org/spearce/egit/ui/GitHistoryPage.java
+++ b/org.spearce.egit.ui/src/org/spearce/egit/ui/GitHistoryPage.java
@@ -840,7 +840,8 @@ public class GitHistoryPage extends HistoryPage implements IAdaptable,
 						.getHistoryFor(startingPoint,
 								-1,
 								monitor,
-								isShowAllRepoVersions());
+								isShowAllRepoVersions(),
+								true);
 				fileRevisions = Collections.synchronizedList(fileHistoryFor.getFileRevisionsList());
 				findToolbar.setFileRevisions(fileRevisions, newtags, branches);
 				Display.getDefault().syncExec(new Runnable() {
-- 
1.5.4.3

```

## Roger C. Soares, 2008-03-24 13:18

Subject: Re: [EGIT PATCH 4/4] Show only commits in the current branch.
Message-ID: <47E7AA42.9010202@intelinet.com.br>
URL: https://gitlist.dev/e/47E7AA42.9010202%40intelinet.com.br
In-Reply-To: <200803240947.15130.robin.rosenberg@dewire.com>

```

Robin Rosenberg escreveu:
> Good ui, but I don't like the filter (seem you didn't either from your comment) so I suggest inserting this patch
> before the 4/4 patch of yours. Then in your patch drop the filter and fix the call to getHistory. This will
> improve performance since the "current branch only" will be the view I (and probably most other people)
> will use the most.
>   
Yep, I did it some time ago and haven't gone back to understand the 
getHistoryFor code. It will be a lot better now, thanks, will do it. :)

[]s,
Roger.

```
