@Test
  public void testUploadPack() throws Exception {
    readConfig(
        "[remote \"example\"]\n"
            + "url = [email protected]:egit.git\n"
            + "fetch = +refs/heads/*:refs/remotes/example/*\n"
            + "uploadpack = /path/to/git/git-upload-pack\n"
            + "receivepack = /path/to/git/git-receive-pack\n");

    final RemoteConfig rc = new RemoteConfig(config, "example");
    final List<URIish> allURIs = rc.getURIs();
    RefSpec spec;

    assertEquals("example", rc.getName());
    assertNotNull(allURIs);
    assertNotNull(rc.getFetchRefSpecs());
    assertNotNull(rc.getPushRefSpecs());

    assertEquals(1, allURIs.size());
    assertEquals("[email protected]:egit.git", allURIs.get(0).toString());

    assertEquals(1, rc.getFetchRefSpecs().size());
    spec = rc.getFetchRefSpecs().get(0);
    assertTrue(spec.isForceUpdate());
    assertTrue(spec.isWildcard());
    assertEquals("refs/heads/*", spec.getSource());
    assertEquals("refs/remotes/example/*", spec.getDestination());

    assertEquals(0, rc.getPushRefSpecs().size());

    assertEquals("/path/to/git/git-upload-pack", rc.getUploadPack());
    assertEquals("/path/to/git/git-receive-pack", rc.getReceivePack());
  }
 @Test
 public void testSimpleAlwaysTags() throws Exception {
   readConfig(
       "[remote \"spearce\"]\n"
           + "url = http://www.spearce.org/egit.git\n"
           + "fetch = +refs/heads/*:refs/remotes/spearce/*\n"
           + "tagopt = --tags\n");
   final RemoteConfig rc = new RemoteConfig(config, "spearce");
   assertSame(TagOpt.FETCH_TAGS, rc.getTagOpt());
 }
 @Test
 public void testSimpleTimeout() throws Exception {
   readConfig(
       "[remote \"spearce\"]\n"
           + "url = http://www.spearce.org/egit.git\n"
           + "fetch = +refs/heads/*:refs/remotes/spearce/*\n"
           + "timeout = 12\n");
   final RemoteConfig rc = new RemoteConfig(config, "spearce");
   assertEquals(12, rc.getTimeout());
 }
 @Test
 public void testCreateOrigin() throws Exception {
   final RemoteConfig rc = new RemoteConfig(config, "origin");
   rc.addURI(new URIish("/some/dir"));
   rc.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/" + rc.getName() + "/*"));
   rc.update(config);
   checkConfig(
       "[remote \"origin\"]\n"
           + "\turl = /some/dir\n"
           + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n");
 }
Example #5
0
 private static List<URIish> getURIs(final RemoteConfig cfg, final Operation op) {
   switch (op) {
     case FETCH:
       return cfg.getURIs();
     case PUSH:
       {
         List<URIish> uris = cfg.getPushURIs();
         if (uris.isEmpty()) uris = cfg.getURIs();
         return uris;
       }
     default:
       throw new IllegalArgumentException(op.toString());
   }
 }
  @Test
  public void testSaveAddURI() throws Exception {
    readConfig(
        "[remote \"spearce\"]\n"
            + "url = http://www.spearce.org/egit.git\n"
            + "fetch = +refs/heads/*:refs/remotes/spearce/*\n");

    final RemoteConfig rc = new RemoteConfig(config, "spearce");
    rc.addURI(new URIish("/some/dir"));
    assertEquals(2, rc.getURIs().size());
    rc.update(config);
    checkConfig(
        "[remote \"spearce\"]\n"
            + "\turl = http://www.spearce.org/egit.git\n"
            + "\turl = /some/dir\n"
            + "\tfetch = +refs/heads/*:refs/remotes/spearce/*\n");
  }
Example #7
0
 /**
  * Open a new transport instance to connect two repositories.
  *
  * @param local existing local repository.
  * @param cfg configuration describing how to connect to the remote repository.
  * @param op planned use of the returned Transport; the URI may differ based on the type of
  *     connection desired.
  * @return the new transport instance. Never null. In case of multiple URIs in remote
  *     configuration, only the first is chosen.
  * @throws NotSupportedException the protocol specified is not supported.
  * @throws IllegalArgumentException if provided remote configuration doesn't have any URI
  *     associated.
  */
 public static Transport open(final Repository local, final RemoteConfig cfg, final Operation op)
     throws NotSupportedException {
   final List<URIish> uris = getURIs(cfg, op);
   if (uris.isEmpty())
     throw new IllegalArgumentException(
         "Remote config \"" + cfg.getName() + "\" has no URIs associated");
   final Transport tn = open(local, uris.get(0));
   tn.applyConfig(cfg);
   return tn;
 }
Example #8
0
 /**
  * Apply provided remote configuration on this transport.
  *
  * @param cfg configuration to apply on this transport.
  */
 public void applyConfig(final RemoteConfig cfg) {
   setOptionUploadPack(cfg.getUploadPack());
   setOptionReceivePack(cfg.getReceivePack());
   setTagOpt(cfg.getTagOpt());
   fetch = cfg.getFetchRefSpecs();
   push = cfg.getPushRefSpecs();
   timeout = cfg.getTimeout();
 }
  @Test
  public void testRemoveLastURI() throws Exception {
    readConfig("");

    final URIish a = new URIish("/some/dir");
    final URIish b = new URIish("/another/dir");
    final URIish c = new URIish("/more/dirs");
    final RemoteConfig rc = new RemoteConfig(config, "backup");
    assertTrue(rc.addURI(a));
    assertTrue(rc.addURI(b));
    assertTrue(rc.addURI(c));

    assertEquals(3, rc.getURIs().size());
    assertSame(a, rc.getURIs().get(0));
    assertSame(b, rc.getURIs().get(1));
    assertSame(c, rc.getURIs().get(2));

    assertTrue(rc.removeURI(c));
    assertEquals(2, rc.getURIs().size());
    assertSame(a, rc.getURIs().get(0));
    assertSame(b, rc.getURIs().get(1));
  }
  @Test
  public void testUnknown() throws Exception {
    readConfig("");

    final RemoteConfig rc = new RemoteConfig(config, "backup");
    assertEquals(0, rc.getURIs().size());
    assertEquals(0, rc.getFetchRefSpecs().size());
    assertEquals(0, rc.getPushRefSpecs().size());
    assertEquals("git-upload-pack", rc.getUploadPack());
    assertEquals("git-receive-pack", rc.getReceivePack());
  }
  @Test
  public void testBackup() throws Exception {
    readConfig(
        "[remote \"backup\"]\n"
            + "url = http://www.spearce.org/egit.git\n"
            + "url = [email protected]:/srv/git/egit.git\n"
            + "push = +refs/heads/*:refs/heads/*\n"
            + "push = refs/tags/*:refs/tags/*\n");

    final RemoteConfig rc = new RemoteConfig(config, "backup");
    final List<URIish> allURIs = rc.getURIs();
    RefSpec spec;

    assertEquals("backup", rc.getName());
    assertNotNull(allURIs);
    assertNotNull(rc.getFetchRefSpecs());
    assertNotNull(rc.getPushRefSpecs());

    assertEquals(2, allURIs.size());
    assertEquals("http://www.spearce.org/egit.git", allURIs.get(0).toString());
    assertEquals("[email protected]:/srv/git/egit.git", allURIs.get(1).toString());

    assertEquals(0, rc.getFetchRefSpecs().size());

    assertEquals(2, rc.getPushRefSpecs().size());
    spec = rc.getPushRefSpecs().get(0);
    assertTrue(spec.isForceUpdate());
    assertTrue(spec.isWildcard());
    assertEquals("refs/heads/*", spec.getSource());
    assertEquals("refs/heads/*", spec.getDestination());

    spec = rc.getPushRefSpecs().get(1);
    assertFalse(spec.isForceUpdate());
    assertTrue(spec.isWildcard());
    assertEquals("refs/tags/*", spec.getSource());
    assertEquals("refs/tags/*", spec.getDestination());
  }
 @Test
 public void testSaveAllTags() throws Exception {
   final RemoteConfig rc = new RemoteConfig(config, "origin");
   rc.addURI(new URIish("/some/dir"));
   rc.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/" + rc.getName() + "/*"));
   rc.setTagOpt(TagOpt.FETCH_TAGS);
   rc.update(config);
   checkConfig(
       "[remote \"origin\"]\n"
           + "\turl = /some/dir\n"
           + "\tfetch = +refs/heads/*:refs/remotes/origin/*\n"
           + "\ttagopt = --tags\n");
 }
  @Test
  public void testRemoveOnlyURI() throws Exception {
    readConfig("");

    final URIish a = new URIish("/some/dir");
    final RemoteConfig rc = new RemoteConfig(config, "backup");
    assertTrue(rc.addURI(a));

    assertEquals(1, rc.getURIs().size());
    assertSame(a, rc.getURIs().get(0));

    assertTrue(rc.removeURI(a));
    assertEquals(0, rc.getURIs().size());
  }
  @Test
  public void testAddURI() throws Exception {
    readConfig("");

    final URIish uri = new URIish("/some/dir");
    final RemoteConfig rc = new RemoteConfig(config, "backup");
    assertEquals(0, rc.getURIs().size());

    assertTrue(rc.addURI(uri));
    assertEquals(1, rc.getURIs().size());
    assertSame(uri, rc.getURIs().get(0));

    assertFalse(rc.addURI(new URIish(uri.toString())));
    assertEquals(1, rc.getURIs().size());
  }
Example #15
0
 private static boolean doesNotExist(final RemoteConfig cfg) {
   return cfg.getURIs().isEmpty() && cfg.getPushURIs().isEmpty();
 }
  @Test
  public void testSimple() throws Exception {
    readConfig(
        "[remote \"spearce\"]\n"
            + "url = http://www.spearce.org/egit.git\n"
            + "fetch = +refs/heads/*:refs/remotes/spearce/*\n");

    final RemoteConfig rc = new RemoteConfig(config, "spearce");
    final List<URIish> allURIs = rc.getURIs();
    RefSpec spec;

    assertEquals("spearce", rc.getName());
    assertNotNull(allURIs);
    assertNotNull(rc.getFetchRefSpecs());
    assertNotNull(rc.getPushRefSpecs());
    assertNotNull(rc.getTagOpt());
    assertEquals(0, rc.getTimeout());
    assertSame(TagOpt.AUTO_FOLLOW, rc.getTagOpt());

    assertEquals(1, allURIs.size());
    assertEquals("http://www.spearce.org/egit.git", allURIs.get(0).toString());

    assertEquals(1, rc.getFetchRefSpecs().size());
    spec = rc.getFetchRefSpecs().get(0);
    assertTrue(spec.isForceUpdate());
    assertTrue(spec.isWildcard());
    assertEquals("refs/heads/*", spec.getSource());
    assertEquals("refs/remotes/spearce/*", spec.getDestination());

    assertEquals(0, rc.getPushRefSpecs().size());
  }
  // add new remote
  private boolean addRemote(HttpServletRequest request, HttpServletResponse response, String path)
      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()) {
      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));
    }
    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);
    Repository db = new FileRepository(gitDir);
    StoredConfig config = db.getConfig();

    RemoteConfig rc = new RemoteConfig(config, remoteName);
    rc.addURI(new URIish(remoteURI));
    // 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));
    // 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();

    URI cloneLocation =
        BaseToCloneConverter.getCloneLocation(getURI(request), BaseToCloneConverter.REMOTE_LIST);
    Remote remote = new Remote(cloneLocation, db, remoteName);
    JSONObject result = new JSONObject();
    result.put(ProtocolConstants.KEY_LOCATION, remote.getLocation());
    OrionServlet.writeJSONResponse(request, response, result);
    response.setHeader(
        ProtocolConstants.HEADER_LOCATION, result.getString(ProtocolConstants.KEY_LOCATION));
    response.setStatus(HttpServletResponse.SC_CREATED);
    return true;
  }