Example usage for org.eclipse.jgit.lib FileMode getBits

List of usage examples for org.eclipse.jgit.lib FileMode getBits

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib FileMode getBits.

Prototype

public int getBits() 

Source Link

Document

Get the mode bits as an integer.

Usage

From source file:com.gitblit.utils.CompressionUtils.java

License:Apache License

/**
 * Zips the contents of the tree at the (optionally) specified revision and
 * the (optionally) specified basepath to the supplied outputstream.
 *
 * @param repository/*from w  ww . j  av a  2 s. c om*/
 * @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;
}

From source file:com.gitblit.utils.CompressionUtils.java

License:Apache License

/**
 * Compresses/archives the contents of the tree at the (optionally)
 * specified revision and the (optionally) specified basepath to the
 * supplied outputstream./*from  ww  w .  ja va2  s  . c om*/
 *
 * @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;
}

From source file:com.google.gerrit.server.patch.PatchScriptBuilder.java

License:Apache License

private static boolean isBothFile(FileMode a, FileMode b) {
    return (a.getBits() & FileMode.TYPE_FILE) == FileMode.TYPE_FILE
            && (b.getBits() & FileMode.TYPE_FILE) == FileMode.TYPE_FILE;
}

From source file:com.google.gitiles.TreeJsonData.java

License:Open Source License

static Tree toJsonData(ObjectId id, TreeWalk tw) throws IOException {
    Tree tree = new Tree();
    tree.id = id.name();//  w  w  w  .  ja v a2  s .co m
    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 = tw.getNameString();
        tree.entries.add(e);
    }
    return tree;
}

From source file:io.fabric8.forge.rest.git.RepositoryResource.java

License:Apache License

protected static int toInt(FileMode fileMode) {
    return fileMode != null ? fileMode.getBits() : 0;
}

From source file:jetbrains.buildServer.buildTriggers.vcs.git.VcsChangeTreeWalk.java

License:Apache License

/**
 * Classify change in tree walker. The first tree is assumed to be a current commit and other
 * trees are assumed to be parent commits. In the case of multiple changes, the changes that
 * come from at lease one parent commit are assumed to be reported in the parent commit.
 * @return change type/* ww  w  .  j  a  v  a2 s.c om*/
 */
@NotNull
public ChangeType classifyChange() {
    final FileMode mode0 = getFileMode(0);
    if (isExtraDebug())
        LOG.debug(getPathString() + " file mode: " + mode0);
    if (FileMode.MISSING.equals(mode0)) {
        for (int i = 1; i < getTreeCount(); i++) {
            if (FileMode.MISSING.equals(getFileMode(i))) {
                // the delete merge
                return ChangeType.UNCHANGED;
            }
        }
        return ChangeType.DELETED;
    }
    boolean fileAdded = true;
    for (int i = 1; i < getTreeCount(); i++) {
        if (!FileMode.MISSING.equals(getFileMode(i))) {
            fileAdded = false;
            break;
        }
    }
    if (fileAdded) {
        return ChangeType.ADDED;
    }
    boolean fileModified = true;
    for (int i = 1; i < getTreeCount(); i++) {
        if (idEqual(0, i)) {
            fileModified = false;
            break;
        }
    }
    if (fileModified) {
        return ChangeType.MODIFIED;
    }
    int modeBits0 = mode0.getBits();
    boolean fileModeModified = true;
    for (int i = 1; i < getTreeCount(); i++) {
        int modeBits = getFileMode(i).getBits();
        if (modeBits == modeBits0) {
            fileModeModified = false;
            break;
        }
    }
    if (fileModeModified) {
        return ChangeType.FILE_MODE_CHANGED;
    }
    return ChangeType.UNCHANGED;
}

From source file:jetbrains.buildServer.buildTriggers.vcs.git.VcsChangeTreeWalk.java

License:Apache License

/**
 * Check if the file mode is executable//from  w w  w .  jav a  2 s.  c om
 *
 * @param m file mode to check
 * @return true if the file is executable
 */
private boolean isExecutable(FileMode m) {
    return (m.getBits() & (1 << 6)) != 0;
}