Example usage for org.eclipse.jgit.dircache DirCacheEntry copyMetaData

List of usage examples for org.eclipse.jgit.dircache DirCacheEntry copyMetaData

Introduction

In this page you can find the example usage for org.eclipse.jgit.dircache DirCacheEntry copyMetaData.

Prototype

public void copyMetaData(DirCacheEntry src) 

Source Link

Document

Copy the ObjectId and other meta fields from an existing entry.

Usage

From source file:org.eclipse.egit.core.GitMoveDeleteHook.java

License:Open Source License

public boolean moveFile(final IResourceTree tree, final IFile srcf, final IFile dstf, final int updateFlags,
        final IProgressMonitor monitor) {
    final boolean force = (updateFlags & IResource.FORCE) == IResource.FORCE;
    if (!force && !tree.isSynchronized(srcf, IResource.DEPTH_ZERO))
        return false;

    final RepositoryMapping srcm = RepositoryMapping.getMapping(srcf);
    if (srcm == null)
        return false;
    final RepositoryMapping dstm = RepositoryMapping.getMapping(dstf);

    try {/*w w w  . jav a 2  s .  c om*/
        final DirCache sCache = srcm.getRepository().lockDirCache();
        final String sPath = srcm.getRepoRelativePath(srcf);
        final DirCacheEntry sEnt = sCache.getEntry(sPath);
        if (sEnt == null) {
            sCache.unlock();
            return false;
        }

        final DirCacheEditor sEdit = sCache.editor();
        sEdit.add(new DirCacheEditor.DeletePath(sEnt));
        if (dstm != null && dstm.getRepository() == srcm.getRepository()) {
            final String dPath = srcm.getRepoRelativePath(dstf);
            sEdit.add(new DirCacheEditor.PathEdit(dPath) {
                @Override
                public void apply(final DirCacheEntry dEnt) {
                    dEnt.copyMetaData(sEnt);
                }
            });
        }
        if (!sEdit.commit())
            tree.failed(new Status(IStatus.ERROR, Activator.getPluginId(), 0,
                    CoreText.MoveDeleteHook_operationError, null));

        tree.standardMoveFile(srcf, dstf, updateFlags, monitor);
    } catch (IOException e) {
        tree.failed(new Status(IStatus.ERROR, Activator.getPluginId(), 0,
                CoreText.MoveDeleteHook_operationError, e));
    }
    return true;
}

From source file:org.eclipse.egit.core.GitMoveDeleteHook.java

License:Open Source License

private boolean moveIndexContent(String dPath, final RepositoryMapping srcm, final String sPath)
        throws IOException {
    final DirCache sCache = srcm.getRepository().lockDirCache();
    final DirCacheEntry[] sEnt = sCache.getEntriesWithin(sPath);
    if (sEnt.length == 0) {
        sCache.unlock();//  w  ww .ja v a2  s .  c o m
        return false;
    }

    final DirCacheEditor sEdit = sCache.editor();
    sEdit.add(new DirCacheEditor.DeleteTree(sPath));
    final int sPathLen = sPath.length() + 1;
    for (final DirCacheEntry se : sEnt) {
        final String p = se.getPathString().substring(sPathLen);
        sEdit.add(new DirCacheEditor.PathEdit(dPath + p) {
            @Override
            public void apply(final DirCacheEntry dEnt) {
                dEnt.copyMetaData(se);
            }
        });
    }
    return sEdit.commit();
}

From source file:org.eclipse.egit.ui.internal.actions.CompareWithIndexActionHandler.java

License:Open Source License

private ITypedElement getHeadTypedElement(final IFile baseFile) throws IOException {
    final RepositoryMapping mapping = RepositoryMapping.getMapping(baseFile.getProject());
    final Repository repository = mapping.getRepository();
    final String gitPath = mapping.getRepoRelativePath(baseFile);

    DirCache dc = repository.lockDirCache();
    final DirCacheEntry entry = dc.getEntry(gitPath);
    dc.unlock();// ww  w  .jav  a 2  s  .  c  o m
    if (entry == null) {
        // the file cannot be found in the index
        return new GitCompareFileRevisionEditorInput.EmptyTypedElement(
                NLS.bind(UIText.CompareWithIndexAction_FileNotInIndex, baseFile.getName()));
    }

    IFileRevision nextFile = GitFileRevision.inIndex(repository, gitPath);
    final EditableRevision next = new EditableRevision(nextFile);

    IContentChangeListener listener = new IContentChangeListener() {
        public void contentChanged(IContentChangeNotifier source) {
            final byte[] newContent = next.getModifiedContent();
            DirCache cache = null;
            try {
                cache = repository.lockDirCache();
                DirCacheEditor editor = cache.editor();
                editor.add(new PathEdit(gitPath) {
                    @Override
                    public void apply(DirCacheEntry ent) {
                        ent.copyMetaData(entry);

                        ObjectInserter inserter = repository.newObjectInserter();
                        ent.copyMetaData(entry);
                        ent.setLength(newContent.length);
                        ent.setLastModified(System.currentTimeMillis());
                        InputStream in = new ByteArrayInputStream(newContent);
                        try {
                            ent.setObjectId(inserter.insert(Constants.OBJ_BLOB, newContent.length, in));
                            inserter.flush();
                        } catch (IOException ex) {
                            throw new RuntimeException(ex);
                        } finally {
                            try {
                                in.close();
                            } catch (IOException e) {
                                // ignore here
                            }
                        }
                    }
                });
                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();
            }
        }
    };

    next.addContentChangeListener(listener);
    return next;
}