Skip to content

Commit 6fc20f7

Browse files
authored
Git: Use environment variables when pushing tags/branches to remote (#1270)
Add SSH based push tag/branch tests Refactor SSH support for tests This closes #1253
1 parent 3a05b81 commit 6fc20f7

File tree

46 files changed

+1359
-194
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1359
-194
lines changed

maven-scm-api/src/main/java/org/apache/maven/scm/command/untag/AbstractUntagCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.apache.maven.scm.command.AbstractCommand;
2828
import org.apache.maven.scm.provider.ScmProviderRepository;
2929

30-
/** {@inheritDoc} */
30+
/** Removes a tag */
3131
public abstract class AbstractUntagCommand extends AbstractCommand {
3232
/**
3333
* execute untag command

maven-scm-plugin/src/test/java/org/apache/maven/scm/plugin/UntagMojoTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void setUp() throws Exception {
6363
checkoutMojo.execute();
6464

6565
// Add a default user to the config
66-
GitScmTestUtils.setDefaulGitConfig(checkoutDir);
66+
GitScmTestUtils.setDefaultGitConfig(checkoutDir);
6767
}
6868

6969
@Test

maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/GitExeScmProvider.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected GitCommand getAddCommand() {
6666

6767
/** {@inheritDoc} */
6868
protected GitCommand getBranchCommand() {
69-
return new GitBranchCommand();
69+
return new GitBranchCommand(environmentVariables);
7070
}
7171

7272
/** {@inheritDoc} */
@@ -106,12 +106,12 @@ protected GitCommand getStatusCommand() {
106106

107107
/** {@inheritDoc} */
108108
protected GitCommand getTagCommand() {
109-
return new GitTagCommand();
109+
return new GitTagCommand(environmentVariables);
110110
}
111111

112112
/** {@inheritDoc} */
113113
protected GitCommand getUntagCommand() {
114-
return new GitUntagCommand();
114+
return new GitUntagCommand(environmentVariables);
115115
}
116116

117117
/** {@inheritDoc} */

maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/branch/GitBranchCommand.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.maven.scm.provider.git.gitexe.command.branch;
2020

2121
import java.io.File;
22+
import java.util.Map;
2223

2324
import org.apache.maven.scm.ScmException;
2425
import org.apache.maven.scm.ScmFileSet;
@@ -40,6 +41,12 @@
4041
*
4142
*/
4243
public class GitBranchCommand extends AbstractBranchCommand implements GitCommand {
44+
private final Map<String, String> environmentVariables;
45+
46+
public GitBranchCommand(Map<String, String> environmentVariables) {
47+
this.environmentVariables = environmentVariables;
48+
}
49+
4350
/** {@inheritDoc} */
4451
public ScmResult executeBranchCommand(ScmProviderRepository repo, ScmFileSet fileSet, String branch, String message)
4552
throws ScmException {
@@ -102,9 +109,9 @@ public static Commandline createCommandLine(
102109
return cl;
103110
}
104111

105-
public static Commandline createPushCommandLine(
106-
GitScmProviderRepository repository, ScmFileSet fileSet, String branch) throws ScmException {
107-
Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(fileSet.getBasedir(), "push");
112+
public Commandline createPushCommandLine(GitScmProviderRepository repository, ScmFileSet fileSet, String branch) {
113+
Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(
114+
fileSet.getBasedir(), "push", repository, environmentVariables);
108115

109116
cl.createArg().setValue(repository.getPushUrl());
110117
cl.createArg().setValue("refs/heads/" + branch);

maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/tag/GitTagCommand.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.io.File;
2222
import java.io.IOException;
23+
import java.util.Map;
2324

2425
import org.apache.maven.scm.ScmException;
2526
import org.apache.maven.scm.ScmFileSet;
@@ -44,6 +45,11 @@
4445
*
4546
*/
4647
public class GitTagCommand extends AbstractTagCommand implements GitCommand {
48+
private final Map<String, String> environmentVariables;
49+
50+
public GitTagCommand(Map<String, String> environmentVariables) {
51+
this.environmentVariables = environmentVariables;
52+
}
4753

4854
public ScmResult executeTagCommand(ScmProviderRepository repo, ScmFileSet fileSet, String tag, String message)
4955
throws ScmException {
@@ -156,9 +162,9 @@ static Commandline createCommandLine(
156162
return cl;
157163
}
158164

159-
public static Commandline createPushCommandLine(GitScmProviderRepository repository, ScmFileSet fileSet, String tag)
160-
throws ScmException {
161-
Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(fileSet.getBasedir(), "push");
165+
public Commandline createPushCommandLine(GitScmProviderRepository repository, ScmFileSet fileSet, String tag) {
166+
Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(
167+
fileSet.getBasedir(), "push", repository, environmentVariables);
162168

163169
cl.createArg().setValue(repository.getPushUrl());
164170
cl.createArg().setValue("refs/tags/" + tag);

maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/untag/GitUntagCommand.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.maven.scm.provider.git.gitexe.command.untag;
2020

2121
import java.io.File;
22+
import java.util.Map;
2223

2324
import org.apache.maven.scm.ScmException;
2425
import org.apache.maven.scm.ScmFileSet;
@@ -35,6 +36,11 @@
3536

3637
/** {@inheritDoc} */
3738
public class GitUntagCommand extends AbstractUntagCommand implements GitCommand {
39+
private final Map<String, String> environmentVariables;
40+
41+
public GitUntagCommand(Map<String, String> environmentVariables) {
42+
this.environmentVariables = environmentVariables;
43+
}
3844

3945
/** {@inheritDoc} */
4046
public ScmResult executeUntagCommand(
@@ -85,9 +91,9 @@ public static Commandline createCommandLine(
8591
return cl;
8692
}
8793

88-
public static Commandline createPushCommandLine(
89-
GitScmProviderRepository repository, ScmFileSet fileSet, String tag) {
90-
Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(fileSet.getBasedir(), "push");
94+
public Commandline createPushCommandLine(GitScmProviderRepository repository, ScmFileSet fileSet, String tag) {
95+
Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(
96+
fileSet.getBasedir(), "push", repository, environmentVariables);
9197

9298
cl.createArg().setValue("--delete");
9399
cl.createArg().setValue(repository.getPushUrl());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.scm.provider.git.gitexe;
20+
21+
import org.apache.commons.io.FilenameUtils;
22+
import org.apache.maven.scm.manager.NoSuchScmProviderException;
23+
import org.apache.maven.scm.manager.ScmManager;
24+
import org.apache.maven.scm.provider.ScmProviderRepositoryWithHost;
25+
import org.apache.maven.scm.repository.ScmRepository;
26+
import org.codehaus.plexus.util.StringUtils;
27+
28+
public class GitExeTestUtils {
29+
30+
/**
31+
* Environment variable to customize the SSH command used by git, requires git 2.3.0 or newer
32+
* @see <a href="https://git-scm.com/docs/git#Documentation/git.txt-GITSSHCOMMAND">GIT_SSH_COMMAND</a>
33+
*/
34+
public static final String VARIABLE_GIT_SSH_COMMAND = "GIT_SSH_COMMAND";
35+
36+
private GitExeTestUtils() {
37+
// Utility class, no instantiation
38+
}
39+
40+
/**
41+
* Configures the git executable to use a custom SSH command that allows lenient host key checking.
42+
* It uses the private key specified in the repository configuration.
43+
* @param scmManager
44+
* @param repository
45+
* @throws NoSuchScmProviderException
46+
*/
47+
public static void configureLenientSshAuthentication(ScmManager scmManager, ScmRepository repository)
48+
throws NoSuchScmProviderException {
49+
ScmProviderRepositoryWithHost providerRepository =
50+
(ScmProviderRepositoryWithHost) repository.getProviderRepository();
51+
GitExeScmProvider provider = (GitExeScmProvider) scmManager.getProviderByRepository(repository);
52+
String privateKey = providerRepository.getPrivateKey();
53+
if (StringUtils.isBlank(privateKey)) {
54+
throw new IllegalArgumentException("Private key must be set in the repository configuration");
55+
}
56+
provider.setEnvironmentVariable(
57+
VARIABLE_GIT_SSH_COMMAND,
58+
"ssh -o StrictHostKeyChecking=no -o IdentitiesOnly=yes -i "
59+
+ FilenameUtils.separatorsToUnix(providerRepository.getPrivateKey()));
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.maven.scm.provider.git.gitexe.command.branch;
20+
21+
import java.security.GeneralSecurityException;
22+
23+
import org.apache.maven.scm.provider.git.command.branch.GitSshBranchCommandTckTest;
24+
import org.apache.maven.scm.provider.git.gitexe.GitExeTestUtils;
25+
import org.apache.maven.scm.repository.ScmRepository;
26+
27+
/**
28+
*
29+
*/
30+
public class GitExeSshBranchCommandTckTest extends GitSshBranchCommandTckTest {
31+
32+
public GitExeSshBranchCommandTckTest() throws GeneralSecurityException {
33+
super();
34+
}
35+
36+
@Override
37+
protected String getScmProvider() {
38+
return "git";
39+
}
40+
41+
@Override
42+
public void configureCredentials(ScmRepository repository, String passphrase) throws Exception {
43+
super.configureCredentials(repository, passphrase);
44+
GitExeTestUtils.configureLenientSshAuthentication(getScmManager(), repository);
45+
}
46+
}

maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/test/java/org/apache/maven/scm/provider/git/gitexe/command/checkin/GitCheckInCommandNoBranchTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void testCheckinNoBranch() throws Exception {
6666
CheckOutScmResult checkOutScmResult = checkoutRepo(scmRepository);
6767

6868
// Add a default user to the config
69-
GitScmTestUtils.setDefaulGitConfig(workingDirectory);
69+
GitScmTestUtils.setDefaultGitConfig(workingDirectory);
7070

7171
assertEquals(0, checkOutScmResult.getCheckedOutFiles().size());
7272

maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/test/java/org/apache/maven/scm/provider/git/gitexe/command/checkin/GitCheckInCommandTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public void testCheckinAfterRename() throws Exception {
100100
checkoutRepoInto(checkedOutRepo, scmRepository);
101101

102102
// Add a default user to the config
103-
GitScmTestUtils.setDefaulGitConfig(checkedOutRepo);
103+
GitScmTestUtils.setDefaultGitConfig(checkedOutRepo);
104104

105105
// Creating foo/bar/wine.xml
106106
File fooDir = new File(checkedOutRepo.getAbsolutePath(), "foo");
@@ -154,7 +154,7 @@ public void testCheckinWithFileSet() throws Exception {
154154
checkoutRepoInto(checkedOutRepo, scmRepository);
155155

156156
// Add a default user to the config
157-
GitScmTestUtils.setDefaulGitConfig(checkedOutRepo);
157+
GitScmTestUtils.setDefaultGitConfig(checkedOutRepo);
158158

159159
// Creating beer.xml and whiskey.xml
160160
File beerFile = new File(checkedOutRepo.getAbsolutePath(), "beer.xml");

0 commit comments

Comments
 (0)