Skip to content
Permalink
Browse files

GitCommand takes care of sanitizing git messages

  • Loading branch information...
maheshp committed Feb 13, 2017
1 parent 7403f84 commit a837fc05cca60a9bbb8dcd7d3a3c7b1228a51358
@@ -424,6 +424,11 @@ public CommandLine withNonArgSecret(SecretString argument) {
return this;
}

public CommandLine withNonArgSecrets(List<SecretString> secrets) {
this.secrets.addAll(secrets);
return this;
}

public List<CommandArgument> getArguments() {
return arguments;
}
@@ -292,7 +292,7 @@ private void updateTo(GitMaterial material, RevisionContext revisionContext, Job
}

private GitCommand localRepoFor(GitMaterial material) {
return new GitCommand(material.getFingerprint(), workingDir, GitMaterialConfig.DEFAULT_BRANCH, false, new HashMap<>());
return new GitCommand(material.getFingerprint(), workingDir, GitMaterialConfig.DEFAULT_BRANCH, false, new HashMap<>(), null);
}

private List<File> allFilesIn(File directory, String prefixOfFiles) {

Large diffs are not rendered by default.

@@ -29,6 +29,7 @@
import com.thoughtworks.go.util.StringUtil;
import com.thoughtworks.go.util.command.InMemoryStreamConsumer;
import com.thoughtworks.go.util.command.ProcessOutputStreamConsumer;
import com.thoughtworks.go.util.command.SecretString;
import com.thoughtworks.go.util.command.UrlArgument;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.math.NumberUtils;
@@ -160,12 +161,13 @@ public void updateTo(ProcessOutputStreamConsumer outputStreamConsumer, File base
}

public ValidationBean checkConnection(final SubprocessExecutionContext execCtx) {
GitCommand gitCommand = new GitCommand(null, null, null, false, null, secrets());
try {
GitCommand.checkConnection(url, branch, execCtx.getDefaultEnvironmentVariables());
gitCommand.checkConnection(url, branch, execCtx.getDefaultEnvironmentVariables());
return ValidationBean.valid();
} catch (Exception e) {
try {
return handleException(e, GitCommand.version(execCtx.getDefaultEnvironmentVariables()));
return handleException(e, gitCommand.version(execCtx.getDefaultEnvironmentVariables()));
} catch (Exception notInstallGitException) {
return ValidationBean.notValid(ERR_GIT_NOT_FOUND);
}
@@ -213,10 +215,10 @@ private GitCommand getGit(File workingdir, int preferredCloneDepth, SubprocessEx

private GitCommand git(ProcessOutputStreamConsumer outputStreamConsumer, final File workingFolder, int preferredCloneDepth, SubprocessExecutionContext executionContext) throws Exception {
if (isSubmoduleFolder()) {
return new GitCommand(getFingerprint(), new File(workingFolder.getPath()), GitMaterialConfig.DEFAULT_BRANCH, true, executionContext.getDefaultEnvironmentVariables());
return new GitCommand(getFingerprint(), new File(workingFolder.getPath()), GitMaterialConfig.DEFAULT_BRANCH, true, executionContext.getDefaultEnvironmentVariables(), secrets());
}

GitCommand gitCommand = new GitCommand(getFingerprint(), workingFolder, getBranch(), false, executionContext.getDefaultEnvironmentVariables());
GitCommand gitCommand = new GitCommand(getFingerprint(), workingFolder, getBranch(), false, executionContext.getDefaultEnvironmentVariables(), secrets());
if (!isGitRepository(workingFolder) || isRepositoryChanged(gitCommand, workingFolder)) {
if (LOG.isDebugEnabled()) {
LOG.debug("Invalid git working copy or repository changed. Delete folder: " + workingFolder);
@@ -249,6 +251,16 @@ public void afterCompletion(int status) {
return gitCommand;
}

private List<SecretString> secrets() {
SecretString secretSubstitution = new SecretString() {
@Override
public String replaceSecretInfo(String line) {
return line.replace(url.forCommandline(), url.forDisplay());
}
};

return Arrays.asList(secretSubstitution);
}
// Unshallow local repo to include a revision operating on via two step process:
// First try to fetch forward 100 level with "git fetch -depth 100". If revision still missing,
// unshallow the whole repo with "git fetch --2147483647".
@@ -282,7 +294,7 @@ private boolean isRepositoryChanged(GitCommand command, File workingDirectory) {
LOG.trace("Current repository url of [" + workingDirectory + "]: " + currentWorkingUrl);
LOG.trace("Target repository url: " + url);
}
return !MaterialUrl.sameUrl(url.forCommandline(), currentWorkingUrl.forCommandline())
return !MaterialUrl.sameUrl(url.forDisplay(), currentWorkingUrl.forCommandline())
|| !isBranchEqual(command)
|| (!shallowClone && command.isShallow());
}
@@ -42,13 +42,16 @@
private static final Pattern GIT_DIFF_TREE_PATTERN = Pattern.compile("^(.)\\s+(.+)$");

private final File workingDir;
private final List<SecretString> secrets;
private final String branch;
private final boolean isSubmodule;
private Map<String, String> environment;

public GitCommand(String materialFingerprint, File workingDir, String branch, boolean isSubmodule, Map<String, String> environment) {
public GitCommand(String materialFingerprint, File workingDir, String branch, boolean isSubmodule, Map<String,
String> environment, List<SecretString> secrets) {
super(materialFingerprint);
this.workingDir = workingDir;
this.secrets = secrets != null ? secrets : new ArrayList<>();
this.branch = StringUtil.isBlank(branch)? GitMaterialConfig.DEFAULT_BRANCH : branch ;
this.isSubmodule = isSubmodule;
this.environment = environment;
@@ -186,8 +189,9 @@ public void resetHard(ProcessOutputStreamConsumer outputStreamConsumer, Revision
}
}

private static CommandLine git(Map<String, String> environment) {
private CommandLine git(Map<String, String> environment) {
CommandLine git = CommandLine.createCommandLine("git").withEncoding("UTF-8");
git.withNonArgSecrets(secrets);
return git.withEnv(environment);
}

@@ -246,7 +250,7 @@ public UrlArgument workingRepositoryUrl() {
return new UrlArgument(runOrBomb(gitConfig).outputForDisplay().get(0));
}

public static void checkConnection(UrlArgument repoUrl, String branch, Map<String, String> environment) {
public void checkConnection(UrlArgument repoUrl, String branch, Map<String, String> environment) {
CommandLine commandLine = git(environment).withArgs("ls-remote").withArg(repoUrl).withArg("refs/heads/" + branch);
ConsoleResult result = commandLine.runOrBomb(repoUrl.forDisplay());
if(!hasOnlyOneMatchingBranch(result)){
@@ -258,11 +262,7 @@ private static boolean hasOnlyOneMatchingBranch(ConsoleResult branchList) {
return (branchList.output().size() == 1);
}

public static CommandLine commandToCheckConnection(UrlArgument url, Map<String, String> environment) {
return git(environment).withArgs("ls-remote").withArg(url);
}

public static String version(Map<String, String> map) {
public String version(Map<String, String> map) {
CommandLine gitLsRemote = git(map).withArgs("version");

return gitLsRemote.runOrBomb("git version check").outputAsString();
@@ -192,6 +192,6 @@ private TestSubprocessExecutionContext context() {
}

private GitCommand localRepoFor(GitMaterial material) {
return new GitCommand(material.getFingerprint(), workingDir, GitMaterialConfig.DEFAULT_BRANCH, false, new HashMap<>());
return new GitCommand(material.getFingerprint(), workingDir, GitMaterialConfig.DEFAULT_BRANCH, false, new HashMap<>(), null);
}
}
@@ -112,7 +112,7 @@ private void cloneBundleToFolder(File from, File workingDir) {
}

private GitCommand git(File workingDir) {
return new GitCommand(null, workingDir, GitMaterialConfig.DEFAULT_BRANCH, false, new HashMap<>());
return new GitCommand(null, workingDir, GitMaterialConfig.DEFAULT_BRANCH, false, new HashMap<>(), null);
}

public GitMaterial createMaterial() {
@@ -124,29 +124,29 @@ public GitMaterial createMaterial() {
}

private void checkoutRemoteBranchToLocal(String branch) {
new GitCommand(null, gitRepo, branch, false, new HashMap<>()).checkoutRemoteBranchToLocal();
new GitCommand(null, gitRepo, branch, false, new HashMap<>(), null).checkoutRemoteBranchToLocal();
}

public List<Modification> addFileAndPush(String fileName, String message) throws IOException {
File newFile = new File(gitRepo, fileName);
newFile.createNewFile();
new GitCommand(null, gitRepo, GitMaterialConfig.DEFAULT_BRANCH, false, new HashMap<>()).add(newFile);
new GitCommand(null, gitRepo, GitMaterialConfig.DEFAULT_BRANCH, false, new HashMap<>()).commit(message);
new GitCommand(null, gitRepo, GitMaterialConfig.DEFAULT_BRANCH, false, new HashMap<>(), null).add(newFile);
new GitCommand(null, gitRepo, GitMaterialConfig.DEFAULT_BRANCH, false, new HashMap<>(), null).commit(message);
return createMaterial().latestModification(TestFileUtil.createUniqueTempFolder("working-dir-"), new TestSubprocessExecutionContext());
}

public List<Modification> addFileAndAmend(String fileName, String message) throws IOException {
File newFile = new File(gitRepo, fileName);
newFile.createNewFile();
new GitCommand(null, gitRepo, GitMaterialConfig.DEFAULT_BRANCH, false, new HashMap<>()).add(newFile);
new GitCommand(null, gitRepo, GitMaterialConfig.DEFAULT_BRANCH, false, new HashMap<>(), null).add(newFile);
new GitCommandWithAmend(null, gitRepo, GitMaterialConfig.DEFAULT_BRANCH, false, new HashMap<>()).commitWithAmend(message, gitRepo);
return createMaterial().latestModification(TestFileUtil.createUniqueTempFolder("working-dir-"), new TestSubprocessExecutionContext());
}

private static class GitCommandWithAmend extends GitCommand {

public GitCommandWithAmend(String materialFingerprint, File workingDir, String branch, boolean isSubmodule, Map<String, String> environment) {
super(materialFingerprint, workingDir, branch, isSubmodule, environment);
super(materialFingerprint, workingDir, branch, isSubmodule, environment, null);
}

public void commitWithAmend(String message, File gitRepo) {
@@ -96,7 +96,7 @@ private void checkInOneFile(File repoFolder, File testFile, String comments) {
}

private GitCommand git(File workingDir) {
return new GitCommand(null, workingDir, GitMaterialConfig.DEFAULT_BRANCH, false, new HashMap<>());
return new GitCommand(null, workingDir, GitMaterialConfig.DEFAULT_BRANCH, false, new HashMap<>(), null);
}

public GitMaterial mainRepo() {

0 comments on commit a837fc0

Please sign in to comment.
You can’t perform that action at this time.