Skip to content

Commit

Permalink
Support local-to-remote mirroring (#717)
Browse files Browse the repository at this point in the history
Motivation
We currently support only remote-to-local mirroring. We should support the opposite direction as well.

Modifications:
- Implement `GitMirror#mirrorToLocalRemote()` which used to throw `UnsupportedOperationException` before.

Result:
- Close #53
- You can now enable mirroring from Central Dogma to a remote Git server.
  • Loading branch information
minwoox authored Oct 27, 2022
1 parent 4ea0de9 commit 3baa748
Show file tree
Hide file tree
Showing 10 changed files with 967 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -426,16 +426,19 @@ private void pushMirrorSettings(@Nullable String localPath, @Nullable String rem

private void pushMirrorSettings(String localRepo, @Nullable String localPath, @Nullable String remotePath,
@Nullable String gitignore) {
final String localPath0 = localPath == null ? "/" : localPath;
final String remoteUri = gitUri + firstNonNull(remotePath, "");
client.forRepo(projName, Project.REPO_META)
.commit("Add /mirrors.json",
Change.ofJsonUpsert("/mirrors.json",
"[{" +
" \"type\": \"single\"," +
" \"direction\": \"REMOTE_TO_LOCAL\"," +
" \"localRepo\": \"" + localRepo + "\"," +
(localPath != null ? "\"localPath\": \"" + localPath + "\"," : "") +
" \"remoteUri\": \"" + gitUri + firstNonNull(remotePath, "") + '"' +
",\"gitignore\": " + firstNonNull(gitignore, "\"\"") +
" \"localPath\": \"" + localPath0 + "\"," +
" \"remoteUri\": \"" + remoteUri + "\"," +
" \"schedule\": \"0 0 0 1 1 ? 2099\"," +
" \"gitignore\": " + firstNonNull(gitignore, "\"\"") +
"}]"))
.push().join();
}
Expand All @@ -453,8 +456,8 @@ private void addToGitIndex(String path, String content) throws IOException, GitA
addToGitIndex(git, gitWorkTree, path, content);
}

private static void addToGitIndex(Git git, File gitWorkTree,
String path, String content) throws IOException, GitAPIException {
static void addToGitIndex(Git git, File gitWorkTree,
String path, String content) throws IOException, GitAPIException {
final File file = Paths.get(gitWorkTree.getAbsolutePath(), path.split("/")).toFile();
file.getParentFile().mkdirs();
Files.asCharSink(file, StandardCharsets.UTF_8).write(content);
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void onSuccess(@Nullable Object result) {}

@Override
public void onFailure(Throwable cause) {
logger.error("Git-to-CD mirroring scheduler stopped due to an unexpected exception:", cause);
logger.error("Git mirroring scheduler stopped due to an unexpected exception:", cause);
}
}, MoreExecutors.directExecutor());
}
Expand Down Expand Up @@ -207,7 +207,7 @@ public void onSuccess(@Nullable Object result) {}

@Override
public void onFailure(Throwable cause) {
logger.warn("Unexpected Git-to-CD mirroring failure: {}", m, cause);
logger.warn("Unexpected Git mirroring failure: {}", m, cause);
}
}, MoreExecutors.directExecutor());
});
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.annotations.VisibleForTesting;

final class MirrorState {
@VisibleForTesting
public final class MirrorState {

private final String sourceRevision;

Expand All @@ -30,7 +32,8 @@ final class MirrorState {
this.sourceRevision = requireNonNull(sourceRevision, "sourceRevision");
}

String sourceRevision() {
@JsonProperty("sourceRevision")
public String sourceRevision() {
return sourceRevision;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static String normalizePath(String path) {
*
* <p>e.g. git+ssh://foo.com/bar.git/some-path#master is split into:
* - remoteRepoUri: git+ssh://foo.com/bar.git
* - remotePath: /some-path
* - remotePath: /some-path/
* - remoteBranch: master
*
* <p>e.g. dogma://foo.com/bar/qux.dogma is split into:
Expand Down
2 changes: 1 addition & 1 deletion site/src/sphinx/concepts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Concepts
- Meta repository

- A *meta repository* is a repository whose name is ``meta`` in a project. It is dedicated to store the
metadata related with the project it belongs to, such as Git-to-CD mirroring settings.
metadata related with the project it belongs to, such as Git mirroring settings.

- Commit

Expand Down
5 changes: 5 additions & 0 deletions site/src/sphinx/mirroring.rst
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ If everything was configured correctly, the repository you specified in ``localR
"sourceRevision": "22fb176e4d8096d709d34ffe985c5f3acea83ef2"
}
Setting up a CD-to-Git mirror
-----------------------------
It's exactly the same as setting up a Git-to-CD mirror which is described above, except you need to specify
``direction`` with ``LOCAL_TO_REMOTE``.

Mirror limit settings
---------------------
Central Dogma limits the number of files and the total size of the files in a mirror for its reliability.
Expand Down
2 changes: 1 addition & 1 deletion site/src/sphinx/setup-configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ Core properties

- ``mirroringEnabled`` (boolean)

- whether to enable Git-to-CD mirroring. It's enabled by default. For more information about mirroring,
- whether to enable Git mirroring. It's enabled by default. For more information about mirroring,
refer to :ref:`mirroring`.

- ``numMirroringThreads`` (integer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public static <T> void assertJsonConversion(T value, Class<T> valueType, String
}

public static String normalizedDisplayName(TestInfo testInfo) {
return DISALLOWED_CHARS.matcher(testInfo.getDisplayName()).replaceAll("");
return DISALLOWED_CHARS.matcher(testInfo.getDisplayName() + testInfo.getTestMethod().get().getName())
.replaceAll("");
}

private TestUtil() {}
Expand Down

0 comments on commit 3baa748

Please sign in to comment.