Package org.eclipse.jgit.dircache

Examples of org.eclipse.jgit.dircache.DirCacheEditor


  protected CheckoutCommand checkoutPaths() throws IOException,
      RefNotFoundException {
    RevWalk revWalk = new RevWalk(repo);
    DirCache dc = repo.lockDirCache();
    try {
      DirCacheEditor editor = dc.editor();
      TreeWalk startWalk = new TreeWalk(revWalk.getObjectReader());
      startWalk.setRecursive(true);
      if (!checkoutAllPaths)
        startWalk.setFilter(PathFilterGroup.createFromStrings(paths));
      boolean checkoutIndex = startCommit == null && startPoint == null;
      if (!checkoutIndex)
        startWalk.addTree(revWalk.parseCommit(getStartPoint())
            .getTree());
      else
        startWalk.addTree(new DirCacheIterator(dc));

      final File workTree = repo.getWorkTree();
      final ObjectReader r = repo.getObjectDatabase().newReader();
      try {
        while (startWalk.next()) {
          final ObjectId blobId = startWalk.getObjectId(0);
          final FileMode mode = startWalk.getFileMode(0);
          editor.add(new PathEdit(startWalk.getPathString()) {
            public void apply(DirCacheEntry ent) {
              ent.setObjectId(blobId);
              ent.setFileMode(mode);
              try {
                DirCacheCheckout.checkoutEntry(repo, new File(
                    workTree, ent.getPathString()), ent, r);
              } catch (IOException e) {
                throw new JGitInternalException(
                    MessageFormat.format(
                        JGitText.get().checkoutConflictWithFile,
                        ent.getPathString()), e);
              }
            }
          });
        }
        editor.commit();
      } finally {
        startWalk.release();
        r.release();
      }
    } finally {
View Full Code Here


            headCommit.getShortMessage()));
        ObjectId indexCommit = inserter.insert(builder);

        // Commit working tree changes
        if (!wtEdits.isEmpty() || !wtDeletes.isEmpty()) {
          DirCacheEditor editor = cache.editor();
          for (PathEdit edit : wtEdits)
            editor.add(edit);
          for (String path : wtDeletes)
            editor.add(new DeletePath(path));
          editor.finish();
        }
        builder.addParentId(indexCommit);
        builder.setMessage(MessageFormat.format(
            workingDirectoryMessage, branch,
            headCommit.abbreviate(7).name(),
View Full Code Here

  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:
          EolCanonicalizingInputStream in = new EolCanonicalizingInputStream(
              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(
              "Unknown autocrlf option " + autoCRLF); //$NON-NLS-1$
        }

        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;
View Full Code Here

        tree.failed(new Status(IStatus.WARNING, Activator.getPluginId(),
            CoreText.MoveDeleteHook_unmergedFileError));
        return I_AM_DONE;
      }

      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) {
View Full Code Here

      if (sEnt.length == 0) {
        sCache.unlock();
        return MoveResult.UNTRACKED;
      }

      final DirCacheEditor sEdit = sCache.editor();
      sEdit.add(new DirCacheEditor.DeleteTree(sPath));
      final int sPathLen = sPath.length() == 0 ? sPath.length() : sPath
          .length() + 1;
      for (final DirCacheEntry se : sEnt) {
        if (!se.isMerged())
          return MoveResult.UNMERGED;
        final String p = se.getPathString().substring(sPathLen);
        sEdit.add(new DirCacheEditor.PathEdit(dPath + p) {
          @Override
          public void apply(final DirCacheEntry dEnt) {
            dEnt.copyMetaData(se);
          }
        });
      }
      if (sEdit.commit())
        return MoveResult.SUCCESS;
      else
        return MoveResult.FAILED;
    } finally {
      if (sCache != null)
View Full Code Here

        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();
      for (DirCacheEditor editor:edits.values())
        if (editor.getDirCache() != null)
          editor.getDirCache().unlock();
      edits.clear();
      mappings.clear();
      monitor.done();
    }
  }
View Full Code Here

    final RepositoryMapping rm = pd.getRepositoryMapping(path);
    if (rm == null)
      return;
    final Repository db = rm.getRepository();

    DirCacheEditor e = edits.get(db);
    if (e == null) {
      try {
        e = db.lockDirCache().editor();
      } catch (IOException err) {
        throw new CoreException(Activator.error(CoreText.UntrackOperation_failed, err));
      }
      edits.put(db, e);
      mappings.put(rm, rm);
    }

    if (path instanceof IContainer)
      e.add(new DirCacheEditor.DeleteTree(rm.getRepoRelativePath(path)));
    else
      e.add(new DirCacheEditor.DeletePath(rm.getRepoRelativePath(path)));
  }
