From ed9d6746e53baa42d4e3e476736592c160184ac7 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Fri, 29 Mar 2013 16:01:26 -0400
Subject: [PATCH] Improve blame page error checking

---
 src/main/java/com/gitblit/wicket/pages/BlamePage.html |    2 ++
 releases.moxie                                        |    2 +-
 src/main/java/com/gitblit/wicket/pages/BlamePage.java |   29 +++++++++++++++++++++++++++++
 3 files changed, 32 insertions(+), 1 deletions(-)

diff --git a/releases.moxie b/releases.moxie
index a274b07..0f5aa11 100644
--- a/releases.moxie
+++ b/releases.moxie
@@ -9,7 +9,7 @@
 	- Raw servlet was insecure. If someone knew the exact repository name and path to a file, the raw blob could be retrieved bypassing security constraints. (issue 198)
     fixes:
      - Could not reset settings with $ or { characters through Gitblit Manager because they are not properly escaped
-	 - Added more error checking to blob page
+	 - Added more error checking to blob page and blame page
 	 - Fix NPE when getting user's fork without repository list caching (issue 182)
 	 - Fix internal error on folder history links (issue 192)
 	 - Fixed incorrect icon file name for .doc files (issue 200)
diff --git a/src/main/java/com/gitblit/wicket/pages/BlamePage.html b/src/main/java/com/gitblit/wicket/pages/BlamePage.html
index 9391eaf..722cf3d 100644
--- a/src/main/java/com/gitblit/wicket/pages/BlamePage.html
+++ b/src/main/java/com/gitblit/wicket/pages/BlamePage.html
@@ -17,6 +17,8 @@
 
 	<!-- breadcrumbs -->
 	<div wicket:id="breadcrumbs">[breadcrumbs]</div>
+	
+	<div wicket:id="missingBlob">[missing blob]</div>
 		
 	<!--  blame content -->
 	<table class="annotated" style="margin-bottom:5px;">
diff --git a/src/main/java/com/gitblit/wicket/pages/BlamePage.java b/src/main/java/com/gitblit/wicket/pages/BlamePage.java
index d76181d..9b4c15c 100644
--- a/src/main/java/com/gitblit/wicket/pages/BlamePage.java
+++ b/src/main/java/com/gitblit/wicket/pages/BlamePage.java
@@ -32,7 +32,9 @@
 import com.gitblit.GitBlit;
 import com.gitblit.Keys;
 import com.gitblit.models.AnnotatedLine;
+import com.gitblit.models.PathModel;
 import com.gitblit.utils.DiffUtils;
+import com.gitblit.utils.JGitUtils;
 import com.gitblit.utils.StringUtils;
 import com.gitblit.wicket.WicketUtils;
 import com.gitblit.wicket.panels.CommitHeaderPanel;
@@ -69,6 +71,24 @@
 				"EEEE, MMMM d, yyyy HH:mm Z");
 		final DateFormat df = new SimpleDateFormat(format);
 		df.setTimeZone(getTimeZone());
+		
+		PathModel pathModel = null;
+		List<PathModel> paths = JGitUtils.getFilesInPath(getRepository(), StringUtils.getRootPath(blobPath), commit);
+		for (PathModel path : paths) {
+			if (path.path.equals(blobPath)) {
+				pathModel = path;
+				break;
+			}
+		}
+		
+		if (pathModel == null) {
+			add(new Label("annotation").setVisible(false));
+			add(new Label("missingBlob", missingBlob(blobPath, commit)).setEscapeModelStrings(false));
+			return;
+		}
+		
+		add(new Label("missingBlob").setVisible(false));
+		
 		List<AnnotatedLine> lines = DiffUtils.blame(getRepository(), blobPath, objectId);
 		ListDataProvider<AnnotatedLine> blameDp = new ListDataProvider<AnnotatedLine>(lines);
 		DataView<AnnotatedLine> blameView = new DataView<AnnotatedLine>("annotation", blameDp) {
@@ -126,4 +146,13 @@
 	protected String getPageName() {
 		return getString("gb.blame");
 	}
+	
+	protected String missingBlob(String blobPath, RevCommit commit) {
+		StringBuilder sb = new StringBuilder();
+		sb.append("<div class=\"alert alert-error\">");
+		String pattern = getString("gb.doesNotExistInTree").replace("{0}", "<b>{0}</b>").replace("{1}", "<b>{1}</b>");
+		sb.append(MessageFormat.format(pattern, blobPath, commit.getTree().getId().getName()));
+		sb.append("</div>");
+		return sb.toString();
+	}
 }

--
Gitblit v1.9.1