From f97bf09263fe8ef7ba4dcf231dfe7b8265b1e0df Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Wed, 27 Apr 2011 21:22:12 -0400
Subject: [PATCH] Centralized markdown transforms. Moved config ops to GitBlit.
---
src/com/gitblit/utils/JGitUtils.java | 126 +++++++++++++++++++++++++++++-------------
1 files changed, 87 insertions(+), 39 deletions(-)
diff --git a/src/com/gitblit/utils/JGitUtils.java b/src/com/gitblit/utils/JGitUtils.java
index 5118425..4cfed0d 100644
--- a/src/com/gitblit/utils/JGitUtils.java
+++ b/src/com/gitblit/utils/JGitUtils.java
@@ -4,22 +4,25 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
-import java.io.RandomAccessFile;
+import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.diff.DiffEntry;
+import org.eclipse.jgit.diff.DiffEntry.ChangeType;
import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.diff.RawTextComparator;
-import org.eclipse.jgit.diff.DiffEntry.ChangeType;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.MissingObjectException;
@@ -42,8 +45,10 @@
import org.eclipse.jgit.revwalk.filter.RevFilter;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
+import org.eclipse.jgit.treewalk.filter.OrTreeFilter;
import org.eclipse.jgit.treewalk.filter.PathFilter;
import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
+import org.eclipse.jgit.treewalk.filter.PathSuffixFilter;
import org.eclipse.jgit.treewalk.filter.TreeFilter;
import org.eclipse.jgit.util.io.DisabledOutputStream;
import org.slf4j.Logger;
@@ -65,6 +70,11 @@
public static final String R_NOTES_COMMITS = R_NOTES + "commits";
private final static Logger LOGGER = LoggerFactory.getLogger(JGitUtils.class);
+
+ public static Repository createRepository(File repositoriesFolder, String name, boolean bare) {
+ Git git = Git.init().setDirectory(new File(repositoriesFolder, name)).setBare(bare).call();
+ return git.getRepository();
+ }
public static List<String> getRepositoryList(File repositoriesFolder, boolean exportAll, boolean readNested) {
List<String> list = new ArrayList<String>();
@@ -79,8 +89,18 @@
for (File file : folder.listFiles()) {
if (file.isDirectory() && !file.getName().equalsIgnoreCase(Constants.DOT_GIT)) {
// if this is a git repository add it to the list
+ //
+ // first look for standard folder/.git structure
File gitFolder = new File(file, Constants.DOT_GIT);
boolean isGitRepository = gitFolder.exists() && gitFolder.isDirectory();
+
+ // then look for folder.git/HEAD
+ if (!isGitRepository) {
+ if (file.getName().endsWith(Constants.DOT_GIT_EXT) && new File(file, Constants.HEAD).exists()) {
+ gitFolder = file;
+ isGitRepository = true;
+ }
+ }
boolean exportRepository = isGitRepository && (exportAll || new File(gitFolder, "git-daemon-export-ok").exists());
if (exportRepository) {
@@ -123,6 +143,10 @@
public static Date getFirstChange(Repository r, String branch) {
try {
RevCommit commit = getFirstCommit(r, branch);
+ if (commit == null) {
+ // fresh repository
+ return new Date(r.getDirectory().lastModified());
+ }
return getCommitDate(commit);
} catch (Throwable t) {
LOGGER.error("Failed to determine first change", t);
@@ -130,8 +154,12 @@
return null;
}
- public static Date getLastChange(Repository r) {
+ public static Date getLastChange(Repository r) {
RevCommit commit = getCommit(r, Constants.HEAD);
+ if (commit == null) {
+ // fresh repository
+ return new Date(r.getDirectory().lastModified());
+ }
return getCommitDate(commit);
}
@@ -237,12 +265,14 @@
}
public static String getRawContentAsString(Repository r, RevBlob blob) {
- return new String(getRawContent(r, blob));
+ byte [] content = getRawContent(r, blob);
+ return new String(content, Charset.forName("UTF-8"));
}
public static String getRawContentAsString(Repository r, RevCommit commit, String blobPath) {
RevObject obj = getRevObject(r, commit.getTree(), blobPath);
- return new String(getRawContent(r, (RevBlob) obj));
+ byte [] content = getRawContent(r, (RevBlob) obj);
+ return new String(content, Charset.forName("UTF-8"));
}
public static List<PathModel> getFilesInPath(Repository r, String basePath, String objectId) {
@@ -252,6 +282,9 @@
public static List<PathModel> getFilesInPath(Repository r, String basePath, RevCommit commit) {
List<PathModel> list = new ArrayList<PathModel>();
+ if (commit == null) {
+ return list;
+ }
final TreeWalk walk = new TreeWalk(r);
try {
walk.addTree(commit.getTree());
@@ -324,6 +357,53 @@
LOGGER.error("failed to determine files in commit!", t);
}
return list;
+ }
+
+ public static List<PathModel> getDocuments(Repository r, List<String> extensions) {
+ List<PathModel> list = new ArrayList<PathModel>();
+ RevCommit commit = getCommit(r, Constants.HEAD);
+ final TreeWalk walk = new TreeWalk(r);
+ try {
+ walk.addTree(commit.getTree());
+ if (extensions != null && extensions.size() > 0) {
+ Collection<TreeFilter> suffixFilters = new ArrayList<TreeFilter>();
+ for (String extension:extensions) {
+ if (extension.charAt(0) == '.') {
+ suffixFilters.add(PathSuffixFilter.create(extension));
+ } else {
+ // escape the . since this is a regexp filter
+ suffixFilters.add(PathSuffixFilter.create("\\." + extension));
+ }
+ }
+ TreeFilter filter = OrTreeFilter.create(suffixFilters);
+ walk.setFilter(filter);
+ walk.setRecursive(true);
+ while (walk.next()) {
+ list.add(getPathModel(walk, null, commit));
+ }
+ } else {
+ while (walk.next()) {
+ list.add(getPathModel(walk, null, commit));
+ }
+ }
+ } catch (IOException e) {
+ LOGGER.error("Failed to get files for commit " + commit.getName(), e);
+ } finally {
+ walk.release();
+ }
+ Collections.sort(list);
+ return list;
+ }
+
+ public static Map<ChangeType, AtomicInteger> getChangedPathsStats(List<PathChangeModel> paths) {
+ Map<ChangeType, AtomicInteger> stats = new HashMap<ChangeType, AtomicInteger>();
+ for (PathChangeModel path : paths) {
+ if (!stats.containsKey(path.changeType)) {
+ stats.put(path.changeType, new AtomicInteger(0));
+ }
+ stats.get(path.changeType).incrementAndGet();
+ }
+ return stats;
}
public static enum DiffOutputType {
@@ -695,39 +775,7 @@
return r.toString();
}
- public static String getRepositoryDescription(Repository r) {
- File dir = r.getDirectory();
- if (dir.exists()) {
- File description = new File(dir, "description");
- if (description.exists() && description.length() > 0) {
- RandomAccessFile raf = null;
- try {
- raf = new RandomAccessFile(description, "r");
- byte[] buffer = new byte[(int) description.length()];
- raf.readFully(buffer);
- return new String(buffer);
- } catch (Throwable t) {
- } finally {
- try {
- raf.close();
- } catch (Throwable t) {
- }
- }
- }
- }
- return "";
- }
-
- public static String getRepositoryOwner(Repository r) {
- StoredConfig c = readConfig(r);
- if (c == null) {
- return "";
- }
- String o = c.getString("gitweb", null, "owner");
- return o == null ? "" : o;
- }
-
- private static StoredConfig readConfig(Repository r) {
+ public static StoredConfig readConfig(Repository r) {
StoredConfig c = r.getConfig();
if (c != null) {
try {
@@ -797,7 +845,7 @@
metrics.add(0, total);
return metrics;
}
-
+
public static RefModel getTicketsBranch(Repository r) {
RefModel ticgitBranch = null;
try {
--
Gitblit v1.9.1