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

devonfw / IDEasy / 19636171626

24 Nov 2025 01:34PM UTC coverage: 69.054% (+0.1%) from 68.924%
19636171626

Pull #1577

github

web-flow
Merge 679477cd5 into ffcb5d97f
Pull Request #1577: #1561: Fix BASH_PATH not used properly

3562 of 5647 branches covered (63.08%)

Branch coverage included in aggregate %.

9269 of 12934 relevant lines covered (71.66%)

3.15 hits per line

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

51.87
cli/src/main/java/com/devonfw/tools/ide/git/GitContextImpl.java
1
package com.devonfw.tools.ide.git;
2

3
import java.io.IOException;
4
import java.nio.file.Files;
5
import java.nio.file.Path;
6
import java.util.ArrayList;
7
import java.util.List;
8
import java.util.Objects;
9

10
import com.devonfw.tools.ide.cli.CliException;
11
import com.devonfw.tools.ide.context.IdeContext;
12
import com.devonfw.tools.ide.os.SystemInfoImpl;
13
import com.devonfw.tools.ide.process.ProcessContext;
14
import com.devonfw.tools.ide.process.ProcessErrorHandling;
15
import com.devonfw.tools.ide.process.ProcessMode;
16
import com.devonfw.tools.ide.process.ProcessResult;
17
import com.devonfw.tools.ide.variable.IdeVariables;
18

19
/**
20
 * Implements the {@link GitContext}.
21
 */
