@Test
  public void testSomeCommits()
      throws NoHeadException, NoMessageException, UnmergedPathException,
          ConcurrentRefUpdateException, JGitInternalException, WrongRepositoryStateException {

    // do 4 commits
    Git git = new Git(db);
    git.commit().setMessage("initial commit").call();
    git.commit().setMessage("second commit").setCommitter(committer).call();
    git.commit().setMessage("third commit").setAuthor(author).call();
    git.commit().setMessage("fourth commit").setAuthor(author).setCommitter(committer).call();
    Iterable<RevCommit> commits = git.log().call();

    // check that all commits came in correctly
    PersonIdent defaultCommitter = new PersonIdent(db);
    PersonIdent expectedAuthors[] = new PersonIdent[] {defaultCommitter, committer, author, author};
    PersonIdent expectedCommitters[] =
        new PersonIdent[] {defaultCommitter, committer, defaultCommitter, committer};
    String expectedMessages[] =
        new String[] {"initial commit", "second commit", "third commit", "fourth commit"};
    int l = expectedAuthors.length - 1;
    for (RevCommit c : commits) {
      assertEquals(expectedAuthors[l].getName(), c.getAuthorIdent().getName());
      assertEquals(expectedCommitters[l].getName(), c.getCommitterIdent().getName());
      assertEquals(c.getFullMessage(), expectedMessages[l]);
      l--;
    }
    assertEquals(l, -1);
  }
  @Test
  public void testCommitRange()
      throws NoHeadException, NoMessageException, UnmergedPathException,
          ConcurrentRefUpdateException, JGitInternalException, WrongRepositoryStateException,
          IncorrectObjectTypeException, MissingObjectException {
    // do 4 commits and set the range to the second and fourth one
    Git git = new Git(db);
    git.commit().setMessage("first commit").call();
    RevCommit second = git.commit().setMessage("second commit").setCommitter(committer).call();
    git.commit().setMessage("third commit").setAuthor(author).call();
    RevCommit last =
        git.commit().setMessage("fourth commit").setAuthor(author).setCommitter(committer).call();
    Iterable<RevCommit> commits = git.log().addRange(second.getId(), last.getId()).call();

    // check that we have the third and fourth commit
    PersonIdent defaultCommitter = new PersonIdent(db);
    PersonIdent expectedAuthors[] = new PersonIdent[] {author, author};
    PersonIdent expectedCommitters[] = new PersonIdent[] {defaultCommitter, committer};
    String expectedMessages[] = new String[] {"third commit", "fourth commit"};
    int l = expectedAuthors.length - 1;
    for (RevCommit c : commits) {
      assertEquals(expectedAuthors[l].getName(), c.getAuthorIdent().getName());
      assertEquals(expectedCommitters[l].getName(), c.getCommitterIdent().getName());
      assertEquals(c.getFullMessage(), expectedMessages[l]);
      l--;
    }
    assertEquals(l, -1);
  }
 public PatchSetInfo get(RevWalk rw, RevCommit src, PatchSet.Id psi) throws IOException {
   rw.parseBody(src);
   PatchSetInfo info = new PatchSetInfo(psi);
   info.setSubject(src.getShortMessage());
   info.setMessage(src.getFullMessage());
   info.setAuthor(toUserIdentity(src.getAuthorIdent()));
   info.setCommitter(toUserIdentity(src.getCommitterIdent()));
   info.setRevId(src.getName());
   return info;
 }
 private PersonIdent getLatestPersonIdent(Collection<RevCommit> commits) {
   PersonIdent latest = null;
   for (RevCommit commit : commits) {
     PersonIdent ident = commit.getCommitterIdent();
     Date when = ident.getWhen();
     if (latest == null || when.after(latest.getWhen())) {
       latest = ident;
     }
   }
   return latest;
 }
  private String createChangeSetLabel(GitModelCommit commit) {
    String format = store.getString(UIPreferences.SYNC_VIEW_CHANGESET_LABEL_FORMAT);

    RevCommit baseCommit = commit.getBaseCommit();
    Map<String, String> bindings = new HashMap<String, String>();
    bindings.put(BINDING_CHANGESET_DATE, DATE_FORMAT.format(baseCommit.getAuthorIdent().getWhen()));
    bindings.put(BINDING_CHANGESET_AUTHOR, baseCommit.getAuthorIdent().getName());
    bindings.put(BINDING_CHANGESET_COMMITTER, baseCommit.getCommitterIdent().getName());
    bindings.put(BINDING_CHANGESET_SHORT_MESSAGE, baseCommit.getShortMessage());

    return formatName(format, bindings);
  }
 @Test
 public void submitWithFastForward() throws Exception {
   RevCommit oldHead = getRemoteHead();
   PushOneCommit.Result change = createChange();
   submit(change.getChangeId());
   RevCommit head = getRemoteHead();
   assertThat(head.getId()).isEqualTo(change.getCommitId());
   assertThat(head.getParent(0)).isEqualTo(oldHead);
   assertSubmitter(change.getChangeId(), 1);
   assertPersonEquals(admin.getIdent(), head.getAuthorIdent());
   assertPersonEquals(admin.getIdent(), head.getCommitterIdent());
 }
 private Git.Head getHead(final Repository repository) throws IOException {
   ObjectId revision = repository.resolve(Constants.HEAD);
   RevCommit commit = new RevWalk(repository).parseCommit(revision);
   Git.Head head =
       new Git.Head(
           revision.getName(),
           commit.getAuthorIdent().getName(),
           commit.getAuthorIdent().getEmailAddress(),
           commit.getCommitterIdent().getName(),
           commit.getCommitterIdent().getEmailAddress(),
           commit.getFullMessage());
   return head;
 }
