Example usage for org.eclipse.jgit.dircache DirCache write

List of usage examples for org.eclipse.jgit.dircache DirCache write

Introduction

In this page you can find the example usage for org.eclipse.jgit.dircache DirCache write.

Prototype

public void write() throws IOException 

Source Link

Document

Write the entry records from memory to disk.

Usage

From source file:ezbake.deployer.publishers.openShift.RhcApplication.java

License:Apache License

public void addStreamAsFile(File p, InputStream ios, Set<PosixFilePermission> filePermissions)
        throws DeploymentException {
    File resolvedPath = Files.resolve(getGitRepoPath(), p);
    if (Files.isDirectory(resolvedPath)) {
        throw new DeploymentException(
                "Directory exist by the name of file wishing to write to: " + resolvedPath.toString());
    }/* w  w  w.j  a  v a 2  s  . c  o  m*/

    try {
        Files.createDirectories(resolvedPath.getParent());
        OutputStream oos = new FileOutputStream(resolvedPath);
        boolean permissions = (filePermissions != null && !filePermissions.isEmpty());
        DirCache cache = null;
        try {
            IOUtils.copy(ios, oos);
            if (permissions) {
                Files.setPosixFilePermissions(resolvedPath, filePermissions);
            }

            cache = gitRepo.add().addFilepattern(p.toString()).call();
            if (permissions) {
                // Add executable permissions
                cache.lock();
                // Most of these mthods throw an IOException so we will catch that and unlock
                DirCacheEntry entry = cache.getEntry(0);
                log.debug("Setting executable permissions for: " + entry.getPathString());
                entry.setFileMode(FileMode.EXECUTABLE_FILE);
                cache.write();
                cache.commit();
            }
        } catch (IOException e) {
            log.error("[" + getApplicationName() + "]" + "Error writing to file: " + resolvedPath.toString(),
                    e);
            // Most of the DirCache entries should just thow IOExceptions
            if (cache != null) {
                cache.unlock();
            }
            throw new DeploymentException("Error writing to file: " + resolvedPath.toString());
        } catch (GitAPIException e) {
            log.error("[" + getApplicationName() + "]" + "Error writing to file: " + resolvedPath.toString(),
                    e);
            throw new DeploymentException("Error writing to file: " + resolvedPath.toString());
        } finally {
            IOUtils.closeQuietly(oos);
        }
    } catch (IOException e) {
        log.error(
                "[" + getApplicationName() + "]" + "Error opening file for output: " + resolvedPath.toString(),
                e);
        throw new DeploymentException("Error opening file for output: " + resolvedPath.toString());
    }

}

From source file:org.eclipse.che.git.impl.jgit.JGitConnection.java

License:Open Source License

@Override
public void cloneWithSparseCheckout(String directory, String remoteUrl, String branch)
        throws GitException, UnauthorizedException {
    //TODO rework this code when jgit will support sparse-checkout. Tracked issue: https://bugs.eclipse.org/bugs/show_bug.cgi?id=383772
    clone(newDto(CloneRequest.class).withRemoteUri(remoteUrl));
    if (!"master".equals(branch)) {
        checkout(newDto(CheckoutRequest.class).withName(branch));
    }/*from   w ww. jav  a2 s .c o  m*/
    final String sourcePath = getWorkingDir().getPath();
    final String keepDirectoryPath = sourcePath + "/" + directory;
    IOFileFilter folderFilter = new DirectoryFileFilter() {
        public boolean accept(File dir) {
            String directoryPath = dir.getPath();
            return !(directoryPath.startsWith(keepDirectoryPath)
                    || directoryPath.startsWith(sourcePath + "/.git"));
        }
    };
    Collection<File> files = org.apache.commons.io.FileUtils.listFilesAndDirs(getWorkingDir(),
            TrueFileFilter.INSTANCE, folderFilter);
    try {
        DirCache index = getRepository().lockDirCache();
        int sourcePathLength = sourcePath.length() + 1;
        files.stream().filter(File::isFile).forEach(
                file -> index.getEntry(file.getPath().substring(sourcePathLength)).setAssumeValid(true));
        index.write();
        index.commit();
        for (File file : files) {
            if (keepDirectoryPath.startsWith(file.getPath())) {
                continue;
            }
            if (file.exists()) {
                FileUtils.delete(file, FileUtils.RECURSIVE);
            }
        }
    } catch (IOException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
}

From source file:org.eclipse.egit.core.op.AssumeUnchangedOperation.java

License:Open Source License

public void execute(IProgressMonitor m) throws CoreException {
    IProgressMonitor monitor;//from   w  ww. j av  a 2s .c om
    if (m == null)
        monitor = new NullProgressMonitor();
    else
        monitor = m;

    caches.clear();
    mappings.clear();

    monitor.beginTask(CoreText.AssumeUnchangedOperation_adding, rsrcList.size() * 200);
    try {
        for (IResource resource : rsrcList) {
            assumeValid(resource);
            monitor.worked(200);
        }

        for (Map.Entry<Repository, DirCache> e : caches.entrySet()) {
            final Repository db = e.getKey();
            final DirCache editor = e.getValue();
            monitor.setTaskName(NLS.bind(CoreText.AssumeUnchangedOperation_writingIndex, db.getDirectory()));
            editor.write();
            editor.commit();
        }
    } catch (RuntimeException e) {
        throw new CoreException(Activator.error(CoreText.UntrackOperation_failed, e));
    } catch (IOException e) {
        throw new CoreException(Activator.error(CoreText.UntrackOperation_failed, e));
    } finally {
        for (final RepositoryMapping rm : mappings.keySet())
            rm.fireRepositoryChanged();
        caches.clear();
        mappings.clear();
        monitor.done();
    }
}

From source file:org.jboss.arquillian.container.openshift.express.util.GitUtil.java

License:Apache License

private void updateCache(DirCache cache) throws IOException {
    if (!cache.lock()) {
        throw new IllegalStateException("Unable to lock Git repository cache");
    }/*  w w w . j a  v  a2 s .  c om*/
    cache.write();
    if (!cache.commit()) {
        throw new IllegalStateException("Unable to commit Git repository cache");
    }

}