mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-06 21:45:43 +01:00
moves more git specific classes from ssh-plugin to git-plugin
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package sonia.scm.protocolcommand.git;
|
||||
|
||||
import sonia.scm.protocolcommand.CommandInterpreter;
|
||||
import sonia.scm.protocolcommand.RepositoryContextResolver;
|
||||
import sonia.scm.protocolcommand.ScmCommandProtocol;
|
||||
|
||||
class GitCommandInterpreter implements CommandInterpreter {
|
||||
private final GitRepositoryContextResolver gitRepositoryContextResolver;
|
||||
private final GitCommandProtocol gitCommandProtocol;
|
||||
private final String[] args;
|
||||
|
||||
GitCommandInterpreter(GitRepositoryContextResolver gitRepositoryContextResolver, GitCommandProtocol gitCommandProtocol, String[] args) {
|
||||
this.gitRepositoryContextResolver = gitRepositoryContextResolver;
|
||||
this.gitCommandProtocol = gitCommandProtocol;
|
||||
this.args = args;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getParsedArgs() {
|
||||
return args;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScmCommandProtocol getProtocolHandler() {
|
||||
return gitCommandProtocol;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RepositoryContextResolver getRepositoryContextResolver() {
|
||||
return gitRepositoryContextResolver;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package sonia.scm.protocolcommand.git;
|
||||
|
||||
import sonia.scm.plugin.Extension;
|
||||
import sonia.scm.protocolcommand.CommandInterpreter;
|
||||
import sonia.scm.protocolcommand.CommandInterpreterFactory;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.Optional;
|
||||
|
||||
import static java.util.Optional.empty;
|
||||
import static java.util.Optional.of;
|
||||
|
||||
@Extension
|
||||
public class GitCommandInterpreterFactory implements CommandInterpreterFactory {
|
||||
private final GitCommandProtocol gitCommandProtocol;
|
||||
private final GitRepositoryContextResolver gitRepositoryContextResolver;
|
||||
|
||||
@Inject
|
||||
public GitCommandInterpreterFactory(GitCommandProtocol gitCommandProtocol, GitRepositoryContextResolver gitRepositoryContextResolver) {
|
||||
this.gitCommandProtocol = gitCommandProtocol;
|
||||
this.gitRepositoryContextResolver = gitRepositoryContextResolver;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<CommandInterpreter> canHandle(String command) {
|
||||
try {
|
||||
String[] args = GitCommandParser.parse(command);
|
||||
if (args[0].startsWith("git")) {
|
||||
return of(new GitCommandInterpreter(gitRepositoryContextResolver, gitCommandProtocol, args));
|
||||
} else {
|
||||
return empty();
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
return empty();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package sonia.scm.protocolcommand.git;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
class GitCommandParser {
|
||||
|
||||
private GitCommandParser() {
|
||||
}
|
||||
|
||||
static String[] parse(String command) {
|
||||
List<String> strs = parseDelimitedString(command, " ", true);
|
||||
String[] args = strs.toArray(new String[strs.size()]);
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
String argVal = args[i];
|
||||
if (argVal.startsWith("'") && argVal.endsWith("'")) {
|
||||
args[i] = argVal.substring(1, argVal.length() - 1);
|
||||
argVal = args[i];
|
||||
}
|
||||
if (argVal.startsWith("\"") && argVal.endsWith("\"")) {
|
||||
args[i] = argVal.substring(1, argVal.length() - 1);
|
||||
}
|
||||
}
|
||||
|
||||
if (args.length != 2) {
|
||||
throw new IllegalArgumentException("Invalid git command line (no arguments): " + command);
|
||||
}
|
||||
return args;
|
||||
}
|
||||
|
||||
private static List<String> parseDelimitedString(String value, String delim, boolean trim) {
|
||||
if (value == null) {
|
||||
value = "";
|
||||
}
|
||||
|
||||
List<String> list = new ArrayList<>();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int expecting = 7;
|
||||
boolean isEscaped = false;
|
||||
|
||||
for(int i = 0; i < value.length(); ++i) {
|
||||
char c = value.charAt(i);
|
||||
boolean isDelimiter = delim.indexOf(c) >= 0;
|
||||
if (!isEscaped && c == '\\') {
|
||||
isEscaped = true;
|
||||
} else {
|
||||
if (isEscaped) {
|
||||
sb.append(c);
|
||||
} else if (isDelimiter && (expecting & 2) != 0) {
|
||||
if (trim) {
|
||||
String str = sb.toString();
|
||||
list.add(str.trim());
|
||||
} else {
|
||||
list.add(sb.toString());
|
||||
}
|
||||
|
||||
sb.delete(0, sb.length());
|
||||
expecting = 7;
|
||||
} else if (c == '"' && (expecting & 4) != 0) {
|
||||
sb.append(c);
|
||||
expecting = 9;
|
||||
} else if (c == '"' && (expecting & 8) != 0) {
|
||||
sb.append(c);
|
||||
expecting = 7;
|
||||
} else {
|
||||
if ((expecting & 1) == 0) {
|
||||
throw new IllegalArgumentException("Invalid delimited string: " + value);
|
||||
}
|
||||
|
||||
sb.append(c);
|
||||
}
|
||||
|
||||
isEscaped = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (sb.length() > 0) {
|
||||
if (trim) {
|
||||
String str = sb.toString();
|
||||
list.add(str.trim());
|
||||
} else {
|
||||
list.add(sb.toString());
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package sonia.scm.protocolcommand.git;
|
||||
|
||||
import com.google.inject.servlet.ServletModule;
|
||||
import sonia.scm.plugin.Extension;
|
||||
import sonia.scm.protocolcommand.RepositoryContextResolver;
|
||||
|
||||
@Extension
|
||||
public class GitProtocolModule extends ServletModule {
|
||||
@Override
|
||||
protected void configureServlets() {
|
||||
bind(RepositoryContextResolver.class).to(GitRepositoryContextResolver.class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user