Esempio n. 8
0
  private void createHeaderArea(Composite parent, FormToolkit toolkit, int span) {
    RevCommit commit = getCommit().getRevCommit();
    Composite top = toolkit.createComposite(parent);
    GridDataFactory.fillDefaults().grab(true, false).span(span, 1).applyTo(top);
    GridLayoutFactory.fillDefaults().numColumns(2).applyTo(top);

    Composite userArea = toolkit.createComposite(top);
    GridLayoutFactory.fillDefaults().spacing(2, 2).numColumns(1).applyTo(userArea);
    GridDataFactory.fillDefaults().grab(true, false).applyTo(userArea);

    PersonIdent author = commit.getAuthorIdent();
    if (author != null) createUserArea(userArea, toolkit, author, true);

    PersonIdent committer = commit.getCommitterIdent();
    if (committer != null && !committer.equals(author))
      createUserArea(userArea, toolkit, committer, false);

    int count = commit.getParentCount();
    if (count > 0) {
      Composite parents = toolkit.createComposite(top);
      GridLayoutFactory.fillDefaults().spacing(2, 2).numColumns(2).applyTo(parents);
      GridDataFactory.fillDefaults().grab(false, false).applyTo(parents);

      for (int i = 0; i < count; i++) {
        final RevCommit parentCommit = commit.getParent(i);
        toolkit
            .createLabel(parents, UIText.CommitEditorPage_LabelParent)
            .setForeground(toolkit.getColors().getColor(IFormColors.TB_TOGGLE));
        final Hyperlink link =
            toolkit.createHyperlink(
                parents, parentCommit.abbreviate(PARENT_LENGTH).name(), SWT.NONE);
        link.addHyperlinkListener(
            new HyperlinkAdapter() {

              public void linkActivated(HyperlinkEvent e) {
                try {
                  CommitEditor.open(
                      new RepositoryCommit(getCommit().getRepository(), parentCommit));
                  if ((e.getStateMask() & SWT.MOD1) != 0) getEditor().close(false);
                } catch (PartInitException e1) {
                  Activator.logError("Error opening commit editor", e1); // $NON-NLS-1$
                }
              }
            });
      }
    }

    createTagsArea(userArea, toolkit, 2);
  }
  @Test
  public void submitMultipleChanges() throws Exception {
    RevCommit initialHead = getRemoteHead();

    testRepo.reset(initialHead);
    PushOneCommit.Result change2 = createChange("Change 2", "b", "b");

    testRepo.reset(initialHead);
    PushOneCommit.Result change3 = createChange("Change 3", "c", "c");

    testRepo.reset(initialHead);
    PushOneCommit.Result change4 = createChange("Change 4", "d", "d");

    // Change 2 stays untouched.
    approve(change2.getChangeId());
    // Change 3 is a fast-forward, no need to merge.
    submit(change3.getChangeId());

    RevCommit tip = getRemoteLog().get(0);
    assertThat(tip.getShortMessage()).isEqualTo(change3.getCommit().getShortMessage());
    assertThat(tip.getParent(0).getId()).isEqualTo(initialHead.getId());
    assertPersonEquals(admin.getIdent(), tip.getAuthorIdent());
    assertPersonEquals(admin.getIdent(), tip.getCommitterIdent());

    // We need to merge change 4.
    submit(change4.getChangeId());

    tip = getRemoteLog().get(0);
    assertThat(tip.getParent(1).getShortMessage()).isEqualTo(change4.getCommit().getShortMessage());
    assertThat(tip.getParent(0).getShortMessage()).isEqualTo(change3.getCommit().getShortMessage());

    assertPersonEquals(admin.getIdent(), tip.getAuthorIdent());
    assertPersonEquals(serverIdent.get(), tip.getCommitterIdent());

    assertNew(change2.getChangeId());
  }
