Fix map file parsing, we are only interested in GIT entries

Bug: 328745
Change-Id: Ic49997072ffc94cb056ba8b9ff7b99afff15bf7a
Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com>
diff --git a/org.eclipse.egit.relengtools/src/org/eclipse/egit/internal/relengtools/GetBugsOperation.java b/org.eclipse.egit.relengtools/src/org/eclipse/egit/internal/relengtools/GetBugsOperation.java
index 1b41e9b..a1dc122 100755
--- a/org.eclipse.egit.relengtools/src/org/eclipse/egit/internal/relengtools/GetBugsOperation.java
+++ b/org.eclipse.egit.relengtools/src/org/eclipse/egit/internal/relengtools/GetBugsOperation.java
@@ -17,7 +17,6 @@
 import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.URLConnection;
-import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 import java.util.TreeMap;
@@ -31,7 +30,12 @@
 import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.SubProgressMonitor;
 import org.eclipse.egit.core.GitTag;
+import org.eclipse.egit.core.project.RepositoryMapping;
 import org.eclipse.jface.operation.IRunnableWithProgress;
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.api.LogCommand;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.revwalk.RevCommit;
 import org.eclipse.ui.statushandlers.StatusManager;
 
 public class GetBugsOperation {
@@ -74,13 +78,26 @@
 					monitor.beginTask(
 							Messages.getString("GetBugsOperation.0"), totalWork); //$NON-NLS-1$
 
-					// task 1 -- get bug number from comments
-					final Set bugTree = new HashSet();
+					final IProject[] selectedProjects = wizard
+							.getSelectedProjects();
+
+					// task 1 -- get bug numbers from comments
+					Set<Integer> bugTree;
+					try {
+						bugTree = getBugNumbersFromComments(
+								selectedProjects,
+								new SubProgressMonitor(
+										monitor,
+										85,
+										SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
+					} catch (final Exception e) {
+						monitor.done();
+						return;
+					}
 
 					// task 2 -- create map of bugs and summaries
-					final Integer[] bugs = (Integer[]) bugTree
-							.toArray(new Integer[0]);
-					final TreeMap map = (TreeMap) getBugzillaSummaries(bugs,
+					final Integer[] bugs = bugTree.toArray(new Integer[0]);
+					final Map<Integer, String> map = getBugzillaSummaries(bugs,
 							new SubProgressMonitor(monitor, 15,
 									SubProgressMonitor.SUPPRESS_SUBTASK_LABEL));
 					page.getShell().getDisplay().asyncExec(new Runnable() {
@@ -99,29 +116,45 @@
 		}
 	}
 
-	protected Set getBugNumbersFromComments(Object[] syncInfos,
-			IProgressMonitor monitor) {
-		monitor.beginTask("Scanning comments for bug numbers", syncInfos.length);
-		final TreeSet set = new TreeSet();
-		for (int i = 0; i < syncInfos.length; i++) {
-			final Object info = syncInfos[i];
-			getBugNumbersForSyncInfo(info, monitor, set);
+	protected Set<Integer> getBugNumbersFromComments(IProject[] projects,
+			IProgressMonitor monitor) throws Exception {
+		monitor.beginTask("Scanning comments for bug numbers", projects.length);
+		final Set<Integer> set = new TreeSet<Integer>();
+		for (int i = 0; i < projects.length; i++) {
+			getBugNumbersForProject(projects[i], monitor, set);
 			monitor.worked(1);
 		}
 		monitor.done();
 		return set;
 	}
 
-	private void getBugNumbersForSyncInfo(Object info,
-			IProgressMonitor monitor, Set set) {
+	private void getBugNumbersForProject(IProject project,
+			IProgressMonitor monitor, Set<Integer> set) throws Exception {
 
+		final RepositoryMapping rm = RepositoryMapping.getMapping(project);
+		final Repository repository = rm.getRepository();
+
+		final RevCommit previousCommit = ShowInfoHandler.getCommitForTag(
+				repository, getProjectTag(project).getName());
+		final RevCommit latestCommit = ShowInfoHandler.getLatestCommitFor(rm,
+				repository, project);
+
+		final Git git = new Git(repository);
+		final LogCommand log = git.log();
+		log.addRange(previousCommit, latestCommit);
+		for (final RevCommit commit : log.call()) {
+			findBugNumber(commit.getFullMessage(), set);
+		}
 	}
 
 	private GitTag getProjectTag(IProject project) {
+		final MapEntry mapEntry = wizard.getMapProject().getMapEntry(project);
+		if (mapEntry != null)
+			return mapEntry.getTag();
 		return MapEntry.DEFAULT;
 	}
 
-	protected void findBugNumber(String comment, Set set) {
+	protected void findBugNumber(String comment, Set<Integer> set) {
 		if (comment == null) {
 			return;
 		}
@@ -136,14 +169,15 @@
 	 * Method uses set of bug numbers to query bugzilla and get summary of each
 	 * bug
 	 */
-	protected Map getBugzillaSummaries(Integer[] bugs, IProgressMonitor monitor) {
+	protected Map<Integer, String> getBugzillaSummaries(Integer[] bugs,
+			IProgressMonitor monitor) {
 		monitor.beginTask(
 				Messages.getString("GetBugsOperation.1"), bugs.length + 1); //$NON-NLS-1$
 		HttpURLConnection hURL;
 		DataInputStream in;
 		URLConnection url;
 		StringBuffer buffer;
-		final TreeMap map = new TreeMap();
+		final TreeMap<Integer, String> map = new TreeMap<Integer, String>();
 		for (int i = 0; i < bugs.length; i++) {
 			try {
 				url = (new URL(BUG_DATABASE_PREFIX + bugs[i]
diff --git a/org.eclipse.egit.relengtools/src/org/eclipse/egit/internal/relengtools/MapEntry.java b/org.eclipse.egit.relengtools/src/org/eclipse/egit/internal/relengtools/MapEntry.java
index 6b3a590..bb7f67e 100755
--- a/org.eclipse.egit.relengtools/src/org/eclipse/egit/internal/relengtools/MapEntry.java
+++ b/org.eclipse.egit.relengtools/src/org/eclipse/egit/internal/relengtools/MapEntry.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2010 IBM Corporation and others.
+ * Copyright (c) 2000, 2011 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -43,8 +43,6 @@
 
 	private OrderedMap arguments = new OrderedMap();
 
-	private boolean legacy = false;
-
 	private String version;
 
 	public static void main(String[] args) {
@@ -136,6 +134,8 @@
 		final String[] args = getArrayFromStringWithBlank(
 				entryLine.substring(end + 1), ",");
 		this.arguments = populate(args);
+		if (this.arguments == null)
+			return;
 		final String tag = (String) arguments.get(KEY_TAG);
 		final String repo = (String) arguments.get(REPO);
 		if (tag == null || tag.length() == 0 || repo == null
@@ -145,43 +145,27 @@
 	}
 
 	/*
-	 * Build a table from the given array. In the new format,the array contains
-	 * key=value elements. Otherwise we fill in the key based on the old format.
+	 * Build a table from the given array. We only understand GIT map entries of
+	 * the format "GIT,key=value[,key=value]*
 	 */
 	private OrderedMap populate(String[] entries) {
+		if (entries.length <= 1 || !"GIT".equalsIgnoreCase(entries[0])) {
+			// not a GIT entry, ignore
+			return null;
+		}
+
 		final OrderedMap result = new OrderedMap();
-		for (int i = 0; i < entries.length; i++) {
+		for (int i = 1; i < entries.length; i++) {
 			final String entry = entries[i];
 			final int index = entry.indexOf('=');
 			if (index == -1) {
-				// we only handle CVS entries
-				if (i == 0 && "GIT".equalsIgnoreCase(entry))
-					continue;
-				// legacy story...
-				return legacyPopulate(entries);
+				// bad entry
+				return null;
 			}
 			final String key = entry.substring(0, index);
 			final String value = entry.substring(index + 1);
 			result.put(key, value);
 		}
-		result.toString();
-		return result;
-	}
-
-	private OrderedMap legacyPopulate(String[] entries) {
-		legacy = true;
-		final OrderedMap result = new OrderedMap();
-		// must have at least tag and connect string
-		if (entries.length >= 2) {
-			// Version
-			result.put(KEY_TAG, entries[0]);
-			// Repo Connect String
-			result.put(REPO, entries[1]);
-
-			// Optional CVS Module Name
-			if (entries.length >= 3)
-				result.put(KEY_PATH, entries[3]);
-		}
 		return result;
 	}
 
@@ -263,22 +247,6 @@
 
 	public String getMapString() {
 		final StringBuffer result = new StringBuffer();
-		if (legacy) {
-			result.append(getType());
-			result.append('@');
-			result.append(getId());
-			if (version != null) {
-				result.append(',');
-				result.append(version);
-			}
-			result.append('=');
-			result.append(getTagName());
-			result.append(',');
-			result.append(getRepo());
-			result.append(',');
-			result.append(getPath());
-			return result.toString();
-		}
 		result.append(getType());
 		result.append('@');
 		result.append(getId());
diff --git a/org.eclipse.egit.relengtools/src/org/eclipse/egit/internal/relengtools/MapFile.java b/org.eclipse.egit.relengtools/src/org/eclipse/egit/internal/relengtools/MapFile.java
index 045f716..363954f 100755
--- a/org.eclipse.egit.relengtools/src/org/eclipse/egit/internal/relengtools/MapFile.java
+++ b/org.eclipse.egit.relengtools/src/org/eclipse/egit/internal/relengtools/MapFile.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2010 IBM Corporation and others.
+ * Copyright (c) 2000, 2011 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -19,16 +19,15 @@
 import java.util.List;
 import java.util.Set;
 
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.Status;
-
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.resources.IResourceProxy;
 import org.eclipse.core.resources.IResourceProxyVisitor;
 import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
 
 public class MapFile {
 
@@ -54,7 +53,7 @@
 
 	protected void loadEntries() throws CoreException {
 		InputStream inputStream = null;
-		List list = new ArrayList();
+		final List<MapEntry> list = new ArrayList<MapEntry>();
 
 		try {
 			inputStream = file.getContents();
@@ -63,7 +62,9 @@
 			String aLine = aReader.readLine();
 			while (aLine != null) {
 				if (isMapLine(aLine)) {
-					list.add(new MapEntry(aLine));
+					final MapEntry entry = new MapEntry(aLine);
+					if (entry.isValid())
+						list.add(new MapEntry(aLine));
 				}
 				aLine = aReader.readLine();
 			}
@@ -82,7 +83,7 @@
 			}
 		}
 
-		this.entries = (MapEntry[]) list.toArray(new MapEntry[list.size()]);
+		this.entries = list.toArray(new MapEntry[list.size()]);
 	}
 
 	private boolean isMapLine(String line) {
@@ -102,6 +103,10 @@
 		return false;
 	}
 
+	public int size() {
+		return entries != null ? entries.length : 0;
+	}
+
 	public MapEntry getMapEntry(IProject project) {
 		for (int j = 0; j < entries.length; j++) {
 			if (entries[j].isMappedTo(project)) {
@@ -163,9 +168,14 @@
 
 				if (type == IResource.FILE
 						&& resourceProxy.getName().endsWith(
-								MAP_FILE_NAME_ENDING))
-					mapFiles.add(new MapFile((IFile) resourceProxy
-							.requestResource()));
+								MAP_FILE_NAME_ENDING)) {
+					final MapFile map = new MapFile(
+							(IFile) resourceProxy.requestResource());
+
+					// don't both with map files that didn't contain GIT entries
+					if (map.size() > 0)
+						mapFiles.add(map);
+				}
 
 				return true;
 			}