Example usage for org.eclipse.jgit.dircache DirCacheEditor commit

List of usage examples for org.eclipse.jgit.dircache DirCacheEditor commit

Introduction

In this page you can find the example usage for org.eclipse.jgit.dircache DirCacheEditor commit.

Prototype

@Override
public boolean commit() throws IOException 

Source Link

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 {//from  w w w.j  av  a 2  s .  co  m
        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();/*from ww  w . j  a  v a2s  . com*/
        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.core.op.UntrackOperation.java

License:Open Source License

public void execute(IProgressMonitor m) throws CoreException {
    IProgressMonitor monitor;/*from   www.ja  va 2s  . c o  m*/
    if (m == null)
        monitor = new NullProgressMonitor();
    else
        monitor = m;

    edits.clear();
    mappings.clear();

    monitor.beginTask(CoreText.UntrackOperation_adding, rsrcList.size() * 200);
    try {
        for (IResource obj : rsrcList) {
            remove(obj);
            monitor.worked(200);
        }

        for (Map.Entry<Repository, DirCacheEditor> e : edits.entrySet()) {
            final Repository db = e.getKey();
            final DirCacheEditor editor = e.getValue();
            monitor.setTaskName(NLS.bind(CoreText.UntrackOperation_writingIndex, db.getDirectory()));
            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();
        edits.clear();
        mappings.clear();
        monitor.done();
    }
}

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();/*from  w w w  . j a v 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;
}

From source file:org.eclipse.egit.ui.internal.staging.StagingView.java

License:Open Source License

private void unstage(IStructuredSelection selection) {
    if (selection.isEmpty())
        return;/*from w ww.  j a v  a  2 s.  c  om*/

    RevCommit headRev = null;
    try {
        final Ref head = currentRepository.getRef(Constants.HEAD);
        // head.getObjectId() is null if the repository does not contain any
        // commit
        if (head.getObjectId() != null)
            headRev = new RevWalk(currentRepository).parseCommit(head.getObjectId());
    } catch (IOException e1) {
        // TODO fix text
        MessageDialog.openError(getSite().getShell(), UIText.CommitAction_MergeHeadErrorTitle,
                UIText.CommitAction_ErrorReadingMergeMsg);
        return;
    }

    final DirCache dirCache;
    final DirCacheEditor edit;
    try {
        dirCache = currentRepository.lockDirCache();
        edit = dirCache.editor();
    } catch (IOException e) {
        // TODO fix text
        MessageDialog.openError(getSite().getShell(), UIText.CommitAction_MergeHeadErrorTitle,
                UIText.CommitAction_ErrorReadingMergeMsg);
        return;
    }

    try {
        updateDirCache(selection, headRev, edit);

        try {
            edit.commit();
        } catch (IOException e) {
            // TODO fix text
            MessageDialog.openError(getSite().getShell(), UIText.CommitAction_MergeHeadErrorTitle,
                    UIText.CommitAction_ErrorReadingMergeMsg);
        }
    } finally {
        dirCache.unlock();
    }
}

From source file:util.ChkoutCmd.java

License:Eclipse Distribution License

private void checkoutPathsFromIndex(TreeWalk treeWalk, DirCache dc) throws IOException {
    DirCacheIterator dci = new DirCacheIterator(dc);
    treeWalk.addTree(dci);/*w ww . j  av  a2  s  .co  m*/

    final ObjectReader r = treeWalk.getObjectReader();
    DirCacheEditor editor = dc.editor();
    while (treeWalk.next()) {
        DirCacheEntry entry = dci.getDirCacheEntry();
        // Only add one edit per path
        if (entry != null && entry.getStage() > DirCacheEntry.STAGE_1)
            continue;
        editor.add(new PathEdit(treeWalk.getPathString()) {
            public void apply(DirCacheEntry ent) {
                int stage = ent.getStage();
                if (stage > DirCacheEntry.STAGE_0) {
                    if (checkoutStage != null) {
                        if (stage == checkoutStage.number)
                            checkoutPath(ent, r);
                    } else {
                        UnmergedPathException e = new UnmergedPathException(ent);
                        throw new JGitInternalException(e.getMessage(), e);
                    }
                } else {
                    checkoutPath(ent, r);
                }
            }
        });
    }
    editor.commit();
}

From source file:util.ChkoutCmd.java

License:Eclipse Distribution License

private void checkoutPathsFromCommit(TreeWalk treeWalk, DirCache dc, RevCommit commit) throws IOException {
    treeWalk.addTree(commit.getTree());//from   w ww  .j av a  2s.c  om
    final ObjectReader r = treeWalk.getObjectReader();
    DirCacheEditor editor = dc.editor();
    while (treeWalk.next()) {
        final ObjectId blobId = treeWalk.getObjectId(0);
        final FileMode mode = treeWalk.getFileMode(0);
        editor.add(new PathEdit(treeWalk.getPathString()) {
            public void apply(DirCacheEntry ent) {
                ent.setObjectId(blobId);
                ent.setFileMode(mode);
                checkoutPath(ent, r);
            }
        });
    }
    editor.commit();
}