Search in sources :

Example 1 with AutoLFInputStream

use of org.eclipse.jgit.util.io.AutoLFInputStream 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)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 DirCache (org.eclipse.jgit.dircache.DirCache)1 DirCacheEditor (org.eclipse.jgit.dircache.DirCacheEditor)1 AutoCRLF (org.eclipse.jgit.lib.CoreConfig.AutoCRLF)1 WorkingTreeOptions (org.eclipse.jgit.treewalk.WorkingTreeOptions)1 AutoLFInputStream (org.eclipse.jgit.util.io.AutoLFInputStream)1