Skip to content

Commit f673f7c

Browse files
committed
Use patched GitPackCommandFactory to include the fix from
apache/mina-sshd#794 Otherwise the repository won't be closed and Windows cannot remove the repository directory
1 parent c183b19 commit f673f7c

File tree

6 files changed

+183
-2
lines changed

6 files changed

+183
-2
lines changed

maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gittest/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<description>Tests library for SCM Git Provider.</description>
3333

3434
<properties>
35+
<!-- newer versions clash with Sshd dependencies from JGit 5.x-->
3536
<minaSshdVersion>2.11.0</minaSshdVersion>
3637
</properties>
3738

maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gittest/src/main/java/org/apache/maven/scm/provider/git/GitSshServer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@
3131
import java.util.ArrayList;
3232
import java.util.List;
3333

34+
import org.apache.maven.scm.provider.git.sshd.git.pack.GitPackCommandFactory;
3435
import org.apache.sshd.common.config.keys.KeyUtils;
3536
import org.apache.sshd.common.config.keys.writer.openssh.OpenSSHKeyEncryptionContext;
3637
import org.apache.sshd.common.config.keys.writer.openssh.OpenSSHKeyPairResourceWriter;
3738
import org.apache.sshd.git.GitLocationResolver;
38-
import org.apache.sshd.git.pack.GitPackCommandFactory;
3939
import org.apache.sshd.server.SshServer;
4040
import org.apache.sshd.server.auth.pubkey.KeySetPublickeyAuthenticator;
4141
import org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator;
@@ -132,6 +132,7 @@ public Path resolveRootDirectory(String command, String[] args, ServerSession se
132132
return repositoryRoot;
133133
}
134134
};
135+
// use patched version of GitPackCommandFactory including https://github.com/apache/mina-sshd/pull/794
135136
sshServer.setCommandFactory(new GitPackCommandFactory(gitLocationResolver));
136137
sshServer.start();
137138
}

maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gittest/src/main/java/org/apache/maven/scm/provider/git/command/tag/GitSshTagCommandTckTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ protected CheckOutScmResult checkOut(File workingDirectory, ScmRepository reposi
8484
try {
8585
return super.checkOut(workingDirectory, repository);
8686
} finally {
87-
GitScmTestUtils.setDefaultGitConfig(workingDirectory);
87+
// GitScmTestUtils.setDefaultGitConfig(workingDirectory);
8888
}
8989
}
9090

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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.sshd.git.pack;
20+
21+
import java.io.IOException;
22+
import java.nio.file.Path;
23+
import java.util.List;
24+
25+
import org.apache.sshd.common.util.GenericUtils;
26+
import org.apache.sshd.common.util.ValidateUtils;
27+
import org.apache.sshd.common.util.threads.CloseableExecutorService;
28+
import org.apache.sshd.git.AbstractGitCommand;
29+
import org.apache.sshd.git.GitLocationResolver;
30+
import org.eclipse.jgit.lib.Repository;
31+
import org.eclipse.jgit.lib.RepositoryCache;
32+
import org.eclipse.jgit.transport.ReceivePack;
33+
import org.eclipse.jgit.transport.RemoteConfig;
34+
import org.eclipse.jgit.transport.UploadPack;
35+
import org.eclipse.jgit.util.FS;
36+
37+
/**
38+
* TODO Add javadoc
39+
*
40+
* @author <a href="mailto:[email protected]">Apache MINA SSHD Project</a>
41+
*/
42+
public class GitPackCommand extends AbstractGitCommand {
43+
/**
44+
* @param rootDirResolver Resolver for GIT root directory
45+
* @param command Command to execute
46+
* @param executorService An {@link CloseableExecutorService} to be used when
47+
* {@code start(ChannelSession, Environment)}-ing execution. If {@code null} an ad-hoc
48+
* single-threaded service is created and used.
49+
*/
50+
public GitPackCommand(
51+
GitLocationResolver rootDirResolver, String command, CloseableExecutorService executorService) {
52+
super(rootDirResolver, command, executorService);
53+
}
54+
55+
@Override
56+
public void run() {
57+
String command = getCommand();
58+
try {
59+
List<String> strs = parseDelimitedString(command, " ", true);
60+
String[] args = strs.toArray(new String[strs.size()]);
61+
for (int i = 0; i < args.length; i++) {
62+
String argVal = args[i];
63+
if (argVal.startsWith("'") && argVal.endsWith("'")) {
64+
args[i] = argVal.substring(1, argVal.length() - 1);
65+
argVal = args[i];
66+
}
67+
if (argVal.startsWith("\"") && argVal.endsWith("\"")) {
68+
args[i] = argVal.substring(1, argVal.length() - 1);
69+
}
70+
}
71+
72+
if (args.length != 2) {
73+
throw new IllegalArgumentException("Invalid git command line (no arguments): " + command);
74+
}
75+
76+
Path rootDir = resolveRootDirectory(command, args);
77+
RepositoryCache.FileKey key = RepositoryCache.FileKey.lenient(rootDir.toFile(), FS.DETECTED);
78+
Repository db = key.open(true /* must exist */);
79+
String subCommand = args[0];
80+
if (RemoteConfig.DEFAULT_UPLOAD_PACK.equals(subCommand)) {
81+
new UploadPack(db).upload(getInputStream(), getOutputStream(), getErrorStream());
82+
} else if (RemoteConfig.DEFAULT_RECEIVE_PACK.equals(subCommand)) {
83+
new ReceivePack(db).receive(getInputStream(), getOutputStream(), getErrorStream());
84+
} else {
85+
throw new IllegalArgumentException("Unknown git command: " + command);
86+
}
87+
88+
onExit(0);
89+
} catch (Throwable t) {
90+
onExit(-1, t.getClass().getSimpleName());
91+
}
92+
}
93+
94+
protected Path resolveRootDirectory(String command, String[] args) throws IOException {
95+
GitLocationResolver resolver = getGitLocationResolver();
96+
Path rootDir = resolver.resolveRootDirectory(command, args, getServerSession(), getFileSystem());
97+
ValidateUtils.checkState(rootDir != null, "No root directory provided for %s command", command);
98+
99+
String pathArg = args[1];
100+
int len = GenericUtils.length(pathArg);
101+
// Strip any leading path separator since we use relative to root
102+
if ((len > 0) && (pathArg.charAt(0) == '/')) {
103+
pathArg = pathArg.substring(1);
104+
}
105+
106+
ValidateUtils.checkNotNullAndNotEmpty(pathArg, "No %s command sub-path specified", args[0]);
107+
return rootDir.resolve(pathArg);
108+
}
109+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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.sshd.git.pack;
20+
21+
import java.util.function.Supplier;
22+
23+
import org.apache.sshd.common.util.threads.CloseableExecutorService;
24+
import org.apache.sshd.git.AbstractGitCommandFactory;
25+
import org.apache.sshd.git.GitLocationResolver;
26+
import org.apache.sshd.server.command.CommandFactory;
27+
28+
/**
29+
* TODO Add javadoc
30+
*
31+
* @author <a href="mailto:[email protected]">Apache MINA SSHD Project</a>
32+
*/
33+
public class GitPackCommandFactory extends AbstractGitCommandFactory {
34+
public static final String GIT_FACTORY_NAME = "git-pack";
35+
public static final String GIT_COMMAND_PREFIX = "git-";
36+
37+
public GitPackCommandFactory() {
38+
this(null);
39+
}
40+
41+
public GitPackCommandFactory(GitLocationResolver resolver) {
42+
super(GIT_FACTORY_NAME, GIT_COMMAND_PREFIX);
43+
withGitLocationResolver(resolver);
44+
}
45+
46+
@Override
47+
public GitPackCommandFactory withDelegate(CommandFactory delegate) {
48+
return (GitPackCommandFactory) super.withDelegate(delegate);
49+
}
50+
51+
@Override
52+
public GitPackCommandFactory withGitLocationResolver(GitLocationResolver rootDirResolver) {
53+
return (GitPackCommandFactory) super.withGitLocationResolver(rootDirResolver);
54+
}
55+
56+
@Override
57+
public GitPackCommandFactory withExecutorServiceProvider(Supplier<? extends CloseableExecutorService> provider) {
58+
return (GitPackCommandFactory) super.withExecutorServiceProvider(provider);
59+
}
60+
61+
@Override
62+
public GitPackCommand createGitCommand(String command) {
63+
return new GitPackCommand(getGitLocationResolver(), command, resolveExecutorService(command));
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This is just a minimum fork of https://github.com/apache/mina-sshd/tree/sshd-2.11.0/sshd-git/src/main/java/org/apache/sshd/git/pack
2+
to include the patch https://github.com/apache/mina-sshd/pull/794.
3+
4+
Otherwise the SSH server cannot be used on Windows as it leaves files open.
5+
TODO: Remove once a Mina SSHD release is available that includes this patch.

0 commit comments

Comments
 (0)