Package org.eclipse.jgit.dircache

Examples of org.eclipse.jgit.dircache.DirCache


    filepaths.add(file);
    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


        dc.unlock();
    }
  }

  private void resetIndex(RevCommit commit) throws IOException {
    DirCache dc = null;
    try {
      dc = repo.lockDirCache();
      dc.clear();
      DirCacheBuilder dcb = dc.builder();
      dcb.addTree(new byte[0], 0, repo.newObjectReader(),
          commit.getTree());
      dcb.commit();
    } catch (IOException e) {
      throw e;
    } finally {
      if (dc != null)
        dc.unlock();
    }
  }
View Full Code Here

        dc.unlock();
    }
  }

  private void checkoutIndex(RevCommit commit) throws IOException {
    DirCache dc = null;
    try {
      dc = repo.lockDirCache();
      DirCacheCheckout checkout = new DirCacheCheckout(repo, dc,
          commit.getTree());
      checkout.setFailOnConflict(false);
      checkout.checkout();
    } catch (IOException e) {
      throw e;
    } finally {
      if (dc != null)
        dc.unlock();
    }
  }
View Full Code Here

  public DirCache call() throws NoFilepatternException {

    if (filepatterns.isEmpty())
      throw new NoFilepatternException(JGitText.get().atLeastOnePatternIsRequired);
    checkCallable();
    DirCache dc = null;
    boolean addAll = false;
    if (filepatterns.contains("."))
      addAll = true;

    ObjectInserter inserter = repo.newObjectInserter();
    try {
      dc = repo.lockDirCache();
      DirCacheIterator c;

      DirCacheBuilder builder = dc.builder();
      final TreeWalk tw = new TreeWalk(repo);
      tw.addTree(new DirCacheBuildIterator(builder));
      if (workingTreeIterator == null)
        workingTreeIterator = new FileTreeIterator(repo);
      tw.addTree(workingTreeIterator);
      tw.setRecursive(true);
      if (!addAll)
        tw.setFilter(PathFilterGroup.createFromStrings(filepatterns));

      String lastAddedFile = null;

      while (tw.next()) {
        String path = tw.getPathString();

        WorkingTreeIterator f = tw.getTree(1, WorkingTreeIterator.class);
        if (tw.getTree(0, DirCacheIterator.class) == null &&
            f != null && f.isEntryIgnored()) {
          // file is not in index but is ignored, do nothing
        }
        // In case of an existing merge conflict the
        // DirCacheBuildIterator iterates over all stages of
        // this path, we however want to add only one
        // new DirCacheEntry per path.
        else if (!(path.equals(lastAddedFile))) {
          if (!(update && tw.getTree(0, DirCacheIterator.class) == null)) {
            c = tw.getTree(0, DirCacheIterator.class);
            if (f != null) { // the file exists
              long sz = f.getEntryLength();
              DirCacheEntry entry = new DirCacheEntry(path);
              if (c == null || c.getDirCacheEntry() == null
                  || !c.getDirCacheEntry().isAssumeValid()) {
                FileMode mode = f.getIndexFileMode(c);
                entry.setFileMode(mode);

                if (FileMode.GITLINK != mode) {
                  entry.setLength(sz);
                  entry.setLastModified(f
                      .getEntryLastModified());
                  InputStream in = f.openEntryStream();
                  try {
                    entry.setObjectId(inserter.insert(
                        Constants.OBJ_BLOB, sz, in));
                  } finally {
                    in.close();
                  }
                  builder.add(entry);
                  lastAddedFile = path;
                } else {
                  Repository subRepo = Git.open(
                      new File(repo.getWorkTree(), path))
                      .getRepository();
                  ObjectId subRepoHead = subRepo
                      .resolve(Constants.HEAD);
                  if (subRepoHead != null) {
                    entry.setObjectId(subRepoHead);
                    builder.add(entry);
                    lastAddedFile = path;
                  }
                }
              } else {
                builder.add(c.getDirCacheEntry());
              }

            } else if (!update){
              builder.add(c.getDirCacheEntry());
            }
          }
        }
      }
      inserter.flush();
      builder.commit();
      setCallable(false);
    } catch (IOException e) {
      throw new JGitInternalException(
          JGitText.get().exceptionCaughtDuringExecutionOfAddCommand, e);
    } finally {
      inserter.release();
      if (dc != null)
        dc.unlock();
    }

    return dc;
  }
