Skip to content

Commit 3711a2d

Browse files
committed
Simple configuration file
1 parent f5439c4 commit 3711a2d

File tree

8 files changed

+171
-12
lines changed

8 files changed

+171
-12
lines changed

Diff for: git-as-svn/.idea/runConfigurations/Main.xml

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: git-as-svn/build.gradle

+2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ dependencies {
1414
compile "org.jetbrains:annotations:13.0"
1515
compile "org.eclipse.jgit:org.eclipse.jgit:3.4.0.201406110918-r"
1616
compile "org.tmatesoft.svnkit:svnkit:1.8.5"
17+
compile "org.yaml:snakeyaml:1.13"
1718
compile "org.slf4j:slf4j-simple:1.7.7"
19+
compile "com.beust:jcommander:1.35"
1820
testCompile "org.testng:testng:6.8.8"
1921
}
2022

Diff for: git-as-svn/config.example

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
!!svnserver.config.Config:
2+
port: 3690
3+
repository:
4+
branch: local
5+
path: ../.git
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package svnserver.config;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
/**
6+
* Top configuration object.
7+
*
8+
* @author a.navrotskiy
9+
*/
10+
public final class Config {
11+
private int port = 3690;
12+
13+
@NotNull
14+
private RepositoryConfig repository = new RepositoryConfig();
15+
16+
public void setPort(int port) {
17+
this.port = port;
18+
}
19+
20+
public int getPort() {
21+
return port;
22+
}
23+
24+
public void setRepository(@NotNull RepositoryConfig repository) {
25+
this.repository = repository;
26+
}
27+
28+
@NotNull
29+
public RepositoryConfig getRepository() {
30+
return repository;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package svnserver.config;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
/**
6+
* Repository configuration.
7+
*
8+
* @author a.navrotskiy
9+
*/
10+
public class RepositoryConfig {
11+
@NotNull
12+
private String branch = "master";
13+
@NotNull
14+
private String path = ".git";
15+
16+
@NotNull
17+
public String getBranch() {
18+
return branch;
19+
}
20+
21+
public void setBranch(@NotNull String branch) {
22+
this.branch = branch;
23+
}
24+
25+
@NotNull
26+
public String getPath() {
27+
return path;
28+
}
29+
30+
public void setPath(@NotNull String path) {
31+
this.path = path;
32+
}
33+
}

Diff for: git-as-svn/src/main/java/svnserver/repository/git/GitRepository.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.tmatesoft.svn.core.SVNException;
1515
import svnserver.StringHelper;
1616
import svnserver.auth.User;
17+
import svnserver.config.RepositoryConfig;
1718
import svnserver.repository.VcsCommitBuilder;
1819
import svnserver.repository.VcsDeltaConsumer;
1920
import svnserver.repository.VcsFile;
@@ -22,6 +23,7 @@
2223
import java.io.File;
2324
import java.io.IOException;
2425
import java.io.InputStream;
26+
import java.nio.charset.StandardCharsets;
2527
import java.security.MessageDigest;
2628
import java.security.NoSuchAlgorithmException;
2729
import java.util.*;
@@ -57,12 +59,16 @@ public class GitRepository implements VcsRepository {
5759
@NotNull
5860
private final Map<String, String> cacheMd5 = new ConcurrentHashMap<>();
5961

60-
public GitRepository(@NotNull String uuid, @NotNull String branch) throws IOException {
61-
this.uuid = uuid;
62-
this.repository = new FileRepository(findGitPath());
63-
this.branch = repository.getRef(branch).getName();
62+
public GitRepository(@NotNull RepositoryConfig config) throws IOException, SVNException {
63+
this.repository = new FileRepository(new File(config.getPath()).getAbsolutePath());
64+
final Ref branchRef = repository.getRef(config.getBranch());
65+
if (branchRef == null) {
66+
throw new IOException("Branch not found: " + config.getBranch());
67+
}
68+
this.branch = branchRef.getName();
6469
addRevisionInfo(getEmptyCommit(repository));
6570
updateRevisions();
71+
this.uuid = UUID.nameUUIDFromBytes((getRevisionInfo(1).getCommit().getName() + "\0" + branch).getBytes(StandardCharsets.UTF_8)).toString();
6672
}
6773

6874
@Override

Diff for: git-as-svn/src/main/java/svnserver/server/Main.java

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package svnserver.server;
2+
3+
import com.beust.jcommander.JCommander;
4+
import com.beust.jcommander.Parameter;
5+
import org.jetbrains.annotations.NotNull;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.tmatesoft.svn.core.SVNException;
9+
import org.yaml.snakeyaml.Yaml;
10+
import svnserver.config.Config;
11+
12+
import java.io.*;
13+
import java.nio.charset.StandardCharsets;
14+
15+
/**
16+
* Entry point.
17+
*
18+
* @author a.navrotskiy
19+
*/
20+
public class Main {
21+
@NotNull
22+
private static final Logger log = LoggerFactory.getLogger(SvnServer.class);
23+
24+
public static void main(@NotNull String[] args) throws IOException, SVNException {
25+
final CmdArgs cmd = new CmdArgs();
26+
final JCommander jc = new JCommander(cmd);
27+
jc.parse(args);
28+
if (cmd.help) {
29+
jc.usage();
30+
return;
31+
}
32+
// Load config
33+
Yaml yaml = new Yaml();
34+
Config config;
35+
try (
36+
InputStream stream = new FileInputStream(cmd.configuration);
37+
Reader reader = new InputStreamReader(stream, StandardCharsets.UTF_8)
38+
) {
39+
config = yaml.loadAs(reader, Config.class);
40+
}
41+
if (cmd.showConfig) {
42+
log.info("Actual config:\n{}", yaml.dump(config));
43+
}
44+
new SvnServer(config).start();
45+
}
46+
47+
public static class CmdArgs {
48+
@Parameter(names = {"-c", "--config"}, description = "Configuration file name", required = true)
49+
@NotNull
50+
private File configuration;
51+
52+
@Parameter(names = {"--show-config"}, description = "Show actual configuration on start")
53+
private boolean showConfig = false;
54+
55+
@Parameter(names = {"-h", "--help"}, description = "Show help", help = true)
56+
private boolean help = false;
57+
}
58+
59+
}

Diff for: git-as-svn/src/main/java/svnserver/server/SvnServer.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import svnserver.auth.LocalUserDB;
1212
import svnserver.auth.User;
1313
import svnserver.auth.UserDB;
14+
import svnserver.config.Config;
1415
import svnserver.parser.MessageParser;
1516
import svnserver.parser.SvnServerParser;
1617
import svnserver.parser.SvnServerToken;
@@ -48,8 +49,11 @@ public class SvnServer {
4849
private final Map<String, BaseCmd<?>> commands = new HashMap<>();
4950
@NotNull
5051
private final VcsRepository repository;
52+
@NotNull
53+
private final Config config;
5154

52-
public SvnServer() throws IOException {
55+
public SvnServer(@NotNull Config config) throws IOException, SVNException {
56+
this.config = config;
5357

5458
commands.put("commit", new CommitCmd());
5559
commands.put("diff", new DiffCmd());
@@ -64,15 +68,11 @@ public SvnServer() throws IOException {
6468
commands.put("stat", new StatCmd());
6569
commands.put("update", new UpdateCmd());
6670

67-
repository = new GitRepository("4f0c5325-dd55-4330-b24c-0e9e40eb504b", "local");
68-
}
69-
70-
public static void main(String[] args) throws IOException {
71-
new SvnServer().server(3690);
71+
repository = new GitRepository(config.getRepository());
7272
}
7373

74-
private void server(int port) throws IOException {
75-
final ServerSocket serverSocket = new ServerSocket(port);
74+
public void start() throws IOException {
75+
final ServerSocket serverSocket = new ServerSocket(config.getPort());
7676
while (true) {
7777
final Socket socket = serverSocket.accept();
7878
new Thread(() -> {

0 commit comments

Comments
 (0)