Beispiel #1
1
 @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() 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));
  }