Search in sources :

Example 1 with WorkingTreeOptions

use of org.eclipse.jgit.treewalk.WorkingTreeOptions in project egit by eclipse.

the class CompareUtils method setIndexEntryContents.

/**
 * Set contents on index entry of specified path. Line endings of contents
 * are canonicalized if configured.
 *
 * @param repository
 * @param gitPath
 * @param newContent
 *            content with working directory line endings
 */
private static void setIndexEntryContents(final Repository repository, final String gitPath, final byte[] newContent) {
    DirCache cache = null;
    try {
        cache = repository.lockDirCache();
        DirCacheEditor editor = cache.editor();
        if (newContent.length == 0) {
            editor.add(new DirCacheEditor.DeletePath(gitPath));
        } else {
            int length;
            byte[] content;
            WorkingTreeOptions workingTreeOptions = repository.getConfig().get(WorkingTreeOptions.KEY);
            AutoCRLF autoCRLF = workingTreeOptions.getAutoCRLF();
            switch(autoCRLF) {
                case FALSE:
                    content = newContent;
                    length = newContent.length;
                    break;
                case INPUT:
                case TRUE:
                    AutoLFInputStream in = new AutoLFInputStream(new ByteArrayInputStream(newContent), true);
                    // Canonicalization should lead to same or shorter length
                    // (CRLF to LF), so we don't have to expand the byte[].
                    content = new byte[newContent.length];
                    length = IO.readFully(in, content, 0);
                    break;
                default:
                    throw new IllegalArgumentException(// $NON-NLS-1$
                    "Unknown autocrlf option " + autoCRLF);
            }
            editor.add(new DirCacheEntryEditor(gitPath, repository, content, length));
        }
        try {
            editor.commit();
        } catch (RuntimeException e) {
            if (e.getCause() instanceof IOException)
                throw (IOException) e.getCause();
            else
                throw e;
        }
    } catch (IOException e) {
        Activator.handleError(UIText.CompareWithIndexAction_errorOnAddToIndex, e, true);
    } finally {
        if (cache != null)
            cache.unlock();
    }
}
Also used : AutoLFInputStream(org.eclipse.jgit.util.io.AutoLFInputStream) DirCacheEditor(org.eclipse.jgit.dircache.DirCacheEditor) AutoCRLF(org.eclipse.jgit.lib.CoreConfig.AutoCRLF) IOException(java.io.IOException) DirCache(org.eclipse.jgit.dircache.DirCache) ByteArrayInputStream(java.io.ByteArrayInputStream) WorkingTreeOptions(org.eclipse.jgit.treewalk.WorkingTreeOptions)

Example 2 with WorkingTreeOptions

use of org.eclipse.jgit.treewalk.WorkingTreeOptions in project egit by eclipse.

the class GitBlobStorage method open.

private InputStream open() throws IOException, CoreException, IncorrectObjectTypeException {
    if (blobId == null)
        return new ByteArrayInputStream(new byte[0]);
    try {
        WorkingTreeOptions workingTreeOptions = db.getConfig().get(WorkingTreeOptions.KEY);
        final InputStream objectInputStream = db.open(blobId, Constants.OBJ_BLOB).openStream();
        switch(workingTreeOptions.getAutoCRLF()) {
            case INPUT:
            // itself should ignore line endings.
            case FALSE:
                return objectInputStream;
            case TRUE:
            default:
                return new AutoCRLFInputStream(objectInputStream, true);
        }
    } catch (MissingObjectException notFound) {
        throw new CoreException(Activator.error(NLS.bind(CoreText.BlobStorage_blobNotFound, blobId.name(), path), notFound));
    }
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) AutoCRLFInputStream(org.eclipse.jgit.util.io.AutoCRLFInputStream) InputStream(java.io.InputStream) AutoCRLFInputStream(org.eclipse.jgit.util.io.AutoCRLFInputStream) WorkingTreeOptions(org.eclipse.jgit.treewalk.WorkingTreeOptions) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)2 WorkingTreeOptions (org.eclipse.jgit.treewalk.WorkingTreeOptions)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 CoreException (org.eclipse.core.runtime.CoreException)1 DirCache (org.eclipse.jgit.dircache.DirCache)1 DirCacheEditor (org.eclipse.jgit.dircache.DirCacheEditor)1 MissingObjectException (org.eclipse.jgit.errors.MissingObjectException)1 AutoCRLF (org.eclipse.jgit.lib.CoreConfig.AutoCRLF)1 AutoCRLFInputStream (org.eclipse.jgit.util.io.AutoCRLFInputStream)1 AutoLFInputStream (org.eclipse.jgit.util.io.AutoLFInputStream)1