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 all 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
23 changes: 16 additions & 7 deletions src/main/java/hudson/plugins/git/GitAPI.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package hudson.plugins.git;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.model.TaskListener;
import java.io.*;
import java.util.List;
import java.util.Set;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Repository;
Expand Down Expand Up @@ -113,9 +116,9 @@ public String getRemoteUrl(String name) throws GitException, InterruptedExceptio
@Override
public void push(String remoteName, String refspec) throws GitException, InterruptedException {
if (Git.USE_CLI) {
super.push(remoteName, refspec);
super.push(remoteName, refspec.trim());
} else {
jgit.push(remoteName, refspec);
jgit.push(remoteName, refspec.trim());
}
}

Expand Down Expand Up @@ -368,10 +371,16 @@ public List<String> showRevision(ObjectId r) throws GitException {
@SuppressWarnings("deprecation")
public void fetch(URIish url, List<RefSpec> refspecs) throws GitException, InterruptedException {
/* Intentionally using the deprecated method because the replacement method is not serializable. */
List<RefSpec> trimmedRefSpecs = new ArrayList<>();
for (RefSpec rs : refspecs) {
if (rs != null) {
trimmedRefSpecs.add(new RefSpec(rs.toString().trim()));
}
}
if (Git.USE_CLI) {
super.fetch(url, refspecs);
super.fetch(url, trimmedRefSpecs);
} else {
jgit.fetch(url, refspecs);
jgit.fetch(url, trimmedRefSpecs);
}
}

Expand Down
82 changes: 50 additions & 32 deletions src/main/java/org/jenkinsci/plugins/gitclient/CliGitAPIImpl.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
package org.jenkinsci.plugins.gitclient;

import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Launcher.LocalLauncher;
import hudson.Proc;
import hudson.Util;
import hudson.console.HyperlinkNote;
import hudson.model.TaskListener;
import hudson.plugins.git.Branch;
import hudson.plugins.git.GitException;
import hudson.plugins.git.GitLockFailedException;
import hudson.plugins.git.GitObject;
import hudson.plugins.git.IGitAPI;
import hudson.plugins.git.IndexEntry;
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 @@ -59,6 +45,30 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Launcher.LocalLauncher;
import hudson.Proc;
import hudson.Util;
import hudson.console.HyperlinkNote;
import hudson.model.TaskListener;
import hudson.plugins.git.Branch;
import hudson.plugins.git.GitException;
import hudson.plugins.git.GitLockFailedException;
import hudson.plugins.git.GitObject;
import hudson.plugins.git.IGitAPI;
import hudson.plugins.git.IndexEntry;
import hudson.plugins.git.Revision;
import hudson.util.ArgumentListBuilder;
import hudson.util.Secret;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -524,7 +534,13 @@ public FetchCommand fetch_() {
@Override
public FetchCommand from(URIish remote, List<RefSpec> refspecs) {
this.url = remote;
this.refspecs = refspecs;
List<RefSpec> trimmedRefSpecs = new ArrayList<>();
if (refspecs != null && !refspecs.isEmpty()) {
for (RefSpec rs : refspecs) {
trimmedRefSpecs.add(new RefSpec(rs.toString().trim()));
}
}
this.refspecs = trimmedRefSpecs;
return this;
}

Expand Down Expand Up @@ -609,11 +625,9 @@ public void execute() throws GitException, InterruptedException {
addCheckedRemoteUrl(args, url.toString());
}

if (refspecs != null) {
if (refspecs != null && !refspecs.isEmpty()) {
for (RefSpec rs : refspecs) {
if (rs != null) {
args.add(rs.toString());
}
args.add(rs.toString().trim());
}
}

Expand Down Expand Up @@ -667,7 +681,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 +803,11 @@ public CloneCommand depth(Integer depth) {

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

Expand Down Expand Up @@ -2885,7 +2903,7 @@ public PushCommand to(URIish remote) {

@Override
public PushCommand ref(String refspec) {
this.refspec = refspec;
this.refspec = refspec.trim();
return this;
}

Expand Down
51 changes: 30 additions & 21 deletions src/main/java/org/jenkinsci/plugins/gitclient/JGitAPIImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@
import static org.jenkinsci.plugins.gitclient.CliGitAPIImpl.TIMEOUT;
import static org.jenkinsci.plugins.gitclient.CliGitAPIImpl.TIMEOUT_LOG_PREFIX;

import com.cloudbees.plugins.credentials.common.StandardCredentials;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.FilePath;
import hudson.Util;
import hudson.model.TaskListener;
import hudson.plugins.git.Branch;
import hudson.plugins.git.GitException;
import hudson.plugins.git.GitLockFailedException;
import hudson.plugins.git.GitObject;
import hudson.plugins.git.IndexEntry;
import hudson.plugins.git.Revision;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
Expand All @@ -49,6 +37,19 @@
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import com.cloudbees.plugins.credentials.common.StandardCredentials;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.FilePath;
import hudson.Util;
import hudson.model.TaskListener;
import hudson.plugins.git.Branch;
import hudson.plugins.git.GitException;
import hudson.plugins.git.GitLockFailedException;
import hudson.plugins.git.GitObject;
import hudson.plugins.git.IndexEntry;
import hudson.plugins.git.Revision;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.time.FastDateFormat;
import org.eclipse.jgit.api.AddNoteCommand;
Expand Down Expand Up @@ -624,7 +625,13 @@ public org.jenkinsci.plugins.gitclient.FetchCommand fetch_() {
@Override
public org.jenkinsci.plugins.gitclient.FetchCommand from(URIish remote, List<RefSpec> refspecs) {
this.url = remote;
this.refspecs = refspecs;
List<RefSpec> trimmedRefSpecs = new ArrayList<>();
if (refspecs != null && !refspecs.isEmpty()) {
for (RefSpec rs : refspecs) {
trimmedRefSpecs.add(new RefSpec(rs.toString().trim()));
}
}
this.refspecs = trimmedRefSpecs;
return this;
}

Expand Down Expand Up @@ -669,11 +676,9 @@ public void execute() throws GitException {
Git git = git(repo);

List<RefSpec> allRefSpecs = new ArrayList<>();
if (refspecs != null) {
if (refspecs != null && !refspecs.isEmpty()) {
for (RefSpec rs : refspecs) {
if (rs != null) {
allRefSpecs.add(rs);
}
allRefSpecs.add(rs);
}
}

Expand Down Expand Up @@ -1432,7 +1437,11 @@ public CloneCommand reference(String reference) {

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

Expand Down Expand Up @@ -1992,7 +2001,7 @@ public PushCommand to(URIish remote) {

@Override
public PushCommand ref(String refspec) {
this.refspec = refspec;
this.refspec = refspec.trim();
return this;
}

Expand Down Expand Up @@ -2023,8 +2032,8 @@ public PushCommand timeout(Integer timeout) {
public void execute() throws GitException {
try (Repository repo = getRepository()) {
RefSpec ref =
(refspec != null) ? new RefSpec(fixRefSpec(refspec, repo)) : Transport.REFSPEC_PUSH_ALL;
listener.getLogger().println("RefSpec is \"" + ref + "\".");
(refspec != null) ? new RefSpec(fixRefSpec(refspec.trim(), repo)) : Transport.REFSPEC_PUSH_ALL;
listener.getLogger().println("RefSpec is \"" + ref.toString().trim() + "\".");
Git g = git(repo);
Config config = g.getRepository().getConfig();
if (remote == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
import static java.util.Arrays.copyOfRange;
import static org.apache.commons.lang.StringUtils.join;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import hudson.model.TaskListener;
import hudson.plugins.git.GitException;
import hudson.plugins.git.IGitAPI;
import hudson.plugins.git.IndexEntry;
import hudson.plugins.git.Revision;
import hudson.plugins.git.Tag;
import hudson.remoting.Channel;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
Expand Down Expand Up @@ -107,7 +108,7 @@ public void setupSubmoduleUrls(String remote, TaskListener listener) throws GitE
@Override
@Deprecated
public void fetch(String repository, String refspec) throws GitException, InterruptedException {
fetch(repository, new RefSpec(refspec));
fetch(repository, new RefSpec(refspec.trim()));
}

/** {@inheritDoc} */
Expand Down Expand Up @@ -146,7 +147,7 @@ public void reset() throws GitException, InterruptedException {
@Override
@Deprecated
public void push(URIish url, String refspec) throws GitException, InterruptedException {
push().ref(refspec).to(url).execute();
push().ref(refspec.trim()).to(url).execute();
}

/** {@inheritDoc} */
Expand All @@ -159,7 +160,7 @@ public void push(String remoteName, String refspec) throws GitException, Interru
}

try {
push(new URIish(url), refspec);
push(new URIish(url), refspec.trim());
} catch (URISyntaxException e) {
throw new GitException("bad repository URL", e);
}
Expand Down
Loading