Skip to content

Commit

Permalink
[JENKINS-10131] ls-remote for general usage to lookup all branches
Browse files Browse the repository at this point in the history
  • Loading branch information
ndeloof committed Oct 23, 2013
1 parent 2d88a4e commit 649c086
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java
Expand Up @@ -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];
Expand Down Expand Up @@ -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()));
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/jenkinsci/plugins/gitclient/GitClient.java
Expand Up @@ -22,6 +22,7 @@
import java.io.OutputStream;
import java.io.Writer;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
Expand Down Expand Up @@ -260,6 +261,8 @@ public interface GitClient {

// --- lookup revision

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

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

/**
Expand Down Expand Up @@ -380,4 +383,5 @@ public interface GitClient {
String describe(String commitIsh) throws GitException, InterruptedException;

void setCredentials(StandardUsernameCredentials cred);

}
24 changes: 24 additions & 0 deletions src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java
Expand Up @@ -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))
Expand Down
Expand Up @@ -34,6 +34,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
Expand Down Expand Up @@ -358,6 +359,10 @@ public Set<String> getTagNames(String tagPattern) throws GitException, Interrupt
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);
}
Expand Down
Expand Up @@ -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;

Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 649c086

Please sign in to comment.