Esempio n. 10
0
 // format the output like in --pretty="%H<#>%aN<#>%at<#>%cN<#>%ct<#>%s
 private String printCommit(RevCommit c) {
   String out = "";
   PersonIdent author = c.getAuthorIdent();
   PersonIdent committer = c.getCommitterIdent();
   // long commitTime = (long)c.getCommitTime() == committer.getWhen().getTime() / 1000 (in
   // seconds)
   out +=
       ""
           + c.name()
           + GitWorks.log_sep
           + author.getName()
           + GitWorks.log_sep
           + author.getWhen().getTime()
           + GitWorks.log_sep
           + committer.getName()
           + GitWorks.log_sep
           + committer.getWhen().getTime()
           + GitWorks.log_sep
           + c.getShortMessage(); // c.getFullMessage(); c.getShortMessage();
   return out;
 }
Esempio n. 11
0
  private static void lastCommit(Git git, String path, AnyObjectId revId, JSONObject jsonObject) {
    JSONObject latestCommitObj = new JSONObject();
    JSONObject authorObj = new JSONObject();
    JSONObject committerObj = new JSONObject();
    Iterable<RevCommit> log = null;
    try {
      if (path != null) {
        log = git.log().addPath(path).setMaxCount(1).call();
      } else if (revId != null) {
        log = git.log().add(revId).setMaxCount(1).call();
      }
      Iterator<RevCommit> it = log.iterator();
      while (it.hasNext()) {
        RevCommit rev = (RevCommit) it.next();
        PersonIdent committer = rev.getCommitterIdent();
        committerObj.put("Name", committer.getName());
        committerObj.put("Email", committer.getEmailAddress());
        committerObj.put("Date", committer.getWhen().toString());

        PersonIdent author = rev.getAuthorIdent();
        authorObj.put("Name", author.getName());
        String authorEmail = author.getEmailAddress();
        authorObj.put("Email", authorEmail);
        authorObj.put("Date", author.getWhen().toString());

        latestCommitObj.put("Author", authorObj);
        latestCommitObj.put("Committer", committerObj);
        latestCommitObj.put("Message", rev.getFullMessage());
        latestCommitObj.put("SHA1", rev.getId().getName());
        latestCommitObj.put("AvatarURL", getImageLink(authorEmail));

        jsonObject.put("LastCommit", latestCommitObj);
      }
    } catch (GitAPIException e) {
    } catch (MissingObjectException e) {
    } catch (IncorrectObjectTypeException e) {
    } catch (JSONException e) {
    }
  }
Esempio n. 12
0
  private void createMessageArea(Composite parent, FormToolkit toolkit, int span) {
    Section messageSection = createSection(parent, toolkit, span);
    Composite messageArea = createSectionClient(messageSection, toolkit);

    messageSection.setText(UIText.CommitEditorPage_SectionMessage);

    RevCommit commit = getCommit().getRevCommit();
    String message = commit.getFullMessage();

    PersonIdent author = commit.getAuthorIdent();
    if (author != null) message = replaceSignedOffByLine(message, author);
    PersonIdent committer = commit.getCommitterIdent();
    if (committer != null) message = replaceSignedOffByLine(message, committer);

    SpellcheckableMessageArea textContent =
        new SpellcheckableMessageArea(messageArea, message, true, toolkit.getBorderStyle()) {

          @Override
          protected IAdaptable getDefaultTarget() {
            return new PlatformObject() {
              public Object getAdapter(Class adapter) {
                return Platform.getAdapterManager().getAdapter(getEditorInput(), adapter);
              }
            };
          }

          protected void createMarginPainter() {
            // Disabled intentionally
          }
        };
    if ((toolkit.getBorderStyle() & SWT.BORDER) == 0)
      textContent.setData(FormToolkit.KEY_DRAW_BORDER, FormToolkit.TEXT_BORDER);
    GridDataFactory.fillDefaults().hint(SWT.DEFAULT, 80).grab(true, true).applyTo(textContent);

    updateSectionClient(messageSection, messageArea, toolkit);
  }
Esempio n. 13
0
 /** @return current committer being blamed. */
 public PersonIdent getSourceCommitter() {
   RevCommit c = getSourceCommit();
   return c != null ? c.getCommitterIdent() : null;
 }
