Search in sources :

Example 1 with PathFilter

use of org.eclipse.jgit.treewalk.filter.PathFilter 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 2 with PathFilter

use of org.eclipse.jgit.treewalk.filter.PathFilter 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 pathString = tw.getPathString();
            String child = isPathEmpty ? pathString : pathString.replaceFirst(Pattern.quote(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 3 with PathFilter

use of org.eclipse.jgit.treewalk.filter.PathFilter in project gitblit by gitblit.

the class RawServlet method streamFromRepo.

protected boolean streamFromRepo(HttpServletRequest request, HttpServletResponse response, Repository repository, RevCommit commit, String requestedPath) throws IOException {
    boolean served = false;
    RevWalk rw = new RevWalk(repository);
    TreeWalk tw = new TreeWalk(repository);
    try {
        tw.reset();
        tw.addTree(commit.getTree());
        PathFilter f = PathFilter.create(requestedPath);
        tw.setFilter(f);
        tw.setRecursive(true);
        MutableObjectId id = new MutableObjectId();
        ObjectReader reader = tw.getObjectReader();
        while (tw.next()) {
            FileMode mode = tw.getFileMode(0);
            if (mode == FileMode.GITLINK || mode == FileMode.TREE) {
                continue;
            }
            tw.getObjectId(id, 0);
            String filename = StringUtils.getLastPathElement(requestedPath);
            try {
                String userAgent = request.getHeader("User-Agent");
                if (userAgent != null && userAgent.indexOf("MSIE 5.5") > -1) {
                    response.setHeader("Content-Disposition", "filename=\"" + URLEncoder.encode(filename, Constants.ENCODING) + "\"");
                } else if (userAgent != null && userAgent.indexOf("MSIE") > -1) {
                    response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(filename, Constants.ENCODING) + "\"");
                } else {
                    response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(filename.getBytes(Constants.ENCODING), "latin1") + "\"");
                }
            } catch (UnsupportedEncodingException e) {
                response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
            }
            long len = reader.getObjectSize(id, org.eclipse.jgit.lib.Constants.OBJ_BLOB);
            setContentType(response, "application/octet-stream");
            response.setIntHeader("Content-Length", (int) len);
            ObjectLoader ldr = repository.open(id);
            ldr.copyTo(response.getOutputStream());
            served = true;
        }
    } finally {
        tw.close();
        rw.dispose();
    }
    response.flushBuffer();
    return served;
}
Also used : FileMode(org.eclipse.jgit.lib.FileMode) MutableObjectId(org.eclipse.jgit.lib.MutableObjectId) PathFilter(org.eclipse.jgit.treewalk.filter.PathFilter) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ObjectReader(org.eclipse.jgit.lib.ObjectReader) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) RevWalk(org.eclipse.jgit.revwalk.RevWalk) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk)

Example 4 with PathFilter

use of org.eclipse.jgit.treewalk.filter.PathFilter in project sonarqube by SonarSource.

the class GitScmProvider method collectChangedLines.

private void collectChangedLines(Repository repo, RevCommit mergeBaseCommit, Map<Path, Set<Integer>> changedLines, Path changedFile) {
    ChangedLinesComputer computer = new ChangedLinesComputer();
    try (DiffFormatter diffFmt = new DiffFormatter(new BufferedOutputStream(computer.receiver()))) {
        // copied from DiffCommand so that we can use a custom DiffFormatter which ignores white spaces.
        diffFmt.setRepository(repo);
        diffFmt.setProgressMonitor(NullProgressMonitor.INSTANCE);
        diffFmt.setDiffComparator(RawTextComparator.WS_IGNORE_ALL);
        Path workTree = repo.getWorkTree().toPath();
        String relativePath = workTree.relativize(changedFile).toString();
        PathFilter pathFilter = PathFilter.create(toGitPath(relativePath));
        diffFmt.setPathFilter(pathFilter);
        AbstractTreeIterator mergeBaseTree = prepareTreeParser(repo, mergeBaseCommit);
        List<DiffEntry> diffEntries = diffFmt.scan(mergeBaseTree, new FileTreeIterator(repo));
        diffFmt.format(diffEntries);
        diffFmt.flush();
        diffEntries.stream().filter(diffEntry -> diffEntry.getChangeType() == DiffEntry.ChangeType.ADD || diffEntry.getChangeType() == DiffEntry.ChangeType.MODIFY).findAny().ifPresent(diffEntry -> changedLines.put(changedFile, computer.changedLines()));
    } catch (Exception e) {
        LOG.warn("Failed to get changed lines from git for file " + changedFile, e);
    }
}
Also used : Path(java.nio.file.Path) AbstractTreeIterator(org.eclipse.jgit.treewalk.AbstractTreeIterator) PathFilter(org.eclipse.jgit.treewalk.filter.PathFilter) DiffFormatter(org.eclipse.jgit.diff.DiffFormatter) BufferedOutputStream(java.io.BufferedOutputStream) FileTreeIterator(org.eclipse.jgit.treewalk.FileTreeIterator) MessageException(org.sonar.api.utils.MessageException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 5 with PathFilter

use of org.eclipse.jgit.treewalk.filter.PathFilter in project egit by eclipse.

the class GitCommitsModelCacheTest method shouldApplyPathFilter.

@Test
public void shouldApplyPathFilter() throws Exception {
    // given
    Git git = new Git(db);
    writeTrashFile(db, "folder/a.txt", "content");
    writeTrashFile(db, "folder2/b.txt", "b content");
    git.add().addFilepattern("folder/a.txt").call();
    git.add().addFilepattern("folder2/b.txt").call();
    RevCommit c = commit(git, "first commit");
    // when
    PathFilter pathFilter = PathFilter.create("folder");
    List<Commit> leftResult = GitCommitsModelCache.build(db, initialTagId(), c, pathFilter);
    // then
    assertThat(leftResult, notNullValue());
    assertThat(Integer.valueOf(leftResult.size()), is(Integer.valueOf(1)));
    assertThat(leftResult.get(0).getShortMessage(), is("first commit"));
    assertThat(leftResult.get(0).getChildren(), notNullValue());
    assertThat(leftResult.get(0).getChildren().size(), is(1));
    assertFileAddition(c, leftResult.get(0).getChildren().get("folder/a.txt"), "a.txt", LEFT);
}
Also used : PathFilter(org.eclipse.jgit.treewalk.filter.PathFilter) Git(org.eclipse.jgit.api.Git) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Commit(org.eclipse.egit.core.synchronize.GitCommitsModelCache.Commit) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Aggregations

PathFilter (org.eclipse.jgit.treewalk.filter.PathFilter)7 IOException (java.io.IOException)5 TreeWalk (org.eclipse.jgit.treewalk.TreeWalk)5 FileMode (org.eclipse.jgit.lib.FileMode)3 MutableObjectId (org.eclipse.jgit.lib.MutableObjectId)3 ObjectLoader (org.eclipse.jgit.lib.ObjectLoader)3 RevCommit (org.eclipse.jgit.revwalk.RevCommit)3 RevWalk (org.eclipse.jgit.revwalk.RevWalk)3 FilestoreModel (com.gitblit.models.FilestoreModel)2 PathModel (com.gitblit.models.PathModel)2 FileInputStream (java.io.FileInputStream)2 ArrayList (java.util.ArrayList)2 ZipArchiveOutputStream (org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream)2 ObjectReader (org.eclipse.jgit.lib.ObjectReader)2 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Path (java.nio.file.Path)1 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)1