Java 类org.eclipse.jgit.api.FetchCommand 实例源码

项目:verigreen    文件:JGitOperator.java   
@Override
public String fetch(String localBranchName, String remoteBranchName) {

    RefSpec spec = new RefSpec().setSourceDestination(localBranchName, remoteBranchName);
    FetchCommand command = _git.fetch();
    command.setRefSpecs(spec);
    FetchResult result = null;
    try {
        result = command.call();
    } catch (Throwable e) {
        throw new RuntimeException(String.format(
                "Failed to fetch from [%s] to [%s]",
                remoteBranchName,
                localBranchName), e);
    }

    return result.getMessages();
}
项目:spring-cloud-config    文件:JGitEnvironmentRepositoryTests.java   
@Test
public void shouldSetTransportConfigCallbackOnCloneAndFetch() throws Exception {
    Git mockGit = mock(Git.class);
    FetchCommand fetchCommand = mock(FetchCommand.class);
    when(mockGit.fetch()).thenReturn(fetchCommand);
    when(fetchCommand.call()).thenReturn(mock(FetchResult.class));

    CloneCommand mockCloneCommand = mock(CloneCommand.class);
    when(mockCloneCommand.setURI(anyString())).thenReturn(mockCloneCommand);
    when(mockCloneCommand.setDirectory(any(File.class))).thenReturn(mockCloneCommand);

    TransportConfigCallback configCallback = mock(TransportConfigCallback.class);
    JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(this.environment);
    envRepository.setGitFactory(new MockGitFactory(mockGit, mockCloneCommand));
    envRepository.setUri("http://somegitserver/somegitrepo");
    envRepository.setTransportConfigCallback(configCallback);
    envRepository.setCloneOnStart(true);

    envRepository.afterPropertiesSet();
    verify(mockCloneCommand, times(1)).setTransportConfigCallback(configCallback);

    envRepository.fetch(mockGit, "master");
    verify(fetchCommand, times(1)).setTransportConfigCallback(configCallback);
}
项目:gerrit-gitblit-plugin    文件:JGitUtils.java   
/**
 * Fetch updates from the remote repository. If refSpecs is unspecifed, remote heads, tags, and notes are retrieved.
 * 
 * @param credentialsProvider
 * @param repository
 * @param refSpecs
 * @return FetchResult
 * @throws Exception
 */
public static FetchResult fetchRepository(CredentialsProvider credentialsProvider, Repository repository, RefSpec... refSpecs) throws Exception {
    try (Git git = new Git(repository)) {
        FetchCommand fetch = git.fetch();
        List<RefSpec> specs = new ArrayList<RefSpec>();
        if (refSpecs == null || refSpecs.length == 0) {
            specs.add(new RefSpec("+refs/heads/*:refs/remotes/origin/*"));
            specs.add(new RefSpec("+refs/tags/*:refs/tags/*"));
            specs.add(new RefSpec("+refs/notes/*:refs/notes/*"));
        } else {
            specs.addAll(Arrays.asList(refSpecs));
        }
        if (credentialsProvider != null) {
            fetch.setCredentialsProvider(credentialsProvider);
        }
        fetch.setRefSpecs(specs);
        FetchResult fetchRes = fetch.call();
        return fetchRes;
    }
}
项目:exposr    文件:Project.java   
private void fetchIfNeeded(final Git git, ObjectId remoteId)
        throws ProjectException {
    final ObjectId localId = resolve(git, ref);

    if (localId == null || !localId.equals(remoteId)) {
        log.info(this + ": Fetching remote object: " + remoteId);

        final FetchCommand fetchCommand = git.fetch().setRemote(url)
                .setRefSpecs(new RefSpec(ref));

        if (auth != null)
            fetchCommand.setCredentialsProvider(auth.build());

        try {
            fetchCommand.call();
        } catch (GitAPIException e) {
            throw new ProjectException(this
                    + ": Failed to fetch remote ref", e);
        }
    }
}
项目:gitblit    文件:JGitUtils.java   
/**
 * Fetch updates from the remote repository. If refSpecs is unspecifed,
 * remote heads, tags, and notes are retrieved.
 * 
 * @param credentialsProvider
 * @param repository
 * @param refSpecs
 * @return FetchResult
 * @throws Exception
 */
