Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
JENKINS-26417 Allow customization of refspec/tags of the clone/fetch …
…commands

- Add the option to omit tags to FetchCommand and CloneCommand
- Allow use of custom refspecs for CloneCommand
  • Loading branch information
Vincent LATOMBE authored and MarkEWaite committed Jan 24, 2015
1 parent 9e4c9f4 commit 1a5f40d
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 15 deletions.
40 changes: 34 additions & 6 deletions src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java
Expand Up @@ -12,6 +12,7 @@
import hudson.Functions;
import hudson.Launcher;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import hudson.Launcher.LocalLauncher;
import hudson.Util;
import hudson.model.TaskListener;
Expand Down Expand Up @@ -231,13 +232,19 @@ public FetchCommand fetch_() {
public boolean prune;
public boolean shallow;
public Integer timeout;
public boolean tags = true;

public FetchCommand from(URIish remote, List<RefSpec> refspecs) {
this.url = remote;
this.refspecs = refspecs;
return this;
}

public FetchCommand tags(boolean tags) {
this.tags = tags;
return this;
}

public FetchCommand prune() {
this.prune = true;
return this;
Expand All @@ -258,7 +265,8 @@ public void execute() throws GitException, InterruptedException {
"Fetching upstream changes from " + url);

ArgumentListBuilder args = new ArgumentListBuilder();
args.add("fetch", "--tags");
args.add("fetch");
args.add(tags ? "--tags" : "--no-tags");
if (isAtLeastVersion(1,7,1,0))
args.add("--progress");

Expand Down Expand Up @@ -341,6 +349,8 @@ public CloneCommand clone_() {
String reference;
boolean shallow,shared;
Integer timeout;
boolean tags = true;
List<RefSpec> refspecs;

public CloneCommand url(String url) {
this.url = url;
Expand All @@ -367,6 +377,11 @@ public CloneCommand noCheckout() {
return this;
}

public CloneCommand tags(boolean tags) {
this.tags = tags;
return this;
}

public CloneCommand reference(String reference) {
this.reference = reference;
return this;
Expand All @@ -377,6 +392,11 @@ public CloneCommand timeout(Integer timeout) {
return this;
}

public CloneCommand refspecs(List<RefSpec> refspecs) {
this.refspecs = new ArrayList<RefSpec>(refspecs);
return this;
}

public void execute() throws GitException, InterruptedException {

URIish urIish = null;
Expand Down Expand Up @@ -439,13 +459,18 @@ else if (!referencePath.isDirectory())
}
}

RefSpec refSpec = new RefSpec("+refs/heads/*:refs/remotes/"+origin+"/*");
fetch_().from(urIish, Collections.singletonList(refSpec))
if (refspecs == null) {
refspecs = Collections.singletonList(new RefSpec("+refs/heads/*:refs/remotes/"+origin+"/*"));
}
fetch_().from(urIish, refspecs)
.shallow(shallow)
.timeout(timeout)
.execute();
.tags(tags)
.execute();;
setRemoteUrl(origin, url);
launchCommand("config", "remote." + origin + ".fetch", refSpec.toString());
for (RefSpec refSpec : refspecs) {
launchCommand("config", "remote." + origin + ".fetch", refSpec.toString());
}
}

};
Expand Down Expand Up @@ -1554,7 +1579,10 @@ protected Set<Branch> parseBranches(String fos) throws GitException, Interrupted
}

public Set<Branch> getBranches() throws GitException, InterruptedException {
return parseBranches(launchCommand("branch", "-a"));
// git branch -a will return remote branches prefixed by remotes/,
// while git branch -r will return them with the correct name
return Sets.union(parseBranches(launchCommand("branch")),
parseBranches(launchCommand("branch", "-r")));
}

public Set<Branch> getRemoteBranches() throws GitException, InterruptedException {
Expand Down
@@ -1,5 +1,9 @@
package org.jenkinsci.plugins.gitclient;

import java.util.List;

import org.eclipse.jgit.transport.RefSpec;

/**
* Command to clone a repository. This command behaves differently from CLI clone command, it never actually checks out
* into the workspace.
Expand Down Expand Up @@ -32,4 +36,8 @@ public interface CloneCommand extends GitCommand {
*/
@Deprecated
CloneCommand noCheckout();

CloneCommand tags(boolean tags);

CloneCommand refspecs(List<RefSpec> refspecs);
}
Expand Up @@ -17,4 +17,6 @@ public interface FetchCommand extends GitCommand {
FetchCommand shallow(boolean shallow);

FetchCommand timeout(Integer timeout);

FetchCommand tags(boolean tags);
}
44 changes: 35 additions & 9 deletions src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java
Expand Up @@ -491,6 +491,7 @@ public org.jenkinsci.plugins.gitclient.FetchCommand fetch_() {
// JGit 3.3.0 thru 3.6.0 prune more branches than expected
// Refer to GitAPITestCase.test_fetch_with_prune()
// private boolean shouldPrune = false;
public boolean tags = true;

public org.jenkinsci.plugins.gitclient.FetchCommand from(URIish remote, List<RefSpec> refspecs) {
this.url = remote;
Expand All @@ -513,22 +514,30 @@ public org.jenkinsci.plugins.gitclient.FetchCommand timeout(Integer timeout) {
return this;
}

public org.jenkinsci.plugins.gitclient.FetchCommand tags(boolean tags) {
this.tags = tags;
return this;
}

public void execute() throws GitException, InterruptedException {
Repository repo = null;
FetchCommand fetch = null;
try {
repo = getRepository();
fetch = git(repo).fetch().setTagOpt(TagOpt.FETCH_TAGS);
fetch = git(repo).fetch();
fetch.setTagOpt(tags ? TagOpt.FETCH_TAGS : TagOpt.NO_TAGS);
fetch.setRemote(url.toString());
fetch.setCredentialsProvider(getProvider());

// see http://stackoverflow.com/questions/14876321/jgit-fetch-dont-update-tag
List<RefSpec> refSpecs = new ArrayList<RefSpec>();
refSpecs.add(new RefSpec("+refs/tags/*:refs/tags/*"));
if (tags) {
// see http://stackoverflow.com/questions/14876321/jgit-fetch-dont-update-tag
refSpecs.add(new RefSpec("+refs/tags/*:refs/tags/*"));
}
if (refspecs != null)
for (RefSpec rs: refspecs)
if (rs != null)
refSpecs.add(rs);
for (RefSpec rs: refspecs)
if (rs != null)
refSpecs.add(rs);
fetch.setRefSpecs(refSpecs);
// fetch.setRemoveDeletedRefs(shouldPrune);

Expand Down Expand Up @@ -1161,6 +1170,8 @@ public CloneCommand clone_() {
String reference;
Integer timeout;
boolean shared;
boolean tags = true;
List<RefSpec> refspecs;

public CloneCommand url(String url) {
this.url = url;
Expand All @@ -1187,11 +1198,21 @@ public CloneCommand reference(String reference) {
return this;
}

public CloneCommand refspecs(List<RefSpec> refspecs) {
this.refspecs = new ArrayList<RefSpec>(refspecs);
return this;
}

public CloneCommand timeout(Integer timeout) {
this.timeout = timeout;
return this;
}

public CloneCommand tags(boolean tags) {
this.tags = tags;
return this;
}

public CloneCommand noCheckout() {
// this.noCheckout = true; ignored, we never do a checkout
return this;
Expand Down Expand Up @@ -1261,18 +1282,23 @@ else if (!referencePath.isDirectory())
repository.close();
repository = getRepository();

RefSpec refSpec = new RefSpec("+refs/heads/*:refs/remotes/"+remote+"/*");
if (refspecs == null) {
refspecs = Collections.singletonList(new RefSpec("+refs/heads/*:refs/remotes/"+remote+"/*"));
}
FetchCommand fetch = new Git(repository).fetch()
.setProgressMonitor(new JGitProgressMonitor(listener))
.setRemote(url)
.setCredentialsProvider(getProvider())
.setRefSpecs(refSpec);
.setTagOpt(tags ? TagOpt.FETCH_TAGS : TagOpt.NO_TAGS)
.setRefSpecs(refspecs);
if (timeout != null) fetch.setTimeout(timeout);
fetch.call();

StoredConfig config = repository.getConfig();
config.setString("remote", remote, "url", url);
config.setString("remote", remote, "fetch", refSpec.toString());
for (RefSpec refSpec : refspecs) {
config.setString("remote", remote, "fetch", refSpec.toString());
}
config.save();

} catch (GitAPIException e) {
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/org/jenkinsci/plugins/gitclient/GitAPITestCase.java
Expand Up @@ -1152,6 +1152,28 @@ public void test_fetch_with_updated_tag() throws Exception {

}

@NotImplementedInJGit
public void test_fetch_shallow() throws Exception {
w.init();
w.git.setRemoteUrl("origin", localMirror());
w.git.fetch_().from(new URIish("origin"), Collections.singletonList(new RefSpec("refs/heads/*:refs/remotes/origin/*"))).shallow(true).execute();
check_remote_url("origin");
assertBranchesExist(w.git.getBranches(), "origin/master");
final String alternates = ".git" + File.separator + "objects" + File.separator + "info" + File.separator + "alternates";
assertFalse("Alternates file found: " + alternates, w.exists(alternates));
final String shallow = ".git" + File.separator + "shallow";
assertTrue("Shallow file not found: " + shallow, w.exists(shallow));
}

public void test_fetch_noTags() throws Exception {
w.init();
w.git.setRemoteUrl("origin", localMirror());
w.git.fetch_().from(new URIish("origin"), Collections.singletonList(new RefSpec("refs/heads/*:refs/remotes/origin/*"))).tags(false).execute();
check_remote_url("origin");
assertBranchesExist(w.git.getBranches(), "origin/master");
Set<String> tags = w.git.getTagNames("");
assertTrue("Tags have been found : " + tags, tags.isEmpty());
}

public void test_create_branch() throws Exception {
w.init();
Expand Down

0 comments on commit 1a5f40d

Please sign in to comment.