Package org.eclipse.jgit.dircache

Examples of org.eclipse.jgit.dircache.DirCacheEditor$PathEdit


  }

  @Test
  public void testSymlinkNotModifiedThoughNormalized() throws Exception {
    DirCache dc = db.lockDirCache();
    DirCacheEditor dce = dc.editor();
    final String UNNORMALIZED = "target/";
    final byte[] UNNORMALIZED_BYTES = Constants.encode(UNNORMALIZED);
    ObjectInserter oi = db.newObjectInserter();
    final ObjectId linkid = oi.insert(Constants.OBJ_BLOB,
        UNNORMALIZED_BYTES, 0,
        UNNORMALIZED_BYTES.length);
    oi.release();
    dce.add(new DirCacheEditor.PathEdit("link") {
      @Override
      public void apply(DirCacheEntry ent) {
        ent.setFileMode(FileMode.SYMLINK);
        ent.setObjectId(linkid);
        ent.setLength(UNNORMALIZED_BYTES.length);
      }
    });
    assertTrue(dce.commit());
    new Git(db).commit().setMessage("Adding link").call();
    new Git(db).reset().setMode(ResetType.HARD).call();
    DirCacheIterator dci = new DirCacheIterator(db.readDirCache());
    FileTreeIterator fti = new FileTreeIterator(db);
View Full Code Here


   * @throws Exception
   */
  @Test
  public void testSymlinkModifiedNotNormalized() throws Exception {
    DirCache dc = db.lockDirCache();
    DirCacheEditor dce = dc.editor();
    final String NORMALIZED = "target";
    final byte[] NORMALIZED_BYTES = Constants.encode(NORMALIZED);
    ObjectInserter oi = db.newObjectInserter();
    final ObjectId linkid = oi.insert(Constants.OBJ_BLOB, NORMALIZED_BYTES,
        0, NORMALIZED_BYTES.length);
    oi.release();
    dce.add(new DirCacheEditor.PathEdit("link") {
      @Override
      public void apply(DirCacheEntry ent) {
        ent.setFileMode(FileMode.SYMLINK);
        ent.setObjectId(linkid);
        ent.setLength(NORMALIZED_BYTES.length);
      }
    });
    assertTrue(dce.commit());
    new Git(db).commit().setMessage("Adding link").call();
    new Git(db).reset().setMode(ResetType.HARD).call();
    DirCacheIterator dci = new DirCacheIterator(db.readDirCache());
    FileTreeIterator fti = new FileTreeIterator(db);

View Full Code Here

    ObjectInserter oi = db.newObjectInserter();
    final ObjectId linkid = oi.insert(Constants.OBJ_BLOB, NORMALIZED_BYTES,
        0, NORMALIZED_BYTES.length);
    oi.release();
    DirCache dc = db.lockDirCache();
    DirCacheEditor dce = dc.editor();
    dce.add(new DirCacheEditor.PathEdit("link") {
      @Override
      public void apply(DirCacheEntry ent) {
        ent.setFileMode(FileMode.SYMLINK);
        ent.setObjectId(linkid);
        ent.setLength(NORMALIZED_BYTES.length);
      }
    });
    assertTrue(dce.commit());
    new Git(db).commit().setMessage("Adding link").call();
    new Git(db).reset().setMode(ResetType.HARD).call();

    FileUtils.delete(new File(trash, "link"), FileUtils.NONE);
    FS.DETECTED.createSymLink(new File(trash, "link"), "newtarget");
View Full Code Here

      return add(path, blob(content));
    }

    public CommitBuilder add(String path, final RevBlob id)
        throws Exception {
      DirCacheEditor e = tree.editor();
      e.add(new PathEdit(path) {
        @Override
        public void apply(DirCacheEntry ent) {
          ent.setFileMode(FileMode.REGULAR_FILE);
          ent.setObjectId(id);
        }
      });
      e.finish();
      return this;
    }
View Full Code Here

      e.finish();
      return this;
    }

    public CommitBuilder rm(String path) {
      DirCacheEditor e = tree.editor();
      e.add(new DeletePath(path));
      e.add(new DeleteTree(path));
      e.finish();
      return this;
    }
