Example usage for org.eclipse.jgit.lib Constants DEFAULT_REMOTE_NAME

List of usage examples for org.eclipse.jgit.lib Constants DEFAULT_REMOTE_NAME

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Constants DEFAULT_REMOTE_NAME.

Prototype

String DEFAULT_REMOTE_NAME

To view the source code for org.eclipse.jgit.lib Constants DEFAULT_REMOTE_NAME.

Click Source Link

Document

Default remote name used by clone, push and fetch operations

Usage

From source file:org.eclipse.orion.server.tests.servlets.git.GitPushTest.java

License:Open Source License

@Test
public void testPushHead() throws Exception {
    URI workspaceLocation = createWorkspace(getMethodName());

    // clone1//from   w  ww .j ava 2s  .c  o m
    JSONObject project1 = createProjectOrLink(workspaceLocation, getMethodName() + "1", null);
    String projectId1 = project1.getString(ProtocolConstants.KEY_ID);
    IPath clonePath1 = new Path("file").append(project1.getString(ProtocolConstants.KEY_ID)).makeAbsolute();
    clone(clonePath1);

    // get project1 metadata
    WebRequest request = getGetFilesRequest(project1.getString(ProtocolConstants.KEY_CONTENT_LOCATION));
    WebResponse response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    project1 = new JSONObject(response.getText());

    JSONObject gitSection1 = project1.optJSONObject(GitConstants.KEY_GIT);
    assertNotNull(gitSection1);
    String gitRemoteUri1 = gitSection1.optString(GitConstants.KEY_REMOTE);
    String gitIndexUri1 = gitSection1.optString(GitConstants.KEY_INDEX);
    String gitHeadUri1 = gitSection1.optString(GitConstants.KEY_HEAD);

    // clone2
    JSONObject project2 = createProjectOrLink(workspaceLocation, getMethodName() + "2", null);
    String projectId2 = project2.getString(ProtocolConstants.KEY_ID);
    IPath clonePath2 = new Path("file").append(project2.getString(ProtocolConstants.KEY_ID)).makeAbsolute();
    clone(clonePath2);

    // get project2 metadata
    request = getGetFilesRequest(project2.getString(ProtocolConstants.KEY_CONTENT_LOCATION));
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    project2 = new JSONObject(response.getText());
    JSONObject gitSection2 = project2.optJSONObject(GitConstants.KEY_GIT);
    assertNotNull(gitSection2);
    String gitRemoteUri2 = gitSection2.getString(GitConstants.KEY_REMOTE);
    String gitHeadUri2 = gitSection2.getString(GitConstants.KEY_HEAD);

    // clone1: list remotes
    request = GitRemoteTest.getGetGitRemoteRequest(gitRemoteUri1);
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    JSONObject remotes = new JSONObject(response.getText());
    JSONArray remotesArray = remotes.getJSONArray(ProtocolConstants.KEY_CHILDREN);
    assertEquals(1, remotesArray.length());
    JSONObject remote = remotesArray.getJSONObject(0);
    assertNotNull(remote);
    assertEquals(Constants.DEFAULT_REMOTE_NAME, remote.getString(ProtocolConstants.KEY_NAME));

    // clone1: change
    request = getPutFileRequest(projectId1 + "/test.txt", "incoming change");
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

    // clone1: add
    request = GitAddTest.getPutGitIndexRequest(gitIndexUri1);
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

    // clone1: commit
    request = GitCommitTest.getPostGitCommitRequest(gitHeadUri1, "incoming change commit", false);
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

    // clone1: push
    ServerStatus pushStatus = push(gitRemoteUri1, 1, 0, Constants.MASTER, Constants.HEAD, false);
    assertEquals(true, pushStatus.isOK());

    // clone2: get remote branch location
    JSONObject remoteBranch = getRemoteBranch(gitRemoteUri2, 1, 0, Constants.MASTER);
    String remoteBranchLocation2 = remoteBranch.getString(ProtocolConstants.KEY_LOCATION);

    // clone2: fetch
    fetch(remoteBranchLocation2);

    // clone2: get remote details
    JSONObject remoteBranch2 = getRemoteBranch(gitRemoteUri2, 1, 0, Constants.MASTER);
    String newRefId2 = remoteBranch2.getString(ProtocolConstants.KEY_ID);

    // clone2: merge into HEAD, "git merge origin/master"
    gitHeadUri2 = remoteBranch2.getString(GitConstants.KEY_HEAD);
    JSONObject merge = merge(gitHeadUri2, newRefId2);
    MergeStatus mergeResult = MergeStatus.valueOf(merge.getString(GitConstants.KEY_RESULT));
    assertEquals(MergeStatus.FAST_FORWARD, mergeResult);

    // clone2: assert change from clone1 is in place
    request = getGetFilesRequest(projectId2 + "/test.txt");
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    assertEquals("incoming change", response.getText());
}

