@Before
  public void setUp() throws Exception {

    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

    FileUtils.mkdir(new File(root.getLocation().toFile(), "Sub"), true);
    gitDir = new File(new File(root.getLocation().toFile(), "Sub"), Constants.DOT_GIT);

    repository = FileRepositoryBuilder.create(gitDir);
    repository.create();

    project = root.getProject(TEST_PROJECT);
    project.create(null);
    project.open(null);
    IProjectDescription description = project.getDescription();
    description.setLocation(root.getLocation().append(TEST_PROJECT_LOC));
    project.move(description, IResource.FORCE, null);

    project2 = root.getProject(TEST_PROJECT2);
    project2.create(null);
    project2.open(null);
    gitDir2 = new File(project2.getLocation().toFile().getAbsoluteFile(), Constants.DOT_GIT);
    repository2 = FileRepositoryBuilder.create(gitDir2);
    repository2.create();

    RepositoryMapping mapping = RepositoryMapping.create(project, gitDir);
    RepositoryMapping mapping2 = RepositoryMapping.create(project2, gitDir2);

    GitProjectData projectData = new GitProjectData(project);
    GitProjectData projectData2 = new GitProjectData(project2);
    projectData.setRepositoryMappings(Collections.singletonList(mapping));
    projectData.store();
    projectData2.setRepositoryMappings(Collections.singletonList(mapping2));
    projectData2.store();
    GitProjectData.add(project, projectData);
    GitProjectData.add(project2, projectData2);

    RepositoryProvider.map(project, GitProvider.class.getName());
    RepositoryProvider.map(project2, GitProvider.class.getName());

    JGitTestUtil.write(
        new File(repository.getWorkTree(), TEST_PROJECT + "/" + TEST_FILE), "Some data");
    JGitTestUtil.write(new File(repository2.getWorkTree(), TEST_FILE2), "Some other data");
    project.refreshLocal(IResource.DEPTH_INFINITE, null);
    project2.refreshLocal(IResource.DEPTH_INFINITE, null);
    git = new Git(repository);
    git.add().addFilepattern(".").call();
    git.commit().setMessage("Initial commit").call();

    git = new Git(repository2);
    git.add().addFilepattern(".").call();
    git.commit().setMessage("Initial commit").call();
    git.branchRename().setNewName("main").call();
  }
Пример #2
0
  @Test
  public void testKeyToSegmentsMethod() throws Exception {
    Object configOption =
        ReflectionUtils.callConstructor(
            ConfigOption.class,
            new Object[] {new URI(""), FileRepositoryBuilder.create(new File(""))});

    String[] segments =
        (String[])
            ReflectionUtils.callMethod(configOption, "keyToSegments", new Object[] {"a.b.c"});
    assertArrayEquals(new String[] {"a", "b", "c"}, segments);

    segments =
        (String[]) ReflectionUtils.callMethod(configOption, "keyToSegments", new Object[] {"a.c"});
    assertArrayEquals(new String[] {"a", null, "c"}, segments);

    segments =
        (String[])
            ReflectionUtils.callMethod(configOption, "keyToSegments", new Object[] {"a.b.c.d"});
    assertArrayEquals(new String[] {"a", "b.c", "d"}, segments);

    segments =
        (String[]) ReflectionUtils.callMethod(configOption, "keyToSegments", new Object[] {"a"});
    assertArrayEquals(null, segments);
  }
  @Test
  public void test() throws Exception {
    // prepare a new folder
    File localPath = File.createTempFile("TestGitRepository", "");
    localPath.delete();

    // create the directory
    Repository repository = FileRepositoryBuilder.create(new File(localPath, ".git"));
    repository.create(false);

    System.out.println("Having repository: " + repository.getDirectory());

    repository.close();
  }
Пример #4
0
  @Test
  public void testSegmentsToKeyMethod() throws Exception {
    Object configOption =
        ReflectionUtils.callConstructor(
            ConfigOption.class,
            new Object[] {new URI(""), FileRepositoryBuilder.create(new File(""))});

    String key =
        (String)
            ReflectionUtils.callMethod(
                configOption, "segmentsToKey", new Object[] {new String[] {"a", "b", "c"}});
    assertEquals("a.b.c", key);

    key =
        (String)
            ReflectionUtils.callMethod(
                configOption, "segmentsToKey", new Object[] {new String[] {"a", null, "c"}});
    assertEquals("a.c", key);

    key =
        (String)
            ReflectionUtils.callMethod(
                configOption, "segmentsToKey", new Object[] {new String[] {"a", "b.c", "d"}});
    assertEquals("a.b.c.d", key);

    key =
        (String)
            ReflectionUtils.callMethod(
                configOption, "segmentsToKey", new Object[] {new String[] {"a", "b"}});
    assertEquals(null, key);

    key =
        (String)
            ReflectionUtils.callMethod(
                configOption, "segmentsToKey", new Object[] {new String[] {"a", "b", "c", "d"}});
    assertEquals(null, key);
  }
