From 58be4a33a1ca70baae779d80eff5d79ab7c523a3 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Mon, 30 Sep 2013 10:10:36 -0400
Subject: [PATCH] Change diffstat character to improve look and layout

---
 src/main/java/com/gitblit/utils/JGitUtils.java |   84 ++++++++++++++++++++++++++----------------
 1 files changed, 52 insertions(+), 32 deletions(-)

diff --git a/src/main/java/com/gitblit/utils/JGitUtils.java b/src/main/java/com/gitblit/utils/JGitUtils.java
index 66dbd60..c494ccc 100644
--- a/src/main/java/com/gitblit/utils/JGitUtils.java
+++ b/src/main/java/com/gitblit/utils/JGitUtils.java
@@ -81,7 +81,6 @@
 import org.eclipse.jgit.treewalk.filter.PathSuffixFilter;
 import org.eclipse.jgit.treewalk.filter.TreeFilter;
 import org.eclipse.jgit.util.FS;
-import org.eclipse.jgit.util.io.DisabledOutputStream;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -403,8 +402,22 @@
 		if (! path.exists()) return -1;
 
 		int perm = configShared.getPerm();
-		int mode = JnaUtils.getFilemode(path);
+		JnaUtils.Filestat stat = JnaUtils.getFilestat(path);
+		if (stat == null) return -1;
+		int mode = stat.mode;
 		if (mode < 0) return -1;
+
+		// Now, here is the kicker: Under Linux, chmod'ing a sgid file whose guid is different from the process'
+		// effective guid will reset the sgid flag of the file. Since there is no way to get the sgid flag back in
+		// that case, we decide to rather not touch is and getting the right permissions will have to be achieved
+		// in a different way, e.g. by using an appropriate umask for the Gitblit process.
+		if (System.getProperty("os.name").toLowerCase().startsWith("linux")) {
+			if ( ((mode & (JnaUtils.S_ISGID | JnaUtils.S_ISUID)) != 0)
+				&& stat.gid != JnaUtils.getegid() ) {
+				LOGGER.debug("Not adjusting permissions to prevent clearing suid/sgid bits for '" + path + "'" );
+				return 0;
+			}
+		}
 
 		// If the owner has no write access, delete it from group and other, too.
 		if ((mode & JnaUtils.S_IWUSR) == 0) perm &= ~0222;
@@ -728,6 +741,8 @@
 		try {
 			if (tree == null) {
 				ObjectId object = getDefaultBranch(repository);
+				if (object == null)
+					return null;
 				RevCommit commit = rw.parseCommit(object);
 				tree = commit.getTree();
 			}
@@ -885,7 +900,7 @@
 		Collections.sort(list);
 		return list;
 	}
-
+	
 	/**
 	 * Returns the list of files changed in a specified commit. If the
 	 * repository does not exist or is empty, an empty list is returned.
@@ -896,6 +911,21 @@
 	 * @return list of files changed in a commit
 	 */
 	public static List<PathChangeModel> getFilesInCommit(Repository repository, RevCommit commit) {
+		return getFilesInCommit(repository, commit, true);
+	}
+
+	/**
+	 * Returns the list of files changed in a specified commit. If the
+	 * repository does not exist or is empty, an empty list is returned.
+	 * 
+	 * @param repository
+	 * @param commit
+	 *            if null, HEAD is assumed.
+	 * @param calculateDiffStat
+	 *            if true, each PathChangeModel will have insertions/deletions
+	 * @return list of files changed in a commit
+	 */
+	public static List<PathChangeModel> getFilesInCommit(Repository repository, RevCommit commit, boolean calculateDiffStat) {
 		List<PathChangeModel> list = new ArrayList<PathChangeModel>();
 		if (!hasCommits(repository)) {
 			return list;
@@ -920,26 +950,25 @@
 				tw.release();
 			} else {
 				RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
-				DiffFormatter df = new DiffFormatter(DisabledOutputStream.INSTANCE);
+				DiffStatFormatter df = new DiffStatFormatter(commit.getName());
 				df.setRepository(repository);
 				df.setDiffComparator(RawTextComparator.DEFAULT);
 				df.setDetectRenames(true);
 				List<DiffEntry> diffs = df.scan(parent.getTree(), commit.getTree());
 				for (DiffEntry diff : diffs) {
-					String objectId = diff.getNewId().name();
-					if (diff.getChangeType().equals(ChangeType.DELETE)) {
-						list.add(new PathChangeModel(diff.getOldPath(), diff.getOldPath(), 0, diff
-								.getNewMode().getBits(), objectId, commit.getId().getName(), diff
-								.getChangeType()));
-					} else if (diff.getChangeType().equals(ChangeType.RENAME)) {
-						list.add(new PathChangeModel(diff.getOldPath(), diff.getNewPath(), 0, diff
-								.getNewMode().getBits(), objectId, commit.getId().getName(), diff
-								.getChangeType()));
-					} else {
-						list.add(new PathChangeModel(diff.getNewPath(), diff.getNewPath(), 0, diff
-								.getNewMode().getBits(), objectId, commit.getId().getName(), diff
-								.getChangeType()));
+					// create the path change model
+					PathChangeModel pcm = PathChangeModel.from(diff, commit.getName());
+					
+					if (calculateDiffStat) {
+						// update file diffstats
+						df.format(diff);
+						PathChangeModel pathStat = df.getDiffStat().getPath(pcm.path);
+						if (pathStat != null) {
+							pcm.insertions = pathStat.insertions;
+							pcm.deletions = pathStat.deletions;
+						}
 					}
+					list.add(pcm);
 				}
 			}
 		} catch (Throwable t) {
@@ -974,20 +1003,8 @@
 
 			List<DiffEntry> diffEntries = df.scan(startCommit.getTree(), endCommit.getTree());
 			for (DiffEntry diff : diffEntries) {
-				
-				if (diff.getChangeType().equals(ChangeType.DELETE)) {
-					list.add(new PathChangeModel(diff.getOldPath(), diff.getOldPath(), 0, diff
-							.getNewMode().getBits(), diff.getOldId().name(), null, diff
-							.getChangeType()));
-				} else if (diff.getChangeType().equals(ChangeType.RENAME)) {
-					list.add(new PathChangeModel(diff.getOldPath(), diff.getNewPath(), 0, diff
-							.getNewMode().getBits(), diff.getNewId().name(), null, diff
-							.getChangeType()));
-				} else {
-					list.add(new PathChangeModel(diff.getNewPath(), diff.getNewPath(), 0, diff
-							.getNewMode().getBits(), diff.getNewId().name(), null, diff
-							.getChangeType()));
-				}
+				PathChangeModel pcm = PathChangeModel.from(diff,  null);
+				list.add(pcm);
 			}			
 			Collections.sort(list);
 		} catch (Throwable t) {
@@ -1326,14 +1343,17 @@
 	 */
 	public static List<RevCommit> searchRevlogs(Repository repository, String objectId,
 			String value, final com.gitblit.Constants.SearchType type, int offset, int maxCount) {
-		final String lcValue = value.toLowerCase();
 		List<RevCommit> list = new ArrayList<RevCommit>();
+		if (StringUtils.isEmpty(value)) {
+			return list;
+		}
 		if (maxCount == 0) {
 			return list;
 		}
 		if (!hasCommits(repository)) {
 			return list;
 		}
+		final String lcValue = value.toLowerCase();
 		try {
 			// resolve branch
 			ObjectId branchObject;

--
Gitblit v1.9.1