Exemplo n.º 1
0
  @Test
  public void getMarkdown() throws IOException, GitAPIException {
    register(globalRepoManager.createProjectCentralRepository(PROJECT, USER));
    ILockedRepository repo =
        globalRepoManager.createProjectBranchRepository(PROJECT, BRANCH_1, null);
    register(repo);

    Page page1 = Page.fromText("title", UUID.randomUUID().toString()); // $NON-NLS-1$
    pageStore.savePage(PROJECT, BRANCH_1, "home", page1, null, USER); // $NON-NLS-1$
    RevCommit commit1 = CommitUtils.getLastCommit(repo.r(), "pages/home.page"); // $NON-NLS-1$
    Page page2 = Page.fromText("title", UUID.randomUUID().toString()); // $NON-NLS-1$
    pageStore.savePage(PROJECT, BRANCH_1, "home", page2, null, USER); // $NON-NLS-1$
    RevCommit commit2 = CommitUtils.getLastCommit(repo.r(), "pages/home.page"); // $NON-NLS-1$
    Page page3 = Page.fromText("title", UUID.randomUUID().toString()); // $NON-NLS-1$
    pageStore.savePage(PROJECT, BRANCH_1, "home", page3, null, USER); // $NON-NLS-1$
    RevCommit commit3 = CommitUtils.getLastCommit(repo.r(), "pages/home.page"); // $NON-NLS-1$

    Map<String, String> result =
        pageStore.getMarkdown(
            PROJECT,
            BRANCH_1,
            "home", //$NON-NLS-1$
            Sets.newHashSet(
                "latest",
                "previous",
                commit2.getName(),
                commit1.getName())); // $NON-NLS-1$ //$NON-NLS-2$
    assertEquals(commit3.getName(), result.get("latest")); // $NON-NLS-1$
    assertEquals(commit2.getName(), result.get("previous")); // $NON-NLS-1$
    assertEquals(((PageTextData) page2.getData()).getText(), result.get(commit2.getName()));
    assertEquals(((PageTextData) page1.getData()).getText(), result.get(commit1.getName()));
  }
Exemplo n.º 2
0
 protected String defaultObjectId(Git git, String objectId) {
   if (objectId == null || objectId.trim().length() == 0) {
     RevCommit commit = CommitUtils.getHead(git.getRepository());
     objectId = commit.getName();
   }
   return objectId;
 }
Exemplo n.º 3
0
  protected List<CommitInfo> doHistory(
      Git git, String branch, String objectId, String path, int limit) {
    Repository r = git.getRepository();

    CommitFinder finder = new CommitFinder(r);
    CommitListFilter block = new CommitListFilter();
    if (Strings.isNotBlank(path)) {
      finder.setFilter(PathFilterUtils.and(path));
    }
    finder.setFilter(block);

    if (limit > 0) {
      finder.setFilter(new CommitLimitFilter(100).setStop(true));
    }
    if (Strings.isNotBlank(objectId)) {
      finder.findFrom(objectId);
    } else {
      if (Strings.isNotBlank(branch)) {
        RevCommit base = CommitUtils.getBase(r, branch);
        finder.findFrom(base);
      } else {
        finder.find();
      }
    }
    List<RevCommit> commits = block.getCommits();
    List<CommitInfo> results = new ArrayList<CommitInfo>();
    for (RevCommit entry : commits) {
      CommitInfo commitInfo = createCommitInfo(entry);
      results.add(commitInfo);
    }
    return results;
  }
Exemplo n.º 4
0
  @Test
  public void listPageVersions() throws IOException, GitAPIException {
    register(globalRepoManager.createProjectCentralRepository(PROJECT, USER));
    ILockedRepository repo =
        globalRepoManager.createProjectBranchRepository(PROJECT, BRANCH_1, null);
    register(repo);

    saveRandomPage(BRANCH_1, "home"); // $NON-NLS-1$
    RevCommit commit1 = CommitUtils.getLastCommit(repo.r(), "pages/home.page"); // $NON-NLS-1$
    saveRandomPage(BRANCH_1, "home/foo"); // $NON-NLS-1$
    saveRandomPage(BRANCH_1, "home"); // $NON-NLS-1$
    RevCommit commit2 = CommitUtils.getLastCommit(repo.r(), "pages/home.page"); // $NON-NLS-1$
    saveRandomPage(BRANCH_1, "home/bar"); // $NON-NLS-1$
    saveRandomPage(BRANCH_1, "home"); // $NON-NLS-1$
    RevCommit commit3 = CommitUtils.getLastCommit(repo.r(), "pages/home.page"); // $NON-NLS-1$
    saveRandomPage(BRANCH_1, "home/baz"); // $NON-NLS-1$

    List<PageVersion> versions =
        pageStore.listPageVersions(PROJECT, BRANCH_1, "home"); // $NON-NLS-1$
    assertEquals(3, versions.size());
    assertPageVersion(commit3, versions.get(0));
    assertPageVersion(commit2, versions.get(1));
    assertPageVersion(commit1, versions.get(2));
  }
