Esempio n. 1
0
 public CommitBuilder rm(String path) {
   DirCacheEditor e = tree.editor();
   e.add(new DeletePath(path));
   e.add(new DeleteTree(path));
   e.finish();
   return this;
 }
Esempio n. 2
0
 protected void saveFile(String fileName, byte[] raw) throws IOException {
   DirCacheEditor editor = newTree.editor();
   if (raw != null && 0 < raw.length) {
     final ObjectId blobId = inserter.insert(Constants.OBJ_BLOB, raw);
     editor.add(
         new PathEdit(fileName) {
           @Override
           public void apply(DirCacheEntry ent) {
             ent.setFileMode(FileMode.REGULAR_FILE);
             ent.setObjectId(blobId);
           }
         });
   } else {
     editor.add(new DeletePath(fileName));
   }
   editor.finish();
 }
Esempio n. 3
0
  @Test
  public void testAdded() throws IOException {
    writeTrashFile("file1", "file1");
    writeTrashFile("dir/subfile", "dir/subfile");
    ObjectId tree = insertTree(new TreeFormatter());

    DirCache index = db.lockDirCache();
    DirCacheEditor editor = index.editor();
    editor.add(add(db, trash, "file1"));
    editor.add(add(db, trash, "dir/subfile"));
    editor.commit();
    FileTreeIterator iterator = new FileTreeIterator(db);
    IndexDiff diff = new IndexDiff(db, tree, iterator);
    diff.diff();
    assertEquals(2, diff.getAdded().size());
    assertTrue(diff.getAdded().contains("file1"));
    assertTrue(diff.getAdded().contains("dir/subfile"));
    assertEquals(0, diff.getChanged().size());
    assertEquals(0, diff.getModified().size());
    assertEquals(0, diff.getRemoved().size());
    assertEquals(Collections.EMPTY_SET, diff.getUntrackedFolders());
  }
Esempio n. 4
0
  /**
   * Update the submodules in one branch of one repository.
   *
   * @param subscriber the branch of the repository which should be changed.
   * @param updates submodule updates which should be updated to.
   * @throws SubmoduleException
   */
  private void updateGitlinks(
      ReviewDb db, Branch.NameKey subscriber, Collection<SubmoduleSubscription> updates)
      throws SubmoduleException {
    PersonIdent author = null;

    Repository pdb = null;
    RevWalk recRw = null;

    StringBuilder msgbuf = new StringBuilder("Updated git submodules\n\n");
    try {
      boolean sameAuthorForAll = true;

      pdb = repoManager.openRepository(subscriber.getParentKey());
      if (pdb.getRef(subscriber.get()) == null) {
        throw new SubmoduleException(
            "The branch was probably deleted from the subscriber repository");
      }

      DirCache dc = readTree(pdb, pdb.getRef(subscriber.get()));
      DirCacheEditor ed = dc.editor();

      for (SubmoduleSubscription s : updates) {
        try (Repository subrepo = repoManager.openRepository(s.getSubmodule().getParentKey());
            RevWalk rw = CodeReviewCommit.newRevWalk(subrepo)) {
          Ref ref = subrepo.getRefDatabase().exactRef(s.getSubmodule().get());
          if (ref == null) {
            ed.add(new DeletePath(s.getPath()));
            continue;
          }

          final ObjectId updateTo = ref.getObjectId();
          RevCommit newCommit = rw.parseCommit(updateTo);

          if (author == null) {
            author = newCommit.getAuthorIdent();
          } else if (!author.equals(newCommit.getAuthorIdent())) {
            sameAuthorForAll = false;
          }

          DirCacheEntry dce = dc.getEntry(s.getPath());
          ObjectId oldId = null;
          if (dce != null) {
            if (!dce.getFileMode().equals(FileMode.GITLINK)) {
              log.error(
                  "Requested to update gitlink "
                      + s.getPath()
                      + " in "
                      + s.getSubmodule().getParentKey().get()
                      + " but entry "
                      + "doesn't have gitlink file mode.");
              continue;
            }
            oldId = dce.getObjectId();
          } else {
            // This submodule did not exist before. We do not want to add
            // the full submodule history to the commit message, so omit it.
            oldId = updateTo;
          }

          ed.add(
              new PathEdit(s.getPath()) {
                @Override
                public void apply(DirCacheEntry ent) {
                  ent.setFileMode(FileMode.GITLINK);
                  ent.setObjectId(updateTo);
                }
              });
          if (verboseSuperProject) {
            msgbuf.append("Project: " + s.getSubmodule().getParentKey().get());
            msgbuf.append(" " + s.getSubmodule().getShortName());
            msgbuf.append(" " + updateTo.getName());
            msgbuf.append("\n\n");

            try {
              rw.markStart(newCommit);

              if (oldId != null) {
                rw.markUninteresting(rw.parseCommit(oldId));
              }
              for (RevCommit c : rw) {
                msgbuf.append(c.getFullMessage() + "\n\n");
              }
            } catch (IOException e) {
              logAndThrowSubmoduleException(
                  "Could not perform a revwalk to " + "create superproject commit message", e);
            }
          }
        }
      }
      ed.finish();

      if (!sameAuthorForAll || author == null) {
        author = myIdent;
      }

      ObjectInserter oi = pdb.newObjectInserter();
      ObjectId tree = dc.writeTree(oi);

      ObjectId currentCommitId = pdb.getRef(subscriber.get()).getObjectId();

      CommitBuilder commit = new CommitBuilder();
      commit.setTreeId(tree);
      commit.setParentIds(new ObjectId[] {currentCommitId});
      commit.setAuthor(author);
      commit.setCommitter(myIdent);
      commit.setMessage(msgbuf.toString());
      oi.insert(commit);
      oi.flush();

      ObjectId commitId = oi.idFor(Constants.OBJ_COMMIT, commit.build());

      final RefUpdate rfu = pdb.updateRef(subscriber.get());
      rfu.setForceUpdate(false);
      rfu.setNewObjectId(commitId);
      rfu.setExpectedOldObjectId(currentCommitId);
      rfu.setRefLogMessage("Submit to " + subscriber.getParentKey().get(), true);

      switch (rfu.update()) {
        case NEW:
        case FAST_FORWARD:
          gitRefUpdated.fire(subscriber.getParentKey(), rfu);
          changeHooks.doRefUpdatedHook(subscriber, rfu, account);
          // TODO since this is performed "in the background" no mail will be
          // sent to inform users about the updated branch
          break;

        default:
          throw new IOException(rfu.getResult().name());
      }
      recRw = new RevWalk(pdb);
      // Recursive call: update subscribers of the subscriber
      updateSuperProjects(db, Sets.newHashSet(subscriber));
    } catch (IOException e) {
      throw new SubmoduleException("Cannot update gitlinks for " + subscriber.get(), e);
    } finally {
      if (recRw != null) {
        recRw.close();
      }
      if (pdb != null) {
        pdb.close();
      }
    }
  }
Esempio n. 5
0
 private void removeFromIndex(String path) throws IOException {
   final DirCache dirc = db.lockDirCache();
   final DirCacheEditor edit = dirc.editor();
   edit.add(new DirCacheEditor.DeletePath(path));
   if (!edit.commit()) throw new IOException("could not commit");
 }
Esempio n. 6
0
 public CommitBuilder edit(PathEdit edit) {
   DirCacheEditor e = tree.editor();
   e.add(edit);
   e.finish();
   return this;
 }