Search in sources :

Example 1 with TreeWalk

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;
}
Also used : FileMode(org.eclipse.jgit.lib.FileMode) Ref(org.eclipse.jgit.lib.Ref) ObjectId(org.eclipse.jgit.lib.ObjectId) Config(org.eclipse.jgit.lib.Config) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) RevWalk(org.eclipse.jgit.revwalk.RevWalk) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) File(java.io.File) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with TreeWalk

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

Example 3 with TreeWalk

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;
}
Also used : PathFilter(org.eclipse.jgit.treewalk.filter.PathFilter) PathModel(com.gitblit.models.PathModel) ArrayList(java.util.ArrayList) IOException(java.io.IOException) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk)

Example 4 with TreeWalk

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;
}
Also used : PathModel(com.gitblit.models.PathModel) ArrayList(java.util.ArrayList) TreeFilter(org.eclipse.jgit.treewalk.filter.TreeFilter) AndTreeFilter(org.eclipse.jgit.treewalk.filter.AndTreeFilter) OrTreeFilter(org.eclipse.jgit.treewalk.filter.OrTreeFilter) IOException(java.io.IOException) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 5 with TreeWalk

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;
}
Also used : DirCacheEntry(org.eclipse.jgit.dircache.DirCacheEntry) AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser)

Aggregations

TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)31 RevWalk (org.eclipse.jgit.revwalk.RevWalk)19 RevCommit (org.eclipse.jgit.revwalk.RevCommit)14 ObjectId (org.eclipse.jgit.lib.ObjectId)11 ArrayList (java.util.ArrayList)10 IOException (java.io.IOException)9 ObjectLoader (org.eclipse.jgit.lib.ObjectLoader)9 ObjectReader (org.eclipse.jgit.lib.ObjectReader)6 Repository (org.eclipse.jgit.lib.Repository)6 FileMode (org.eclipse.jgit.lib.FileMode)5 RevTree (org.eclipse.jgit.revwalk.RevTree)5 PathFilter (org.eclipse.jgit.treewalk.filter.PathFilter)5 FilestoreModel (com.gitblit.models.FilestoreModel)4 PathModel (com.gitblit.models.PathModel)4 IncorrectObjectTypeException (org.eclipse.jgit.errors.IncorrectObjectTypeException)3 LargeObjectException (org.eclipse.jgit.errors.LargeObjectException)3 CanonicalTreeParser (org.eclipse.jgit.treewalk.CanonicalTreeParser)3 RefModel (com.gitblit.models.RefModel)2 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2