/*
   * Pull latest from 'source' and 'binary' repository.
   */
  public boolean gitpull() throws GitException {

    boolean result = false;

    // do 'pull' on 'source'
    Git srcgit = Git.wrap(sourceRepository);
    Git bingit = Git.wrap(binaryRepository);

    try {

      PullResult srcpull = srcgit.pull().call();
      PullResult binpull = bingit.pull().call();

      if (srcpull.isSuccessful() && binpull.isSuccessful()) {
        if (srcpull.getFetchResult() != null
            && (srcpull.getFetchResult().getTrackingRefUpdates().size() > 0)) {
          result = true;
        }
        if (srcpull.getMergeResult() != null
            && (srcpull.getMergeResult().getMergeStatus() == MergeStatus.FAST_FORWARD
                || srcpull.getMergeResult().getMergeStatus() == MergeStatus.MERGED)) {
          result = true;
        }

        // TODO: rebase status needs to be checked but it is ignored for now
      }

    } catch (WrongRepositoryStateException e) {
      throw new GitException(e);
    } catch (InvalidConfigurationException e) {
      throw new GitException(e);
    } catch (DetachedHeadException e) {
      throw new GitException(e);
    } catch (InvalidRemoteException e) {
      throw new GitException(e);
    } catch (CanceledException e) {
      throw new GitException(e);
    } catch (RefNotFoundException e) {
      throw new GitException(e);
    } catch (NoHeadException e) {
      throw new GitException(e);
    } catch (TransportException e) {
      throw new GitException(e);
    } catch (GitAPIException e) {
      throw new GitException(e);
    }

    return result;
  }
示例#2
0
  @Test
  public void testGetCloneAndPull() throws Exception {
    // see bug 339254
    URI workspaceLocation = createWorkspace(getMethodName());
    String workspaceId = getWorkspaceId(workspaceLocation);

    JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), null);
    IPath clonePath =
        new Path("file").append(project.getString(ProtocolConstants.KEY_ID)).makeAbsolute();
    String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION);

    // get clones for workspace
    WebRequest request = listGitClonesRequest(workspaceId, null);
    WebResponse response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    JSONObject clones = new JSONObject(response.getText());
    JSONArray clonesArray = clones.getJSONArray(ProtocolConstants.KEY_CHILDREN);
    assertEquals(1, clonesArray.length());

    Git git = new Git(getRepositoryForContentLocation(contentLocation));
    // TODO: replace with RESTful API when ready, see bug 339114
    PullResult pullResult = git.pull().call();
    assertEquals(pullResult.getMergeResult().getMergeStatus(), MergeStatus.ALREADY_UP_TO_DATE);
    assertEquals(RepositoryState.SAFE, git.getRepository().getRepositoryState());
  }
  public static void updateRepository(String localPath, String remotePath) throws Exception {
    Repository localRepo;
    try {
      localRepo = new FileRepository(localPath + "/.git");

      Git git = new Git(localRepo);
      {
        AsposeConstants.println("Cloning Repository [" + remotePath + "]....");
      }

      // First try to clone the repository
      try {
        Git.cloneRepository().setURI(remotePath).setDirectory(new File(localPath)).call();
      } catch (Exception ex) {
        // If clone fails, try to pull the changes
        try {
          git.pull().call();
        } catch (Exception exPull) {
          // Pull also failed. Throw this exception to caller
          {
            AsposeConstants.println("Pull also failed.");
          }
          throw exPull; // throw it
        }
      }
    } catch (Exception ex) {
      throw new Exception("Could not download Repository from Github. Error: " + ex.getMessage());
    }
  }
 /**
  * Fetches from a remote repository and tries to merge into the current branch.
  *
  * @throws WrongRepositoryStateException
  * @throws InvalidConfigurationException
  * @throws DetachedHeadException
  * @throws InvalidRemoteException
  * @throws CanceledException
  * @throws RefNotFoundException
  * @throws NoHeadException
  * @throws TransportException
  * @throws GitAPIException
  */
 public void pull()
     throws WrongRepositoryStateException, InvalidConfigurationException, DetachedHeadException,
         InvalidRemoteException, CanceledException, RefNotFoundException, NoHeadException,
         TransportException, GitAPIException {
   PullCommand pullCommand = git.pull();
   pullCommand.call();
 }