22
public class GitContextImpl implements GitContext {
23

24
  /** @see #getContext() */
25
  protected final IdeContext context;
26
  private Path git;
27

28
  /**
29
   * @param context the {@link IdeContext context}.
30
   */
31
  public GitContextImpl(IdeContext context) {
2✔
32

33
    this.context = context;
3✔
34
  }
1✔
35

36
  @Override
37
  public void pullOrCloneIfNeeded(GitUrl gitUrl, Path repository) {
38

39
    GitOperation.PULL_OR_CLONE.executeIfNeeded(this.context, gitUrl, repository, null);
8✔
40
  }
1✔
41

42
  @Override
43
  public boolean fetchIfNeeded(Path repository) {
44

45
    return fetchIfNeeded(repository, null, null);
×
46
  }
47

48
  @Override
49
  public boolean fetchIfNeeded(Path repository, String remote, String branch) {
50

51
    return GitOperation.FETCH.executeIfNeeded(this.context, new GitUrl("https://dummy.url/repo.git", branch), repository, remote);
12✔
52
  }
53

54
  @Override
55
  public boolean isRepositoryUpdateAvailable(Path repository) {
56

57
    findGitRequired();
3✔
58
    String localFailureMessage = String.format("Failed to get the local commit id of settings repository '%s'.", repository);
9✔
59
    String remoteFailureMessage = String.format("Failed to get the remote commit id of settings repository '%s', missing remote upstream branch?", repository);
9✔
60
    String localCommitId = runGitCommandAndGetSingleOutput(localFailureMessage, repository, ProcessMode.DEFAULT_CAPTURE, "rev-parse", "HEAD");
16✔
61
    String remoteCommitId = runGitCommandAndGetSingleOutput(remoteFailureMessage, repository, ProcessMode.DEFAULT_CAPTURE, "rev-parse", "@{u}");
16✔
62
    if ((localCommitId == null) || (remoteCommitId == null)) {
2!
63
      return false;
2✔
64
    }
65
    return !localCommitId.equals(remoteCommitId);
×
66
  }
67

68
  @Override
69
  public boolean isRepositoryUpdateAvailable(Path repository, Path trackedCommitIdPath) {
70

71
    findGitRequired();
×
72
    String trackedCommitId;
73
    try {
74
      trackedCommitId = Files.readString(trackedCommitIdPath);
×
75
    } catch (IOException e) {
×
76
      return false;
×
77
    }
×
78
    String remoteFailureMessage = String.format("Failed to get the remote commit id of settings repository '%s', missing remote upstream branch?", repository);
×
79
    String remoteCommitId = runGitCommandAndGetSingleOutput(remoteFailureMessage, repository, ProcessMode.DEFAULT_CAPTURE, "rev-parse", "@{u}");
×
80
    return !trackedCommitId.equals(remoteCommitId);
×
81
  }
82

83
  @Override
84
  public void pullOrCloneAndResetIfNeeded(GitUrl gitUrl, Path repository, String remoteName) {
85

86
    pullOrCloneIfNeeded(gitUrl, repository);
4✔
87
    reset(repository, gitUrl.branch(), remoteName);
6✔
88
    cleanup(repository);
3✔
89
  }
1✔
90

91
  @Override
92
  public void pullOrClone(GitUrl gitUrl, Path repository) {
93

94
    Objects.requireNonNull(repository);
3✔
95
    Objects.requireNonNull(gitUrl);
3✔
96
    if (Files.isDirectory(repository.resolve(GIT_FOLDER))) {
7✔
97
      // checks for remotes
98
      String remote = determineRemote(repository);
4✔
99
      if (remote == null) {
2!
100
        String message = repository + " is a local git repository with no remote - if you did this for testing, you may continue...\n"
×
101
            + "Do you want to ignore the problem and continue anyhow?";
102
        this.context.askToContinue(message);
×
103
      } else {
×
104
        pull(repository);
3✔
105
      }
106
    } else {
1✔
107
      clone(gitUrl, repository);
4✔
108
    }
109
  }
1✔
110

111
  /**
112
   * Handles errors which occurred during git pull.
113
   *
114
   * @param targetRepository the {@link Path} to the target folder where the git repository should be cloned or pulled. It is not the parent directory where
115
   *     git will by default create a sub-folder by default on clone but the * final folder that will contain the ".git" subfolder.
116
   * @param result the {@link ProcessResult} to evaluate.
117
   */
118
  private void handleErrors(Path targetRepository, ProcessResult result) {
119

120
    if (!result.isSuccessful()) {
×
121
      String message = "Failed to update git repository at " + targetRepository;
×
122
      if (this.context.isOfflineMode()) {
×
123
        this.context.warning(message);
×
124
        this.context.interaction("Continuing as we are in offline mode - results may be outdated!");
×
125
      } else {
126
        this.context.error(message);
×
127
        if (this.context.isOnline()) {
×
128
          this.context.error("See above error for details. If you have local changes, please stash or revert and retry.");
×
129
        } else {
130
          this.context.error("It seems you are offline - please ensure Internet connectivity and retry or activate offline mode (-o or --offline).");
×
131
        }
132
        this.context.askToContinue("Typically you should abort and fix the problem. Do you want to continue anyways?");
×
133
      }
134
    }
135
  }
×
136

137
  @Override
138
  public void clone(GitUrl gitUrl, Path repository) {
139

140
    findGitRequired();
3✔
141
    GitUrlSyntax gitUrlSyntax = IdeVariables.PREFERRED_GIT_PROTOCOL.get(getContext());
6✔
142
    gitUrl = gitUrlSyntax.format(gitUrl);
4✔
143
    if (this.context.isOfflineMode()) {
4✔
144
      this.context.requireOnline("git clone of " + gitUrl, false);
×
145
    }
146
    this.context.getFileAccess().mkdirs(repository);
5✔
147
    List<String> args = new ArrayList<>(7);
5✔
148
    args.add("clone");
4✔
149
    if (this.context.isQuietMode()) {
4!
150
      args.add("-q");
×
151
    }
152
    args.add("--recursive");
4✔
153
    args.add(gitUrl.url());
5✔
154
    args.add("--config");
4✔
155
    args.add("core.autocrlf=false");
4✔
156
    args.add(".");
4✔
157
    runGitCommand(repository, args);
4✔
158
    String branch = gitUrl.branch();
3✔
159
    if (branch != null) {
2!
160
      runGitCommand(repository, "switch", branch);
×
161
    }
162
  }
1✔
163

164
  @Override
165
  public void pull(Path repository) {
166

167
    findGitRequired();
3✔
168
    if (this.context.isOffline()) {
4!
169
      this.context.info("Skipping git pull on {} because offline", repository);
×
170
      return;
×
171
    }
172
    ProcessResult result = runGitCommand(repository, ProcessMode.DEFAULT, "--no-pager", "pull", "--quiet");
19✔
173
    if (!result.isSuccessful()) {
3!
174
      String branchName = determineCurrentBranch(repository);
×
175
      this.context.warning("Git pull on branch {} failed for repository {}.", branchName, repository);
×
176
      handleErrors(repository, result);
×
177
    }
178
  }
1✔
179

180
  @Override
181
  public void fetch(Path repository, String remote, String branch) {
182

183
    findGitRequired();
3✔
184
    if (branch == null) {
2!
185
      branch = determineCurrentBranch(repository);
×
186
    }
187
    if (remote == null) {
2!
188
      remote = determineRemote(repository);
×
189
    }
190

191
    ProcessResult result = runGitCommand(repository, ProcessMode.DEFAULT_CAPTURE, "fetch", Objects.requireNonNullElse(remote, "origin"), branch);
22✔
192

193
    if (!result.isSuccessful()) {
3!
194
      this.context.warning("Git fetch for '{}/{} failed.'.", remote, branch);
×
195
    }
196
  }
1✔
197

198
  @Override
199
  public String determineCurrentBranch(Path repository) {
200

201
    findGitRequired();
×
202
    return runGitCommandAndGetSingleOutput("Failed to determine current branch of git repository", repository, "branch", "--show-current");
×
203
  }
204

205
  @Override
206
  public String determineRemote(Path repository) {
207

208
    findGitRequired();
3✔
209
    return runGitCommandAndGetSingleOutput("Failed to determine current origin of git repository.", repository, "remote");
11✔
210
  }
211

212
  @Override
213
  public void reset(Path repository, String branchName, String remoteName) {
214

215
    findGitRequired();
3✔
216
    if ((remoteName == null) || remoteName.isEmpty()) {
5!
217
      remoteName = DEFAULT_REMOTE;
2✔
218
    }
219
    if ((branchName == null) || branchName.isEmpty()) {
5!
220
      branchName = GitUrl.BRANCH_MASTER;
2✔
221
    }
222
    ProcessResult result = runGitCommand(repository, ProcessMode.DEFAULT, "diff-index", "--quiet", "HEAD");
19✔
223
    if (!result.isSuccessful()) {
3!
224
      // reset to origin/master
225
      this.context.warning("Git has detected modified files -- attempting to reset {} to '{}/{}'.", repository, remoteName, branchName);
18✔
226
      result = runGitCommand(repository, ProcessMode.DEFAULT, "reset", "--hard", remoteName + "/" + branchName);
21✔
227
      if (!result.isSuccessful()) {
3!
228
        this.context.warning("Git failed to reset {} to '{}/{}'.", remoteName, branchName, repository);
×
229
        handleErrors(repository, result);
×
230
      }
231
    }
232
  }
1✔
233

234
  @Override
235
  public void cleanup(Path repository) {
236

237
    findGitRequired();
3✔
238
    // check for untracked files
239
    ProcessResult result = runGitCommand(repository, ProcessMode.DEFAULT_CAPTURE, "ls-files", "--other", "--directory", "--exclude-standard");
23✔
240
    if (!result.getOut().isEmpty()) {
4✔
241
      // delete untracked files
242
      this.context.warning("Git detected untracked files in {} and is attempting a cleanup.", repository);
10✔
243
      runGitCommand(repository, "clean", "-df");
13✔
244
    }
245
  }
1✔
246

247
  @Override
248
  public String retrieveGitUrl(Path repository) {
249

250
    findGitRequired();
×
251
    return runGitCommandAndGetSingleOutput("Failed to retrieve git URL for repository", repository, "config", "--get", "remote.origin.url");
×
252
  }
253

254
  IdeContext getContext() {
255

256
    return this.context;
3✔
257
  }
258

259
  @Override
260
  public Path findGitRequired() {
261

262
    Path gitPath = findGit();
×
263
    if (gitPath == null) {
×
264
      String message = "Git " + IdeContext.IS_NOT_INSTALLED_BUT_REQUIRED;
×
265
      if (SystemInfoImpl.INSTANCE.isWindows()) {
×
266
        message += IdeContext.PLEASE_DOWNLOAD_AND_INSTALL_GIT + ":\n "
×
267
            + IdeContext.WINDOWS_GIT_DOWNLOAD_URL;
268
      }
269
      throw new CliException(message);
×
270
    }
271
    return gitPath;
×
272
  }
273

274
  /**
275
   * Finds the path to the Git executable.
276
   *
277
   * @return the {@link Path} to the Git executable, or {@code null} if Git is not found.
278
   */
279
  public Path findGit() {
280
    if (this.git != null) {
×
281
      return this.git;
×
282
    }
283
    Path gitPath = Path.of("git");
×
284
    Path binaryGitPath = this.context.getPath().findBinary(gitPath);
×
285
    if (SystemInfoImpl.INSTANCE.isWindows()) {
×
286
      Path bashBinary = this.context.findBashRequired();
×
287
      this.context.trace("Trying to find git path on Windows");
×
288
      if (Files.exists(bashBinary)) {
×
289
        gitPath = bashBinary.getParent().resolve("git.exe");
×
290
        if (Files.exists(gitPath)) {
×
291
          this.context.trace("Git path was extracted from bash path at: {}", gitPath);
×
292
        } else {
293
          this.context.error("Git path: {} was extracted from bash path at: {} but it does not exist", gitPath, bashBinary);
×
294
          return null;
×
295
        }
296
      } else {
297
        this.context.error("Bash path was checked at: {} but it does not exist", bashBinary);
×
298
        return null;
×
299
      }
300
    } else {
×
301
      if (gitPath == binaryGitPath) {
×
302
        this.context.error("No git executable was found on your system");
×
303
        return null;
×
304
      } else {
305
        gitPath = binaryGitPath;
×
306
      }
307
    }
308
    this.git = gitPath;
×
309
    this.context.trace("Found git at: {}", gitPath);
×
310
    return gitPath;
×
311
  }
312

313
  private void runGitCommand(Path directory, String... args) {
314

315
    ProcessResult result = runGitCommand(directory, ProcessMode.DEFAULT, args);
6✔
316
    if (!result.isSuccessful()) {
3!
317
      String command = result.getCommand();
×
318
      this.context.requireOnline(command, false);
×
319
      result.failOnError();
×
320
    }
321
  }
1✔
322

323
  private void runGitCommand(Path directory, List<String> args) {
324

325
    runGitCommand(directory, args.toArray(String[]::new));
10✔
326
  }
1✔
327

328
  private String runGitCommandAndGetSingleOutput(String warningOnError, Path directory, String... args) {
329

330
    return runGitCommandAndGetSingleOutput(warningOnError, directory, ProcessMode.DEFAULT_CAPTURE, args);
7✔
331
  }
332

333
  private String runGitCommandAndGetSingleOutput(String warningOnError, Path directory, ProcessMode mode, String... args) {
334
    ProcessErrorHandling errorHandling = ProcessErrorHandling.NONE;
2✔
335
    if (this.context.debug().isEnabled()) {
5!
336
      errorHandling = ProcessErrorHandling.LOG_WARNING;
2✔
337
    }
338
    ProcessResult result = runGitCommand(directory, mode, errorHandling, args);
7✔
339
    if (result.isSuccessful()) {
3!
340
      List<String> out = result.getOut();
3✔
341
      int size = out.size();
3✔
342
      if (size == 1) {
3✔
343
        return out.get(0);
5✔
344
      } else if (size == 0) {
2!
345
        warningOnError += " - No output received from " + result.getCommand();
6✔
346
      } else {
347
        warningOnError += " - Expected single line of output but received " + size + " lines from " + result.getCommand();
×
348
      }
349
    }
350
    this.context.warning(warningOnError);
4✔
351
    return null;
2✔
352
  }
353

354
  private ProcessResult runGitCommand(Path directory, ProcessMode mode, String... args) {
355

356
    return runGitCommand(directory, mode, ProcessErrorHandling.LOG_WARNING, args);
7✔
357
  }
358

359
  private ProcessResult runGitCommand(Path directory, ProcessMode mode, ProcessErrorHandling errorHandling, String... args) {
360

361
    ProcessContext processContext;
362

363
    if (this.context.isBatchMode()) {
4!
364
      processContext = this.context.newProcess().executable(findGitRequired()).withEnvVar("GIT_TERMINAL_PROMPT", "0").withEnvVar("GCM_INTERACTIVE", "never")
×
365
          .withEnvVar("GIT_ASKPASS", "echo").withEnvVar("SSH_ASKPASS", "echo").errorHandling(errorHandling).directory(directory);
×
366
    } else {
367
      processContext = this.context.newProcess().executable(findGitRequired()).errorHandling(errorHandling)
9✔
368
          .directory(directory);
2✔
369
    }
370

371
    processContext.addArgs(args);
4✔
372
    return processContext.run(mode);
4✔
373
  }
374

375
  @Override
376
  public void saveCurrentCommitId(Path repository, Path trackedCommitIdPath) {
377

378
    if ((repository == null) || (trackedCommitIdPath == null)) {
4!
379
      this.context.warning("Invalid usage of saveCurrentCommitId with null value");
×
380
      return;
×
381
    }
382
    this.context.trace("Saving commit Id of {} into {}", repository, trackedCommitIdPath);
14✔
383
    String currentCommitId = determineCurrentCommitId(repository);
4✔
384
    if (currentCommitId != null) {
2!
385
      try {
386
        Files.writeString(trackedCommitIdPath, currentCommitId);
6✔
387
      } catch (IOException e) {
×
388
        throw new IllegalStateException("Failed to save commit ID", e);
×
389
      }
1✔
390
    }
391
  }
1✔
392

393
  /**
394
   * @param repository the {@link Path} to the git repository.
395
   * @return the current commit ID of the given {@link Path repository}.
396
   */
397
  protected String determineCurrentCommitId(Path repository) {
398
    return runGitCommandAndGetSingleOutput("Failed to get current commit id.", repository, "rev-parse", FILE_HEAD);
×
399
  }
400
}
401

402

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc