Java Code Examples for org.eclipse.jgit.transport.RemoteConfig#addFetchRefSpec()

The following examples show how to use org.eclipse.jgit.transport.RemoteConfig#addFetchRefSpec() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractGitTest.java    From spring-cloud-contract with Apache License 2.0 6 votes vote down vote up
void setOriginOnProjectToTmp(File origin, File project, boolean push)
		throws GitAPIException, IOException, URISyntaxException {
	try (Git git = openGitProject(project)) {
		RemoteRemoveCommand remove = git.remoteRemove();
		remove.setName("origin");
		remove.call();
		RemoteSetUrlCommand command = git.remoteSetUrl();
		command.setUri(new URIish(origin.toURI().toURL()));
		command.setName("origin");
		command.setPush(push);
		command.call();
		StoredConfig config = git.getRepository().getConfig();
		RemoteConfig originConfig = new RemoteConfig(config, "origin");
		originConfig
				.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/origin/*"));
		originConfig.update(config);
		config.save();
	}
}
 
Example 2
Source File: LocalRepoMock.java    From gitflow-incremental-builder with MIT License 6 votes vote down vote up
private void configureRemote(Git git, String repoUrl) throws URISyntaxException, IOException, GitAPIException {
    StoredConfig config = git.getRepository().getConfig();
    config.clear();
    config.setString("remote", "origin" ,"fetch", "+refs/heads/*:refs/remotes/origin/*");
    config.setString("remote", "origin" ,"push", "+refs/heads/*:refs/remotes/origin/*");
    config.setString("branch", "master", "remote", "origin");
    config.setString("baseBranch", "master", "merge", "refs/heads/master");
    config.setString("push", null, "default", "current");

    // disable all gc
    // http://download.eclipse.org/jgit/site/5.2.1.201812262042-r/apidocs/org/eclipse/jgit/internal/storage/file/GC.html#setAuto-boolean-
    config.setString("gc", null, "auto", "0");
    config.setString("gc", null, "autoPackLimit", "0");
    config.setBoolean("receive", null, "autogc", false);

    RemoteConfig remoteConfig = new RemoteConfig(config, "origin");
    URIish uri = new URIish(repoUrl);
    remoteConfig.addURI(uri);
    remoteConfig.addFetchRefSpec(new RefSpec("refs/heads/master:refs/heads/master"));
    remoteConfig.addPushRefSpec(new RefSpec("refs/heads/master:refs/heads/master"));
    remoteConfig.update(config);

    config.save();
}
 
Example 3
Source File: RemotesTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testRemoveRemote () throws Exception {
    File otherWT = new File(workDir.getParentFile(), "repo2");
    GitClient client = getClient(otherWT);
    client.init(NULL_PROGRESS_MONITOR);
    File f = new File(otherWT, "f");
    write(f, "init");
    client.add(new File[] { f }, NULL_PROGRESS_MONITOR);
    client.commit(new File[] { f }, "init commit", null, null, NULL_PROGRESS_MONITOR);
    
    RemoteConfig cfg = new RemoteConfig(repository.getConfig(), "origin");
    cfg.addURI(new URIish(otherWT.toURI().toURL().toString()));
    cfg.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/origin/*"));
    cfg.update(repository.getConfig());
    repository.getConfig().save();
    
    client = getClient(workDir);
    client.fetch("origin", NULL_PROGRESS_MONITOR);
    client.createBranch("master", "origin/master", NULL_PROGRESS_MONITOR);
    client.createBranch("nova", "origin/master", NULL_PROGRESS_MONITOR);
    
    StoredConfig config = repository.getConfig();
    assertEquals("+refs/heads/*:refs/remotes/origin/*", config.getString("remote", "origin", "fetch"));
    assertEquals("origin", config.getString("branch", "master", "remote"));
    assertEquals("refs/heads/master", config.getString("branch", "master", "merge"));
    assertEquals("origin", config.getString("branch", "nova", "remote"));
    assertEquals("refs/heads/master", config.getString("branch", "nova", "merge"));
    
    // now try to remove the remote
    client.removeRemote("origin", NULL_PROGRESS_MONITOR);
    config = repository.getConfig();
    config.load();
    // is everything deleted?
    assertEquals(0, config.getSubsections("remote").size());
    assertNull(config.getString("branch", "master", "remote"));
    assertNull(config.getString("branch", "master", "merge"));
    assertNull(config.getString("branch", "nova", "remote"));
    assertNull(config.getString("branch", "nova", "merge"));
}
 
Example 4
Source File: GetRemotesTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testGetRemotes () throws Exception {
    GitClient client = getClient(workDir);
    StoredConfig cfg = repository.getConfig();
    RemoteConfig remoteConfig = new RemoteConfig(cfg, "origin");
    
    Map<String, GitRemoteConfig> remotes = client.getRemotes(NULL_PROGRESS_MONITOR);
    assertEquals(0, remotes.size());
    remoteConfig.update(cfg);
    cfg.save();
    
    remotes = client.getRemotes(NULL_PROGRESS_MONITOR);
    assertEquals(0, remotes.size());
    
    remoteConfig.addURI(new URIish("file:///home/repository"));
    remoteConfig.addURI(new URIish("file:///home/repository2"));
    remoteConfig.addPushURI(new URIish("file:///home/repository"));
    remoteConfig.addPushURI(new URIish("file:///home/repository3"));
    remoteConfig.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/origin/*"));
    remoteConfig.addFetchRefSpec(new RefSpec("+refs/heads/master:refs/remotes/origin/my-master"));
    remoteConfig.addPushRefSpec(new RefSpec("refs/remotes/origin/*:refs/heads/*"));
    remoteConfig.update(cfg);
    cfg.save();
    
    remotes = client.getRemotes(NULL_PROGRESS_MONITOR);
    assertEquals(1, remotes.size());
    assertEquals("origin", remotes.get("origin").getRemoteName());
    assertEquals(Arrays.asList(new String[] { "file:///home/repository", "file:///home/repository2" }), remotes.get("origin").getUris());
    assertEquals(Arrays.asList(new String[] { "file:///home/repository", "file:///home/repository3" }), remotes.get("origin").getPushUris());
    assertEquals(Arrays.asList(new String[] { "+refs/heads/*:refs/remotes/origin/*", "+refs/heads/master:refs/remotes/origin/my-master" }), remotes.get("origin").getFetchRefSpecs());
    assertEquals(Arrays.asList(new String[] { "refs/remotes/origin/*:refs/heads/*" }), remotes.get("origin").getPushRefSpecs());
    
    GitRemoteConfig remote = client.getRemote("origin", NULL_PROGRESS_MONITOR);
    assertEquals("origin", remote.getRemoteName());
    assertEquals(Arrays.asList(new String[] { "file:///home/repository", "file:///home/repository2" }), remote.getUris());
    assertEquals(Arrays.asList(new String[] { "file:///home/repository", "file:///home/repository3" }), remote.getPushUris());
    assertEquals(Arrays.asList(new String[] { "+refs/heads/*:refs/remotes/origin/*", "+refs/heads/master:refs/remotes/origin/my-master" }), remote.getFetchRefSpecs());
    assertEquals(Arrays.asList(new String[] { "refs/remotes/origin/*:refs/heads/*" }), remote.getPushRefSpecs());
}
 
Example 5
Source File: PullTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void setupRemoteSpec (String remote, String fetchSpec) throws URISyntaxException, IOException {
    RemoteConfig cfg = new RemoteConfig(repository.getConfig(), remote);
    cfg.addFetchRefSpec(new RefSpec(fetchSpec));
    cfg.update(repository.getConfig());
    repository.getConfig().save();
}
 
Example 6
Source File: FetchTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void setupRemoteSpec (String remote, String fetchSpec) throws URISyntaxException, IOException {
    RemoteConfig cfg = new RemoteConfig(repository.getConfig(), remote);
    cfg.addFetchRefSpec(new RefSpec(fetchSpec));
    cfg.update(repository.getConfig());
    repository.getConfig().save();
}
 
Example 7
Source File: GitRemoteHandlerV1.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
private boolean addRemote(HttpServletRequest request, HttpServletResponse response, String path, Boolean isGerrit) throws IOException, JSONException, ServletException,
		CoreException, URISyntaxException {
	// expected path: /git/remote/file/{path}
	Path p = new Path(path);
	JSONObject toPut = OrionServlet.readJSONRequest(request);
	String remoteName = toPut.optString(GitConstants.KEY_REMOTE_NAME, null);
	// remoteName is required
	if (remoteName == null || remoteName.isEmpty() || remoteName.contains(" ")) { //$NON-NLS-1$
		return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST,
				"Remote name must be provided", null));
	}
	String remoteURI = toPut.optString(GitConstants.KEY_REMOTE_URI, null);
	// remoteURI is required
	if (remoteURI == null || remoteURI.isEmpty()) {
		return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST,
				"Remote URI must be provided", null));
	}

	try {
		URIish uri = new URIish(remoteURI);
		if (GitUtils.isForbiddenGitUri(uri)) {
			statusHandler.handleRequest(
					request,
					response,
					new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, NLS.bind(
							"Remote URI {0} does not appear to be a git repository", remoteURI), null)); //$NON-NLS-1$
			return false;
		}
	} catch (URISyntaxException e) {
		statusHandler.handleRequest(request, response,
				new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, NLS.bind("Invalid remote URI: {0}", remoteURI), e)); //$NON-NLS-1$
		return false;
	}

	String fetchRefSpec = toPut.optString(GitConstants.KEY_REMOTE_FETCH_REF, null);
	String remotePushURI = toPut.optString(GitConstants.KEY_REMOTE_PUSH_URI, null);
	String pushRefSpec = toPut.optString(GitConstants.KEY_REMOTE_PUSH_REF, null);

	File gitDir = GitUtils.getGitDir(p);
	URI cloneLocation = BaseToCloneConverter.getCloneLocation(getURI(request), BaseToCloneConverter.REMOTE_LIST);
	Repository db = null;
	try {
		db = FileRepositoryBuilder.create(gitDir);
		StoredConfig config = db.getConfig();

		RemoteConfig rc = new RemoteConfig(config, remoteName);
		rc.addURI(new URIish(remoteURI));

		if(!isGerrit){
			// FetchRefSpec is required, but default version can be generated
			// if it isn't provided
			if (fetchRefSpec == null || fetchRefSpec.isEmpty()) {
				fetchRefSpec = String.format("+refs/heads/*:refs/remotes/%s/*", remoteName); //$NON-NLS-1$
			}
			rc.addFetchRefSpec(new RefSpec(fetchRefSpec));
		}else{
			rc.addFetchRefSpec(new RefSpec(String.format("+refs/heads/*:refs/remotes/%s/for/*", remoteName)));
			rc.addFetchRefSpec(new RefSpec(String.format("+refs/changes/*:refs/remotes/%s/changes/*", remoteName)));
		}
		// pushURI is optional
		if (remotePushURI != null && !remotePushURI.isEmpty())
			rc.addPushURI(new URIish(remotePushURI));
		// PushRefSpec is optional
		if (pushRefSpec != null && !pushRefSpec.isEmpty())
			rc.addPushRefSpec(new RefSpec(pushRefSpec));

		rc.update(config);
		config.save();

		Remote remote = new Remote(cloneLocation, db, remoteName);
		JSONObject result = new JSONObject();
		result.put(ProtocolConstants.KEY_LOCATION, remote.getLocation());
		OrionServlet.writeJSONResponse(request, response, result, JsonURIUnqualificationStrategy.ALL_NO_GIT);
		response.setHeader(ProtocolConstants.HEADER_LOCATION, result.getString(ProtocolConstants.KEY_LOCATION));
		response.setStatus(HttpServletResponse.SC_CREATED);
	} finally {
		if (db != null) {
			db.close();
		}
	}
	return true;
}
 
Example 8
Source File: PushTask.java    From ant-git-tasks with Apache License 2.0 4 votes vote down vote up
@Override
protected void doExecute() {
        try {
                StoredConfig config = git.getRepository().getConfig();
                List<RemoteConfig> remoteConfigs = RemoteConfig.getAllRemoteConfigs(config);

                if (remoteConfigs.isEmpty()) {
                        URIish uri = new URIish(getUri());

                        RemoteConfig remoteConfig = new RemoteConfig(config, Constants.DEFAULT_REMOTE_NAME);
                        
                        remoteConfig.addURI(uri);
                        remoteConfig.addFetchRefSpec(new RefSpec(DEFAULT_REFSPEC_STRING));
                        remoteConfig.addPushRefSpec(new RefSpec(DEFAULT_REFSPEC_STRING));
                        remoteConfig.update(config);

                        config.save();
                }
                
                String currentBranch = git.getRepository().getBranch();
                List<RefSpec> specs = Arrays.asList(new RefSpec(currentBranch + ":" + currentBranch));

                PushCommand pushCommand = git.push().
                        setPushAll().
                        setRefSpecs(specs).
                        setDryRun(false).
                        setRemote(getUri());

                setupCredentials(pushCommand);

                if (includeTags) {
                        pushCommand.setPushTags();
                }

                if (getProgressMonitor() != null) {
                        pushCommand.setProgressMonitor(getProgressMonitor());
                }

                Iterable<PushResult> pushResults = pushCommand.setForce(true).call();

                for (PushResult pushResult : pushResults) {
                        GitTaskUtils.validateTrackingRefUpdates(PUSH_FAILED_MESSAGE, pushResult.getTrackingRefUpdates());
                        log(pushResult.getMessages());
                }
        } catch (Exception e) {
                if (pushFailedProperty != null) {
                        getProject().setProperty(pushFailedProperty, e.getMessage());
                }

                throw new GitBuildException(PUSH_FAILED_MESSAGE, e);
        }
}