View Full Code Here

  protected CheckoutCommand checkoutPaths() throws IOException,
      RefNotFoundException {
    RevWalk revWalk = new RevWalk(repo);
    DirCache dc = repo.lockDirCache();
    try {
      DirCacheEditor editor = dc.editor();
      TreeWalk startWalk = new TreeWalk(revWalk.getObjectReader());
      startWalk.setRecursive(true);
      if (!checkoutAllPaths)
        startWalk.setFilter(PathFilterGroup.createFromStrings(paths));
      boolean checkoutIndex = startCommit == null && startPoint == null;
      if (!checkoutIndex)
        startWalk.addTree(revWalk.parseCommit(getStartPoint())
            .getTree());
      else
        startWalk.addTree(new DirCacheIterator(dc));

      final File workTree = repo.getWorkTree();
      final ObjectReader r = repo.getObjectDatabase().newReader();
      try {
        while (startWalk.next()) {
          final ObjectId blobId = startWalk.getObjectId(0);
          final FileMode mode = startWalk.getFileMode(0);
          editor.add(new PathEdit(startWalk.getPathString()) {
            public void apply(DirCacheEntry ent) {
              ent.setObjectId(blobId);
              ent.setFileMode(mode);
              File file = new File(workTree, ent.getPathString());
              File parentDir = file.getParentFile();
              try {
                FileUtils.mkdirs(parentDir, true);
                DirCacheCheckout.checkoutEntry(repo, file, ent, r);
              } catch (IOException e) {
                throw new JGitInternalException(
                    MessageFormat.format(
                        JGitText.get().checkoutConflictWithFile,
                        ent.getPathString()), e);
              }
            }
          });
        }
        editor.commit();
      } finally {
        startWalk.release();
        r.release();
      }
    } finally {
View Full Code Here

    return this;
  }

  private void resetIndexForPaths(RevCommit commit) {
    DirCache dc = null;
    final DirCacheEditor edit;
    try {
      dc = repo.lockDirCache();
      edit = dc.editor();

      final TreeWalk tw = new TreeWalk(repo);
      tw.addTree(new DirCacheIterator(dc));
      tw.addTree(commit.getTree());
      tw.setFilter(PathFilterGroup.createFromStrings(filepaths));
      tw.setRecursive(true);

      while (tw.next()) {
        final String path = tw.getPathString();
        // DirCacheIterator dci = tw.getTree(0, DirCacheIterator.class);
        final CanonicalTreeParser tree = tw.getTree(1,
            CanonicalTreeParser.class);
        if (tree == null)
          // file is not in the commit, remove from index
          edit.add(new DirCacheEditor.DeletePath(path));
        else { // revert index to commit
          // it seams that there is concurrent access to tree
          // variable, therefore we need to keep references to
          // entryFileMode and entryObjectId in local
          // variables
          final FileMode entryFileMode = tree.getEntryFileMode();
          final ObjectId entryObjectId = tree.getEntryObjectId();
          edit.add(new DirCacheEditor.PathEdit(path) {
            @Override
            public void apply(DirCacheEntry ent) {
              ent.setFileMode(entryFileMode);
              ent.setObjectId(entryObjectId);
              ent.setLastModified(0);
            }
          });
        }
      }

      edit.commit();
    } catch (IOException e) {
      throw new RuntimeException(e);
    } finally {
      if (dc != null)
        dc.unlock();
View Full Code Here

  private void resetIndex(RevCommit commit) throws IOException {
    DirCache dc = repo.lockDirCache();
    TreeWalk walk = null;
    try {
      DirCacheEditor editor = dc.editor();

      walk = new TreeWalk(repo);
      walk.addTree(commit.getTree());
      walk.addTree(new DirCacheIterator(dc));
      walk.setRecursive(true);

      while (walk.next()) {
        AbstractTreeIterator cIter = walk.getTree(0,
            AbstractTreeIterator.class);
        if (cIter == null) {
          editor.add(new DeletePath(walk.getPathString()));
          continue;
        }

        final DirCacheEntry entry = new DirCacheEntry(walk.getRawPath());
        entry.setFileMode(cIter.getEntryFileMode());
        entry.setObjectIdFromRaw(cIter.idBuffer(), cIter.idOffset());

        DirCacheIterator dcIter = walk.getTree(1,
            DirCacheIterator.class);
        if (dcIter != null && dcIter.idEqual(cIter)) {
          DirCacheEntry indexEntry = dcIter.getDirCacheEntry();
          entry.setLastModified(indexEntry.getLastModified());
          entry.setLength(indexEntry.getLength());
        }

        editor.add(new PathEdit(entry) {

          @Override
          public void apply(DirCacheEntry ent) {
            ent.copyMetaData(entry);
          }
        });
      }

      editor.commit();
    } finally {
      dc.unlock();
      if (walk != null)
        walk.release();
    }
View Full Code Here

TOP

Related Classes of org.eclipse.jgit.dircache.DirCacheEditor

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.