From source file:org.eclipse.orion.server.tests.servlets.git.GitPushTest.java

License:Open Source License

@Test
public void testPushHeadSshWithPrivateKeyPassphrase() throws Exception {
    Assume.assumeTrue(sshRepo2 != null);
    Assume.assumeTrue(knownHosts2 != null);
    Assume.assumeTrue(privateKey != null);
    Assume.assumeTrue(passphrase != null);

    URI workspaceLocation = createWorkspace(getMethodName());
    URIish uri = new URIish(sshRepo2);

    // clone1: create
    JSONObject project1 = createProjectOrLink(workspaceLocation, getMethodName() + "1", null);
    String projectId1 = project1.getString(ProtocolConstants.KEY_ID);
    IPath clonePath = new Path("file").append(projectId1).makeAbsolute();
    WebRequest request = new PostGitCloneRequest().setURIish(uri).setFilePath(clonePath)
            .setKnownHosts(knownHosts2).setPrivateKey(privateKey).setPublicKey(publicKey)
            .setPassphrase(passphrase).getWebRequest();
    String cloneContentLocation1 = clone(request);

    // clone1: get project/folder metadata
    request = getGetFilesRequest(cloneContentLocation1);
    WebResponse response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    project1 = new JSONObject(response.getText());

    // clone1: get git links
    JSONObject gitSection1 = project1.optJSONObject(GitConstants.KEY_GIT);
    assertNotNull(gitSection1);/*from   w  w  w  .  jav  a  2 s  .co  m*/
    String gitRemoteUri1 = gitSection1.optString(GitConstants.KEY_REMOTE);
    String gitIndexUri1 = gitSection1.optString(GitConstants.KEY_INDEX);
    String gitHeadUri1 = gitSection1.optString(GitConstants.KEY_HEAD);

    // clone2: create
    JSONObject project2 = createProjectOrLink(workspaceLocation, getMethodName() + "2", null);
    String projectId2 = project2.getString(ProtocolConstants.KEY_ID);
    clonePath = new Path("file").append(projectId2).makeAbsolute();
    request = new PostGitCloneRequest().setURIish(uri).setFilePath(clonePath).setKnownHosts(knownHosts2)
            .setPrivateKey(privateKey).setPublicKey(publicKey).setPassphrase(passphrase).getWebRequest();
    String cloneContentLocation2 = clone(request);

    // clone2: get project/folder metadata
    request = getGetFilesRequest(cloneContentLocation2);
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    project2 = new JSONObject(response.getText());

    // clone2: get git links
    JSONObject gitSection2 = project2.optJSONObject(GitConstants.KEY_GIT);
    assertNotNull(gitSection2);
    String gitRemoteUri2 = gitSection2.getString(GitConstants.KEY_REMOTE);
    String gitCommitUri2 = gitSection2.getString(GitConstants.KEY_COMMIT);

    // clone1: list remotes
    request = GitRemoteTest.getGetGitRemoteRequest(gitRemoteUri1);
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    JSONObject remotes = new JSONObject(response.getText());
    JSONArray remotesArray = remotes.getJSONArray(ProtocolConstants.KEY_CHILDREN);
    assertEquals(1, remotesArray.length());
    JSONObject remote = remotesArray.getJSONObject(0);
    assertNotNull(remote);
    assertEquals(Constants.DEFAULT_REMOTE_NAME, remote.getString(ProtocolConstants.KEY_NAME));

    // clone1: change
    request = getPutFileRequest(projectId1 + "/test.txt", "incoming change");
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

    // clone1: add
    request = GitAddTest.getPutGitIndexRequest(gitIndexUri1);
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

    // clone1: commit
    request = GitCommitTest.getPostGitCommitRequest(gitHeadUri1, "incoming change commit", false);
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

    // clone1: push
    ServerStatus pushStatus = push(gitRemoteUri1, 1, 0, Constants.MASTER, Constants.HEAD, false, null,
            knownHosts2, privateKey, publicKey, passphrase, true);
    assertEquals(true, pushStatus.isOK());

    // clone2: get remote branch location
    JSONObject remoteBranch = getRemoteBranch(gitRemoteUri2, 1, 0, Constants.MASTER);
    String remoteBranchLocation2 = remoteBranch.getString(ProtocolConstants.KEY_LOCATION);

    // clone2: fetch
    fetch(remoteBranchLocation2, null, knownHosts2, privateKey, publicKey, passphrase, true);

    // clone2: get remote details
    JSONObject remoteBranch2 = getRemoteBranch(gitRemoteUri2, 1, 0, Constants.MASTER);
    String newRefId2 = remoteBranch2.getString(ProtocolConstants.KEY_ID);

    // clone2: merge into HEAD, "git merge origin/master"
    gitCommitUri2 = remoteBranch2.getString(GitConstants.KEY_HEAD);
    JSONObject merge = merge(gitCommitUri2, newRefId2);
    MergeStatus mergeResult = MergeStatus.valueOf(merge.getString(GitConstants.KEY_RESULT));
    assertEquals(MergeStatus.FAST_FORWARD, mergeResult);

    // clone2: assert change from clone1 is in place
    request = getGetFilesRequest(projectId2 + "/test.txt");
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    assertEquals("incoming change", response.getText());
}

