• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

devonfw / IDEasy / 45148417760

23 Nov 2026 09:35AM UTC coverage: 75.516% (+0.03%) from 75.488%
45148417760

Pull #2921

github

web-flow
Merge 479644e5d into f1ecc99d4
Pull Request #2921: Feature/2871 auto check new repos after cloning

6608 of 9705 branches covered (68.09%)

Branch coverage included in aggregate %.

16978 of 21528 relevant lines covered (78.86%)

3.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

73.08
cli/src/main/java/com/devonfw/tools/ide/git/repository/RepositoryCommandlet.java
1
package com.devonfw.tools.ide.git.repository;
2

3
import java.nio.file.Files;
4
import java.nio.file.Path;
5
import java.util.ArrayList;
6
import java.util.HashMap;
7
import java.util.List;
8
import java.util.Map;
9
import java.util.Set;
10

11
import org.slf4j.Logger;
12
import org.slf4j.LoggerFactory;
13

14
import com.devonfw.tools.ide.cli.CliAbortException;
15
import com.devonfw.tools.ide.commandlet.AbstractCommandlet;
16
import com.devonfw.tools.ide.commandlet.check.CheckCommandlet;
17
import com.devonfw.tools.ide.context.IdeContext;
18
import com.devonfw.tools.ide.git.GitContext;
19
import com.devonfw.tools.ide.git.GitUrl;
20
import com.devonfw.tools.ide.io.FileAccess;
21
import com.devonfw.tools.ide.property.RepositoryProperty;
22
import com.devonfw.tools.ide.step.Step;
23
import com.devonfw.tools.ide.tool.AbstractToolCommandlet;
24
import com.devonfw.tools.ide.tool.ide.IdeToolCommandlet;
25

26
/**
27
 * {@link AbstractCommandlet} to setup one or multiple GIT repositories for development.
28
 */
