use of org.eclipse.jgit.treewalk.TreeWalk in project gitblit by gitblit.
the class BugtraqConfig method getBaseConfig.
// Utils ==================================================================
@Nullable
private static Config getBaseConfig(@NotNull Repository repository, @NotNull String configFileName) throws IOException, ConfigInvalidException {
final Config baseConfig;
if (repository.isBare()) {
// read bugtraq config directly from the repository
String content = null;
RevWalk rw = new RevWalk(repository);
TreeWalk tw = new TreeWalk(repository);
tw.setFilter(PathFilterGroup.createFromStrings(configFileName));
try {
final Ref ref = repository.getRef(Constants.HEAD);
if (ref == null) {
return null;
}
ObjectId headId = ref.getTarget().getObjectId();
if (headId == null || ObjectId.zeroId().equals(headId)) {
return null;
}
RevCommit commit = rw.parseCommit(headId);
RevTree tree = commit.getTree();
tw.reset(tree);
while (tw.next()) {
ObjectId entid = tw.getObjectId(0);
FileMode entmode = tw.getFileMode(0);
if (FileMode.REGULAR_FILE == entmode) {
ObjectLoader ldr = repository.open(entid, Constants.OBJ_BLOB);
content = new String(ldr.getCachedBytes(), commit.getEncoding());
break;
}
}
} finally {
rw.dispose();
tw.close();
}
if (content == null) {
// config not found
baseConfig = null;
} else {
// parse the config
Config config = new Config();
config.fromText(content);
baseConfig = config;
}
} else {
// read bugtraq config from work tree
final File baseFile = new File(repository.getWorkTree(), configFileName);
if (baseFile.isFile()) {
FileBasedConfig fileConfig = new FileBasedConfig(baseFile, repository.getFS());
fileConfig.load();
baseConfig = fileConfig;
} else {
baseConfig = null;
}
}
return baseConfig;
}
use of org.eclipse.jgit.treewalk.TreeWalk in project gitblit by gitblit.
the class CompressionUtils method tar.
/**
* Compresses/archives the contents of the tree at the (optionally)
* specified revision and the (optionally) specified basepath to the
* supplied outputstream.
*
* @param algorithm
* compression algorithm for tar (optional)
* @param repository
* @param basePath
* if unspecified, entire repository is assumed.
* @param objectId
* if unspecified, HEAD is assumed.
* @param os
* @return true if repository was successfully zipped to supplied output
* stream
*/
private static boolean tar(String algorithm, Repository repository, IFilestoreManager filestoreManager, String basePath, String objectId, OutputStream os) {
RevCommit commit = JGitUtils.getCommit(repository, objectId);
if (commit == null) {
return false;
}
OutputStream cos = os;
if (!StringUtils.isEmpty(algorithm)) {
try {
cos = new CompressorStreamFactory().createCompressorOutputStream(algorithm, os);
} catch (CompressorException e1) {
error(e1, repository, "{0} failed to open {1} stream", algorithm);
}
}
boolean success = false;
RevWalk rw = new RevWalk(repository);
TreeWalk tw = new TreeWalk(repository);
try {
tw.reset();
tw.addTree(commit.getTree());
TarArchiveOutputStream tos = new TarArchiveOutputStream(cos);
tos.setAddPaxHeadersForNonAsciiNames(true);
tos.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
if (!StringUtils.isEmpty(basePath)) {
PathFilter f = PathFilter.create(basePath);
tw.setFilter(f);
}
tw.setRecursive(true);
MutableObjectId id = new MutableObjectId();
long modified = commit.getAuthorIdent().getWhen().getTime();
while (tw.next()) {
FileMode mode = tw.getFileMode(0);
if (mode == FileMode.GITLINK || mode == FileMode.TREE) {
continue;
}
tw.getObjectId(id, 0);
ObjectLoader loader = repository.open(id);
if (FileMode.SYMLINK == mode) {
TarArchiveEntry entry = new TarArchiveEntry(tw.getPathString(), TarArchiveEntry.LF_SYMLINK);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
loader.copyTo(bos);
entry.setLinkName(bos.toString());
entry.setModTime(modified);
tos.putArchiveEntry(entry);
tos.closeArchiveEntry();
} else {
TarArchiveEntry entry = new TarArchiveEntry(tw.getPathString());
entry.setMode(mode.getBits());
entry.setModTime(modified);
FilestoreModel filestoreItem = null;
if (JGitUtils.isPossibleFilestoreItem(loader.getSize())) {
filestoreItem = JGitUtils.getFilestoreItem(tw.getObjectReader().open(id));
}
final long size = (filestoreItem == null) ? loader.getSize() : filestoreItem.getSize();
entry.setSize(size);
tos.putArchiveEntry(entry);
if (filestoreItem == null) {
//Copy repository stored file
loader.copyTo(tos);
} else {
//Copy filestore file
try (FileInputStream streamIn = new FileInputStream(filestoreManager.getStoragePath(filestoreItem.oid))) {
IOUtils.copyLarge(streamIn, tos);
} catch (Throwable e) {
LOGGER.error(MessageFormat.format("Failed to archive filestore item {0}", filestoreItem.oid), e);
//Handle as per other errors
throw e;
}
}
tos.closeArchiveEntry();
}
}
tos.finish();
tos.close();
cos.close();
success = true;
} catch (IOException e) {
error(e, repository, "{0} failed to {1} stream files from commit {2}", algorithm, commit.getName());
} finally {
tw.close();
rw.dispose();
}
return success;
}
use of org.eclipse.jgit.treewalk.TreeWalk in project gitblit by gitblit.
the class JGitUtils method getFilesInPath2.
/**
* Returns the list of files in the specified folder at the specified
* commit. If the repository does not exist or is empty, an empty list is
* returned.
*
* This is modified version that implements path compression feature.
*
* @param repository
* @param path
* if unspecified, root folder is assumed.
* @param commit
* if null, HEAD is assumed.
* @return list of files in specified path
*/
public static List<PathModel> getFilesInPath2(Repository repository, String path, RevCommit commit) {
List<PathModel> list = new ArrayList<PathModel>();
if (!hasCommits(repository)) {
return list;
}
if (commit == null) {
commit = getCommit(repository, null);
}
final TreeWalk tw = new TreeWalk(repository);
try {
tw.addTree(commit.getTree());
final boolean isPathEmpty = Strings.isNullOrEmpty(path);
if (!isPathEmpty) {
PathFilter f = PathFilter.create(path);
tw.setFilter(f);
}
tw.setRecursive(true);
List<String> paths = new ArrayList<>();
while (tw.next()) {
String child = isPathEmpty ? tw.getPathString() : tw.getPathString().replaceFirst(String.format("%s/", path), "");
paths.add(child);
}
for (String p : PathUtils.compressPaths(paths)) {
String pathString = isPathEmpty ? p : String.format("%s/%s", path, p);
list.add(getPathModel(repository, pathString, path, commit));
}
} catch (IOException e) {
error(e, repository, "{0} failed to get files for commit {1}", commit.getName());
} finally {
tw.close();
}
Collections.sort(list);
return list;
}
use of org.eclipse.jgit.treewalk.TreeWalk 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;
}
use of org.eclipse.jgit.treewalk.TreeWalk in project gitblit by gitblit.
the class JGitUtils method getTreeEntries.
/**
* Returns all tree entries that do not match the ignore paths.
*
* @param db
* @param ignorePaths
* @param dcBuilder
* @throws IOException
*/
public static List<DirCacheEntry> getTreeEntries(Repository db, String branch, Collection<String> ignorePaths) throws IOException {
List<DirCacheEntry> list = new ArrayList<DirCacheEntry>();
TreeWalk tw = null;
try {
ObjectId treeId = db.resolve(branch + "^{tree}");
if (treeId == null) {
// branch does not exist yet
return list;
}
tw = new TreeWalk(db);
int hIdx = tw.addTree(treeId);
tw.setRecursive(true);
while (tw.next()) {
String path = tw.getPathString();
CanonicalTreeParser hTree = null;
if (hIdx != -1) {
hTree = tw.getTree(hIdx, CanonicalTreeParser.class);
}
if (!ignorePaths.contains(path)) {
// add all other tree entries
if (hTree != null) {
final DirCacheEntry entry = new DirCacheEntry(path);
entry.setObjectId(hTree.getEntryObjectId());
entry.setFileMode(hTree.getEntryFileMode());
list.add(entry);
}
}
}
} finally {
if (tw != null) {
tw.close();
}
}
return list;
}
Aggregations