Examples of DirCacheBuilder


Examples of org.eclipse.jgit.dircache.DirCacheBuilder

  private void resetIndexForPaths(RevCommit commit) {
    DirCache dc = null;
    try {
      dc = repo.lockDirCache();
      DirCacheBuilder builder = dc.builder();

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

      while (tw.next()) {
        final CanonicalTreeParser tree = tw.getTree(1,
            CanonicalTreeParser.class);
        // only keep file in index if it's in the commit
        if (tree != null) {
            // revert index to commit
          DirCacheEntry entry = new DirCacheEntry(tw.getRawPath());
          entry.setFileMode(tree.getEntryFileMode());
          entry.setObjectId(tree.getEntryObjectId());
          builder.add(entry);
        }
      }

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

Examples of org.eclipse.jgit.dircache.DirCacheBuilder

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

      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) {
          // Not in commit, don't add to new index
          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());
        }

        builder.add(entry);
      }

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

Examples of org.eclipse.jgit.dircache.DirCacheBuilder

    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()) {
                entry.setLength(sz);
                entry.setLastModified(f.getEntryLastModified());
                entry.setFileMode(f.getEntryFileMode());

                InputStream in = f.openEntryStream();
                try {
                  entry.setObjectId(inserter.insert(
                      Constants.OBJ_BLOB, sz, in));
                } finally {
                  in.close();
                }

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

Examples of org.eclipse.jgit.dircache.DirCacheBuilder

   * @throws IOException
   */
  protected void resetIndex(FileTreeIterator treeItr)
      throws FileNotFoundException, IOException {
    ObjectInserter inserter = db.newObjectInserter();
    DirCacheBuilder builder = db.lockDirCache().builder();
    DirCacheEntry dce;

    while (!treeItr.eof()) {
      long len = treeItr.getEntryLength();

      dce = new DirCacheEntry(treeItr.getEntryPathString());
      dce.setFileMode(treeItr.getEntryFileMode());
      dce.setLastModified(treeItr.getEntryLastModified());
      dce.setLength((int) len);
      FileInputStream in = new FileInputStream(treeItr.getEntryFile());
      dce.setObjectId(inserter.insert(Constants.OBJ_BLOB, len, in));
      in.close();
      builder.add(dce);
      treeItr.next(1);
    }
    builder.commit();
    inserter.flush();
    inserter.release();
  }
View Full Code Here

Examples of org.eclipse.jgit.dircache.DirCacheBuilder

    checkCallable();
    DirCache dc = null;

    try {
      dc = repo.lockDirCache();
      DirCacheBuilder builder = dc.builder();
      final TreeWalk tw = new TreeWalk(repo);
      tw.reset(); // drop the first empty tree, which we do not need here
      tw.setRecursive(true);
      tw.setFilter(PathFilterGroup.createFromStrings(filepatterns));
      tw.addTree(new DirCacheBuildIterator(builder));

      while (tw.next()) {
        final File path = new File(repo.getWorkTree(),
            tw.getPathString());
        final FileMode mode = tw.getFileMode(0);
        if (mode.getObjectType() == Constants.OBJ_BLOB) {
          // Deleting a blob is simply a matter of removing
          // the file or symlink named by the tree entry.
          delete(path);
        }
      }
      builder.commit();
      setCallable(false);
    } catch (IOException e) {
      throw new JGitInternalException(
          JGitText.get().exceptionCaughtDuringExecutionOfRmCommand, e);
    } finally {
View Full Code Here

Examples of org.eclipse.jgit.dircache.DirCacheBuilder

    if (getTicketsBranch(db) == null) {
      createTicketsBranch(db);
    }

    DirCache newIndex = DirCache.newInCore();
    DirCacheBuilder builder = newIndex.builder();
    ObjectInserter inserter = db.newObjectInserter();

    try {
      // create an index entry for the revised index
      final DirCacheEntry idIndexEntry = new DirCacheEntry(file);
      idIndexEntry.setLength(content.length());
      idIndexEntry.setLastModified(System.currentTimeMillis());
      idIndexEntry.setFileMode(FileMode.REGULAR_FILE);

      // insert new ticket index
      idIndexEntry.setObjectId(inserter.insert(org.eclipse.jgit.lib.Constants.OBJ_BLOB,
          content.getBytes(Constants.ENCODING)));

      // add to temporary in-core index
      builder.add(idIndexEntry);

      Set<String> ignorePaths = new HashSet<String>();
      ignorePaths.add(file);

      for (DirCacheEntry entry : getTreeEntries(db, ignorePaths)) {
        builder.add(entry);
      }

      // finish temporary in-core index used for this commit
      builder.finish();

      // commit the change
      commitIndex(db, newIndex, createdBy, msg);

    } catch (ConcurrentRefUpdateException e) {
View Full Code Here

Examples of org.eclipse.jgit.dircache.DirCacheBuilder

      try {
        ObjectId treeId = db.resolve(BRANCH + "^{tree}");

        // Create the in-memory index of the new/updated ticket
        DirCache index = DirCache.newInCore();
        DirCacheBuilder builder = index.builder();

        // Traverse HEAD to add all other paths
        treeWalk = new TreeWalk(db);
        int hIdx = -1;
        if (treeId != null) {
          hIdx = treeWalk.addTree(treeId);
        }
        treeWalk.setRecursive(true);
        while (treeWalk.next()) {
          String path = treeWalk.getPathString();
          CanonicalTreeParser hTree = null;
          if (hIdx != -1) {
            hTree = treeWalk.getTree(hIdx, CanonicalTreeParser.class);
          }
          if (!path.startsWith(ticketPath)) {
            // add entries from HEAD for all other paths
            if (hTree != null) {
              final DirCacheEntry entry = new DirCacheEntry(path);
              entry.setObjectId(hTree.getEntryObjectId());
              entry.setFileMode(hTree.getEntryFileMode());

              // add to temporary in-core index
              builder.add(entry);
            }
          }
        }

        // finish temporary in-core index used for this commit
        builder.finish();

        success = commitIndex(db, index, deletedBy, "- " + ticket.number);

      } catch (Throwable t) {
        log.error(MessageFormat.format("Failed to delete ticket {0,number,0} from {1}",
View Full Code Here

Examples of org.eclipse.jgit.dircache.DirCacheBuilder

  private DirCache createIndex(Repository db, long ticketId, Change change)
      throws IOException, ClassNotFoundException, NoSuchFieldException {

    String ticketPath = toTicketPath(ticketId);
    DirCache newIndex = DirCache.newInCore();
    DirCacheBuilder builder = newIndex.builder();
    ObjectInserter inserter = db.newObjectInserter();

    Set<String> ignorePaths = new TreeSet<String>();
    try {
      // create/update the journal
      // exclude the attachment content
      List<Change> changes = getJournal(db, ticketId);
      changes.add(change);
      String journal = TicketSerializer.serializeJournal(changes).trim();

      byte [] journalBytes = journal.getBytes(Constants.ENCODING);
      String journalPath = ticketPath + "/" + JOURNAL;
      final DirCacheEntry journalEntry = new DirCacheEntry(journalPath);
      journalEntry.setLength(journalBytes.length);
      journalEntry.setLastModified(change.date.getTime());
      journalEntry.setFileMode(FileMode.REGULAR_FILE);
      journalEntry.setObjectId(inserter.insert(org.eclipse.jgit.lib.Constants.OBJ_BLOB, journalBytes));

      // add journal to index
      builder.add(journalEntry);
      ignorePaths.add(journalEntry.getPathString());

      // Add any attachments to the index
      if (change.hasAttachments()) {
        for (Attachment attachment : change.attachments) {
          // build a path name for the attachment and mark as ignored
          String path = toAttachmentPath(ticketId, attachment.name);
          ignorePaths.add(path);

          // create an index entry for this attachment
          final DirCacheEntry entry = new DirCacheEntry(path);
          entry.setLength(attachment.content.length);
          entry.setLastModified(change.date.getTime());
          entry.setFileMode(FileMode.REGULAR_FILE);

          // insert object
          entry.setObjectId(inserter.insert(org.eclipse.jgit.lib.Constants.OBJ_BLOB, attachment.content));

          // add to temporary in-core index
          builder.add(entry);
        }
      }

      for (DirCacheEntry entry : getTreeEntries(db, ignorePaths)) {
        builder.add(entry);
      }

      // finish the index
      builder.finish();
    } finally {
      inserter.release();
    }
    return newIndex;
  }
View Full Code Here

Examples of org.eclipse.jgit.dircache.DirCacheBuilder

  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

Examples of org.eclipse.jgit.dircache.DirCacheBuilder

      UserModel user = GitBlitWebSession.get().getUser();
      String email = Optional.fromNullable(user.emailAddress).or(user.username + "@" + "gitblit");
      PersonIdent author = new PersonIdent(user.getDisplayName(), email);

      DirCache newIndex = DirCache.newInCore();
      DirCacheBuilder indexBuilder = newIndex.builder();

      if (addReadme) {
        // insert a README
        String title = StringUtils.stripDotGit(StringUtils.getLastPathElement(repositoryModel.name));
        String description = repositoryModel.description == null ? "" : repositoryModel.description;
        String readme = String.format("## %s\n\n%s\n\n", title, description);
        byte [] bytes = readme.getBytes(Constants.ENCODING);

        DirCacheEntry entry = new DirCacheEntry("README.md");
        entry.setLength(bytes.length);
        entry.setLastModified(System.currentTimeMillis());
        entry.setFileMode(FileMode.REGULAR_FILE);
        entry.setObjectId(odi.insert(org.eclipse.jgit.lib.Constants.OBJ_BLOB, bytes));

        indexBuilder.add(entry);
      }

      if (!StringUtils.isEmpty(gitignore)) {
        // insert a .gitignore file
        File dir = app().runtime().getFileOrFolder(Keys.git.gitignoreFolder, "${baseFolder}/gitignore");
        File file = new File(dir, gitignore + ".gitignore");
        if (file.exists() && file.length() > 0) {
          byte [] bytes = FileUtils.readContent(file);
          if (!ArrayUtils.isEmpty(bytes)) {
            DirCacheEntry entry = new DirCacheEntry(".gitignore");
            entry.setLength(bytes.length);
            entry.setLastModified(System.currentTimeMillis());
            entry.setFileMode(FileMode.REGULAR_FILE);
            entry.setObjectId(odi.insert(org.eclipse.jgit.lib.Constants.OBJ_BLOB, bytes));

            indexBuilder.add(entry);
          }
        }
      }

      if (addGitFlow) {
        // insert a .gitflow file
        Config config = new Config();
        config.setString("gitflow", null, "masterBranch", Constants.MASTER);
        config.setString("gitflow", null, "developBranch", Constants.DEVELOP);
        config.setString("gitflow", null, "featureBranchPrefix", "feature/");
        config.setString("gitflow", null, "releaseBranchPrefix", "release/");
        config.setString("gitflow", null, "hotfixBranchPrefix", "hotfix/");
        config.setString("gitflow", null, "supportBranchPrefix", "support/");
        config.setString("gitflow", null, "versionTagPrefix", "");

        byte [] bytes = config.toText().getBytes(Constants.ENCODING);

        DirCacheEntry entry = new DirCacheEntry(".gitflow");
        entry.setLength(bytes.length);
        entry.setLastModified(System.currentTimeMillis());
        entry.setFileMode(FileMode.REGULAR_FILE);
        entry.setObjectId(odi.insert(org.eclipse.jgit.lib.Constants.OBJ_BLOB, bytes));

        indexBuilder.add(entry);
      }

      indexBuilder.finish();

      if (newIndex.getEntryCount() == 0) {
        // nothing to commit
        return false;
      }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.