@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(); }
@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); }
/** * 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; } }
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); } } }
/** * 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; }
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(); } }
@Override public void run() throws GitAPIException { FetchCommand fetchCommand = myGit.fetch(); fetchCommand.setRemote(myUrl); fetchCommand.setRefSpecs(myRefSpecs); fetchCommand.setCredentialsProvider(myCredentialsProvider); fetchCommand.call(); }
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; } }
@Override public FetchCommand fetch() { return configure(super.fetch()).setProgressMonitor(progressMonitor("fetch")); }
default FetchCommand configure(FetchCommand cmd) { return cmd; }
public void fetch() throws Exception { FetchCommand fetchCommand = git.fetch(); if (credentialsProvider != null) fetchCommand.setCredentialsProvider(credentialsProvider); fetchCommand.call(); }
@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); } }
public FetchCommand _fetch() { return git.fetch(); }