View Full Code Here

  private DirCache createTemporaryIndex(ObjectId headId, DirCache index)
      throws IOException {
    ObjectInserter inserter = null;

    // 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);
    int dcIdx = treeWalk.addTree(new DirCacheIterator(index));
    int fIdx = treeWalk.addTree(new FileTreeIterator(repo));
    int hIdx = -1;
    if (headId != null)
      hIdx = treeWalk.addTree(new RevWalk(repo).parseTree(headId));
    treeWalk.setRecursive(true);

    while (treeWalk.next()) {
      String path = treeWalk.getPathString();
      // check if current entry's path matches a specified path
      int pos = lookupOnly(path);

      CanonicalTreeParser hTree = null;
      if (hIdx != -1)
        hTree = treeWalk.getTree(hIdx, CanonicalTreeParser.class);

      if (pos >= 0) {
        // include entry in commit

        DirCacheIterator dcTree = treeWalk.getTree(dcIdx,
            DirCacheIterator.class);
        FileTreeIterator fTree = treeWalk.getTree(fIdx,
            FileTreeIterator.class);

        // check if entry refers to a tracked file
        boolean tracked = dcTree != null || hTree != null;
        if (!tracked)
          break;

        if (fTree != null) {
          // create a new DirCacheEntry with data retrieved from disk
          final DirCacheEntry dcEntry = new DirCacheEntry(path);
          long entryLength = fTree.getEntryLength();
          dcEntry.setLength(entryLength);
          dcEntry.setLastModified(fTree.getEntryLastModified());
          dcEntry.setFileMode(fTree.getIndexFileMode(dcTree));

          boolean objectExists = (dcTree != null && fTree
              .idEqual(dcTree))
              || (hTree != null && fTree.idEqual(hTree));
          if (objectExists) {
            dcEntry.setObjectId(fTree.getEntryObjectId());
          } else {
            if (FileMode.GITLINK.equals(dcEntry.getFileMode()))
              dcEntry.setObjectId(fTree.getEntryObjectId());
            else {
              // insert object
              if (inserter == null)
                inserter = repo.newObjectInserter();
              long contentLength = fTree.getEntryContentLength();
              InputStream inputStream = fTree.openEntryStream();
              try {
                dcEntry.setObjectId(inserter.insert(
                    Constants.OBJ_BLOB, contentLength,
                    inputStream));
              } finally {
                inputStream.close();
              }
            }
          }

          // update index
          dcEditor.add(new PathEdit(path) {
            @Override
            public void apply(DirCacheEntry ent) {
              ent.copyMetaData(dcEntry);
            }
          });
          // add to temporary in-core index
          dcBuilder.add(dcEntry);

          if (emptyCommit
              && (hTree == null || !hTree.idEqual(fTree) || hTree
                  .getEntryRawMode() != fTree
                  .getEntryRawMode()))
            // this is a change
            emptyCommit = false;
        } else {
          // if no file exists on disk, remove entry from index and
          // don't add it to temporary in-core index
          dcEditor.add(new DeletePath(path));

          if (emptyCommit && hTree != null)
            // this is a change
            emptyCommit = false;
        }

        // keep track of processed path
        onlyProcessed[pos] = true;
      } else {
        // add entries from HEAD for all other paths
        if (hTree != null) {
          // create a new DirCacheEntry with data retrieved from HEAD
          final DirCacheEntry dcEntry = new DirCacheEntry(path);
          dcEntry.setObjectId(hTree.getEntryObjectId());
          dcEntry.setFileMode(hTree.getEntryFileMode());

          // add to temporary in-core index
          dcBuilder.add(dcEntry);
        }
      }
    }

    // there must be no unprocessed paths left at this point; otherwise an
    // untracked or unknown path has been specified
    for (int i = 0; i < onlyProcessed.length; i++)
      if (!onlyProcessed[i])
        throw new JGitInternalException(MessageFormat.format(
            JGitText.get().entryNotFoundByPath, only.get(i)));

    // there must be at least one change
    if (emptyCommit)
      throw new JGitInternalException(JGitText.get().emptyCommit);

    // update index
    dcEditor.commit();
    // finish temporary in-core index used for this commit
    dcBuilder.finish();
    return inCoreIndex;
  }
