use of com.gitblit.models.FilestoreModel in project gitblit by gitblit.
the class JGitUtils method getFilesInCommit.
/**
* 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;
}
RevWalk rw = new RevWalk(repository);
try {
if (commit == null) {
ObjectId object = getDefaultBranch(repository);
commit = rw.parseCommit(object);
}
if (commit.getParentCount() == 0) {
TreeWalk tw = new TreeWalk(repository);
tw.reset();
tw.setRecursive(true);
tw.addTree(commit.getTree());
while (tw.next()) {
long size = 0;
FilestoreModel filestoreItem = null;
ObjectId objectId = tw.getObjectId(0);
try {
if (!tw.isSubtree() && (tw.getFileMode(0) != FileMode.GITLINK)) {
size = tw.getObjectReader().getObjectSize(objectId, Constants.OBJ_BLOB);
if (isPossibleFilestoreItem(size)) {
filestoreItem = getFilestoreItem(tw.getObjectReader().open(objectId));
}
}
} catch (Throwable t) {
error(t, null, "failed to retrieve blob size for " + tw.getPathString());
}
list.add(new PathChangeModel(tw.getPathString(), tw.getPathString(), filestoreItem, size, tw.getRawMode(0), objectId.getName(), commit.getId().getName(), ChangeType.ADD));
}
tw.close();
} else {
RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
DiffStatFormatter df = new DiffStatFormatter(commit.getName(), repository);
df.setRepository(repository);
df.setDiffComparator(RawTextComparator.DEFAULT);
df.setDetectRenames(true);
List<DiffEntry> diffs = df.scan(parent.getTree(), commit.getTree());
for (DiffEntry diff : diffs) {
// create the path change model
PathChangeModel pcm = PathChangeModel.from(diff, commit.getName(), repository);
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) {
error(t, repository, "{0} failed to determine files in commit!");
} finally {
rw.dispose();
}
return list;
}
use of com.gitblit.models.FilestoreModel in project gitblit by gitblit.
the class JGitUtils method getPathModel.
/**
* Returns a path model of the current file in the treewalk.
*
* @param tw
* @param basePath
* @param commit
* @return a path model of the current file in the treewalk
*/
private static PathModel getPathModel(TreeWalk tw, String basePath, RevCommit commit) {
String name;
long size = 0;
if (StringUtils.isEmpty(basePath)) {
name = tw.getPathString();
} else {
name = tw.getPathString().substring(basePath.length() + 1);
}
ObjectId objectId = tw.getObjectId(0);
FilestoreModel filestoreItem = null;
try {
if (!tw.isSubtree() && (tw.getFileMode(0) != FileMode.GITLINK)) {
size = tw.getObjectReader().getObjectSize(objectId, Constants.OBJ_BLOB);
if (isPossibleFilestoreItem(size)) {
filestoreItem = getFilestoreItem(tw.getObjectReader().open(objectId));
}
}
} catch (Throwable t) {
error(t, null, "failed to retrieve blob size for " + tw.getPathString());
}
return new PathModel(name, tw.getPathString(), filestoreItem, size, tw.getFileMode(0).getBits(), objectId.getName(), commit.getName());
}
use of com.gitblit.models.FilestoreModel 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 com.gitblit.models.FilestoreModel in project gitblit by gitblit.
the class FilestoreManager method uploadBlob.
@Override
public FilestoreModel.Status uploadBlob(String oid, long size, UserModel user, RepositoryModel repo, InputStream streamIn) {
//Access control and object logic
Status state = addObject(oid, size, user, repo);
if (state != Status.Upload_Pending) {
return state;
}
FilestoreModel model = fileCache.get(oid);
if (!model.actionUpload(user)) {
return Status.Upload_In_Progress;
} else {
long actualSize = 0;
File file = getStoragePath(oid);
try {
file.getParentFile().mkdirs();
file.createNewFile();
try (FileOutputStream streamOut = new FileOutputStream(file)) {
actualSize = IOUtils.copyLarge(streamIn, streamOut);
streamOut.flush();
streamOut.close();
if (model.getSize() != actualSize) {
model.setStatus(Status.Error_Size_Mismatch, user);
logger.warn(MessageFormat.format("Failed to upload blob {0} due to size mismatch, expected {1} got {2}", oid, model.getSize(), actualSize));
} else {
String actualOid = "";
try (FileInputStream fileForHash = new FileInputStream(file)) {
actualOid = DigestUtils.sha256Hex(fileForHash);
fileForHash.close();
}
if (oid.equalsIgnoreCase(actualOid)) {
model.setStatus(Status.Available, user);
} else {
model.setStatus(Status.Error_Hash_Mismatch, user);
logger.warn(MessageFormat.format("Failed to upload blob {0} due to hash mismatch, got {1}", oid, actualOid));
}
}
}
} catch (Exception e) {
model.setStatus(Status.Error_Unknown, user);
logger.warn(MessageFormat.format("Failed to upload blob {0}", oid), e);
} finally {
saveFilestoreModel(model);
}
if (model.isInErrorState()) {
file.delete();
model.removeRepository(repo.name);
}
}
return model.getStatus();
}
use of com.gitblit.models.FilestoreModel in project gitblit by gitblit.
the class FilestoreManager method getAllObjects.
@Override
public List<FilestoreModel> getAllObjects(UserModel user) {
final List<RepositoryModel> viewableRepositories = repositoryManager.getRepositoryModels(user);
List<String> viewableRepositoryNames = new ArrayList<String>(viewableRepositories.size());
for (RepositoryModel repository : viewableRepositories) {
viewableRepositoryNames.add(repository.name);
}
if (viewableRepositoryNames.size() == 0) {
return null;
}
final Collection<FilestoreModel> allFiles = fileCache.values();
List<FilestoreModel> userViewableFiles = new ArrayList<FilestoreModel>(allFiles.size());
for (FilestoreModel file : allFiles) {
if (file.isInRepositoryList(viewableRepositoryNames)) {
userViewableFiles.add(file);
}
}
return userViewableFiles;
}
Aggregations