@NotNull
 private Map<String, Ref> getRemoteRefs(@NotNull Repository db, @NotNull GitVcsRoot gitRoot)
     throws Exception {
   long retryInterval = myConfig.getConnectionRetryIntervalMillis();
   int attemptsLeft = myConfig.getConnectionRetryAttempts();
   while (true) {
     final long start = System.currentTimeMillis();
     Transport transport = null;
     FetchConnection connection = null;
     try {
       transport =
           myTransportFactory.createTransport(
               db, gitRoot.getRepositoryFetchURL(), gitRoot.getAuthSettings());
       connection = transport.openFetch();
       return connection.getRefsMap();
     } catch (NotSupportedException nse) {
       throw friendlyNotSupportedException(gitRoot, nse);
     } catch (TransportException te) {
       attemptsLeft--;
       if (isRecoverable(te) && attemptsLeft > 0) {
         LOG.warn(
             "List remote refs failed: "
                 + te.getMessage()
                 + ", "
                 + attemptsLeft
                 + " attempt(s) left");
       } else {
         throw friendlyTransportException(te, gitRoot);
       }
     } catch (WrongPassphraseException e) {
       throw new VcsException(e.getMessage(), e);
     } finally {
       if (connection != null) connection.close();
       if (transport != null) transport.close();
       final long finish = System.currentTimeMillis();
       PERFORMANCE_LOG.debug(
           "[getRemoteRefs] repository: "
               + LogUtil.describe(gitRoot)
               + ", took "
               + (finish - start)
               + "ms");
     }
     Thread.sleep(retryInterval);
     retryInterval *= 2;
   }
 }
  @Test
  public void testListRemote_Smart_WithQueryParameters() throws Exception {
    URIish myURI = toURIish("/snone/do?r=1&p=test.git");
    Repository dst = createBareRepository();
    Transport t = Transport.open(dst, myURI);
    try {
      try {
        t.openFetch();
        fail("test did not fail to find repository as expected");
      } catch (NoRemoteRepositoryException err) {
        // expected
      }
    } finally {
      t.close();
    }

    List<AccessEvent> requests = getRequests();
    assertEquals(1, requests.size());

    AccessEvent info = requests.get(0);
    assertEquals("GET", info.getMethod());
    assertEquals("/snone/do", info.getPath());
    assertEquals(3, info.getParameters().size());
    assertEquals("1", info.getParameter("r"));
    assertEquals("test.git/info/refs", info.getParameter("p"));
    assertEquals("git-upload-pack", info.getParameter("service"));
    assertEquals(404, info.getStatus());
  }
  @Test
  public void testInitialClone_Small() throws Exception {
    Repository dst = createBareRepository();
    assertFalse(dst.hasObject(A_txt));

    Transport t = Transport.open(dst, remoteURI);
    ((TransportHttp) t).setUseSmartHttp(false);
    try {
      t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
    } finally {
      t.close();
    }

    assertTrue(dst.hasObject(A_txt));
    assertEquals(B, dst.getRef(master).getObjectId());
    fsck(dst, B);

    List<AccessEvent> loose = getRequests(loose(remoteURI, A_txt));
    assertEquals(1, loose.size());
    assertEquals("GET", loose.get(0).getMethod());
    assertEquals(0, loose.get(0).getParameters().size());
    assertEquals(200, loose.get(0).getStatus());
    assertEquals(
        "application/x-git-loose-object", loose.get(0).getResponseHeader(HDR_CONTENT_TYPE));
  }
  @Test
  public void testInitialClone_Packed() throws Exception {
    new TestRepository<Repository>(remoteRepository).packAndPrune();

    Repository dst = createBareRepository();
    assertFalse(dst.hasObject(A_txt));

    Transport t = Transport.open(dst, remoteURI);
    ((TransportHttp) t).setUseSmartHttp(false);
    try {
      t.fetch(NullProgressMonitor.INSTANCE, mirror(master));
    } finally {
      t.close();
    }

    assertTrue(dst.hasObject(A_txt));
    assertEquals(B, dst.getRef(master).getObjectId());
    fsck(dst, B);

    List<AccessEvent> req;

    req = getRequests(loose(remoteURI, B));
    assertEquals(1, req.size());
    assertEquals("GET", req.get(0).getMethod());
    assertEquals(0, req.get(0).getParameters().size());
    assertEquals(404, req.get(0).getStatus());

    req = getRequests(join(remoteURI, "objects/info/packs"));
    assertEquals(1, req.size());
    assertEquals("GET", req.get(0).getMethod());
    assertEquals(0, req.get(0).getParameters().size());
    assertEquals(200, req.get(0).getStatus());
    assertEquals("text/plain;charset=UTF-8", req.get(0).getResponseHeader(HDR_CONTENT_TYPE));
  }
  @Test
  public void testListRemote() throws IOException {
    Repository dst = createBareRepository();

    assertEquals("http", remoteURI.getScheme());

    Map<String, Ref> map;
    Transport t = Transport.open(dst, remoteURI);
    ((TransportHttp) t).setUseSmartHttp(false);
    try {
      // I didn't make up these public interface names, I just
      // approved them for inclusion into the code base. Sorry.
      // --spearce
      //
      assertTrue("isa TransportHttp", t instanceof TransportHttp);
      assertTrue("isa HttpTransport", t instanceof HttpTransport);

      FetchConnection c = t.openFetch();
      try {
        map = c.getRefsMap();
      } finally {
        c.close();
      }
    } finally {
      t.close();
    }

    assertNotNull("have map of refs", map);
    assertEquals(2, map.size());

    assertNotNull("has " + master, map.get(master));
    assertEquals(B, map.get(master).getObjectId());

    assertNotNull("has " + Constants.HEAD, map.get(Constants.HEAD));
    assertEquals(B, map.get(Constants.HEAD).getObjectId());

    List<AccessEvent> requests = getRequests();
    assertEquals(2, requests.size());
    assertEquals(0, getRequests(remoteURI, "git-upload-pack").size());

    AccessEvent info = requests.get(0);
    assertEquals("GET", info.getMethod());
    assertEquals(join(remoteURI, "info/refs"), info.getPath());
    assertEquals(0, info.getParameters().size());
    assertNull("no service parameter", info.getParameter("service"));
    assertEquals("no-cache", info.getRequestHeader(HDR_PRAGMA));
    assertNotNull("has user-agent", info.getRequestHeader(HDR_USER_AGENT));
    assertTrue("is jgit agent", info.getRequestHeader(HDR_USER_AGENT).startsWith("JGit/"));
    assertEquals("*/*", info.getRequestHeader(HDR_ACCEPT));
    assertEquals(200, info.getStatus());
    assertEquals("text/plain;charset=UTF-8", info.getResponseHeader(HDR_CONTENT_TYPE));

    AccessEvent head = requests.get(1);
    assertEquals("GET", head.getMethod());
    assertEquals(join(remoteURI, "HEAD"), head.getPath());
    assertEquals(0, head.getParameters().size());
    assertEquals(200, head.getStatus());
    assertEquals("text/plain", head.getResponseHeader(HDR_CONTENT_TYPE));
  }