From source file:org.eclipse.orion.server.tests.servlets.git.GitPushTest.java

License:Open Source License

@Test
public void testPushToDelete() throws Exception {
    URI workspaceLocation = createWorkspace(getMethodName());
    JSONObject projectTop1 = createProjectOrLink(workspaceLocation, getMethodName() + "-top1", null);
    IPath clonePathTop1 = new Path("file").append(projectTop1.getString(ProtocolConstants.KEY_ID))
            .makeAbsolute();/*from  www. j av  a  2 s. c om*/

    JSONObject projectTop2 = createProjectOrLink(workspaceLocation, getMethodName() + "-top2", null);
    IPath clonePathTop2 = new Path("file").append(projectTop2.getString(ProtocolConstants.KEY_ID))
            .makeAbsolute();

    JSONObject projectFolder1 = createProjectOrLink(workspaceLocation, getMethodName() + "-folder1", null);
    IPath clonePathFolder1 = new Path("file").append(projectFolder1.getString(ProtocolConstants.KEY_ID))
            .append("folder1").makeAbsolute();

    JSONObject projectFolder2 = createProjectOrLink(workspaceLocation, getMethodName() + "-folder2", null);
    IPath clonePathFolder2 = new Path("file").append(projectFolder2.getString(ProtocolConstants.KEY_ID))
            .append("folder2").makeAbsolute();

    JSONObject projectTop3 = createProjectOrLink(workspaceLocation, getMethodName() + "-top3", null);
    IPath clonePathTop3 = new Path("file").append(projectTop3.getString(ProtocolConstants.KEY_ID))
            .makeAbsolute();

    JSONObject projectFolder3 = createProjectOrLink(workspaceLocation, getMethodName() + "-folder3", null);
    IPath clonePathFolder3 = new Path("file").append(projectFolder3.getString(ProtocolConstants.KEY_ID))
            .append("folder1").makeAbsolute();

    IPath[] clonePathsTop = new IPath[] { clonePathTop1, clonePathTop2 };
    IPath[] clonePathsFolder = new IPath[] { clonePathFolder1, clonePathFolder2 };
    IPath[] clonePathsMixed = new IPath[] { clonePathTop3, clonePathFolder3 };
    IPath[][] clonePaths = new IPath[][] { clonePathsTop, clonePathsFolder, clonePathsMixed };

    for (IPath[] clonePath : clonePaths) {
        // clone 1
        JSONObject clone1 = clone(clonePath[0]);
        String cloneLocation1 = clone1.getString(ProtocolConstants.KEY_LOCATION);
        String contentLocation1 = clone1.getString(ProtocolConstants.KEY_CONTENT_LOCATION);
        String branchesLocation1 = clone1.getString(GitConstants.KEY_BRANCH);

        // clone 1 - get project1 metadata
        WebRequest request = getGetFilesRequest(contentLocation1);
        WebResponse response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
        JSONObject project1 = new JSONObject(response.getText());
        String projectLocation1 = project1.getString(ProtocolConstants.KEY_LOCATION);
        JSONObject gitSection1 = project1.getJSONObject(GitConstants.KEY_GIT);
        String gitRemoteUri1 = gitSection1.getString(GitConstants.KEY_REMOTE);
        String gitIndexUri1 = gitSection1.getString(GitConstants.KEY_INDEX);
        String gitHeadUri1 = gitSection1.getString(GitConstants.KEY_HEAD);

        // clone 1 - create branch "a"
        response = branch(branchesLocation1, "a");
        JSONObject newBranch = new JSONObject(response.getText());
        String remoteBranchLocation1 = newBranch.getString(GitConstants.KEY_REMOTE);

        // clone 1 - checkout "a"
        final String newBranchName = "a";
        response = checkoutBranch(cloneLocation1, newBranchName);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // clone 1 - change
        request = getPutFileRequest(projectLocation1 + "/test.txt", "clone1 change");
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // clone 1 - add
        request = GitAddTest.getPutGitIndexRequest(gitIndexUri1);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // clone 1 - commit
        request = GitCommitTest.getPostGitCommitRequest(gitHeadUri1, "clone1 change commit", false);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

        // clone 1 - push "a"
        ServerStatus pushStatus = push(remoteBranchLocation1, Constants.HEAD, false);
        assertEquals(true, pushStatus.isOK());

        // clone 1 - list remote branches - expect 2
        JSONObject remote1 = getRemote(gitRemoteUri1, 1, 0, Constants.DEFAULT_REMOTE_NAME);
        String remoteLocation1 = remote1.getString(ProtocolConstants.KEY_LOCATION);

        request = GitRemoteTest.getGetGitRemoteRequest(remoteLocation1);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
        remote1 = new JSONObject(response.getText());
        JSONArray refsArray = remote1.getJSONArray(ProtocolConstants.KEY_CHILDREN);
        assertEquals(2, refsArray.length());
        JSONObject ref = refsArray.getJSONObject(0);
        assertEquals(Constants.R_REMOTES + Constants.DEFAULT_REMOTE_NAME + "/" + newBranchName,
                ref.getString(ProtocolConstants.KEY_FULL_NAME));
        ref = refsArray.getJSONObject(1);
        assertEquals(Constants.R_REMOTES + Constants.DEFAULT_REMOTE_NAME + "/" + Constants.MASTER,
                ref.getString(ProtocolConstants.KEY_FULL_NAME));

        // clone 2 
        JSONObject clone2 = clone(clonePath[1]);
        String cloneLocation2 = clone2.getString(ProtocolConstants.KEY_LOCATION);
        String contentLocation2 = clone2.getString(ProtocolConstants.KEY_CONTENT_LOCATION);

        // clone 2 - get project2 metadata
        request = getGetFilesRequest(contentLocation2);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
        JSONObject project2 = new JSONObject(response.getText());
        JSONObject gitSection2 = project2.getJSONObject(GitConstants.KEY_GIT);
        String gitRemoteUri2 = gitSection2.getString(GitConstants.KEY_REMOTE);

        // clone 2 - check if the branch "a" is available
        JSONObject remote2 = getRemote(gitRemoteUri2, 1, 0, Constants.DEFAULT_REMOTE_NAME);
        String remoteLocation2 = remote2.getString(ProtocolConstants.KEY_LOCATION);

        request = GitRemoteTest.getGetGitRemoteRequest(remoteLocation2);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
        remote2 = new JSONObject(response.getText());
        refsArray = remote2.getJSONArray(ProtocolConstants.KEY_CHILDREN);
        assertEquals(2, refsArray.length());
        ref = refsArray.getJSONObject(0);
        assertEquals(Constants.R_REMOTES + Constants.DEFAULT_REMOTE_NAME + "/" + Constants.MASTER,
                ref.getString(ProtocolConstants.KEY_FULL_NAME));
        ref = refsArray.getJSONObject(1);
        assertEquals(Constants.R_REMOTES + Constants.DEFAULT_REMOTE_NAME + "/" + newBranchName,
                ref.getString(ProtocolConstants.KEY_FULL_NAME));
        String remoteBranchLocation2 = ref.getString(ProtocolConstants.KEY_LOCATION);

        // clone 2 - checkout branch "a"
        response = checkoutBranch(cloneLocation2, newBranchName);

        // clone 1 - delete remote branch "a"
        push(remoteBranchLocation1, "", false, false);

        // clone 1 - list remote branches - expect 1
        request = GitRemoteTest.getGetGitRemoteRequest(remoteLocation1);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
        remote1 = new JSONObject(response.getText());
        refsArray = remote1.getJSONArray(ProtocolConstants.KEY_CHILDREN);
        assertEquals(1, refsArray.length());
        ref = refsArray.getJSONObject(0);
        assertEquals(Constants.R_REMOTES + Constants.DEFAULT_REMOTE_NAME + "/" + Constants.MASTER,
                ref.getString(ProtocolConstants.KEY_FULL_NAME));

        // clone 2 - fetch
        request = GitFetchTest.getPostGitRemoteRequest(remoteBranchLocation2, true, false);
        response = webConversation.getResponse(request);
        assertEquals(HttpURLConnection.HTTP_ACCEPTED, response.getResponseCode());
        String taskLocation = response.getHeaderField(ProtocolConstants.HEADER_LOCATION);
        assertNotNull(taskLocation);
        String location = waitForTaskCompletion(taskLocation);

        // clone 2 - fetch task should fail
        request = getGetRequest(location);
        response = webConversation.getResponse(request);
        JSONObject status = new JSONObject(response.getText());
        assertFalse(status.getBoolean("Running"));
        assertEquals("Error", status.getJSONObject("Result").getString("Severity"));
    }
}

