Package org.eclipse.jgit.lib

Examples of org.eclipse.jgit.lib.ObjectInserter$Filter


    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()) {
                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 {
      inserter.release();
      if (dc != null)
        dc.unlock();
    }

    return dc;
View Full Code Here


      } else {
        newTag.setObjectId(id);
      }

      // write the tag object
      ObjectInserter inserter = repo.newObjectInserter();
      try {
        ObjectId tagId = inserter.insert(newTag);
        inserter.flush();

        RevWalk revWalk = new RevWalk(repo);
        try {
          RevTag revTag = revWalk.parseTag(tagId);
          String refName = Constants.R_TAGS + newTag.getTag();
          RefUpdate tagRef = repo.updateRef(refName);
          tagRef.setNewObjectId(tagId);
          tagRef.setForceUpdate(forceUpdate);
          tagRef.setRefLogMessage("tagged " + name, false);
          Result updateResult = tagRef.update(revWalk);
          switch (updateResult) {
          case NEW:
          case FORCED:
            return revTag;
          case LOCK_FAILURE:
            throw new ConcurrentRefUpdateException(
                JGitText.get().couldNotLockHEAD,
                tagRef.getRef(), updateResult);
          default:
            throw new JGitInternalException(MessageFormat.format(
                JGitText.get().updatingRefFailed, refName,
                newTag.toString(), updateResult));
          }

        } finally {
          revWalk.release();
        }

      } finally {
        inserter.release();
      }

    } catch (IOException e) {
      throw new JGitInternalException(
          JGitText.get().exceptionCaughtDuringExecutionOfTagCommand,
View Full Code Here

   * @throws FileNotFoundException
   * @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

  private void receivePack(final ProgressMonitor monitor) throws IOException {
    InputStream input = in;
    if (sideband)
      input = new SideBandInputStream(input, monitor, getMessageWriter());

    ObjectInserter ins = local.newObjectInserter();
    try {
      PackParser parser = ins.newPackParser(input);
      parser.setAllowThin(thinPack);
      parser.setObjectChecking(transport.isCheckFetchedObjects());
      parser.setLockMessage(lockMessage);
      packLock = parser.parse(monitor);
      ins.flush();
    } finally {
      ins.release();
    }
  }
View Full Code Here

  protected void doFetch(final ProgressMonitor monitor,
      final Collection<Ref> want, final Set<ObjectId> have)
      throws TransportException {
    verifyPrerequisites();
    try {
      ObjectInserter ins = transport.local.newObjectInserter();
      try {
        PackParser parser = ins.newPackParser(bin);
        parser.setAllowThin(true);
        parser.setObjectChecking(transport.isCheckFetchedObjects());
        parser.setLockMessage(lockMessage);
        packLock = parser.parse(NullProgressMonitor.INSTANCE);
        ins.flush();
      } finally {
        ins.release();
      }
    } catch (IOException err) {
      close();
      throw new TransportException(transport.uri, err.getMessage(), err);
    } catch (RuntimeException err) {
View Full Code Here

    ProgressMonitor receiving = NullProgressMonitor.INSTANCE;
    ProgressMonitor resolving = NullProgressMonitor.INSTANCE;
    if (sideBand)
      resolving = new SideBandProgressMonitor(msgOut);

    ObjectInserter ins = db.newObjectInserter();
    try {
      String lockMsg = "jgit receive-pack";
      if (getRefLogIdent() != null)
        lockMsg += " from " + getRefLogIdent().toExternalString();

      parser = ins.newPackParser(rawIn);
      parser.setAllowThin(true);
      parser.setNeedNewObjectIds(checkReferencedIsReachable);
      parser.setNeedBaseObjectIds(checkReferencedIsReachable);
      parser.setObjectChecking(isCheckReceivedObjects());
      parser.setLockMessage(lockMsg);
      packLock = parser.parse(receiving, resolving);
      ins.flush();
    } finally {
      ins.release();
    }

    if (timeoutIn != null)
      timeoutIn.setTimeout(timeout * 1000);
  }
 
View Full Code Here

    String message = "Created branch " + branchName;
    if (author == null) {
      author = new PersonIdent("Gitblit", "gitblit@localhost");
    }
    try {
      ObjectInserter odi = repository.newObjectInserter();
      try {
        // Create a blob object to insert into a tree
        ObjectId blobId = odi.insert(Constants.OBJ_BLOB,
            message.getBytes(Constants.CHARACTER_ENCODING));

        // Create a tree object to reference from a commit
        TreeFormatter tree = new TreeFormatter();
        tree.append(".branch", FileMode.REGULAR_FILE, blobId);
        ObjectId treeId = odi.insert(tree);

        // Create a commit object
        CommitBuilder commit = new CommitBuilder();
        commit.setAuthor(author);
        commit.setCommitter(author);
        commit.setEncoding(Constants.CHARACTER_ENCODING);
        commit.setMessage(message);
        commit.setTreeId(treeId);

        // Insert the commit into the repository
        ObjectId commitId = odi.insert(commit);
        odi.flush();

        RevWalk revWalk = new RevWalk(repository);
        try {
          RevCommit revCommit = revWalk.parseCommit(commitId);
          if (!branchName.startsWith("refs/")) {
            branchName = "refs/heads/" + branchName;
          }
          RefUpdate ru = repository.updateRef(branchName);
          ru.setNewObjectId(commitId);
          ru.setRefLogMessage("commit: " + revCommit.getShortMessage(), false);
          Result rc = ru.forceUpdate();
          switch (rc) {
          case NEW:
          case FORCED:
          case FAST_FORWARD:
            success = true;
            break;
          default:
            success = false;
          }
        } finally {
          revWalk.release();
        }
      } finally {
        odi.release();
      }
    } catch (Throwable t) {
      error(t, repository, "Failed to create orphan branch {1} in repository {0}", branchName);
    }
    return success;
View Full Code Here

      RecursiveMerger merger = (RecursiveMerger) MergeStrategy.RECURSIVE.newMerger(repository, true);
      boolean merged = merger.merge(branchTip, srcTip);
      if (merged) {
        // create a merge commit and a reference to track the merge commit
        ObjectId treeId = merger.getResultTreeId();
        ObjectInserter odi = repository.newObjectInserter();
        try {
          // Create a commit object
          CommitBuilder commitBuilder = new CommitBuilder();
          commitBuilder.setCommitter(committer);
          commitBuilder.setAuthor(committer);
          commitBuilder.setEncoding(Constants.CHARSET);
          if (StringUtils.isEmpty(message)) {
            message = MessageFormat.format("merge {0} into {1}", srcTip.getName(), branchTip.getName());
          }
          commitBuilder.setMessage(message);
          commitBuilder.setParentIds(branchTip.getId(), srcTip.getId());
          commitBuilder.setTreeId(treeId);

          // Insert the merge commit into the repository
          ObjectId mergeCommitId = odi.insert(commitBuilder);
          odi.flush();

          // set the merge ref to the merge commit
          RevCommit mergeCommit = revWalk.parseCommit(mergeCommitId);
          RefUpdate mergeRefUpdate = repository.updateRef(toBranch);
          mergeRefUpdate.setNewObjectId(mergeCommitId);
          mergeRefUpdate.setRefLogMessage("commit: " + mergeCommit.getShortMessage(), false);
          RefUpdate.Result rc = mergeRefUpdate.update();
          switch (rc) {
          case FAST_FORWARD:
            // successful, clean merge
            break;
          default:
            throw new GitBlitException(MessageFormat.format("Unexpected result \"{0}\" when merging commit {1} into {2} in {3}",
                rc.name(), srcTip.getName(), branchTip.getName(), repository.getDirectory()));
          }

          // return the merge commit id
          return new MergeResult(MergeStatus.MERGED, mergeCommitId.getName());
        } finally {
          odi.release();
        }
      }
    } catch (IOException e) {
      LOGGER.error("Failed to merge", e);
    } finally {
View Full Code Here

      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) {
      log.error("", e);
    } catch (IOException e) {
      log.error("", e);
    } finally {
      inserter.release();
    }
  }
View Full Code Here

      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

TOP

Related Classes of org.eclipse.jgit.lib.ObjectInserter$Filter

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.