@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);
  }
Пример #2
0
  /*
   * Use the binary git repo and/or the remote service to figure out
   * the new commits made since the last pull on source repository.
   */
  public void findNewCommits() throws GitException {

    // get the history from binary repository
    Git bingit = Git.wrap(binaryRepository);
    RevWalk binwalk = new RevWalk(binaryRepository);

    Iterable<RevCommit> logs;
    try {
      logs = bingit.log().call();
      Iterator<RevCommit> i = logs.iterator();

      while (i.hasNext()) {
        RevCommit commit = binwalk.parseCommit(i.next());
        System.out.println(commit.getFullMessage());
      }

    } catch (NoHeadException e) {
      // TODO Auto-generated catch block
      throw new GitException(e);
    } catch (GitAPIException e) {
      throw new GitException(e);
    } catch (MissingObjectException e) {
      throw new GitException(e);
    } catch (IncorrectObjectTypeException e) {
      throw new GitException(e);
    } catch (IOException e) {
      throw new GitException(e);
    }
  }
  @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);
  }
Пример #4
0
  @Test
  public void logAllCommits() throws Exception {
    List<RevCommit> commits = new ArrayList<RevCommit>();
    Git git = Git.wrap(db);

    writeTrashFile("Test.txt", "Hello world");
    git.add().addFilepattern("Test.txt").call();
    commits.add(git.commit().setMessage("initial commit").call());

    git.branchCreate().setName("branch1").call();
    Ref checkedOut = git.checkout().setName("branch1").call();
    assertEquals("refs/heads/branch1", checkedOut.getName());
    writeTrashFile("Test1.txt", "Hello world!");
    git.add().addFilepattern("Test1.txt").call();
    commits.add(git.commit().setMessage("branch1 commit").call());

    checkedOut = git.checkout().setName("master").call();
    assertEquals("refs/heads/master", checkedOut.getName());
    writeTrashFile("Test2.txt", "Hello world!!");
    git.add().addFilepattern("Test2.txt").call();
    commits.add(git.commit().setMessage("branch1 commit").call());

    Iterator<RevCommit> log = git.log().all().call().iterator();
    assertTrue(log.hasNext());
    assertTrue(commits.contains(log.next()));
    assertTrue(log.hasNext());
    assertTrue(commits.contains(log.next()));
    assertTrue(log.hasNext());
    assertTrue(commits.contains(log.next()));
    assertFalse(log.hasNext());
  }
Пример #5
0
  @Test
  public void logAllCommitsWithSkipAndMaxCount() throws Exception {
    Git git = Git.wrap(db);
    List<RevCommit> commits = createCommits(git);

    Iterator<RevCommit> log = git.log().all().setSkip(1).setMaxCount(1).call().iterator();
    assertTrue(log.hasNext());
    RevCommit commit = log.next();
    assertTrue(commits.contains(commit));
    assertEquals("commit#2", commit.getShortMessage());
    assertFalse(log.hasNext());
  }
  @Test
  public void testCommitAmend()
      throws NoHeadException, NoMessageException, UnmergedPathException,
          ConcurrentRefUpdateException, JGitInternalException, WrongRepositoryStateException {
    Git git = new Git(db);
    git.commit().setMessage("first comit").call(); // typo
    git.commit().setAmend(true).setMessage("first commit").call();

    Iterable<RevCommit> commits = git.log().call();
    int c = 0;
    for (RevCommit commit : commits) {
      assertEquals("first commit", commit.getFullMessage());
      c++;
    }
    assertEquals(1, c);
  }
  @Test
  public void testRemote() throws Exception {

    File binaryRepoFolder = new File("D:\\dev\\rgiroti_search_raptor\\.search_raptor");
    Git git = Git.open(binaryRepoFolder);
    final Iterable<RevCommit> revCommits = git.log().call();
    for (RevCommit revCommit : revCommits) {
      System.out.println(
          revCommit.getName() + revCommit.getFullMessage() + revCommit.getCommitTime());
    }

    // final org.eclipse.jgit.lib.Repository binRepo = new
    // org.eclipse.jgit.storage.file.FileRepository(binaryRepoFolder);
    // final RevWalk binRepoRevWalk = new RevWalk(binRepo);

    // final ObjectId binRepoResolve = binRepo.resolve(Constants.HEAD);
    // final RevCommit binRepoResolveCommitRev = git.log().call().iterator().next();
    // final String binRepoResolveCommitHash = binRepoResolveCommitRev.getName();

    // final String binRepoBranchname = git.getRepository().getBranch();
  }
 // try to work with Commands after command has been invoked. Should throw
 // exceptions
 @Test
 public void testMultipleInvocations()
     throws NoHeadException, ConcurrentRefUpdateException, NoMessageException,
         UnmergedPathException, JGitInternalException, WrongRepositoryStateException {
   Git git = new Git(db);
   CommitCommand commitCmd = git.commit();
   commitCmd.setMessage("initial commit").call();
   try {
     // check that setters can't be called after invocation
     commitCmd.setAuthor(author);
     fail("didn't catch the expected exception");
   } catch (IllegalStateException e) {
     // expected
   }
   LogCommand logCmd = git.log();
   logCmd.call();
   try {
     // check that call can't be called twice
     logCmd.call();
     fail("didn't catch the expected exception");
   } catch (IllegalStateException e) {
     // expected
   }
 }