From source file:org.eclipse.orion.server.tests.servlets.git.GitRemoteTest.java

License:Open Source License

@Test
public void testAddRemoveRemote() throws Exception {
    URI workspaceLocation = createWorkspace(getMethodName());

    JSONObject project = createProjectOrLink(workspaceLocation, getMethodName(), null);
    JSONObject clone = clone(/*from  ww  w  .j a  v a2s.  co m*/
            new Path("file").append(project.getString(ProtocolConstants.KEY_ID)).makeAbsolute());
    String remotesLocation = clone.getString(GitConstants.KEY_REMOTE);

    // expect only origin
    getRemote(remotesLocation, 1, 0, Constants.DEFAULT_REMOTE_NAME);

    // create remote
    WebResponse response = addRemote(remotesLocation, "a", "https://a.b");
    String remoteLocation = response.getHeaderField(ProtocolConstants.HEADER_LOCATION);
    String remoteLocationFromBody = new JSONObject(response.getText())
            .getString(ProtocolConstants.KEY_LOCATION);
    assertEquals(remoteLocation, remoteLocationFromBody);

    // check details
    WebRequest request = getGetRequest(remoteLocation);
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

    // list remotes
    request = getGetRequest(remotesLocation);
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    JSONObject remotes = new JSONObject(response.getText());
    JSONArray remotesArray = remotes.getJSONArray(ProtocolConstants.KEY_CHILDREN);
    // expect origin and new remote
    assertEquals(2, remotesArray.length());

    // remove remote
    request = getDeleteGitRemoteRequest(remoteLocation);
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

    // list remotes again, make sure it's gone
    request = getGetRequest(remotesLocation);
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    remotes = new JSONObject(response.getText());
    remotesArray = remotes.getJSONArray(ProtocolConstants.KEY_CHILDREN);
    // expect origin only
    assertEquals(1, remotesArray.length());
    getRemote(remotesLocation, 1, 0, Constants.DEFAULT_REMOTE_NAME);
}