Esempio n. 14
0
 private void assertPageVersion(RevCommit commit, PageVersion version) {
   assertEquals(commit.getName(), version.getCommitName());
   assertEquals(commit.getCommitterIdent().getName(), version.getLastEditedBy());
   assertEquals(new Date(commit.getCommitTime() * 1000L), version.getLastEdited());
 }
Esempio n. 15
0
  @Override
  public Document execute(
      Repository repository, Git git, CallSpecification spec, DocumentWriter writer, Values values)
      throws GitAPIException, IOException {
    if (spec.parameterCount() == 0) {
      // This is the top-level "/branches" node
      writer.setPrimaryType(GitLexicon.TREES);

      // Generate the child references to the branches and tags. Branches are likely used more
      // often, so list them first...
      addBranchesAsChildren(git, spec, writer);
      addTagsAsChildren(git, spec, writer);
      addCommitsAsChildren(git, spec, writer, pageSize);

    } else if (spec.parameterCount() == 1) {
      // This is a particular branch/tag/commit node ...
      String branchOrTagOrObjectId = spec.parameter(0);
      ObjectId objId = resolveBranchOrTagOrCommitId(repository, branchOrTagOrObjectId);
      RevWalk walker = new RevWalk(repository);
      walker.setRetainBody(true); // we need to parse the commit for the top-level
      try {
        RevCommit commit = walker.parseCommit(objId);

        // Add the properties for this node ...
        String committer = commit.getCommitterIdent().getName();
        String author = commit.getAuthorIdent().getName();
        DateTime committed = values.dateFrom(commit.getCommitTime());
        writer.setPrimaryType(GitLexicon.FOLDER);
        writer.addProperty(JcrLexicon.CREATED, committed);
        writer.addProperty(JcrLexicon.CREATED_BY, committer);
        writer.addProperty(GitLexicon.OBJECT_ID, objId.name());
        writer.addProperty(GitLexicon.AUTHOR, author);
        writer.addProperty(GitLexicon.COMMITTER, committer);
        writer.addProperty(GitLexicon.COMMITTED, committed);
        writer.addProperty(GitLexicon.TITLE, commit.getShortMessage());
        writer.addProperty(
            GitLexicon.HISTORY,
            GitHistory.referenceToHistory(objId, branchOrTagOrObjectId, values));
        writer.addProperty(GitLexicon.DETAIL, GitCommitDetails.referenceToCommit(objId, values));

        // Add the top-level children of the directory ...
        addInformationForPath(repository, git, writer, commit, "", spec, values);

      } finally {
        walker.dispose();
      }

    } else {
      // This is a folder or file within the directory structure ...
      String branchOrTagOrObjectId = spec.parameter(0);
      String path = spec.parametersAsPath(1);
      ObjectId objId = resolveBranchOrTagOrCommitId(repository, branchOrTagOrObjectId);
      RevWalk walker = new RevWalk(repository);
      walker.setRetainBody(false); // we don't need top-level commit information
      try {
        // Get the commit information ...
        RevCommit commit = walker.parseCommit(objId);

        // Add the top-level children of the directory ...
        addInformationForPath(repository, git, writer, commit, path, spec, values);

      } finally {
        walker.dispose();
      }
    }
    return writer.document();
  }
