Search in sources :

Example 6 with FilestoreModel

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;
}
Also used : PathChangeModel(com.gitblit.models.PathModel.PathChangeModel) AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) FilestoreModel(com.gitblit.models.FilestoreModel) ArrayList(java.util.ArrayList) RevWalk(org.eclipse.jgit.revwalk.RevWalk) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 7 with FilestoreModel

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());
}
Also used : PathModel(com.gitblit.models.PathModel) AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) FilestoreModel(com.gitblit.models.FilestoreModel)

Example 8 with FilestoreModel

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;
}
Also used : FileMode(org.eclipse.jgit.lib.FileMode) PathFilter(org.eclipse.jgit.treewalk.filter.PathFilter) FilestoreModel(com.gitblit.models.FilestoreModel) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) FileInputStream(java.io.FileInputStream) MutableObjectId(org.eclipse.jgit.lib.MutableObjectId) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) ObjectReader(org.eclipse.jgit.lib.ObjectReader) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 9 with FilestoreModel

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();
}
Also used : Status(com.gitblit.models.FilestoreModel.Status) FilestoreModel(com.gitblit.models.FilestoreModel) FileOutputStream(java.io.FileOutputStream) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) EOFException(java.io.EOFException)

Example 10 with FilestoreModel

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;
}
Also used : FilestoreModel(com.gitblit.models.FilestoreModel) ArrayList(java.util.ArrayList) RepositoryModel(com.gitblit.models.RepositoryModel)

Aggregations

FilestoreModel (com.gitblit.models.FilestoreModel)12 IOException (java.io.IOException)5 FileInputStream (java.io.FileInputStream)4 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)4 Status (com.gitblit.models.FilestoreModel.Status)3 RevCommit (org.eclipse.jgit.revwalk.RevCommit)3 RevWalk (org.eclipse.jgit.revwalk.RevWalk)3 PathModel (com.gitblit.models.PathModel)2 EOFException (java.io.EOFException)2 File (java.io.File)2 RandomAccessFile (java.io.RandomAccessFile)2 ArrayList (java.util.ArrayList)2 ZipArchiveOutputStream (org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream)2 AnyObjectId (org.eclipse.jgit.lib.AnyObjectId)2 FileMode (org.eclipse.jgit.lib.FileMode)2 MutableObjectId (org.eclipse.jgit.lib.MutableObjectId)2 ObjectId (org.eclipse.jgit.lib.ObjectId)2 ObjectLoader (org.eclipse.jgit.lib.ObjectLoader)2 PathFilter (org.eclipse.jgit.treewalk.filter.PathFilter)2 PathChangeModel (com.gitblit.models.PathModel.PathChangeModel)1