From source file:org.eclipse.orion.server.tests.servlets.git.GitTest.java

License:Open Source License

protected JSONObject getRemoteBranch(String gitRemoteUri, int size, int i, String name)
        throws IOException, SAXException, JSONException {
    assertRemoteUri(gitRemoteUri);/*  w  w w .  ja  va2s .c o m*/
    JSONObject remote = getRemote(gitRemoteUri, 1, 0, Constants.DEFAULT_REMOTE_NAME);
    String remoteLocation = remote.getString(ProtocolConstants.KEY_LOCATION);

    WebRequest request = GitRemoteTest.getGetGitRemoteRequest(remoteLocation);
    WebResponse response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    remote = new JSONObject(response.getText());
    assertNotNull(remote);
    assertEquals(Constants.DEFAULT_REMOTE_NAME, remote.getString(ProtocolConstants.KEY_NAME));
    assertNotNull(remote.getString(ProtocolConstants.KEY_LOCATION));
    JSONArray refsArray = remote.getJSONArray(ProtocolConstants.KEY_CHILDREN);
    assertEquals(size, refsArray.length());
    JSONObject ref = refsArray.getJSONObject(i);
    assertEquals(Constants.R_REMOTES + Constants.DEFAULT_REMOTE_NAME + "/" + name,
            ref.getString(ProtocolConstants.KEY_FULL_NAME));
    String newRefId = ref.getString(ProtocolConstants.KEY_ID);
    assertNotNull(newRefId);
    assertTrue(ObjectId.isId(newRefId));
    String remoteBranchLocation = ref.getString(ProtocolConstants.KEY_LOCATION);
    ref.getString(GitConstants.KEY_COMMIT);

    request = GitRemoteTest.getGetGitRemoteRequest(remoteBranchLocation);
    response = webConversation.getResponse(request);
    assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
    JSONObject remoteBranch = new JSONObject(response.getText());
    remoteBranch.getString(GitConstants.KEY_COMMIT);
    remoteBranch.getString(GitConstants.KEY_HEAD);

    return remoteBranch;
}