Esempio n. 16
0
  protected void addInformationForPath(
      Repository repository,
      Git git,
      DocumentWriter writer,
      RevCommit commit,
      String path,
      CallSpecification spec,
      Values values)
      throws GitAPIException, IOException {
    // Make sure the path is in the canonical form we need ...
    if (path.startsWith("/")) {
      if (path.length() == 1) path = "";
      else path = path.substring(1);
    }

    // Now see if we're actually referring to the "jcr:content" node ...
    boolean isContentNode = false;
    if (path.endsWith(JCR_CONTENT_SUFFIX)) {
      isContentNode = true;
      path = path.substring(0, path.length() - JCR_CONTENT_SUFFIX.length());
    }

    // Create the TreeWalk that we'll use to navigate the files/directories ...
    final TreeWalk tw = new TreeWalk(repository);
    tw.addTree(commit.getTree());
    if ("".equals(path)) {
      // This is the top-level directory, so we don't need to pre-walk to find anything ...
      tw.setRecursive(false);
      while (tw.next()) {
        String childName = tw.getNameString();
        String childId = spec.childId(childName);
        writer.addChild(childId, childName);
      }
    } else {
      // We need to first find our path *before* we can walk the children ...
      PathFilter filter = PathFilter.create(path);
      tw.setFilter(filter);
      while (tw.next()) {
        if (filter.isDone(tw)) {
          break;
        } else if (tw.isSubtree()) {
          tw.enterSubtree();
        }
      }
      // Now that the TreeWalk is the in right location given by the 'path', we can get the
      if (tw.isSubtree()) {
        // The object at the 'path' is a directory, so go into it ...
        tw.enterSubtree();

        // Find the commit in which this folder was last modified ...
        // This may not be terribly efficient, but it seems to work faster on subsequent runs ...
        RevCommit folderCommit = git.log().addPath(path).call().iterator().next();

        // Add folder-related properties ...
        String committer = folderCommit.getCommitterIdent().getName();
        String author = folderCommit.getAuthorIdent().getName();
        DateTime committed = values.dateFrom(folderCommit.getCommitTime());
        writer.setPrimaryType(GitLexicon.FOLDER);
        writer.addProperty(JcrLexicon.CREATED, committed);
        writer.addProperty(JcrLexicon.CREATED_BY, committer);
        writer.addProperty(GitLexicon.OBJECT_ID, folderCommit.getId().name());
        writer.addProperty(GitLexicon.AUTHOR, author);
        writer.addProperty(GitLexicon.COMMITTER, committer);
        writer.addProperty(GitLexicon.COMMITTED, committed);
        writer.addProperty(GitLexicon.TITLE, folderCommit.getShortMessage());

        // And now walk the contents of the directory ...
        while (tw.next()) {
          String childName = tw.getNameString();
          String childId = spec.childId(childName);
          writer.addChild(childId, childName);
        }
      } else {
        // The path specifies a file (or a content node) ...

        // Find the commit in which this folder was last modified ...
        // This may not be terribly efficient, but it seems to work faster on subsequent runs ...
        RevCommit fileCommit = git.log().addPath(path).call().iterator().next();

        // Add file-related properties ...
        String committer = fileCommit.getCommitterIdent().getName();
        String author = fileCommit.getAuthorIdent().getName();
        DateTime committed = values.dateFrom(fileCommit.getCommitTime());
        if (isContentNode) {
          writer.setPrimaryType(GitLexicon.RESOURCE);
          writer.addProperty(JcrLexicon.LAST_MODIFIED, committed);
          writer.addProperty(JcrLexicon.LAST_MODIFIED_BY, committer);
          writer.addProperty(GitLexicon.OBJECT_ID, fileCommit.getId().name());
          writer.addProperty(GitLexicon.AUTHOR, author);
          writer.addProperty(GitLexicon.COMMITTER, committer);
          writer.addProperty(GitLexicon.COMMITTED, committed);
          writer.addProperty(GitLexicon.TITLE, fileCommit.getShortMessage());
          // Create the BinaryValue ...
          ObjectId fileObjectId = tw.getObjectId(0);
          ObjectLoader fileLoader = repository.open(fileObjectId);
          BinaryKey key = new BinaryKey(fileObjectId.getName());
          BinaryValue value = values.binaryFor(key, fileLoader.getSize());
          if (value == null) {
            // It wasn't found in the binary store ...
            if (fileLoader.isLarge()) {
              // Too large to hold in memory, so use the binary store (which reads the file
              // immediately) ...
              value = values.binaryFrom(fileLoader.openStream());
            } else {
              // This is small enough to fit into a byte[], but it still may be pretty big ...
              value =
                  new GitBinaryValue(
                      fileObjectId,
                      fileLoader,
                      connector.getSourceName(),
                      name,
                      connector.getMimeTypeDetector());
            }
          }
          writer.addProperty(JcrLexicon.DATA, value);
          if (connector.includeMimeType()) {
            try {
              String filename =
                  spec.parameter(spec.parameterCount() - 1); // the last is 'jcr:content'
              String mimeType = value.getMimeType(filename);
              if (mimeType != null) writer.addProperty(JcrLexicon.MIMETYPE, mimeType);
            } catch (RepositoryException e) {
              // do nothing
            } catch (IOException e) {
              // do nothing
            }
          }
        } else {
          writer.setPrimaryType(GitLexicon.FILE);
          writer.addProperty(JcrLexicon.CREATED, committed);
          writer.addProperty(JcrLexicon.CREATED_BY, committer);
          writer.addProperty(GitLexicon.OBJECT_ID, fileCommit.getId().name());
          writer.addProperty(GitLexicon.AUTHOR, author);
          writer.addProperty(GitLexicon.COMMITTER, committer);
          writer.addProperty(GitLexicon.COMMITTED, committed);
          writer.addProperty(GitLexicon.TITLE, fileCommit.getShortMessage());

          // Add the "jcr:content" child node ...
          String childId = spec.childId(JCR_CONTENT);
          writer.addChild(childId, JCR_CONTENT);
        }
      }
    }
  }