public static FetchResult fetchRepository(CredentialsProvider credentialsProvider,
        Repository repository, RefSpec... refSpecs) throws Exception {
    Git git = new Git(repository);
    FetchCommand fetch = git.fetch();
    List<RefSpec> specs = new ArrayList<RefSpec>();
    if (refSpecs == null || refSpecs.length == 0) {
        specs.add(new RefSpec("+refs/heads/*:refs/remotes/origin/*"));
        specs.add(new RefSpec("+refs/tags/*:refs/tags/*"));
        specs.add(new RefSpec("+refs/notes/*:refs/notes/*"));
    } else {
        specs.addAll(Arrays.asList(refSpecs));
    }
    if (credentialsProvider != null) {
        fetch.setCredentialsProvider(credentialsProvider);
    }
    fetch.setRefSpecs(specs);
    FetchResult fetchRes = fetch.call();
    return fetchRes;
}
项目:IRCBlit    文件:JGitUtils.java   
/**
 * Fetch updates from the remote repository. If refSpecs is unspecifed,
 * remote heads, tags, and notes are retrieved.
 * 
 * @param credentialsProvider
 * @param repository
 * @param refSpecs
 * @return FetchResult
 * @throws Exception
 */
public static FetchResult fetchRepository(CredentialsProvider credentialsProvider,
        Repository repository, RefSpec... refSpecs) throws Exception {
    Git git = new Git(repository);
    FetchCommand fetch = git.fetch();
    List<RefSpec> specs = new ArrayList<RefSpec>();
    if (refSpecs == null || refSpecs.length == 0) {
        specs.add(new RefSpec("+refs/heads/*:refs/remotes/origin/*"));
        specs.add(new RefSpec("+refs/tags/*:refs/tags/*"));
        specs.add(new RefSpec("+refs/notes/*:refs/notes/*"));
    } else {
        specs.addAll(Arrays.asList(refSpecs));
    }
    if (credentialsProvider != null) {
        fetch.setCredentialsProvider(credentialsProvider);
    }
    fetch.setRefSpecs(specs);
    FetchResult fetchRes = fetch.call();
    return fetchRes;
}
项目:plugins_github    文件:PullRequestImportJob.java   
private void fetchGitHubPullRequest(Repository gitRepo, GHPullRequest pr)
    throws GitAPIException, InvalidRemoteException, TransportException {
  status.update(Code.SYNC, "Fetching", "Fetching PullRequests from GitHub");

  try (Git git = Git.wrap(gitRepo)) {
    FetchCommand fetch = git.fetch();
    fetch.setRemote(ghRepository.getCloneUrl());
    fetch.setRefSpecs(
        new RefSpec(
            "+refs/pull/" + pr.getNumber() + "/head:refs/remotes/origin/pr/" + pr.getNumber()));
    fetch.setProgressMonitor(this);
    fetch.setCredentialsProvider(ghRepository.getCredentialsProvider());
    fetch.call();
  }
}
项目:tools-idea    文件:GitHttpRemoteCommand.java   
@Override
public void run() throws GitAPIException {
  FetchCommand fetchCommand = myGit.fetch();
  fetchCommand.setRemote(myUrl);
  fetchCommand.setRefSpecs(myRefSpecs);
  fetchCommand.setCredentialsProvider(myCredentialsProvider);
  fetchCommand.call();
}
项目:mOrgAnd    文件:JGitWrapper.java   
public void updateChanges(ProgressMonitor monitor) throws Exception {
    Git git = getGit(monitor);

    FetchCommand fetch = git.fetch();
    fetch.setCredentialsProvider(credentialsProvider);
    if (monitor != null)
        fetch.setProgressMonitor(monitor);
    fetch.call();

    SyncState state = getSyncState(git);
    Ref fetchHead = git.getRepository().getRef("FETCH_HEAD");
    switch (state) {
        case Equal:
            // Do nothing
            Log.d("Git", "Local branch is up-to-date");
            break;

        case Ahead:
            Log.d("Git", "Local branch ahead, pushing changes to remote");
            git.push().setCredentialsProvider(credentialsProvider).setRemote(remotePath).call();
            break;

        case Behind:
            Log.d("Git", "Local branch behind, fast forwarding changes");
            MergeResult result = git.merge().include(fetchHead).setFastForward(MergeCommand.FastForwardMode.FF_ONLY).call();
            if (result.getMergeStatus().isSuccessful() == false)
                throw new IllegalStateException("Fast forward failed on behind merge");
            break;

        case Diverged:
            Log.d("Git", "Branches are diverged, merging with strategy " + mergeStrategy.getName());
            MergeResult mergeResult = git.merge().include(fetchHead).setStrategy(mergeStrategy).call();
            if (mergeResult.getMergeStatus().isSuccessful()) {
                git.push().setCredentialsProvider(credentialsProvider).setRemote(remotePath).call();
            } else
                throw new IllegalStateException("Merge failed for diverged branches using strategy " + mergeStrategy.getName());
            break;
    }
}
项目:centraldogma    文件:GitWithAuth.java   
@Override
public FetchCommand fetch() {
    return configure(super.fetch()).setProgressMonitor(progressMonitor("fetch"));
}
项目:github-bucket    文件:Config.java   
default FetchCommand configure(FetchCommand cmd) {
    return cmd;
}
项目:mdw    文件:VersionControlGit.java   
public void fetch() throws Exception {
    FetchCommand fetchCommand = git.fetch();
    if (credentialsProvider != null)
        fetchCommand.setCredentialsProvider(credentialsProvider);
    fetchCommand.call();
}
项目:che    文件:JGitConnection.java   
@Override
public void fetch(FetchParams params) throws GitException, UnauthorizedException {
  String remoteName = params.getRemote();
  String remoteUri;
  try {
    List<RefSpec> fetchRefSpecs;
    List<String> refSpec = params.getRefSpec();
    if (!refSpec.isEmpty()) {
      fetchRefSpecs = new ArrayList<>(refSpec.size());
      for (String refSpecItem : refSpec) {
        RefSpec fetchRefSpec =
            (refSpecItem.indexOf(':') < 0) //
                ? new RefSpec(R_HEADS + refSpecItem + ":") //
                : new RefSpec(refSpecItem);
        fetchRefSpecs.add(fetchRefSpec);
      }
    } else {
      fetchRefSpecs = emptyList();
    }

    FetchCommand fetchCommand = getGit().fetch();

    // If this an unknown remote with no refspecs given, put HEAD
    // (otherwise JGit fails)
    if (remoteName != null && refSpec.isEmpty()) {
      boolean found = false;
      List<Remote> configRemotes = remoteList(null, false);
      for (Remote configRemote : configRemotes) {
        if (remoteName.equals(configRemote.getName())) {
          found = true;
          break;
        }
      }
      if (!found) {
        fetchRefSpecs = singletonList(new RefSpec(HEAD + ":" + FETCH_HEAD));
      }
    }

    if (remoteName == null) {
      remoteName = DEFAULT_REMOTE_NAME;
    }
    fetchCommand.setRemote(remoteName);
    remoteUri =
        getRepository()
            .getConfig()
            .getString(
                ConfigConstants.CONFIG_REMOTE_SECTION,
                remoteName,
                ConfigConstants.CONFIG_KEY_URL);
    fetchCommand.setRefSpecs(fetchRefSpecs);

    int timeout = params.getTimeout();
    if (timeout > 0) {
      fetchCommand.setTimeout(timeout);
    }
    fetchCommand.setRemoveDeletedRefs(params.isRemoveDeletedRefs());

    executeRemoteCommand(remoteUri, fetchCommand, params.getUsername(), params.getPassword());
  } catch (GitException | GitAPIException exception) {
    String errorMessage;
    if (exception.getMessage().contains("Invalid remote: ")) {
      errorMessage = ERROR_NO_REMOTE_REPOSITORY;
    } else if ("Nothing to fetch.".equals(exception.getMessage())) {
      return;
    } else {
      errorMessage = generateExceptionMessage(exception);
    }
    throw new GitException(errorMessage, exception);
  }
}
项目:appformer    文件:GitImpl.java   
public FetchCommand _fetch() {
    return git.fetch();
}