Exemple #6
0
 private FetchResult runFetch() throws URISyntaxException, IOException {
   final Transport tn = Transport.open(db, remoteName);
   final FetchResult r;
   try {
     tn.setTagOpt(TagOpt.FETCH_TAGS);
     r = tn.fetch(new TextProgressMonitor(), null);
   } finally {
     tn.close();
   }
   showFetchResult(r);
   return r;
 }
 @Test
 public void testListRemote_Smart_UploadPackNeedsAuth() throws Exception {
   Repository dst = createBareRepository();
   Transport t = Transport.open(dst, smartAuthBasicURI);
   try {
     try {
       t.openFetch();
       fail("connection opened even though service disabled");
     } catch (TransportException err) {
       String exp = smartAuthBasicURI + ": " + JGitText.get().notAuthorized;
       assertEquals(exp, err.getMessage());
     }
   } finally {
     t.close();
   }
 }
 @Test
 public void testListRemote_Dumb_NeedsAuth() throws Exception {
   Repository dst = createBareRepository();
   Transport t = Transport.open(dst, dumbAuthBasicURI);
   try {
     try {
       t.openFetch();
       fail("connection opened even info/refs needs auth basic");
     } catch (TransportException err) {
       String exp = dumbAuthBasicURI + ": " + JGitText.get().notAuthorized;
       assertEquals(exp, err.getMessage());
     }
   } finally {
     t.close();
   }
 }
 @Test
 public void testRepositoryNotFound_Smart() throws Exception {
   URIish uri = toURIish("/smart.none/not-found");
   Repository dst = createBareRepository();
   Transport t = Transport.open(dst, uri);
   try {
     try {
       t.openFetch();
       fail("connection opened to not found repository");
     } catch (NoRemoteRepositoryException err) {
       String exp = uri + ": " + uri + "/info/refs?service=git-upload-pack not found";
       assertEquals(exp, err.getMessage());
     }
   } finally {
     t.close();
   }
 }
  /**
   * @param pm the monitor to be used for reporting progress and responding to cancellation. The
   *     monitor is never <code>null</code>
   * @throws InvocationTargetException
   * @throws InterruptedException
   */
  public void run(IProgressMonitor pm) throws InvocationTargetException, InterruptedException {
    Transport transport = null;
    Connection connection = null;
    try {
      transport = Transport.open(localDb, uri);

      if (pm != null) pm.beginTask(CoreText.ListRemoteOperation_title, IProgressMonitor.UNKNOWN);
      connection = transport.openFetch();
      remoteRefsMap = connection.getRefsMap();
    } catch (NotSupportedException e) {
      throw new InvocationTargetException(e);
    } catch (TransportException e) {
      throw new InvocationTargetException(e);
    } finally {
      if (connection != null) connection.close();
      if (transport != null) transport.close();
      if (pm != null) pm.done();
    }
  }
  @Test
  public void testPushNotSupported() throws Exception {
    final TestRepository src = createTestRepository();
    final RevCommit Q = src.commit().create();
    final Repository db = src.getRepository();

    Transport t = Transport.open(db, remoteURI);
    ((TransportHttp) t).setUseSmartHttp(false);
    try {
      try {
        t.push(NullProgressMonitor.INSTANCE, push(src, Q));
        fail("push incorrectly completed against a smart server");
      } catch (NotSupportedException nse) {
        String exp = "smart HTTP push disabled";
        assertEquals(exp, nse.getMessage());
      }
    } finally {
      t.close();
    }
  }
  @Test
  public void testListRemote_Smart_UploadPackDisabled() throws Exception {
    FileRepository src = remoteRepository.getRepository();
    final FileBasedConfig cfg = src.getConfig();
    cfg.setBoolean("http", null, "uploadpack", false);
    cfg.save();

    Repository dst = createBareRepository();
    Transport t = Transport.open(dst, smartAuthNoneURI);
    try {
      try {
        t.openFetch();
        fail("connection opened even though service disabled");
      } catch (TransportException err) {
        String exp = smartAuthNoneURI + ": git-upload-pack not permitted";
        assertEquals(exp, err.getMessage());
      }
    } finally {
      t.close();
    }
  }
  @Test
  public void testListRemote_Dumb_NoHEAD() throws Exception {
    FileRepository src = remoteRepository.getRepository();
    File headref = new File(src.getDirectory(), Constants.HEAD);
    assertTrue("HEAD used to be present", headref.delete());
    assertFalse("HEAD is gone", headref.exists());

    Repository dst = createBareRepository();
    Ref head;
    Transport t = Transport.open(dst, dumbAuthNoneURI);
    try {
      FetchConnection c = t.openFetch();
      try {
        head = c.getRef(Constants.HEAD);
      } finally {
        c.close();
      }
    } finally {
      t.close();
    }
    assertNull("has no " + Constants.HEAD, head);
  }
  @Test
  public void testListRemote_Smart_DetachedHEAD() throws Exception {
    Repository src = remoteRepository.getRepository();
    RefUpdate u = src.updateRef(Constants.HEAD, true);
    RevCommit Q = remoteRepository.commit().message("Q").create();
    u.setNewObjectId(Q);
    assertEquals(RefUpdate.Result.FORCED, u.forceUpdate());

    Repository dst = createBareRepository();
    Ref head;
    Transport t = Transport.open(dst, smartAuthNoneURI);
    try {
      FetchConnection c = t.openFetch();
      try {
        head = c.getRef(Constants.HEAD);
      } finally {
        c.close();
      }
    } finally {
      t.close();
    }
    assertNotNull("has " + Constants.HEAD, head);
    assertEquals(Q, head.getObjectId());
  }
 @Test
 public void testListRemote_Dumb_Auth() throws Exception {
   Repository dst = createBareRepository();
   Transport t = Transport.open(dst, dumbAuthBasicURI);
   t.setCredentialsProvider(
       new UsernamePasswordCredentialsProvider(AppServer.username, AppServer.password));
   try {
     t.openFetch();
   } finally {
     t.close();
   }
   t = Transport.open(dst, dumbAuthBasicURI);
   t.setCredentialsProvider(new UsernamePasswordCredentialsProvider(AppServer.username, ""));
   try {
     t.openFetch();
     fail("connection opened even info/refs needs auth basic and we provide wrong password");
   } catch (TransportException err) {
     String exp = dumbAuthBasicURI + ": " + JGitText.get().notAuthorized;
     assertEquals(exp, err.getMessage());
   } finally {
     t.close();
   }
 }
 /**
  * Creates a push operation specification for the given push uris to the given push operation
  * specification.
  *
  * @param pushURIs the push uri's
  * @param pushRefSpecs the push ref specs
  * @param repository the repository
  * @return the push operation specification
  * @throws CoreException the core exception
  */
 private static PushOperationSpecification createPushSpec(
     Collection<URIish> pushURIs, Collection<RefSpec> pushRefSpecs, Repository repository)
     throws CoreException {
   try {
     PushOperationSpecification pushSpec = new PushOperationSpecification();
     for (URIish uri : pushURIs) {
       Collection<RemoteRefUpdate> remoteRefUpdates =
           Transport.open(repository, uri).findRemoteRefUpdatesFor(pushRefSpecs);
       pushSpec.addURIRefUpdates(uri, remoteRefUpdates);
     }
     return pushSpec;
   } catch (NotSupportedException e) {
     throw new CoreException(
         createStatus(
             e, "Could not connect repository \"{0}\" to a remote", repository.toString()));
   } catch (IOException e) {
     throw new CoreException(
         createStatus(
             e,
             "Could not convert remote specifications for repository \"{0}\" to a remote",
             repository.toString()));
   }
 }
  private boolean canHandleProtocol(URIish u) {
    for (TransportProtocol proto : Transport.getTransportProtocols())
      if (proto.canHandle(u)) return true;

    return false;
  }