View Full Code Here

  private DirCache createTemporaryIndex(ObjectId headId, DirCache index)
      throws IOException {
    ObjectInserter inserter = null;

    // 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);
    int dcIdx = treeWalk.addTree(new DirCacheIterator(index));
    int fIdx = treeWalk.addTree(new FileTreeIterator(repo));
    int hIdx = -1;
    if (headId != null)
      hIdx = treeWalk.addTree(new RevWalk(repo).parseTree(headId));
    treeWalk.setRecursive(true);

    while (treeWalk.next()) {
      String path = treeWalk.getPathString();
      // check if current entry's path matches a specified path
      int pos = lookupOnly(path);

      CanonicalTreeParser hTree = null;
      if (hIdx != -1)
        hTree = treeWalk.getTree(hIdx, CanonicalTreeParser.class);

      if (pos >= 0) {
        // include entry in commit

        DirCacheIterator dcTree = treeWalk.getTree(dcIdx,
            DirCacheIterator.class);
        FileTreeIterator fTree = treeWalk.getTree(fIdx,
            FileTreeIterator.class);

        // check if entry refers to a tracked file
        boolean tracked = dcTree != null || hTree != null;
        if (!tracked)
          break;

        if (fTree != null) {
          // create a new DirCacheEntry with data retrieved from disk
          final DirCacheEntry dcEntry = new DirCacheEntry(path);
          long entryLength = fTree.getEntryLength();
          dcEntry.setLength(entryLength);
          dcEntry.setLastModified(fTree.getEntryLastModified());
          dcEntry.setFileMode(fTree.getIndexFileMode(dcTree));

          boolean objectExists = (dcTree != null && fTree
              .idEqual(dcTree))
              || (hTree != null && fTree.idEqual(hTree));
          if (objectExists) {
            dcEntry.setObjectId(fTree.getEntryObjectId());
          } else {
            if (FileMode.GITLINK.equals(dcEntry.getFileMode()))
              dcEntry.setObjectId(fTree.getEntryObjectId());
            else {
              // insert object
              if (inserter == null)
                inserter = repo.newObjectInserter();
              long contentLength = fTree.getEntryContentLength();
              InputStream inputStream = fTree.openEntryStream();
              try {
                dcEntry.setObjectId(inserter.insert(
                    Constants.OBJ_BLOB, contentLength,
                    inputStream));
              } finally {
                inputStream.close();
              }
            }
          }

          // update index
          dcEditor.add(new PathEdit(path) {
            @Override
            public void apply(DirCacheEntry ent) {
              ent.copyMetaData(dcEntry);
            }
          });
          // add to temporary in-core index
          dcBuilder.add(dcEntry);

          if (emptyCommit
              && (hTree == null || !hTree.idEqual(fTree) || hTree
                  .getEntryRawMode() != fTree
                  .getEntryRawMode()))
            // this is a change
            emptyCommit = false;
        } else {
          // if no file exists on disk, remove entry from index and
          // don't add it to temporary in-core index
          dcEditor.add(new DeletePath(path));

          if (emptyCommit && hTree != null)
            // this is a change
            emptyCommit = false;
        }

        // keep track of processed path
        onlyProcessed[pos] = true;
      } else {
        // add entries from HEAD for all other paths
        if (hTree != null) {
          // create a new DirCacheEntry with data retrieved from HEAD
          final DirCacheEntry dcEntry = new DirCacheEntry(path);
          dcEntry.setObjectId(hTree.getEntryObjectId());
          dcEntry.setFileMode(hTree.getEntryFileMode());

          // add to temporary in-core index
          dcBuilder.add(dcEntry);
        }
      }
    }

    // there must be no unprocessed paths left at this point; otherwise an
    // untracked or unknown path has been specified
    for (int i = 0; i < onlyProcessed.length; i++)
      if (!onlyProcessed[i])
        throw new JGitInternalException(MessageFormat.format(
            JGitText.get().entryNotFoundByPath, only.get(i)));

    // there must be at least one change
    if (emptyCommit)
      throw new JGitInternalException(JGitText.get().emptyCommit);

    // update index
    dcEditor.commit();
    // finish temporary in-core index used for this commit
    dcBuilder.finish();
    return inCoreIndex;
  }
View Full Code Here

      stashHeadIter.reset(reader, stashHeadTree);
      CanonicalTreeParser headIter = new CanonicalTreeParser();
      headIter.reset(reader, headTree);

      DirCache cache = repo.lockDirCache();
      DirCacheEditor editor = cache.editor();
      try {
        DirCacheIterator indexIter = new DirCacheIterator(cache);
        FileTreeIterator workingIter = new FileTreeIterator(repo);

        TreeWalk treeWalk = new TreeWalk(reader);
        treeWalk.setRecursive(true);
        treeWalk.setFilter(new StashDiffFilter());

        treeWalk.addTree(stashHeadIter);
        treeWalk.addTree(stashIndexIter);
        treeWalk.addTree(stashWorkingIter);
        treeWalk.addTree(headIter);
        treeWalk.addTree(indexIter);
        treeWalk.addTree(workingIter);

        scanForConflicts(treeWalk);

        // Reset trees and walk
        treeWalk.reset();
        stashWorkingIter.reset(reader, stashWorkingTree);
        stashIndexIter.reset(reader, stashIndexTree);
        stashHeadIter.reset(reader, stashHeadTree);
        treeWalk.addTree(stashHeadIter);
        treeWalk.addTree(stashIndexIter);
        treeWalk.addTree(stashWorkingIter);

        applyChanges(treeWalk, cache, editor);
      } finally {
        editor.commit();
        cache.unlock();
      }
    } catch (JGitInternalException e) {
      throw e;
    } catch (IOException e) {
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$PathEdit

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.