Exemplo n.º 5
0
  public String getProjectVersion(File repoDir) throws IOException, GitAPIException {
    Git git = Git.open(repoDir);
    Repository repo = git.getRepository();

    // Find base commit between current branch and "master":
    String branch = repo.getBranch();
    RevCommit base = CommitUtils.getBase(repo, "master", branch);
    CommitCountFilter count = new CommitCountFilter();
    CommitFinder finder = new CommitFinder(repo).setFilter(count);
    finder.findBetween(branch, base);
    long commitsSinceBase = count.getCount();

    // Find tags in "master" before base commit:
    RevWalk rw = new RevWalk(repo);
    rw.markStart(base);
    rw.setRetainBody(false);
    Ref master = repo.getRef("master");
    List<Ref> masterAsList = Arrays.asList(master);
    List<Ref> tags = git.tagList().call();
    Map<RevCommit, Ref> masterTags = new HashMap<RevCommit, Ref>();
    for (Ref tag : tags) {
      tag = repo.peel(tag);
      ObjectId commitID = tag.getPeeledObjectId();
      if (commitID == null) continue;
      RevCommit commit = rw.parseCommit(commitID);
      // Only remember tags reachable from "master":
      if (!RevWalkUtils.findBranchesReachableFrom(commit, rw, masterAsList).isEmpty()) {
        masterTags.put(commit, tag);
      }
    }

    // Find the shortest distance in commits between base tag in "master":
    long commitsBetweenBaseAndTag = Long.MAX_VALUE;
    String tagName = "";
    for (RevCommit tagCommit : masterTags.keySet()) {
      count.reset();
      finder.findBetween(base, tagCommit);
      if (count.getCount() < commitsBetweenBaseAndTag) {
        commitsBetweenBaseAndTag = count.getCount();
        tagName = masterTags.get(tagCommit).getName();
      }
    }
    if (commitsBetweenBaseAndTag == Long.MAX_VALUE) {
      // If no tag, get total number of commits:
      commitsBetweenBaseAndTag = repo.getRefDatabase().getRefs("").size();
    }
    long commitsSinceLastMasterTag = commitsSinceBase + commitsBetweenBaseAndTag;

    // Construct version string:
    String version = branch.equals("master") ? "" : (branch + "-");
    if (tagName.startsWith("refs/tags/")) {
      tagName = tagName.substring("refs/tags/".length());
    }
    // v1.1 -> 1.1
    if (tagName.matches("v\\d+.*")) {
      tagName = tagName.substring(1);
    }
    if (tagName.isEmpty()) {
      version = "0";
    }
    version += tagName + ((!tagonly) ? "." + commitsSinceLastMasterTag : "");

    return version;
  }
Exemplo n.º 6
0
  protected String doDiff(Git git, String objectId, String baseObjectId, String path) {
    Repository r = git.getRepository();
    /*
            RevCommit commit = JGitUtils.getCommit(r, objectId);

            ObjectId current;
            if (isNotBlank(objectId)) {
                current = BlobUtils.getId(r, objectId, blobPath);
            } else {
                current = CommitUtils.getHead(r).getId();
            }
            ObjectId previous;
            if (isNotBlank(baseObjectId)) {
                previous = BlobUtils.getId(r, baseObjectId, blobPath);
            } else {
                RevCommit revCommit = CommitUtils.getCommit(r, current);
                RevCommit[] parents = revCommit.getParents();
                if (parents.length == 0) {
                    throw new IllegalArgumentException("No parent commits!");
                } else {
                    previous = parents[0];
                }
            }
            Collection<Edit> changes = BlobUtils.diff(r, previous, current);

            // no idea how to format Collection<Edit> :)

    */

    RevCommit commit;
    if (Strings.isNotBlank(objectId)) {
      commit = CommitUtils.getCommit(r, objectId);
    } else {
      commit = CommitUtils.getHead(r);
    }
    RevCommit baseCommit = null;
    if (Strings.isNotBlank(baseObjectId)) {
      baseCommit = CommitUtils.getCommit(r, baseObjectId);
    }

    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    RawTextComparator cmp = RawTextComparator.DEFAULT;
    DiffFormatter formatter = new DiffFormatter(buffer);
    formatter.setRepository(r);
    formatter.setDiffComparator(cmp);
    formatter.setDetectRenames(true);

    RevTree commitTree = commit.getTree();
    RevTree baseTree;
    try {
      if (baseCommit == null) {
        if (commit.getParentCount() > 0) {
          final RevWalk rw = new RevWalk(r);
          RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
          rw.dispose();
          baseTree = parent.getTree();
        } else {
          // FIXME initial commit. no parent?!
          baseTree = commitTree;
        }
      } else {
        baseTree = baseCommit.getTree();
      }

      List<DiffEntry> diffEntries = formatter.scan(baseTree, commitTree);
      if (path != null && path.length() > 0) {
        for (DiffEntry diffEntry : diffEntries) {
          if (diffEntry.getNewPath().equalsIgnoreCase(path)) {
            formatter.format(diffEntry);
            break;
          }
        }
      } else {
        formatter.format(diffEntries);
      }
      formatter.flush();
      return buffer.toString();
    } catch (IOException e) {
      throw new RuntimeIOException(e);
    }
  }
Exemplo n.º 7
0
 protected String doGetHead(Git git) {
   RevCommit commit = CommitUtils.getHead(git.getRepository());
   return commit.getName();
 }