Skip to content
Permalink
Browse files

[JENKINS-10131] ls-remote for general usage to lookup all branches

  • Loading branch information
ndeloof committed Oct 23, 2013
1 parent 2d88a4e commit 649c08617ff01191028e2e744013ed64e8a14431
@@ -1238,6 +1238,26 @@ public String getTagMessage(String tagName) throws GitException, InterruptedExce
return out.substring(tagName.length()).replaceAll("(?m)(^ )", "").trim();
}

public Map<String, ObjectId> getHeadRev(String url) throws GitException, InterruptedException {
ArgumentListBuilder args = new ArgumentListBuilder("ls-remote");
args.add("-h");

StandardCredentials cred = credentials.get(url);
if (cred == null) cred = defaultCredentials;

String urlWithCrendentials = getURLWithCrendentials(url, cred);
args.add(urlWithCrendentials);
String result = launchCommandWithCredentials(args, null, cred, urlWithCrendentials, url);

Map<String, ObjectId> heads = new HashMap<String, ObjectId>();
String[] lines = result.split("\n");
for (String line : lines) {
if (line.length() < 41) throw new GitException("unexpected ls-remote output " + line);
heads.put(line.substring(41), ObjectId.fromString(result.substring(0, 40)));
}
return heads;
}

public ObjectId getHeadRev(String url, String branch) throws GitException, InterruptedException {
String[] branchExploded = branch.split("/");
branch = branchExploded[branchExploded.length-1];
@@ -1368,7 +1388,7 @@ private String getURLWithCrendentials(URIish u, StandardCredentials cred) {
client.getState().setCredentials(AuthScope.ANY, defaultcreds);
}

ProxyConfiguration proxy = Jenkins.getInstance().proxy;
ProxyConfiguration proxy = Jenkins.getInstance() != null ? Jenkins.getInstance().proxy : null; // null when running unit tests
if (proxy != null) {
client.getHostConfiguration().setProxy(proxy.name, proxy.port);
client.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials(proxy.getUserName(), proxy.getPassword()));
@@ -22,6 +22,7 @@
import java.io.OutputStream;
import java.io.Writer;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
@@ -260,6 +261,8 @@

// --- lookup revision

Map<String, ObjectId> getHeadRev(String url) throws GitException, InterruptedException;

ObjectId getHeadRev(String remoteRepoUrl, String branch) throws GitException, InterruptedException;

/**
@@ -380,4 +383,5 @@
String describe(String commitIsh) throws GitException, InterruptedException;

void setCredentials(StandardUsernameCredentials cred);

}
@@ -375,6 +375,30 @@ public void fetch(String remoteName, RefSpec refspec) throws GitException {
fetch(remoteName, new RefSpec[] {refspec});
}

public Map<String, ObjectId> getHeadRev(String url) throws GitException, InterruptedException {
Map<String, ObjectId> heads = new HashMap<String, ObjectId>();
try {
Repository repo = openDummyRepository();
final Transport tn = Transport.open(repo, new URIish(url));
tn.setCredentialsProvider(getProvider());
final FetchConnection c = tn.openFetch();
try {
for (final Ref r : c.getRefs()) {
heads.put(r.getName(), r.getPeeledObjectId() != null ? r.getPeeledObjectId() : r.getObjectId());
}
} finally {
c.close();
tn.close();
repo.close();
}
} catch (IOException e) {
throw new GitException(e);
} catch (URISyntaxException e) {
throw new GitException(e);
}
return heads;
}

public ObjectId getHeadRev(String remoteRepoUrl, String branch) throws GitException {
try {
if (!branch.startsWith(R_HEADS))
@@ -34,6 +34,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
@@ -358,6 +359,10 @@ public void deleteTag(String tagName) throws GitException, InterruptedException
return proxy.getTagNames(tagPattern);
}

public Map<String, ObjectId> getHeadRev(String url) throws GitException, InterruptedException {
return proxy.getHeadRev(url);
}

public ObjectId getHeadRev(String remoteRepoUrl, String branch) throws GitException, InterruptedException {
return proxy.getHeadRev(remoteRepoUrl, branch);
}
@@ -28,6 +28,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

@@ -678,6 +679,13 @@ public void test_merge_strategy_correct_fail() throws Exception {
}
}

public void test_getHeadRev() throws Exception {
Map<String,ObjectId> heads = w.git.getHeadRev("https://github.com/jenkinsci/git-client-plugin.git");
System.out.println(heads);
assertTrue(heads.containsKey("refs/heads/master"));
}


public void test_show_revision_for_merge() throws Exception {
w = clone(localMirror());
ObjectId from = ObjectId.fromString("45e76942914664ee19f31d90e6f2edbfe0d13a46");

0 comments on commit 649c086

Please sign in to comment.
You can’t perform that action at this time.