Пример #5
0
  @Test
  public void shareProjectWithAlreadyCreatedRepos()
      throws IOException, InterruptedException, JGitInternalException, GitAPIException {
    Repository repo1 =
        FileRepositoryBuilder.create(
            new File(new File(createProject(projectName1)).getParent(), ".git"));
    repo1.create();
    repo1.close();
    Repository repo2 = FileRepositoryBuilder.create(new File(createProject(projectName2), ".git"));
    repo2.create();
    repo2.close();
    Repository repo3 = FileRepositoryBuilder.create(new File(createProject(projectName3), ".git"));
    repo3.create();
    Git git = new Git(repo3);
    git.add().addFilepattern(".").call();
    git.commit()
        .setAuthor("A U Thior", "*****@*****.**")
        .setMessage("Created Project 3")
        .call();
    repo3.close();

    ExistingOrNewPage existingOrNewPage =
        sharingWizard.openWizard(projectName1, projectName2, projectName3);
    existingOrNewPage.setInternalMode(true);

    // initial state
    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    String projectPath1 = workspace.getRoot().getProject(projectName1).getLocation().toOSString();
    String projectPath2 = workspace.getRoot().getProject(projectName2).getLocation().toOSString();
    String projectPath3 = workspace.getRoot().getProject(projectName3).getLocation().toOSString();
    existingOrNewPage.assertContents(
        new Row[] {
          new Row(true, projectName1, projectPath1, ".." + File.separator + ".git"),
          new Row(
              false,
              projectName2,
              projectPath2,
              "",
              new Row[] {
                new Row(false, ".", "", ".git"),
                new Row(false, "..", "", ".." + File.separator + ".git")
              }),
          new Row(
              false,
              projectName3,
              projectPath3,
              "",
              new Row[] {
                new Row(true, ".", "", ".git"),
                new Row(false, "..", "", ".." + File.separator + ".git")
              })
        },
        "");

    bot.tree().getAllItems()[1].getItems()[0].check();
    existingOrNewPage.assertEnabling(false, false, true);
    bot.button("Finish").click();
    Thread.sleep(1000);
    assertEquals(
        repo1.getDirectory().getAbsolutePath(),
        RepositoryMapping.getMapping(workspace.getRoot().getProject(projectName1))
            .getRepository()
            .getDirectory()
            .toString());
    assertEquals(
        repo2.getDirectory().getAbsolutePath(),
        RepositoryMapping.getMapping(workspace.getRoot().getProject(projectName2))
            .getRepository()
            .getDirectory()
            .toString());
  }
Пример #6
0
  @Override
  protected IStatus performJob() {
    Repository db = null;
    try {
      File gitDir = GitUtils.getGitDir(path);
      db = FileRepositoryBuilder.create(gitDir);
      Git git = Git.wrap(db);
      List<Ref> branchRefs = git.branchList().call();
      List<Branch> branches = new ArrayList<Branch>(branchRefs.size());
      for (Ref ref : branchRefs) {
        if (nameFilter != null && !nameFilter.equals("")) {
          String shortName = Repository.shortenRefName(ref.getName());
          if (shortName.toLowerCase().contains(nameFilter.toLowerCase())) {
            branches.add(new Branch(cloneLocation, db, ref));
          }
        } else {
          branches.add(new Branch(cloneLocation, db, ref));
        }
      }
      Collections.sort(branches, Branch.COMPARATOR);
      JSONObject result = new JSONObject();
      JSONArray children = new JSONArray();
      int firstBranch = pageSize > 0 ? pageSize * (pageNo - 1) : 0;
      int lastBranch = pageSize > 0 ? firstBranch + pageSize - 1 : branches.size() - 1;
      lastBranch = lastBranch > branches.size() - 1 ? branches.size() - 1 : lastBranch;
      if (pageNo > 1 && baseLocation != null) {
        String prev = baseLocation + "?page=" + (pageNo - 1) + "&pageSize=" + pageSize;
        if (nameFilter != null && !nameFilter.equals("")) {
          prev += "&filter=" + GitUtils.encode(nameFilter);
        }
        if (commitsSize > 0) {
          prev += "&" + GitConstants.KEY_TAG_COMMITS + "=" + commitsSize;
        }
        result.put(ProtocolConstants.KEY_PREVIOUS_LOCATION, prev);
      }
      if (lastBranch < branches.size() - 1) {
        String next = baseLocation + "?page=" + (pageNo + 1) + "&pageSize=" + pageSize;
        if (nameFilter != null && !nameFilter.equals("")) {
          next += "&filter=" + GitUtils.encode(nameFilter);
        }
        if (commitsSize > 0) {
          next += "&" + GitConstants.KEY_TAG_COMMITS + "=" + commitsSize;
        }
        result.put(ProtocolConstants.KEY_NEXT_LOCATION, next);
      }
      for (int i = firstBranch; i <= lastBranch; i++) {
        Branch branch = branches.get(i);
        if (commitsSize == 0) {
          children.put(branch.toJSON());
        } else {
          String branchName = branch.getName(true, false);
          ObjectId toObjectId = db.resolve(branchName);
          Ref toRefId = db.getRef(branchName);
          if (toObjectId == null) {
            String msg = NLS.bind("No ref or commit found: {0}", branchName);
            return new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_NOT_FOUND, msg, null);
          }
          toObjectId = getCommitObjectId(db, toObjectId);

          Log log = null;
          // single commit is requested and we already know it, no need for LogCommand
          if (commitsSize == 1 && toObjectId instanceof RevCommit) {
            log =
                new Log(
                    cloneLocation,
                    db,
                    Collections.singleton((RevCommit) toObjectId),
                    null,
                    null,
                    toRefId);
          } else {
            LogCommand lc = git.log();
            // set the commit range
            lc.add(toObjectId);
            lc.setMaxCount(this.commitsSize);
            Iterable<RevCommit> commits = lc.call();
            log = new Log(cloneLocation, db, commits, null, null, toRefId);
          }
          log.setPaging(1, commitsSize);
          children.put(branch.toJSON(log.toJSON()));
        }
      }
      result.put(ProtocolConstants.KEY_CHILDREN, children);
      result.put(ProtocolConstants.KEY_TYPE, Branch.TYPE);
      return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK, result);
    } catch (Exception e) {
      String msg = NLS.bind("An error occured when listing branches for {0}", path);
      return new Status(IStatus.ERROR, GitActivator.PI_GIT, msg, e);
    } finally {
      if (db != null) {
        db.close();
      }
    }
  }