From source file:org.flowerplatform.web.git.GitUtils.java

License:Open Source License

@SuppressWarnings("restriction")
public Object[] getFetchPushUpstreamDataRefSpecAndRemote(Repository repository)
        throws InvalidConfigurationException, NoHeadException, DetachedHeadException {

    String branchName;/*  w w  w  .  java2  s.  com*/
    String fullBranch;
    try {
        fullBranch = repository.getFullBranch();
        if (fullBranch == null) {
            throw new NoHeadException(JGitText.get().pullOnRepoWithoutHEADCurrentlyNotSupported);
        }
        if (!fullBranch.startsWith(Constants.R_HEADS)) {
            // we can not pull if HEAD is detached and branch is not
            // specified explicitly
            throw new DetachedHeadException();
        }
        branchName = fullBranch.substring(Constants.R_HEADS.length());
    } catch (IOException e) {
        throw new JGitInternalException(JGitText.get().exceptionCaughtDuringExecutionOfPullCommand, e);
    }
    // get the configured remote for the currently checked out branch
    // stored in configuration key branch.<branch name>.remote
    Config repoConfig = repository.getConfig();
    String remote = repoConfig.getString(ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
            ConfigConstants.CONFIG_KEY_REMOTE);
    if (remote == null) {
        // fall back to default remote
        remote = Constants.DEFAULT_REMOTE_NAME;
    }

    // get the name of the branch in the remote repository
    // stored in configuration key branch.<branch name>.merge
    String remoteBranchName = repoConfig.getString(ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
            ConfigConstants.CONFIG_KEY_MERGE);

    if (remoteBranchName == null) {
        String missingKey = ConfigConstants.CONFIG_BRANCH_SECTION + "." + branchName + "."
                + ConfigConstants.CONFIG_KEY_MERGE;
        throw new InvalidConfigurationException(
                MessageFormat.format(JGitText.get().missingConfigurationForKey, missingKey));
    }

    // check if the branch is configured for pull-rebase
    boolean doRebase = repoConfig.getBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
            ConfigConstants.CONFIG_KEY_REBASE, false);

    final boolean isRemote = !remote.equals("."); //$NON-NLS-1$
    String remoteUri;
    if (isRemote) {
        remoteUri = repoConfig.getString(ConfigConstants.CONFIG_REMOTE_SECTION, remote,
                ConfigConstants.CONFIG_KEY_URL);
        if (remoteUri == null) {
            String missingKey = ConfigConstants.CONFIG_REMOTE_SECTION + "." + remote + "."
                    + ConfigConstants.CONFIG_KEY_URL;
            throw new InvalidConfigurationException(
                    MessageFormat.format(JGitText.get().missingConfigurationForKey, missingKey));
        }

        return new Object[] { fullBranch, remoteBranchName, remoteUri, doRebase };
    }
    return null;
}