Пример #9
0
  public void createBinaryRepository() throws IOException, GitException, MapServiceException {
    // check whether "binary repository" exists
    if (isBinaryRepositoryAvailable()) throw new GitException("Repository already exists");

    // find the name of the "source repository"
    // String sourceRepoName = getRepositoryName();

    // find where ".git" folder is found
    File f = sourceRepository.getDirectory();
    File sourceRepoFolder = f.getParentFile();

    String sourceRepoFolderName = f.getParentFile().getName();

    // calculate binary repository folder
    File parent = f.getParentFile().getParentFile();
    File binaryRepoFolder = new File(parent, ("." + sourceRepoFolderName));

    // create binary repository folder
    FileUtils.mkdir(binaryRepoFolder, true);

    // initialize "git" repository
    InitCommand initCmd = Git.init();
    initCmd.setDirectory(binaryRepoFolder);
    Git binaryRepo = null;
    try {
      System.out.println("initializing bare repository");
      binaryRepo = initCmd.call();
    } catch (GitAPIException e) {
      throw new GitException("unable to initialize repository", e);
    }

    System.out.println("adding readme.md file");
    createReadMeFile(binaryRepoFolder);

    // get "status"
    StatusCommand statusC = binaryRepo.status();
    Collection<String> toadd = GitUtils.getFilesToStage(statusC);

    // add "readme" file to staging
    if (toadd.size() > 0) {
      AddCommand add = binaryRepo.add();
      for (String file : toadd) {
        add.addFilepattern(file);
      }
      try {
        add.call();
        CommitCommand commit = binaryRepo.commit();
        commit.setMessage("initial commit");
        System.out.println("performing first commit");
        commit.call();
      } catch (NoFilepatternException e) {
        throw new GitException("unable to add file(s)", e);
      } catch (GitAPIException e) {
        throw new GitException("Unable to add or commit", e);
      }
    }

    // Calculate the remote url for binary repository
    String remoteUrl = calculateBinaryRepositoryUrl();

    // TODO: check whether the remote exists, if not create it, else fail
    GitHub github = new GitHubClient().getGithub();
    GHOrganization githubOrg = github.getOrganization("Binary");
    GHRepository repository = githubOrg.getRepository(GitUtils.getRepositoryName(remoteUrl));

    if (repository == null) {

      System.out.println("creating remote repository : " + remoteUrl);
      GHRepository repo =
          githubOrg.createRepository(
              GitUtils.getRepositoryName(remoteUrl),
              "Binary repository",
              "https://github.scm.corp.ebay.com",
              "Owners",
              true);

      System.out.println(repo.getUrl() + " created successfully ");

    } else {
      // fail, it shouldn't come here
    }

    // add "remote" repository
    StoredConfig config = binaryRepo.getRepository().getConfig();
    config.setString("remote", "origin", "url", remoteUrl);
    System.out.println("adding remote origin " + remoteUrl);
    config.save();

    // get "status"
    StatusCommand stat = binaryRepo.status();
    Collection<String> filesToAdd = GitUtils.getFilesToStage(stat);

    // add files to "staging"
    if (filesToAdd.size() > 0) {
      AddCommand addCmd = binaryRepo.add();
      for (String file : filesToAdd) {
        addCmd.addFilepattern(file);
      }
      try {
        addCmd.call();
      } catch (NoFilepatternException e) {
        throw new GitException("unable to add files", e);
      } catch (GitAPIException e) {
        throw new GitException("unable to add files", e);
      }
    }

    // commit
    System.out.println("commiting the files");
    CommitCommand commit = binaryRepo.commit();
    // add the 'source' repository git-url, commit-id and branch
    commit.setMessage("adding readme.md file");

    try {
      commit.call();
    } catch (NoHeadException e) {
      throw new GitException("unable to commit", e);
    } catch (NoMessageException e) {
      throw new GitException("unable to commit", e);
    } catch (UnmergedPathsException e) {
      throw new GitException("unable to commit", e);
    } catch (ConcurrentRefUpdateException e) {
      throw new GitException("unable to commit", e);
    } catch (WrongRepositoryStateException e) {
      throw new GitException("unable to commit", e);
    } catch (GitAPIException e) {
      throw new GitException("unable to commit", e);
    }

    // push
    System.out.println("pushing to remote");
    PushCommand push = binaryRepo.push();
    try {
      push.call();
    } catch (InvalidRemoteException e) {
      throw new GitException("unable to push", e);
    } catch (TransportException e) {
      throw new GitException("unable to push", e);
    } catch (GitAPIException e) {
      throw new GitException("unable to push", e);
    }

    // read the branch from "source" repository
    String branchname = sourceRepository.getBranch();

    // create a "branch"
    if (!branchname.toLowerCase().equals("master")) {
      CreateBranchCommand branchCmd = binaryRepo.branchCreate();
      branchCmd.setName(branchname);
      try {
        // create branch
        branchCmd.call();

        // checkout the branch
        CheckoutCommand checkout = binaryRepo.checkout();
        checkout.setName(branchname);
        checkout.call();
      } catch (RefAlreadyExistsException e) {
        throw new GitException("unable to create a branch", e);
      } catch (RefNotFoundException e) {
        throw new GitException("unable to create a branch", e);
      } catch (InvalidRefNameException e) {
        throw new GitException("unable to create a branch", e);
      } catch (GitAPIException e) {
        throw new GitException("unable to create a branch", e);
      }
    }

    // find the "localobr" folders and exclude them during copy
    List<String> excludes = new ArrayList<String>();
    Collection<File> excludeFiles =
        FileUtil.findDirectoriesThatEndWith(sourceRepoFolder, "localobr");
    for (File file : excludeFiles) {
      excludes.add(file.getCanonicalPath());
    }

    // copy the classes
    System.out.println("copying binary files");
    copyBinaryFolders("target", excludes, binaryRepoFolder);

    // get "status"
    StatusCommand statusCmd = binaryRepo.status();
    Collection<String> tobeAdded = GitUtils.getFilesToStage(statusCmd);

    // add files to "staging"
    if (tobeAdded.size() > 0) {
      AddCommand addCmd = binaryRepo.add();
      for (String file : tobeAdded) {
        addCmd.addFilepattern(file);
      }
      try {
        addCmd.call();
      } catch (NoFilepatternException e) {
        throw new GitException("unable to add files", e);
      } catch (GitAPIException e) {
        throw new GitException("unable to add files", e);
      }
    }

    // commit
    System.out.println("commiting the files");
    CommitCommand commit1 = binaryRepo.commit();
    commit1.setMessage("saving the files");

    try {
      commit1.call();
    } catch (NoHeadException e) {
      throw new GitException("unable to commit", e);
    } catch (NoMessageException e) {
      throw new GitException("unable to commit", e);
    } catch (UnmergedPathsException e) {
      throw new GitException("unable to commit", e);
    } catch (ConcurrentRefUpdateException e) {
      throw new GitException("unable to commit", e);
    } catch (WrongRepositoryStateException e) {
      throw new GitException("unable to commit", e);
    } catch (GitAPIException e) {
      throw new GitException("unable to commit", e);
    }

    // push
    System.out.println("pushing to remote");
    PushCommand pushCmd = binaryRepo.push();
    try {
      pushCmd.call();
    } catch (InvalidRemoteException e) {
      throw new GitException("unable to push", e);
    } catch (TransportException e) {
      throw new GitException("unable to push", e);
    } catch (GitAPIException e) {
      throw new GitException("unable to push", e);
    }

    final String repoUrl = getSourceRemoteUrl();
    // branchName was computed above
    final org.eclipse.jgit.lib.Repository repo =
        new org.eclipse.jgit.storage.file.FileRepository(f);
    final RevWalk revWalk = new RevWalk(repo);
    final ObjectId resolve = repo.resolve(Constants.HEAD);
    final RevCommit commitRev = revWalk.parseCommit(resolve);
    final String commitHash = commitRev.getName();

    Git git = Git.open(binaryRepoFolder);
    final RevCommit binRepoResolveCommitRev;
    try {
      binRepoResolveCommitRev = git.log().call().iterator().next();
    } catch (NoHeadException e) {
      throw new GitException("No head found for repo", e);
    } catch (GitAPIException e) {
      throw new GitException("No head found for repo", e);
    }
    final String binRepoResolveCommitHash = binRepoResolveCommitRev.getName();
    final String binRepoBranchName = git.getRepository().getBranch();

    System.out.println("Update Bin Repo Service with the new changes - POST new object to service");
    final BinRepoBranchCommitDO binRepoBranchCommitDO =
        newInstance(
            repoUrl,
            branchname,
            commitHash,
            remoteUrl,
            binRepoBranchName,
            binRepoResolveCommitHash);
    final WebResource resource = client.resource(getUrlForPost());

    BinRepoBranchCommitDO postedDO = null;
    try {
      postedDO =
          resource
              .accept(MediaType.APPLICATION_XML)
              .post(BinRepoBranchCommitDO.class, binRepoBranchCommitDO);
      System.out.println("Posted Object = " + postedDO.toString());
    } catch (UniformInterfaceException e) {
      int statusCode = e.getResponse().getClientResponseStatus().getStatusCode();
      System.out.println("status code: " + statusCode);
      throw new MapServiceException("Unable to register the commit details", e);
    }

    // System.out.println(postedDO != null ? postedDO.toString() : "postedDO was null");
    System.out.println("updated the map service");
  }