Пример #7
0
 @Override
 protected IStatus performJob() {
   Repository db = null;
   try {
     // list all tags
     File gitDir = GitUtils.getGitDir(path);
     db = FileRepositoryBuilder.create(gitDir);
     Git git = Git.wrap(db);
     List<Ref> refs = git.tagList().call();
     JSONObject result = new JSONObject();
     List<Tag> tags = new ArrayList<Tag>();
     for (Ref ref : refs) {
       if (nameFilter != null && !nameFilter.equals("")) {
         String shortName = Repository.shortenRefName(ref.getName());
         if (shortName.toLowerCase().contains(nameFilter.toLowerCase())) {
           Tag tag = new Tag(cloneLocation, db, ref);
           tags.add(tag);
         }
       } else {
         Tag tag = new Tag(cloneLocation, db, ref);
         tags.add(tag);
       }
     }
     Collections.sort(tags, Tag.COMPARATOR);
     JSONArray children = new JSONArray();
     int firstTag = pageSize > 0 ? pageSize * (pageNo - 1) : 0;
     int lastTag = pageSize > 0 ? firstTag + pageSize - 1 : tags.size() - 1;
     lastTag = lastTag > tags.size() - 1 ? tags.size() - 1 : lastTag;
     if (pageNo > 1 && baseLocation != null) {
       String prev = baseLocation + "?page=" + (pageNo - 1) + "&pageSize=" + pageSize;
       if (nameFilter != null && !nameFilter.equals("")) {
         prev += "&filter=" + GitUtils.encode(nameFilter);
       }
       if (commitsSize > 0) {
         prev += "&" + GitConstants.KEY_TAG_COMMITS + "=" + commitsSize;
       }
       result.put(ProtocolConstants.KEY_PREVIOUS_LOCATION, prev);
     }
     if (lastTag < tags.size() - 1) {
       String next = baseLocation + "?page=" + (pageNo + 1) + "&pageSize=" + pageSize;
       if (nameFilter != null && !nameFilter.equals("")) {
         next += "&filter=" + GitUtils.encode(nameFilter);
       }
       if (commitsSize > 0) {
         next += "&" + GitConstants.KEY_TAG_COMMITS + "=" + commitsSize;
       }
       result.put(ProtocolConstants.KEY_NEXT_LOCATION, next);
     }
     for (int i = firstTag; i <= lastTag; i++) {
       Tag tag = tags.get(i);
       if (this.commitsSize == 0) {
         children.put(tag.toJSON());
       } else {
         // add info about commits if requested
         LogCommand lc = git.log();
         String toCommitName = tag.getRevCommitName();
         ObjectId toCommitId = db.resolve(toCommitName);
         Ref toCommitRef = db.getRef(toCommitName);
         toCommitId = getCommitObjectId(db, toCommitId);
         lc.add(toCommitId);
         lc.setMaxCount(this.commitsSize);
         Iterable<RevCommit> commits = lc.call();
         Log log = new Log(cloneLocation, db, commits, null, null, toCommitRef);
         log.setPaging(1, commitsSize);
         children.put(tag.toJSON(log.toJSON()));
       }
     }
     result.put(ProtocolConstants.KEY_CHILDREN, children);
     result.put(ProtocolConstants.KEY_TYPE, Tag.TYPE);
     return new ServerStatus(Status.OK_STATUS, HttpServletResponse.SC_OK, result);
   } catch (Exception e) {
     String msg = NLS.bind("An error occured when listing tags for {0}", path);
     return new Status(IStatus.ERROR, GitActivator.PI_GIT, msg, e);
   } finally {
     if (db != null) {
       db.close();
     }
   }
 }