summaryrefslogtreecommitdiffstats
path: root/gerrit-server/src/main/java/com/google/gerrit/server/git/GitProjectImporter.java
blob: 3f2ff0d2bfa19a206cebd1be968d90dc4882bd1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (C) 2009 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.server.git;

import com.google.gerrit.reviewdb.Project;
import com.google.gerrit.reviewdb.ReviewDb;
import com.google.gerrit.reviewdb.Project.SubmitType;
import com.google.gwtorm.client.OrmException;
import com.google.gwtorm.client.SchemaFactory;
import com.google.inject.Inject;

import org.eclipse.jgit.lib.RepositoryCache.FileKey;
import org.eclipse.jgit.util.FS;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/** Imports all projects found within the repository manager. */
public class GitProjectImporter {
  public interface Messages {
    void info(String msg);
    void warning(String msg);
  }

  private final LocalDiskRepositoryManager repositoryManager;
  private final SchemaFactory<ReviewDb> schema;
  private Messages messages;

  @Inject
  GitProjectImporter(final LocalDiskRepositoryManager repositoryManager,
      final SchemaFactory<ReviewDb> schema) {
    this.repositoryManager = repositoryManager;
    this.schema = schema;
  }

  public void run(final Messages msg) throws OrmException, IOException {
    messages = msg;
    messages.info("Scanning " + repositoryManager.getBasePath());
    final ReviewDb db = schema.open();
    try {
      final HashSet<String> have = new HashSet<String>();
      for (Project p : db.projects().all()) {
        have.add(p.getName());
      }
      importProjects(repositoryManager.getBasePath(), "", db, have);
    } finally {
      db.close();
    }
  }

  private void importProjects(final File dir, final String prefix,
      final ReviewDb db, final Set<String> have) throws OrmException,
      IOException {
    final File[] ls = dir.listFiles();
    if (ls == null) {
      return;
    }

    for (File f : ls) {
      String name = f.getName();
      if (".".equals(name) || "..".equals(name)) {
        continue;
      }

      if (FileKey.isGitRepository(f, FS.DETECTED)) {
        if (name.equals(".git")) {
          if ("".equals(prefix)) {
            // If the git base path is itself a git repository working
            // directory, this is a bit nonsensical for Gerrit Code Review.
            // Skip the path and do the next one.
            messages.warning("Skipping " + f.getAbsolutePath());
            continue;
          }
          name = prefix.substring(0, prefix.length() - 1);

        } else if (name.endsWith(".git")) {
          name = prefix + name.substring(0, name.length() - 4);

        } else {
          name = prefix + name;
          if (!have.contains(name)) {
            messages.warning("Importing non-standard name '" + name + "'");
          }
        }

        if (have.contains(name)) {
          continue;
        }

        final Project.NameKey nameKey = new Project.NameKey(name);
        final Project p = new Project(nameKey);

        p.setDescription(repositoryManager.getProjectDescription(nameKey));
        p.setSubmitType(SubmitType.MERGE_IF_NECESSARY);
        p.setUseContributorAgreements(false);
        p.setUseSignedOffBy(false);
        p.setUseContentMerge(false);
        p.setRequireChangeID(false);
        db.projects().insert(Collections.singleton(p));

      } else if (f.isDirectory()) {
        importProjects(f, prefix + f.getName() + "/", db, have);
      }
    }
  }
}