示例#5
0
文件: GitUtils.java 项目: snjeza/core
  public static PullResult pull(final Git git, final int timeout) throws GitAPIException {
    PullCommand pull = git.pull();
    if (timeout >= 0) pull.setTimeout(timeout);
    pull.setProgressMonitor(new TextProgressMonitor());

    PullResult result = pull.call();
    return result;
  }
 /*
  * (non-Javadoc)
  *
  * @see nl.minicom.gitolite.manager.git.GitManager#pull()
  */
 @Override
 public boolean pull() throws IOException, ServiceUnavailable {
   try {
     PullCommand pull = git.pull();
     return !pull.call().getFetchResult().getTrackingRefUpdates().isEmpty();
   } catch (NullPointerException e) {
     throw new ServiceUnavailable(e);
   } catch (GitAPIException e) {
     throw new IOException(e);
   }
 }
  /** Clones or pulls the remote repository and returns the directory with the checkout */
  public void cloneOrPull(final String repo, final CredentialsProvider credentials)
      throws Exception {
    if (!localRepo.exists() && !localRepo.mkdirs()) {
      throw new IOException("Failed to create local repository");
    }
    File gitDir = new File(localRepo, ".git");
    if (!gitDir.exists()) {
      LOG.info("Cloning remote repo " + repo);
      CloneCommand command =
          Git.cloneRepository()
              .setCredentialsProvider(credentials)
              .setURI(repo)
              .setDirectory(localRepo)
              .setRemote(remoteName);
      git = command.call();
    } else {
      FileRepositoryBuilder builder = new FileRepositoryBuilder();
      Repository repository =
          builder
              .setGitDir(gitDir)
              .readEnvironment() // scan environment GIT_* variables
              .findGitDir() // scan up the file system tree
              .build();

      git = new Git(repository);

      // update the remote repo just in case
      StoredConfig config = repository.getConfig();
      config.setString("remote", remoteName, "url", repo);
      config.setString(
          "remote", remoteName, "fetch", "+refs/heads/*:refs/remotes/" + remoteName + "/*");

      String branch = "master";
      config.setString("branch", branch, "remote", remoteName);
      config.setString("branch", branch, "merge", "refs/heads/" + branch);

      try {
        config.save();
      } catch (IOException e) {
        LOG.error(
            "Failed to save the git configuration to "
                + localRepo
                + " with remote repo: "
                + repo
                + ". "
                + e,
            e);
      }

      // now pull
      LOG.info("Pulling from remote repo " + repo);
      git.pull().setCredentialsProvider(credentials).setRebase(true).call();
    }
  }
示例#8
0
  protected void doPull() throws MojoExecutionException {
    // CredentialsProvider cp = getCredentials();
    CredentialsProvider cp = null;
    try {
      Repository repository = git.getRepository();
      StoredConfig config = repository.getConfig();
      String url = config.getString("remote", "origin", "url");
      if (Strings.isNullOrBlank(url)) {
        getLog()
            .info(
                "No remote repository defined for the git repository at "
                    + getGitBuildPathDescription()
                    + " so not doing a pull");
        return;
      }
      String branch = repository.getBranch();
      String mergeUrl = config.getString("branch", branch, "merge");
      if (Strings.isNullOrBlank(mergeUrl)) {
        getLog()
            .info(
                "No merge spec for branch."
                    + branch
                    + ".merge in the git repository at "
                    + getGitBuildPathDescription()
                    + " so not doing a pull");
        return;
      }
      getLog()
          .info(
              "Performing a pull in git repository "
                  + getGitBuildPathDescription()
                  + " on remote URL: "
                  + url);

      git.pull().setCredentialsProvider(cp).setRebase(true).call();
    } catch (Throwable e) {
      String credText = "";
      if (cp instanceof UsernamePasswordCredentialsProvider) {}
      String message =
          "Failed to pull from the remote git repo with credentials "
              + cp
              + " due: "
              + e.getMessage()
              + ". This exception is ignored.";
      getLog().error(message, e);
      throw new MojoExecutionException(message, e);
    }
  }
示例#9
0
  private void reload(String botName) throws IOException, GitAPIException {
    log.info("Starting bot reload");
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repository =
        builder.setGitDir(new File(arguments.getBasePath() + "/.git")).readEnvironment().build();
    log.info("Starting repository update");
    Git git = new Git(repository);
    git.reset().setMode(ResetCommand.ResetType.HARD).call();
    log.info("Reset complete");
    git.clean().call();
    log.info("Clean compete");
    git.fetch().call();
    log.info("Fetch complete");
    git.pull().call();
    log.info("Repository update finished");

    initBot(botName);
    log.info("Bot reloaded");
  }