|
| 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 | +} |
0 commit comments