Skip to content

Commit

Permalink
JENKINS-33445: Retrieve tags as well as branches for SCMSource support
Browse files Browse the repository at this point in the history
  • Loading branch information
deece committed Jan 8, 2017
1 parent 6e13431 commit ad4ee53
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 17 deletions.
28 changes: 28 additions & 0 deletions src/main/java/hudson/plugins/git/GitSCM.java
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,16 @@ public boolean isCreateAccountBasedOnEmail() {
return (gitDescriptor != null && gitDescriptor.isCreateAccountBasedOnEmail());
}

public boolean isShowRemoteBranches() {
DescriptorImpl gitDescriptor = getDescriptor();
return (gitDescriptor != null && gitDescriptor.isShowRemoteBranches());
}

public boolean isShowTags() {
DescriptorImpl gitDescriptor = getDescriptor();
return (gitDescriptor != null && gitDescriptor.isShowTags());
}

public BuildChooser getBuildChooser() {
BuildChooser bc;

Expand Down Expand Up @@ -1336,6 +1346,8 @@ public static final class DescriptorImpl extends SCMDescriptor<GitSCM> {
private String globalConfigName;
private String globalConfigEmail;
private boolean createAccountBasedOnEmail;
private boolean showRemoteBranches;
private boolean showTags;
// private GitClientType defaultClientType = GitClientType.GITCLI;

public DescriptorImpl() {
Expand Down Expand Up @@ -1427,6 +1439,22 @@ public void setCreateAccountBasedOnEmail(boolean createAccountBasedOnEmail) {
this.createAccountBasedOnEmail = createAccountBasedOnEmail;
}

public boolean isShowRemoteBranches() {
return showRemoteBranches;
}

public void setShowRemoteBranches(boolean showRemoteBranches) {
this.showRemoteBranches = showRemoteBranches;
}

public boolean isShowTags() {
return showTags;
}

public void setShowTags(boolean showTags) {
this.showTags = showTags;
}

/**
* Old configuration of git executable - exposed so that we can
* migrate this setting to GitTool without deprecation warnings.
Expand Down
90 changes: 74 additions & 16 deletions src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,21 @@
import hudson.EnvVars;
import hudson.Extension;
import hudson.Util;
import hudson.model.Hudson;
import hudson.model.Item;
import hudson.model.TaskListener;
import hudson.plugins.git.Tag;
import hudson.plugins.git.Branch;
import hudson.plugins.git.BranchSpec;
import hudson.plugins.git.GitException;
import hudson.plugins.git.GitObject;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.GitTool;
import hudson.plugins.git.Revision;
import hudson.plugins.git.SubmoduleConfig;
import hudson.plugins.git.UserRemoteConfig;
import hudson.plugins.git.browser.GitRepositoryBrowser;
import hudson.plugins.git.GitSCM.DescriptorImpl;
import hudson.plugins.git.extensions.GitSCMExtension;
import hudson.plugins.git.extensions.impl.BuildChooserSetting;
import hudson.plugins.git.util.Build;
Expand Down Expand Up @@ -213,6 +217,36 @@ private <T> T doRetrieve(Retriever<T> retriever, @NonNull TaskListener listener,
}
}

/**
* Add the provided list of git tags to the list of git objects
* @param tags the list of tags
* @param list the list of GitObjects to add the tags to
* @param listener the listener we will use for logging
*/
private void addTagsToGitObjects(@NonNull Set<String> tags, @NonNull List<GitObject> list,
@NonNull TaskListener listener)
throws InterruptedException {
for (String tagName : tags) {
if (tagName.endsWith("^{}")) {
continue;
}

Tag tag = new Tag(tagName, null);
boolean exists = false;

for (GitObject item : list) {
if (item.getName().equals(tagName)) {
exists = true;
break;
}
}

if (!exists) {
list.add(tag);
}
}
}

@CheckForNull
@Override
protected SCMRevision retrieve(@NonNull final SCMHead head, @NonNull TaskListener listener)
Expand Down Expand Up @@ -241,21 +275,45 @@ protected void retrieve(@CheckForNull final SCMSourceCriteria criteria,
@Override
public Void run(GitClient client, String remoteName) throws IOException, InterruptedException {
final Repository repository = client.getRepository();
listener.getLogger().println("Getting remote branches...");
listener.getLogger().println("Getting remote branches/tags...");
try (RevWalk walk = new RevWalk(repository)) {
walk.setRetainBody(false);
for (Branch b : client.getRemoteBranches()) {

List<GitObject> gitObjects = new ArrayList<GitObject>();
gitObjects.addAll(client.getRemoteBranches());

Hudson hudson = Hudson.getInstance();
DescriptorImpl descriptor = (DescriptorImpl) hudson.getDescriptor(GitSCM.class);

if (descriptor.isShowTags()) {
listener.getLogger().println("Adding Tags...");
addTagsToGitObjects(client.getRemoteTagNames(null), gitObjects, listener);
addTagsToGitObjects(client.getTagNames(null), gitObjects, listener);
}

for (GitObject gitObject : gitObjects) {
checkInterrupt();
if (!b.getName().startsWith(remoteName + "/")) {

final String name = StringUtils.removeStart(gitObject.getName(), remoteName + "/");
listener.getLogger().println("Checking branch/tag " + name);
if (isExcluded(name)){
continue;
}
final String branchName = StringUtils.removeStart(b.getName(), remoteName + "/");
listener.getLogger().println("Checking branch " + branchName);
if (isExcluded(branchName)){
continue;

if (gitObject instanceof Tag) {
ObjectId commit = client.revList(gitObject.getName()).get(0);
String temp = commit.toString();
String sha1 = temp.substring(temp.indexOf("[") + 1, temp.indexOf("]"));
/* Recreate the Tag with the commit
* We do this here here rather than initially, as fetching the commit info
* takes time, and can take a considerable amount of time to sync branches if there
* are many tags
*/
gitObject = new GitObject(gitObject.getName(), commit);
}

if (criteria != null) {
RevCommit commit = walk.parseCommit(b.getSHA1());
RevCommit commit = walk.parseCommit(gitObject.getSHA1());
final long lastModified = TimeUnit.SECONDS.toMillis(commit.getCommitTime());
final RevTree tree = commit.getTree();
SCMSourceCriteria.Probe probe = new SCMProbe() {
Expand All @@ -266,7 +324,7 @@ public void close() throws IOException {

@Override
public String name() {
return branchName;
return name;
}

@Override
Expand Down Expand Up @@ -311,8 +369,8 @@ public SCMProbeStat stat(@NonNull String path) throws IOException {
continue;
}
}
SCMHead head = new SCMHead(branchName);
SCMRevision hash = new SCMRevisionImpl(head, b.getSHA1String());
SCMHead head = new SCMHead(name);
SCMRevision hash = new SCMRevisionImpl(head, gitObject.getSHA1String());
observer.observe(head, hash);
if (!observer.isObserving()) {
return null;
Expand Down Expand Up @@ -434,20 +492,20 @@ protected List<UserRemoteConfig> getRemoteConfigs() {
}
return result;
}

/**
* Returns true if the branchName isn't matched by includes or is matched by excludes.
*
*
* @param branchName name of branch to be tested
* @return true if branchName is excluded or is not included
*/
protected boolean isExcluded (String branchName){
return !Pattern.matches(getPattern(getIncludes()), branchName) || (Pattern.matches(getPattern(getExcludes()), branchName));
}

/**
* Returns the pattern corresponding to the branches containing wildcards.
*
* Returns the pattern corresponding to the branches containing wildcards.
*
* @param branches branch names to evaluate
* @return pattern corresponding to the branches containing wildcards
*/
Expand Down
18 changes: 17 additions & 1 deletion src/main/java/jenkins/plugins/git/GitSCMSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.Util;
import hudson.model.Hudson;
import hudson.model.Item;
import hudson.model.Descriptor;
import hudson.model.ParameterValue;
import hudson.model.Queue;
import hudson.model.queue.Tasks;
import hudson.plugins.git.GitStatus;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.GitSCM.DescriptorImpl;
import hudson.plugins.git.browser.GitRepositoryBrowser;
import hudson.plugins.git.extensions.GitSCMExtensionDescriptor;
import hudson.plugins.git.extensions.GitSCMExtension;
Expand Down Expand Up @@ -81,6 +83,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.LinkedList;
import java.util.logging.Logger;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
Expand Down Expand Up @@ -187,7 +190,20 @@ public String getExcludes() {

@Override
protected List<RefSpec> getRefSpecs() {
return Arrays.asList(new RefSpec("+refs/heads/*:refs/remotes/" + getRemoteName() + "/*"));
LinkedList<RefSpec> specs = new LinkedList<RefSpec>();
specs.add(new RefSpec("+refs/heads/*:refs/remotes/" + getRemoteName() + "/*"));

Hudson hudson = Hudson.getInstance();
if (hudson == null) {
return specs;
}

hudson.plugins.git.GitSCM.DescriptorImpl descriptor = (hudson.plugins.git.GitSCM.DescriptorImpl) hudson.getDescriptor(GitSCM.class);
if (descriptor.isShowRemoteBranches()) {
specs.add(new RefSpec("+refs/remotes/*:refs/remotes/*"));
}

return specs;
}

@Extension
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/hudson/plugins/git/GitSCM/global.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
<f:entry title="${%Create new accounts base on author/committer's email}" field="createAccountBasedOnEmail">
<f:checkbox name="createAccountBasedOnEmail" checked="${descriptor.createAccountBasedOnEmail}"/>
</f:entry>
<f:entry title="Show remote branches in SCMSource" field="showRemoteBranches">
<f:checkbox name="showRemoteBranches" checked="${descriptor.showRemoteBranches}"/>
</f:entry>
<f:entry title="Show tags in SCMSource" field="showTags">
<f:checkbox name="showTags" checked="${descriptor.showTags}"/>
</f:entry>
<!--
<f:entry title="${%Default git client implementation}" field="defaultClientType">
<select>
Expand Down

0 comments on commit ad4ee53

Please sign in to comment.