From source file:org.flowerplatform.web.git.operation.internal.PullCommand.java

License:Open Source License

/**
 * Executes the {@code Pull} command with all the options and parameters
 * collected by the setter methods (e.g.
 * {@link #setProgressMonitor(ProgressMonitor)}) of this class. Each
 * instance of this class should only be used for one invocation of the
 * command. Don't call this method twice on an instance.
 *
 * @return the result of the pull//from   w ww . j av a  2 s. com
 * @throws WrongRepositoryStateException
 * @throws InvalidConfigurationException
 * @throws DetachedHeadException
 * @throws InvalidRemoteException
 * @throws CanceledException
 * @throws RefNotFoundException
 * @throws NoHeadException
 * @throws org.eclipse.jgit.api.errors.TransportException
 * @throws GitAPIException
 */
@SuppressWarnings("restriction")
public PullResult call() throws GitAPIException, WrongRepositoryStateException, InvalidConfigurationException,
        DetachedHeadException, InvalidRemoteException, CanceledException, RefNotFoundException, NoHeadException,
        org.eclipse.jgit.api.errors.TransportException {
    checkCallable();

    monitor.beginTask(JGitText.get().pullTaskName, 2);

    Object[] data = GitPlugin.getInstance().getUtils().getFetchPushUpstreamDataRefSpecAndRemote(repo);

    String fullBranch = (String) data[0];
    String branchName = fullBranch.substring(Constants.R_HEADS.length());

    if (!repo.getRepositoryState().equals(RepositoryState.SAFE))
        throw new WrongRepositoryStateException(MessageFormat.format(JGitText.get().cannotPullOnARepoWithState,
                repo.getRepositoryState().name()));

    // get the configured remote for the currently checked out branch
    // stored in configuration key branch.<branch name>.remote
    Config repoConfig = repo.getConfig();
    String remote = repoConfig.getString(ConfigConstants.CONFIG_BRANCH_SECTION, branchName,
            ConfigConstants.CONFIG_KEY_REMOTE);
    if (remote == null)
        // fall back to default remote
        remote = Constants.DEFAULT_REMOTE_NAME;

    // get the name of the branch in the remote repository
    // stored in configuration key branch.<branch name>.merge
    String remoteBranchName = (String) data[1];

    // determines whether rebase should be used after fetching
    boolean doRebase = (boolean) data[3];

    final boolean isRemote = !remote.equals("."); //$NON-NLS-1$
    String remoteUri;
    FetchResult fetchRes;
    if (isRemote) {
        remoteUri = (String) data[2];

        if (monitor.isCancelled())
            throw new CanceledException(
                    MessageFormat.format(JGitText.get().operationCanceled, JGitText.get().pullTaskName));

        RefSpec refSpec = new RefSpec();
        refSpec = refSpec.setForceUpdate(true);
        refSpec = refSpec.setSourceDestination(remoteBranchName, fullBranch);

        FetchCommand fetch = new Git(repo).fetch().setRemote(remote).setRefSpecs(refSpec);

        fetch.setProgressMonitor(monitor);
        configure(fetch);

        fetchRes = fetch.call();
    } else {
        // we can skip the fetch altogether
        remoteUri = "local repository";
        fetchRes = null;
    }

    monitor.update(1);

    if (monitor.isCancelled())
        throw new CanceledException(
                MessageFormat.format(JGitText.get().operationCanceled, JGitText.get().pullTaskName));

    // we check the updates to see which of the updated branches
    // corresponds
    // to the remote branch name
    AnyObjectId commitToMerge;
    if (isRemote) {
        Ref r = null;
        if (fetchRes != null) {
            r = fetchRes.getAdvertisedRef(remoteBranchName);
            if (r == null)
                r = fetchRes.getAdvertisedRef(Constants.R_HEADS + remoteBranchName);
        }
        if (r == null)
            throw new JGitInternalException(
                    MessageFormat.format(JGitText.get().couldNotGetAdvertisedRef, remoteBranchName));
        else
            commitToMerge = r.getObjectId();
    } else {
        try {
            commitToMerge = repo.resolve(remoteBranchName);
            if (commitToMerge == null)
                throw new RefNotFoundException(
                        MessageFormat.format(JGitText.get().refNotResolved, remoteBranchName));
        } catch (IOException e) {
            throw new JGitInternalException(JGitText.get().exceptionCaughtDuringExecutionOfPullCommand, e);
        }
    }

    String upstreamName = "branch \'" + Repository.shortenRefName(remoteBranchName) + "\' of " + remoteUri;

    PullResult result;
    if (doRebase) {
        RebaseCommand rebase = new Git(repo).rebase();
        RebaseResult rebaseRes = rebase.setUpstream(commitToMerge).setUpstreamName(upstreamName)
                .setProgressMonitor(monitor).setOperation(Operation.BEGIN).call();
        result = new PullResult(fetchRes, remote, rebaseRes);
    } else {
        MergeCommand merge = new Git(repo).merge();
        merge.include(upstreamName, commitToMerge);
        MergeResult mergeRes = merge.call();
        monitor.update(1);
        result = new PullResult(fetchRes, remote, mergeRes);
    }
    monitor.endTask();
    return result;
}