View Full Code Here

    RefUpdate u = clonedRepo.updateRef(Constants.HEAD, detached);
    u.setNewObjectId(commit.getId());
    u.forceUpdate();

    if (!bare) {
      DirCache dc = clonedRepo.lockDirCache();
      DirCacheCheckout co = new DirCacheCheckout(clonedRepo, dc,
          commit.getTree());
      co.checkout();
      if (cloneSubmodules)
        cloneSubmodules(clonedRepo);
View Full Code Here

        } else {
          parents.add(0, headId);
        }

      // lock the index
      DirCache index = repo.lockDirCache();
      try {
        if (!only.isEmpty())
          index = createTemporaryIndex(headId, index);

        ObjectInserter odi = repo.newObjectInserter();
        try {
          // Write the index as tree to the object database. This may
          // fail for example when the index contains unmerged paths
          // (unresolved conflicts)
          ObjectId indexTreeId = index.writeTree(odi);

          if (insertChangeId)
            insertChangeId(indexTreeId);

          // Create a Commit object, populate it and write it
          CommitBuilder commit = new CommitBuilder();
          commit.setCommitter(committer);
          commit.setAuthor(author);
          commit.setMessage(message);

          commit.setParentIds(parents);
          commit.setTreeId(indexTreeId);
          ObjectId commitId = odi.insert(commit);
          odi.flush();

          RevWalk revWalk = new RevWalk(repo);
          try {
            RevCommit revCommit = revWalk.parseCommit(commitId);
            RefUpdate ru = repo.updateRef(Constants.HEAD);
            ru.setNewObjectId(commitId);
            if (reflogComment != null) {
              ru.setRefLogMessage(reflogComment, false);
            } else {
              String prefix = amend ? "commit (amend): "
                  : "commit: ";
              ru.setRefLogMessage(
                  prefix + revCommit.getShortMessage(), false);
            }
            if (headId != null)
              ru.setExpectedOldObjectId(headId);
            else
              ru.setExpectedOldObjectId(ObjectId.zeroId());
            Result rc = ru.forceUpdate();
            switch (rc) {
            case NEW:
            case FORCED:
            case FAST_FORWARD: {
              setCallable(false);
              if (state == RepositoryState.MERGING_RESOLVED) {
                // Commit was successful. Now delete the files
                // used for merge commits
                repo.writeMergeCommitMsg(null);
                repo.writeMergeHeads(null);
              } else if (state == RepositoryState.CHERRY_PICKING_RESOLVED) {
                repo.writeMergeCommitMsg(null);
                repo.writeCherryPickHead(null);
              }
              return revCommit;
            }
            case REJECTED:
            case LOCK_FAILURE:
              throw new ConcurrentRefUpdateException(JGitText
                  .get().couldNotLockHEAD, ru.getRef(), rc);
            default:
              throw new JGitInternalException(MessageFormat
                  .format(JGitText.get().updatingRefFailed,
                      Constants.HEAD,
                      commitId.toString(), rc));
            }
          } finally {
            revWalk.release();
          }
        } finally {
          odi.release();
        }
      } finally {
        index.unlock();
      }
    } catch (UnmergedPathException e) {
      // since UnmergedPathException is a subclass of IOException
      // which should not be wrapped by a JGitInternalException we
      // have to catch and re-throw it here
View Full Code Here

    // get DirCacheEditor to modify the index if required
    DirCacheEditor dcEditor = index.editor();

    // get DirCacheBuilder for newly created in-core index to build a
    // temporary index for this commit
    DirCache inCoreIndex = DirCache.newInCore();
    DirCacheBuilder dcBuilder = inCoreIndex.builder();

    onlyProcessed = new boolean[only.size()];
    boolean emptyCommit = true;

    TreeWalk treeWalk = new TreeWalk(repo);
View Full Code Here

    if (inCore) {
      modifiedFiles.clear();
      return;
    }

    DirCache dc = db.readDirCache();
    ObjectReader or = db.getObjectDatabase().newReader();
    Iterator<String> mpathsIt=modifiedFiles.iterator();
    while(mpathsIt.hasNext()) {
      String mpath=mpathsIt.next();
      DirCacheEntry entry = dc.getEntry(mpath);
      FileOutputStream fos = new FileOutputStream(new File(db.getWorkTree(), mpath));
      try {
        or.open(entry.getObjectId()).copyTo(fos);
      } finally {
        fos.close();
View Full Code Here

   * @throws RefNotFoundException
   */
  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);
      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 {
      dc.unlock();
      revWalk.release();
    }
    return this;
  }
View Full Code Here

      NoHeadException, JGitInternalException {
    ObjectId headTree = repo.resolve(Constants.HEAD + "^{tree}");
    if (headTree == null)
      throw new NoHeadException(
          JGitText.get().cannotRebaseWithoutCurrentHead);
    DirCache dc = repo.lockDirCache();
    try {
      DirCacheCheckout dco = new DirCacheCheckout(repo, dc, headTree);
      dco.setFailOnConflict(false);
      boolean needsDeleteFiles = dco.checkout();
      if (needsDeleteFiles) {
        List<String> fileList = dco.getToBeDeleted();
        for (String filePath : fileList) {
          File fileToDelete = new File(repo.getWorkTree(), filePath);
          if (fileToDelete.exists())
            FileUtils.delete(fileToDelete, FileUtils.RECURSIVE
                | FileUtils.RETRY);
        }
      }
    } finally {
      dc.unlock();
    }
    RevWalk rw = new RevWalk(repo);
    RevCommit commit = rw.parseCommit(repo.resolve(Constants.HEAD));
    rw.release();
    return commit;
View Full Code Here

TOP

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

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.