use of com.gitblit.models.PathModel in project gitblit by gitblit.
the class JGitUtilsTest method testFilesInPath.
@Test
public void testFilesInPath() throws Exception {
assertEquals(0, JGitUtils.getFilesInPath(null, null, null).size());
Repository repository = GitBlitSuite.getHelloworldRepository();
List<PathModel> files = JGitUtils.getFilesInPath(repository, null, null);
repository.close();
assertTrue(files.size() > 10);
}
use of com.gitblit.models.PathModel in project gitblit by gitblit.
the class JGitUtilsTest method testDocuments.
@Test
public void testDocuments() throws Exception {
Repository repository = GitBlitSuite.getTicgitRepository();
List<String> extensions = Arrays.asList(new String[] { ".mkd", ".md" });
List<PathModel> markdownDocs = JGitUtils.getDocuments(repository, extensions);
List<PathModel> allFiles = JGitUtils.getDocuments(repository, null);
repository.close();
assertTrue(markdownDocs.size() > 0);
assertTrue(allFiles.size() > markdownDocs.size());
}
use of com.gitblit.models.PathModel in project gitblit by gitblit.
the class JGitUtilsTest method testFilesInPath2.
@Test
public void testFilesInPath2() throws Exception {
assertEquals(0, JGitUtils.getFilesInPath2(null, null, null).size());
Repository repository = GitBlitSuite.getHelloworldRepository();
List<PathModel> files = JGitUtils.getFilesInPath2(repository, null, null);
assertEquals(GitBlitSuite.helloworldSettings.getInteger(HelloworldKeys.files.top, 15), files.size());
files = JGitUtils.getFilesInPath2(repository, "C", null);
assertEquals(GitBlitSuite.helloworldSettings.getInteger(HelloworldKeys.files.C.top, 1), files.size());
files = JGitUtils.getFilesInPath2(repository, "[C++]", null);
assertEquals(GitBlitSuite.helloworldSettings.getInteger(HelloworldKeys.files.Cpp, 1), files.size());
files = JGitUtils.getFilesInPath2(repository, "C/C (K&R)", null);
assertEquals(GitBlitSuite.helloworldSettings.getInteger(HelloworldKeys.files.C.KnR, 1), files.size());
repository.close();
}
use of com.gitblit.models.PathModel in project gitblit by gitblit.
the class JGitUtils method getPathModel.
/**
* Returns a path model by path string
*
* @param repo
* @param path
* @param filter
* @param commit
* @return a path model of the specified object
*/
private static PathModel getPathModel(Repository repo, String path, String filter, RevCommit commit) throws IOException {
long size = 0;
FilestoreModel filestoreItem = null;
TreeWalk tw = TreeWalk.forPath(repo, path, commit.getTree());
String pathString = path;
if (!tw.isSubtree() && (tw.getFileMode(0) != FileMode.GITLINK)) {
pathString = PathUtils.getLastPathComponent(pathString);
size = tw.getObjectReader().getObjectSize(tw.getObjectId(0), Constants.OBJ_BLOB);
if (isPossibleFilestoreItem(size)) {
filestoreItem = getFilestoreItem(tw.getObjectReader().open(tw.getObjectId(0)));
}
} else if (tw.isSubtree()) {
// do not display dirs that are behind in the path
if (!Strings.isNullOrEmpty(filter)) {
pathString = path.replaceFirst(filter + "/", "");
}
// remove the last slash from path in displayed link
if (pathString != null && pathString.charAt(pathString.length() - 1) == '/') {
pathString = pathString.substring(0, pathString.length() - 1);
}
}
return new PathModel(pathString, tw.getPathString(), filestoreItem, size, tw.getFileMode(0).getBits(), tw.getObjectId(0).getName(), commit.getName());
}
use of com.gitblit.models.PathModel in project gitblit by gitblit.
the class JGitUtils method getDocuments.
/**
* Returns the list of files in the repository in the specified commit that
* match one of the specified extensions. This is a CASE-SENSITIVE search.
* If the repository does not exist or is empty, an empty list is returned.
*
* @param repository
* @param extensions
* @param objectId
* @return list of files in repository with a matching extension
*/
public static List<PathModel> getDocuments(Repository repository, List<String> extensions, String objectId) {
List<PathModel> list = new ArrayList<PathModel>();
if (!hasCommits(repository)) {
return list;
}
RevCommit commit = getCommit(repository, objectId);
final TreeWalk tw = new TreeWalk(repository);
try {
tw.addTree(commit.getTree());
if (extensions != null && extensions.size() > 0) {
List<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;
if (suffixFilters.size() == 1) {
filter = suffixFilters.get(0);
} else {
filter = OrTreeFilter.create(suffixFilters);
}
tw.setFilter(filter);
tw.setRecursive(true);
}
while (tw.next()) {
list.add(getPathModel(tw, null, commit));
}
} catch (IOException e) {
error(e, repository, "{0} failed to get documents for commit {1}", commit.getName());
} finally {
tw.close();
}
Collections.sort(list);
return list;
}
Aggregations