From source file:org.gitective.tests.RepositoryUtilsTest.java

License:Open Source License

/**
 * Test one origin change/*from w  w w .java 2 s.  c  o  m*/
 *
 *
 * @throws Exception
 */
@Test
public void oneOriginChange() throws Exception {
    RevCommit commit1 = add("test.txt", "content");
    File repo2 = initRepo();
    RevCommit commit2 = add(repo2, "test2.txt", "content2.txt");
    Repository repo = new FileRepository(testRepo);
    RefUpdate originMaster = repo
            .updateRef(Constants.R_REMOTES + Constants.DEFAULT_REMOTE_NAME + "/" + Constants.MASTER);
    originMaster.setNewObjectId(commit1);
    originMaster.forceUpdate();
    RemoteConfig config = new RemoteConfig(repo.getConfig(), Constants.DEFAULT_REMOTE_NAME);
    config.addURI(new URIish(repo2.toURI().toString()));
    config.update(repo.getConfig());
    Collection<RefDiff> diffs = RepositoryUtils.diffOriginRefs(repo);
    assertNotNull(diffs);
    assertFalse(diffs.isEmpty());
    assertNotNull(diffs.iterator().next().getLocal());
    assertNotNull(diffs.iterator().next().getRemote());
    assertEquals(commit1, diffs.iterator().next().getLocal().getObjectId());
    assertEquals(commit2, diffs.iterator().next().getRemote().getObjectId());
}

From source file:org.jboss.arquillian.ce.template.GitAdapter.java

License:Open Source License

public GitAdapter pull() throws Exception {
    return pull(Constants.DEFAULT_REMOTE_NAME);
}

From source file:org.jboss.arquillian.ce.template.GitAdapter.java

License:Open Source License

public GitAdapter push() throws Exception {
    return push(Constants.DEFAULT_REMOTE_NAME);
}