29
public class RepositoryCommandlet extends AbstractCommandlet {
30

31
  private static final Logger LOG = LoggerFactory.getLogger(RepositoryCommandlet.class);
4 ✔
32
  private static final String REPOSITORY = "repository";
33

34
  /** the repository to setup. */
35
  public final RepositoryProperty repository;
36

37
  /**
38
   * The constructor.
39
   *
40
   * @param context the {@link IdeContext}.
41
   */
42
  public RepositoryCommandlet(IdeContext context) {
43

44
    super(context);
3 ✔
45
    addKeyword(getName());
4 ✔
46
    addKeyword("setup");
3 ✔
47
    this.repository = add(new RepositoryProperty("", false, REPOSITORY));
11 ✔
48
  }
1 ✔
49

50
  @Override
51
  public String getName() {
52

53
    return REPOSITORY;
2 ✔
54
  }
55

56
  @Override
57
  protected void doRun() {
58

59
    Path repositoryFile = this.repository.getValue();
5 ✔
60

61
    if (repositoryFile != null) {
2 ✔
62
      // Handle the case when a specific repository is provided
63
      RepositoryConfig config = prepareActiveRepository(repositoryFile, true);
5 ✔
64
      if (config != null) {
2 ✔
65
        importRepository(config);
3 ✔
66
      }
67
    } else {
1 ✔
68
      // If no specific repository is provided, check for repositories folder
69
      Path repositoriesPath = this.context.getRepositoriesPath();
4 ✔
70
      if (repositoriesPath == null) {
2 ✔
71
        LOG.warn("Cannot find folder 'repositories' nor 'projects' in your settings.");
3 ✔
72
        return;
1 ✔
73
      }
74
      List<Path> propertiesFiles = this.context.getFileAccess()
5 ✔
75
          .listChildren(repositoriesPath, path -> path.getFileName().toString().endsWith(".properties"));
8 ✔
76
      boolean forceMode = this.context.isForceMode() || this.context.isForceRepositories();
12 !
77
      Map<Path, RepositoryConfig> repositoryConfigMap = new HashMap<>(propertiesFiles.size());
6 ✔
78
      for (Path propertiesFile : propertiesFiles) {
10 ✔
79
        RepositoryConfig config = prepareActiveRepositoryIgnoringInvalid(propertiesFile, forceMode);
5 ✔
80
        if (config != null) {
2 ✔
81
          repositoryConfigMap.put(propertiesFile, config);
5 ✔
82
        }
83
      }
1 ✔
84
      for (Path propertiesFile : propertiesFiles) {
10 ✔
85
        RepositoryConfig config = repositoryConfigMap.get(propertiesFile);
5 ✔
86
        if (config != null) {
2 ✔
87
          importRepository(config);
3 ✔
88
        }
89
      }
1 ✔
90
    }
91
  }
1 ✔
92

93
  /**
94
   * Like {@link #prepareActiveRepository(Path, boolean)} but ignores a single invalid repository instead of aborting the setup of all others. Resolving a
95
   * property value may fail for a malformed expression, and one broken file in the repositories folder must not prevent every other repository from being
96
   * set up. Only used when iterating all repositories - if the user explicitly requested a single repository, the failure is reported instead. A
97
   * {@link CliAbortException} is still propagated as the {@link Step} always re-throws it.
98
   *
99
   * @param repositoryFile the {@link Path} to the repository properties file.
100
   * @param forceMode - {@code true} to setup the repository even if it is not active, {@code false} otherwise.
101
   * @return the {@link RepositoryConfig} or {@code null} if the repository shall be skipped.
102
   */
103
  private RepositoryConfig prepareActiveRepositoryIgnoringInvalid(Path repositoryFile, boolean forceMode) {
104

105
    Step step = this.context.newStep("Prepare repository", repositoryFile);
11 ✔
106
    return step.call(() -> prepareActiveRepository(repositoryFile, forceMode), () -> null);
16 ✔
107
  }
108

109
  private RepositoryConfig prepareActiveRepository(Path repositoryFile, boolean forceMode) {
110

111
    // the active flag is evaluated before the remaining properties are read: reading a property resolves variables and expressions in its value, so an
112
    // expression like @ask-variable would otherwise ask the user for a repository that is skipped anyway
113
    RepositoryProperties properties = new RepositoryProperties(repositoryFile, this.context);
7 ✔
114
    if (!properties.isActive()) {
3 ✔
115
      if (forceMode) {
2 ✔
116
        LOG.info("Setup of repository {} is forced, hence proceeding ...", properties.getId());
6 ✔
117
      } else {
118
        LOG.info("Skipping repository {} because it is not active, use --force-repositories to setup all repositories ...", properties.getId());
5 ✔
119
        return null;
2 ✔
120
      }
121
    }
122
    RepositoryConfig config = RepositoryConfig.loadProperties(properties);
3 ✔
123
    if (config == null) {
2 ✔
124
      return null;
2 ✔
125
    }
126
    // prepare workspace creation for correct resolution of *
127
    List<String> workspaces = config.workspaces();
3 ✔
128
    for (String workspace : workspaces) {
10 ✔
129
      if (!RepositoryConfig.WORKSPACE_NAME_ALL.equals(workspace)) {
4 ✔
130
        Path workspacePath = this.context.getWorkspacePath(workspace);
5 ✔
131
        this.context.getFileAccess().mkdirs(workspacePath);
5 ✔
132
      }
133
    }
1 ✔
134
    return config;
2 ✔
135
  }
136

137
  private void importRepository(RepositoryConfig config) {
138

139
    this.context.newStep("Setup of repository " + config.id()).run(() -> {
11 ✔
140
      doImportRepository(config);
3 ✔
141
    });
1 ✔
142
  }
1 ✔
143

144
  private void doImportRepository(RepositoryConfig config) {
145
    LOG.debug("Repository configuration: {}", config);
4 ✔
146
    String repositoryRelativePath = config.path();
3 ✔
147
    if (repositoryRelativePath == null) {
2 ✔
148
      repositoryRelativePath = config.id();
3 ✔
149
    }
150
    Path ideStatusDir = this.context.getIdeHome().resolve(IdeContext.FOLDER_DOT_IDE);
6 ✔
151
    FileAccess fileAccess = this.context.getFileAccess();
4 ✔
152
    fileAccess.mkdirs(ideStatusDir);
3 ✔
153

154
    List<String> workspaces = config.workspaces();
3 ✔
155
    if ((workspaces.size() == 1) && (RepositoryConfig.WORKSPACE_NAME_ALL.equals(workspaces.getFirst()))) {
9 ✔
156
      // if workspaces=* replace with all existing workspaces
157
      workspaces = fileAccess.listChildren(this.context.getWorkspacesBasePath(), Files::isDirectory).stream().map(Path::getFileName).map(Path::toString)
16 ✔
158
          .toList();
2 ✔
159
    }
160
    // if main is contained in workspaces, it should come first (to ensure physical cloning to main and linking to others)
161
    if (!workspaces.getFirst().equals(IdeContext.WORKSPACE_MAIN) && workspaces.contains(IdeContext.WORKSPACE_MAIN)) {
10 ✔
162
      workspaces = new ArrayList<>(workspaces); // mutable copy
5 ✔
163
      workspaces.remove(IdeContext.WORKSPACE_MAIN);
4 ✔
164
      workspaces.addFirst(IdeContext.WORKSPACE_MAIN);
3 ✔
165
    }
166

167
    Path firstRepository = null;
2 ✔
168

169
    if (config.isVirtualSettingsRepository()) {
3 ✔
170
      firstRepository = this.context.getSettingsPath();
4 ✔
171
    }
172

173
    for (String workspaceName : workspaces) {
10 ✔
174
      Path workspacePath = this.context.getWorkspacePath(workspaceName);
5 ✔
175
      Path repositoryPath = workspacePath.resolve(repositoryRelativePath);
4 ✔
176
      Path repositoryCreatedStatusFile = ideStatusDir.resolve("repository." + config.id() + "." + workspaceName);
7 ✔
177
      boolean createRepository = true;
2 ✔
178
      if (!config.isVirtualSettingsRepository() && Files.isDirectory(repositoryPath.resolve(GitContext.GIT_FOLDER))) {
10 !
179
        if (firstRepository == null) {
×
180
          firstRepository = repositoryPath;
×
181
        }
182
        LOG.info("Repository {} already exists in workspace {} at {}", config.id(), workspaceName, repositoryPath);
×
183
        if (!(this.context.isForceMode() || this.context.isForceRepositories())) {
×
184
          LOG.info("Ignoring repository {} in workspace {}, use --force-repositories to rerun setup.", config.id(), workspaceName);
×
185
          createRepository = false;
×
186
        }
187
      }
188
      if (Files.exists(repositoryCreatedStatusFile)) {
5 !
189
        if (!(this.context.isForceMode() || this.context.isForceRepositories())) {
×
190
          LOG.info("Ignoring repository {} in workspace {} because it was already setup before, use --force-repositories for recreation.",
×
191
              config.id(), workspaceName);
×
192
          createRepository = false;
×
193
        }
194
      }
195
      if (createRepository) {
2 !
196
        if (firstRepository == null) {
2 ✔
197
          GitUrl gitUrl = config.asGitUrl();
3 ✔
198
          boolean newRepository = !Files.isDirectory(repositoryPath.resolve(GitContext.GIT_FOLDER));
10 !
199
          boolean success = cloneOrPullRepository(repositoryPath, gitUrl, repositoryCreatedStatusFile);
6 ✔
200
          if (success) {
2 !
201
            firstRepository = repositoryPath;
2 ✔
202
            if (newRepository) {
2 !
203
              getCommandlet(CheckCommandlet.class).checkRepository(repositoryPath, false);
7 ✔
204
            }
205
            buildRepository(config, repositoryPath);
5 ✔
206
            importRepository(config, repositoryPath, config.id());
6 ✔
207
          }
208
        } else if (!config.isVirtualSettingsRepository()) {
4 ✔
209
          fileAccess.mkdirs(repositoryPath.getParent());
4 ✔
210
          fileAccess.symlink(firstRepository, repositoryPath);
4 ✔
211
        }
212
      }
213
      Path linkRepositoryPath = config.isVirtualSettingsRepository() ? firstRepository : repositoryPath;
7 ✔
214
      if (Files.exists(linkRepositoryPath)) {
5 !
215
        for (RepositoryRemote remote : config.remotes()) {
7 !
216
          this.context.getGitContext().addRemote(linkRepositoryPath, remote.name(), remote.url());
×
217
        }
×
218
        for (RepositoryLink link : config.links()) {
11 ✔
219
          createRepositoryLink(link, linkRepositoryPath, workspacePath);
5 ✔
220
        }
1 ✔
221
      }
222
    }
1 ✔
223
  }
1 ✔
224

225
  private boolean linkTargetExists(RepositoryLink link, Path linkTargetPath) {
226

227
    if (!Files.exists(linkTargetPath)) {
5 !
228
      LOG.error("Skipping link from '{}' to '{}' because target does not exist: {}", link.link(), link.target(), linkTargetPath);
×
229
      return false;
×
230
    }
231
    return true;
2 ✔
232
  }
233

234
  private void createRepositoryLink(RepositoryLink link, Path repositoryPath, Path workspacePath) {
235
    Path linkPath = workspacePath.resolve(link.link());
5 ✔
236
    String target = link.target();
3 ✔
237
    Path linkTargetPath;
238
    if ((target != null) && !target.isBlank()) {
5 !
239
      linkTargetPath = repositoryPath.resolve(target);
4 ✔
240
      if (!linkTargetExists(link, linkTargetPath)) {
5 !
241
        return;
×
242
      }
243
    } else {
244
      linkTargetPath = repositoryPath;
2 ✔
245
    }
246
    FileAccess fileAccess = this.context.getFileAccess();
4 ✔
247
    fileAccess.mkdirs(linkPath.getParent());
4 ✔
248
    fileAccess.symlink(linkTargetPath, linkPath);
4 ✔
249
  }
1 ✔
250

251
  private boolean cloneOrPullRepository(Path repositoryPath, GitUrl gitUrl, Path repositoryCreatedStatusFile) {
252

253
    FileAccess fileAccess = this.context.getFileAccess();
4 ✔
254
    return this.context.newStep("Clone or pull repository").run(() -> {
12 ✔
255
      fileAccess.mkdirs(repositoryPath);
3 ✔
256
      this.context.getGitContext().pullOrClone(gitUrl, repositoryPath);
6 ✔
257
      fileAccess.touch(repositoryCreatedStatusFile);
3 ✔
258
    });
1 ✔
259
  }
260

261
  private boolean buildRepository(RepositoryConfig repositoryConfig, Path repositoryPath) {
262
    String buildCmd = repositoryConfig.buildCmd();
3 ✔
263
    if (buildCmd != null && !buildCmd.isEmpty()) {
2 !
264
      return this.context.newStep("Build repository via: " + buildCmd).run(() -> {
×
265
        String[] command = buildCmd.split("\\s+");
×
266
        AbstractToolCommandlet commandlet = this.context.getCommandletManager().getToolCommandlet(command[0]);
×
267
        if (commandlet == null) {
×
268
          String displayName = (command[0] == null || command[0].isBlank()) ? "<empty>" : "'" + command[0] + "'";
×
269
          LOG.error("Cannot build repository. Required tool '{}' not found. Please check your repository's build_cmd configuration value.",
×
270
              displayName);
271
          return;
×
272
        }
273
        commandlet.reset();
×
274
        for (int i = 1; i < command.length; i++) {
×
275
          commandlet.arguments.addValue(command[i]);
×
276
        }
277
        Path executionDirectory = repositoryPath;
×
278
        String path = repositoryConfig.buildPath();
×
279
        if (path != null) {
×
280
          executionDirectory = executionDirectory.resolve(path);
×
281
        }
282
        commandlet.setExecutionDirectory(executionDirectory);
×
283
        commandlet.run();
×
284
      });
×
285
    } else {
286
      LOG.debug("Build command not set. Skipping build for repository.");
3 ✔
287
      return true;
2 ✔
288
    }
289
  }
290

291
  private void importRepository(RepositoryConfig repositoryConfig, Path repositoryPath, String repositoryId) {
292

293
    Set<String> imports = repositoryConfig.imports();
3 ✔
294
    if ((imports == null) || imports.isEmpty()) {
5 !
295
      LOG.debug("Repository {} has no IDE configured for import.", repositoryId);
4 ✔
296
      return;
1 ✔
297
    }
298
    for (String ide : imports) {
10 ✔
299
      Step step = this.context.newStep("Importing repository " + repositoryId + " into " + ide);
7 ✔
300
      step.run(() -> {
9 ✔
301
        AbstractToolCommandlet commandlet = this.context.getCommandletManager().getToolCommandlet(ide);
6 ✔
302
        if (commandlet == null) {
2 !
303
          String displayName = (ide == null || ide.isBlank()) ? "<empty>" : "'" + ide + "'";
×
304
          step.error("Cannot import repository '{}'. Required IDE '{}' not found. Please check your repository's imports configuration.", repositoryId,
×
305
              displayName);
306
        } else if (commandlet instanceof IdeToolCommandlet ideToolCommandlet) {
6 !
307
          ideToolCommandlet.importRepository(repositoryPath);
4 ✔
308
        } else {
309
          step.error("Repository {} has import {} configured that is not an IDE!", repositoryId, ide);
×
310
        }
311
      });
1 ✔
312
    }
1 ✔
313
  }
1 ✔
314
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc