Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-70303] Remove leading and trailing spaces from refspec #1096

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@
import hudson.plugins.git.Revision;
import hudson.util.ArgumentListBuilder;
import hudson.util.Secret;
import java.io.*;
MarkEWaite marked this conversation as resolved.
Show resolved Hide resolved
import java.io.BufferedReader;
MarkEWaite marked this conversation as resolved.
Show resolved Hide resolved
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -612,7 +621,7 @@ public void execute() throws GitException, InterruptedException {
if (refspecs != null) {
for (RefSpec rs : refspecs) {
if (rs != null) {
args.add(rs.toString());
args.add(rs.toString().trim());
}
}
}
Expand Down Expand Up @@ -667,7 +676,7 @@ public void fetch(String remoteName, RefSpec... refspec) throws GitException, In
if (refspec != null && refspec.length > 0) {
for (RefSpec rs : refspec) {
if (rs != null) {
args.add(rs.toString());
args.add(rs.toString().trim());
}
}
}
Expand Down Expand Up @@ -789,7 +798,11 @@ public CloneCommand depth(Integer depth) {

@Override
public CloneCommand refspecs(List<RefSpec> refspecs) {
this.refspecs = new ArrayList<>(refspecs);
List<RefSpec> refSpecsList = new ArrayList<RefSpec>();
for (RefSpec ref : refspecs) {
refSpecsList.add(new RefSpec(ref.toString().trim()));
}
this.refspecs = refSpecsList;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1432,7 +1432,11 @@ public CloneCommand reference(String reference) {

@Override
public CloneCommand refspecs(List<RefSpec> refspecs) {
this.refspecs = new ArrayList<>(refspecs);
List<RefSpec> refSpecsList = new ArrayList<RefSpec>();
for (RefSpec ref : refspecs) {
refSpecsList.add(new RefSpec(ref.toString().trim()));
}
this.refspecs = refSpecsList;
return this;
}

Expand Down Expand Up @@ -1550,7 +1554,7 @@ public void execute() throws GitException {
FetchCommand fetch = new Git(repository)
.fetch()
.setProgressMonitor(new JGitProgressMonitor(listener))
.setRemote(url)
.setRemote(url.trim())
.setCredentialsProvider(getProvider())
.setTagOpt(tags ? TagOpt.FETCH_TAGS : TagOpt.NO_TAGS)
.setRefSpecs(refspecs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.jvnet.hudson.test.Issue;

@RunWith(Parameterized.class)
public class GitClientCloneTest {
Expand Down Expand Up @@ -328,11 +329,16 @@ public void test_clone_refspec() throws Exception {
}));
}

private String randomSpace() {
return random.nextBoolean() ? " " : "";
}

@Test
@Issue("JENKINS-70303") // spaces at start or end of refspec should be ignored
public void test_clone_refspecs() throws Exception {
List<RefSpec> refspecs = Arrays.asList(
new RefSpec("+refs/heads/master:refs/remotes/origin/master"),
new RefSpec("+refs/heads/1.4.x:refs/remotes/origin/1.4.x"));
new RefSpec(randomSpace() + "+refs/heads/master:refs/remotes/origin/master" + randomSpace()),
new RefSpec(randomSpace() + "+refs/heads/1.4.x:refs/remotes/origin/1.4.x" + randomSpace()));
Comment on lines +340 to +341
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may be exactly the wrong place for this type of test. We generally prefer tests that are nearer to what a user would do. This is changing an argument to the RefSpec constructor when the real end user provides a string into a data entry field on a Jenkins form or they provide a string to an option of a Pipeline checkout step.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test case did pass in my local.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which test case tests the Pipeline checkout step?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that we'll be able to test Pipeline checkout from this plugin because it would require a dependency on the git plugin. That would result in a circular dependency, since the git plugin depends on this plugin. We'll need to test interactively.

testGitClient
.clone_()
.url(workspace.localMirror())
Expand Down