1   /*
2    * Copyright 2023 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package com.linecorp.centraldogma.server.internal.mirror;
18  
19  import static com.linecorp.centraldogma.server.internal.storage.repository.MirrorUtil.split;
20  import static com.linecorp.centraldogma.server.mirror.MirrorSchemes.SCHEME_GIT;
21  import static com.linecorp.centraldogma.server.mirror.MirrorSchemes.SCHEME_GIT_FILE;
22  import static com.linecorp.centraldogma.server.mirror.MirrorSchemes.SCHEME_GIT_HTTP;
23  import static com.linecorp.centraldogma.server.mirror.MirrorSchemes.SCHEME_GIT_HTTPS;
24  import static com.linecorp.centraldogma.server.mirror.MirrorSchemes.SCHEME_GIT_SSH;
25  import static java.util.Objects.requireNonNull;
26  
27  import java.net.URI;
28  
29  import com.linecorp.centraldogma.server.mirror.Mirror;
30  import com.linecorp.centraldogma.server.mirror.MirrorContext;
31  import com.linecorp.centraldogma.server.mirror.MirrorProvider;
32  
33  public final class GitMirrorProvider implements MirrorProvider {
34  
35      @Override
36      public Mirror newMirror(MirrorContext context) {
37          requireNonNull(context, "context");
38  
39          final URI remoteUri = context.remoteUri();
40          final String scheme = remoteUri.getScheme();
41          if (scheme == null) {
42              return null;
43          }
44  
45          switch (scheme) {
46              case SCHEME_GIT_SSH: {
47                  final String[] components = split(remoteUri, "git");
48                  return new SshGitMirror(context.schedule(), context.direction(), context.credential(),
49                                          context.localRepo(), context.localPath(),
50                                          URI.create(components[0]), components[1], components[2],
51                                          context.gitignore());
52              }
53              case SCHEME_GIT_HTTP:
54              case SCHEME_GIT_HTTPS:
55              case SCHEME_GIT:
56              case SCHEME_GIT_FILE: {
57                  final String[] components = split(remoteUri, "git");
58                  return new DefaultGitMirror(context.schedule(), context.direction(), context.credential(),
59                                              context.localRepo(), context.localPath(),
60                                              URI.create(components[0]), components[1], components[2],
61                                              context.gitignore());
62              }
63          }
64  
65          return null;
66      }
67  }