use of org.eclipse.jgit.lib.FileMode in project gitblit by gitblit.
the class CompressionUtils method zip.
/**
* Zips the contents of the tree at the (optionally) specified revision and
* the (optionally) specified basepath to the supplied outputstream.
*
* @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
*/
public static boolean zip(Repository repository, IFilestoreManager filestoreManager, String basePath, String objectId, OutputStream os) {
RevCommit commit = JGitUtils.getCommit(repository, objectId);
if (commit == null) {
return false;
}
boolean success = false;
RevWalk rw = new RevWalk(repository);
TreeWalk tw = new TreeWalk(repository);
try {
tw.reset();
tw.addTree(commit.getTree());
ZipArchiveOutputStream zos = new ZipArchiveOutputStream(os);
zos.setComment("Generated by Gitblit");
if (!StringUtils.isEmpty(basePath)) {
PathFilter f = PathFilter.create(basePath);
tw.setFilter(f);
}
tw.setRecursive(true);
MutableObjectId id = new MutableObjectId();
ObjectReader reader = tw.getObjectReader();
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);
ZipArchiveEntry entry = new ZipArchiveEntry(tw.getPathString());
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);
entry.setComment(commit.getName());
entry.setUnixMode(mode.getBits());
entry.setTime(modified);
zos.putArchiveEntry(entry);
if (filestoreItem == null) {
//Copy repository stored file
loader.copyTo(zos);
} else {
//Copy filestore file
try (FileInputStream streamIn = new FileInputStream(filestoreManager.getStoragePath(filestoreItem.oid))) {
IOUtils.copyLarge(streamIn, zos);
} catch (Throwable e) {
LOGGER.error(MessageFormat.format("Failed to archive filestore item {0}", filestoreItem.oid), e);
//Handle as per other errors
throw e;
}
}
zos.closeArchiveEntry();
}
zos.finish();
success = true;
} catch (IOException e) {
error(e, repository, "{0} failed to zip files from commit {1}", commit.getName());
} finally {
tw.close();
rw.dispose();
}
return success;
}
use of org.eclipse.jgit.lib.FileMode in project gitiles by GerritCodeReview.
the class TreeJsonData method toJsonData.
static Tree toJsonData(ObjectId id, TreeWalk tw, boolean includeSizes, boolean recursive) throws IOException {
Tree tree = new Tree();
tree.id = id.name();
tree.entries = Lists.newArrayList();
while (tw.next()) {
Entry e = new Entry();
FileMode mode = tw.getFileMode(0);
e.mode = mode.getBits();
e.type = Constants.typeString(mode.getObjectType());
e.id = tw.getObjectId(0).name();
e.name = recursive ? tw.getPathString() : tw.getNameString();
if (includeSizes) {
if ((mode.getBits() & FileMode.TYPE_MASK) == FileMode.TYPE_FILE) {
e.size = tw.getObjectReader().getObjectSize(tw.getObjectId(0), Constants.OBJ_BLOB);
} else if ((mode.getBits() & FileMode.TYPE_MASK) == FileMode.TYPE_SYMLINK) {
e.target = new String(tw.getObjectReader().open(tw.getObjectId(0)).getCachedBytes(), UTF_8);
}
}
tree.entries.add(e);
}
return tree;
}
Aggregations