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

devonfw / IDEasy / 26476611208

26 May 2026 09:37PM UTC coverage: 71.02% (-0.1%) from 71.118%
26476611208

Pull #1958

github

web-flow
Merge 3488b9225 into f1383ac34
Pull Request #1958: #611: fix git pull on settings with local branch without remote

4472 of 6970 branches covered (64.16%)

Branch coverage included in aggregate %.

11577 of 15628 relevant lines covered (74.08%)

3.13 hits per line

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

36.89
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 org.slf4j.Logger;
11
import org.slf4j.LoggerFactory;
12

13
import com.devonfw.tools.ide.cli.CliException;
14
import com.devonfw.tools.ide.context.IdeContext;
15
import com.devonfw.tools.ide.log.IdeLogLevel;
16
import com.devonfw.tools.ide.os.SystemInfoImpl;
17
import com.devonfw.tools.ide.process.ProcessContext;
18
import com.devonfw.tools.ide.process.ProcessErrorHandling;
19
import com.devonfw.tools.ide.process.ProcessMode;
20
import com.devonfw.tools.ide.process.ProcessResult;
21
import com.devonfw.tools.ide.variable.IdeVariables;
22

23
/**
24
 * Implements the {@link GitContext}.
25
 */
26
public class GitContextImpl implements GitContext {
27

28
  private static final Logger LOG = LoggerFactory.getLogger(GitContextImpl.class);
4✔
29

30
  /** @see #getContext() */
31
  protected final IdeContext context;
32
  private Path git;
33

34
  /**
35
   * @param context the {@link IdeContext context}.
36
   */
37
  public GitContextImpl(IdeContext context) {
2✔
38

39
    this.context = context;
3✔
40
  }
1✔
41

42
  @Override
43
  public void pullOrCloneIfNeeded(GitUrl gitUrl, Path repository) {
44

45
    GitOperation.PULL_OR_CLONE.executeIfNeeded(this.context, gitUrl, repository, null);
8✔
46
  }
1✔
47

48
  @Override
49
  public boolean fetchIfNeeded(Path repository) {
50

51
    return fetchIfNeeded(repository, null, null);
×
52
  }
53

54
  @Override
55
  public boolean fetchIfNeeded(Path repository, String remote, String branch) {
56

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

60
  @Override
61
  public boolean isRepositoryUpdateAvailable(Path repository) {
62

63
    if (!hasUpstream(repository)) {
4!
64
      return false;
2✔
65
    }
66
    String localFailureMessage = String.format("Failed to get the local commit id of settings repository '%s'.", repository);
×
67
    String remoteFailureMessage = String.format("Failed to get the remote commit id of settings repository '%s', missing remote upstream branch?", repository);
×
68
    String localCommitId = runGitCommandAndGetSingleOutput(localFailureMessage, repository, ProcessMode.DEFAULT_CAPTURE, "rev-parse", "HEAD");
×
69
    String remoteCommitId = runGitCommandAndGetSingleOutput(remoteFailureMessage, repository, ProcessMode.DEFAULT_CAPTURE, "rev-parse", "@{u}");
×
70
    if ((localCommitId == null) || (remoteCommitId == null)) {
×
71
      return false;
×
72
    }
73
    return !localCommitId.equals(remoteCommitId);
×
74
  }
75

76
  @Override
77
  public boolean isRepositoryUpdateAvailable(Path repository, Path trackedCommitIdPath) {
78

79
    String trackedCommitId = this.context.getFileAccess().readFileContent(trackedCommitIdPath);
×
80
    if (trackedCommitId == null) {
×
81
      LOG.warn("Commit ID was not present at {}", trackedCommitIdPath);
×
82
      return true;
×
83
    }
84
    if (!hasUpstream(repository)) {
×
85
      return false;
×
86
    }
87
    String remoteFailureMessage = String.format("Failed to get the remote commit id of settings repository '%s', missing remote upstream branch?", repository);
×
88
    String remoteCommitId = runGitCommandAndGetSingleOutput(remoteFailureMessage, repository, ProcessMode.DEFAULT_CAPTURE, "rev-parse", "@{u}");
×
89
    if (remoteCommitId == null) {
×
90
      return false;
×
91
    }
92
    return !trackedCommitId.equals(remoteCommitId);
×
93
  }
94

95
  @Override
96
  public void pullOrCloneAndResetIfNeeded(GitUrl gitUrl, Path repository, String remoteName) {
97

98
    pullOrCloneIfNeeded(gitUrl, repository);
4✔
99
    reset(repository, gitUrl.branch(), remoteName);
6✔
100
    cleanup(repository);
3✔
101
  }
1✔
102

103
  @Override
104
  public void pullSafelyWithStash(Path repository) {
105
    String token = "autostash:pull:" + java.util.UUID.randomUUID();
×
106
    LOG.debug("Untracked files found. Creating temporary stash with token '{}'", token);
×
107
    ProcessResult stashRes = runGitCommand(repository, ProcessMode.DEFAULT, "--no-pager", "stash", "push", "--include-untracked", "-m", token, "--quiet");
×
108
    if (!stashRes.isSuccessful()) {
×
109
      LOG.warn("Failed to create stash before pull on {}", repository);
×
110
      handleErrors(repository, stashRes);
×
111
    }
112

113
    ProcessResult listRes = runGitCommand(repository, ProcessMode.DEFAULT_CAPTURE, "--no-pager", "stash", "list");
×
114
    if (!listRes.isSuccessful()) {
×
115
      LOG.warn("Failed to list stash after creating temporary stash on {}", repository);
×
116
      handleErrors(repository, listRes);
×
117
    }
118

119
    String stashRef = findStashRefByMessage(listRes.getOut(), token);
×
120
    if (stashRef == null) {
×
121
      LOG.warn("Could not find created stash by token '{}'. Leaving stash untouched.", token);
×
122
    } else {
123
      LOG.debug("Created stash identified as '{}'", stashRef);
×
124
    }
125

126
    pull(repository);
×
127

128
    if (stashRef != null) {
×
129
      ProcessResult popRes = runGitCommand(repository, ProcessMode.DEFAULT, "--no-pager", "stash", "pop", stashRef, "--quiet");
×
130
      if (!popRes.isSuccessful()) {
×
131
        LOG.warn("Applying stash {} failed after successful pull on {}.", stashRef, repository);
×
132
        handleErrors(repository, popRes);
×
133
      } else {
134
        LOG.debug("Stash {} successfully popped after pull.", stashRef);
×
135
      }
136
    } else {
×
137
      LOG.warn("Skipping stash pop because stashRef is unknown (token '{}'). Stash remains on the stack.", token);
×
138
    }
139
  }
×
140

141
  @Override
142
  public boolean hasUntrackedFiles(Path repository) {
143
    ProcessResult status = runGitCommand(repository, ProcessMode.DEFAULT_CAPTURE, "--no-pager", "status", "--porcelain", "-uall");
×
144
    if (!status.isSuccessful()) {
×
145
      handleErrors(repository, status);
×
146
      return false;
×
147
    }
148
    List<String> out = status.getOut();
×
149
    return !out.isEmpty();
×
150
  }
151

152
  private String findStashRefByMessage(List<String> stashList, String needle) {
153
    if (stashList.isEmpty()) {
×
154
      return null;
×
155
    }
156
    for (String line : stashList) {
×
157
      if (line.contains(needle)) {
×
158
        int idx = line.indexOf(':');
×
159
        if (idx > 0) {
×
160
          return line.substring(0, idx).trim();
×
161
        }
162
      }
163
    }
×
164
    return null;
×
165
  }
166

167

168
  @Override
169
  public void pullOrClone(GitUrl gitUrl, Path repository) {
170

171
    Objects.requireNonNull(repository);
3✔
172
    Objects.requireNonNull(gitUrl);
3✔
173
    if (Files.isDirectory(repository.resolve(GIT_FOLDER))) {
7✔
174
      // checks for remotes
175
      String remote = determineRemote(repository);
4✔
176
      if (remote == null) {
2!
177
        String message = repository + " is a local git repository with no remote - if you did this for testing, you may continue...\n"
×
178
            + "Do you want to ignore the problem and continue anyhow?";
179
        this.context.askToContinue(message);
×
180
      } else {
×
181
        pull(repository);
3✔
182
      }
183
    } else {
1✔
184
      clone(gitUrl, repository);
4✔
185
    }
186
  }
1✔
187

188
  /**
189
   * Handles errors which occurred during git pull.
190
   *
191
   * @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
192
   *     git will by default create a sub-folder by default on clone but the * final folder that will contain the ".git" subfolder.
193
   * @param result the {@link ProcessResult} to evaluate.
194
   */
195
  private void handleErrors(Path targetRepository, ProcessResult result) {
196

197
    if (!result.isSuccessful()) {
×
198
      String message = "Failed to update git repository at " + targetRepository;
×
199
      if (this.context.isOfflineMode()) {
×
200
        LOG.warn(message);
×
201
        IdeLogLevel.INTERACTION.log(LOG, "Continuing as we are in offline mode - results may be outdated!");
×
202
      } else {
203
        LOG.error(message);
×
204
        if (this.context.isOnline()) {
×
205
          LOG.error("See above error for details. If you have local changes, please stash or revert and retry.");
×
206
        } else {
207
          LOG.error("It seems you are offline - please ensure Internet connectivity and retry or activate offline mode (-o or --offline).");
×
208
        }
209
        this.context.askToContinue("Typically you should abort and fix the problem. Do you want to continue anyways?");
×
210
      }
211
    }
212
  }
×
213

214
  @Override
215
  public void clone(GitUrl gitUrl, Path repository) {
216

217
    GitUrlSyntax gitUrlSyntax = IdeVariables.PREFERRED_GIT_PROTOCOL.get(getContext());
6✔
218
    gitUrl = gitUrlSyntax.format(gitUrl);
4✔
219
    if (this.context.isOfflineMode()) {
4✔
220
      this.context.requireOnline("git clone of " + gitUrl, false);
×
221
    }
222
    this.context.getFileAccess().mkdirs(repository);
5✔
223
    List<String> args = new ArrayList<>(7);
5✔
224
    args.add("clone");
4✔
225
    if (this.context.isQuietMode()) {
4!
226
      args.add("-q");
×
227
    }
228
    args.add("--recursive");
4✔
229
    args.add(gitUrl.url());
5✔
230
    args.add("--config");
4✔
231
    args.add("core.autocrlf=false");
4✔
232
    args.add(".");
4✔
233
    runGitCommand(repository, args);
4✔
234
    String branch = gitUrl.branch();
3✔
235
    if (branch != null) {
2✔
236
      runGitCommand(repository, "switch", branch);
13✔
237
    }
238
  }
1✔
239

240
  @Override
241
  public void pull(Path repository) {
242

243
    if (this.context.isOffline()) {
4!
244
      LOG.info("Skipping git pull on {} because offline", repository);
×
245
      return;
×
246
    }
247
    ProcessResult result = runGitCommand(repository, ProcessMode.DEFAULT, "--no-pager", "pull", "--quiet");
19✔
248
    if (!result.isSuccessful()) {
3!
249
      String branchName = determineCurrentBranch(repository);
×
250
      LOG.warn("Git pull on branch {} failed for repository {}.", branchName, repository);
×
251
      handleErrors(repository, result);
×
252
    }
253
  }
1✔
254

255
  @Override
256
  public void fetch(Path repository, String remote, String branch) {
257

258
    if (branch == null) {
2!
259
      branch = determineCurrentBranch(repository);
×
260
    }
261
    if (remote == null) {
2!
262
      remote = determineRemote(repository);
×
263
    }
264

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

267
    if (!result.isSuccessful()) {
3!
268
      LOG.warn("Git fetch for '{}/{} failed.'.", remote, branch);
×
269
    }
270
  }
1✔
271

272
  @Override
273
  public String determineCurrentBranch(Path repository) {
274

275
    return runGitCommandAndGetSingleOutput("Failed to determine current branch of git repository", repository, "branch", "--show-current");
15✔
276
  }
277

278
  @Override
279
  public String determineRemote(Path repository) {
280

281
    return runGitCommandAndGetSingleOutput("Failed to determine current origin of git repository.", repository, "remote");
11✔
282
  }
283

284
  @Override
285
  public void reset(Path repository, String branchName, String remoteName) {
286

287
    if ((remoteName == null) || remoteName.isEmpty()) {
5!
288
      remoteName = DEFAULT_REMOTE;
2✔
289
    }
290
    if ((branchName == null) || branchName.isEmpty()) {
5!
291
      branchName = GitUrl.BRANCH_MASTER;
2✔
292
    }
293
    ProcessResult result = runGitCommand(repository, ProcessMode.DEFAULT, "diff-index", "--quiet", "HEAD");
19✔
294
    if (!result.isSuccessful()) {
3!
295
      // reset to origin/master
296
      LOG.warn("Git has detected modified files -- attempting to reset {} to '{}/{}'.", repository, remoteName, branchName);
17✔
297
      result = runGitCommand(repository, ProcessMode.DEFAULT, "reset", "--hard", remoteName + "/" + branchName);
21✔
298
      if (!result.isSuccessful()) {
3!
299
        LOG.warn("Git failed to reset {} to '{}/{}'.", remoteName, branchName, repository);
×
300
        handleErrors(repository, result);
×
301
      }
302
    }
303
  }
1✔
304

305
  @Override
306
  public void cleanup(Path repository) {
307

308
    // check for untracked files
309
    ProcessResult result = runGitCommand(repository, ProcessMode.DEFAULT_CAPTURE, "ls-files", "--other", "--directory", "--exclude-standard");
23✔
310
    if (!result.getOut().isEmpty()) {
4✔
311
      // delete untracked files
312
      LOG.warn("Git detected untracked files in {} and is attempting a cleanup.", repository);
4✔
313
      runGitCommand(repository, "clean", "-df");
13✔
314
    }
315
  }
1✔
316

317
  @Override
318
  public String retrieveGitUrl(Path repository) {
319

320
    return runGitCommandAndGetSingleOutput("Failed to retrieve git URL for repository", repository, "config", "--get", "remote.origin.url");
×
321
  }
322

323
  IdeContext getContext() {
324

325
    return this.context;
3✔
326
  }
327

328
  @Override
329
  public Path findGitRequired() {
330

331
    Path gitPath = findGit();
×
332
    if (gitPath == null) {
×
333
      String message = "Git " + IdeContext.IS_NOT_INSTALLED_BUT_REQUIRED;
×
334
      if (SystemInfoImpl.INSTANCE.isWindows()) {
×
335
        message += IdeContext.PLEASE_DOWNLOAD_AND_INSTALL_GIT + ":\n " + IdeContext.WINDOWS_GIT_DOWNLOAD_URL;
×
336
      }
337
      throw new CliException(message);
×
338
    }
339
    return gitPath;
×
340
  }
341

342
  @Override
343
  public Path findGit() {
344
    if (this.git != null) {
×
345
      return this.git;
×
346
    }
347

348
    Path gitPath = findGitInPath(Path.of("git"));
×
349

350
    if (gitPath == null) {
×
351
      if (SystemInfoImpl.INSTANCE.isWindows()) {
×
352
        gitPath = findGitOnWindowsViaBash();
×
353
      }
354
    }
355

356
    if (gitPath != null) {
×
357
      this.git = gitPath;
×
358
      LOG.trace("Found git at: {}", gitPath);
×
359
    }
360

361
    return gitPath;
×
362
  }
363

364
  private Path findGitOnWindowsViaBash() {
365
    Path gitPath;
366
    Path bashBinary = this.context.findBashRequired();
×
367
    LOG.trace("Trying to find git path on Windows");
×
368
    if (Files.exists(bashBinary)) {
×
369
      gitPath = bashBinary.getParent().resolve("git.exe");
×
370
      if (Files.exists(gitPath)) {
×
371
        LOG.trace("Git path was extracted from bash path at: {}", gitPath);
×
372
      } else {
373
        LOG.error("Git path: {} was extracted from bash path at: {} but it does not exist", gitPath, bashBinary);
×
374
        return null;
×
375
      }
376
    } else {
377
      LOG.error("Bash path was checked at: {} but it does not exist", bashBinary);
×
378
      return null;
×
379
    }
380
    return gitPath;
×
381
  }
382

383
  private Path findGitInPath(Path gitPath) {
384
    LOG.trace("Trying to find git executable within the PATH environment variable");
×
385
    Path binaryGitPath = this.context.getPath().findBinary(gitPath);
×
386
    if (gitPath == binaryGitPath) {
×
387
      LOG.debug("No git executable could be found within the PATH environment variable");
×
388
      return null;
×
389
    }
390
    return binaryGitPath;
×
391
  }
392

393
  /**
394
   * Retrieves an optional git config value from the given repository.
395
   *
396
   * @param directory the git repository directory.
397
   * @param key the git config key.
398
   * @return the config value or {@code null} if not available.
399
   */
400
  private String getOptionalGitConfigValue(Path directory, String key) {
401

402
    ProcessResult result = runGitCommand(
×
403
        directory,
404
        ProcessMode.DEFAULT_CAPTURE,
405
        ProcessErrorHandling.NONE,
406
        "config",
407
        "--get",
408
        key
409
    );
410

411
    if (result.isSuccessful() && result.getOut().size() == 1) {
×
412
      return result.getOut().getFirst();
×
413
    }
414

415
    return null;
×
416

417
  }
418

419
  /**
420
   * Checks if the current branch has an upstream remote branch configured.
421
   *
422
   * @param repository the git repository directory.
423
   * @return {@code true} if an upstream is configured, {@code false} otherwise.
424
   */
425
  private boolean hasUpstream(Path repository) {
426

427
    String branch = determineCurrentBranch(repository);
4✔
428
    if ((branch == null) || branch.isBlank()) {
2!
429
      return false;
2✔
430
    }
431

432
    String remote = getOptionalGitConfigValue(repository, "branch." + branch + ".remote");
×
433
    String merge = getOptionalGitConfigValue(repository, "branch." + branch + ".merge");
×
434

435
    if ((remote == null) || (merge == null)) {
×
436
      LOG.warn("No upstream configured for branch {} in settings repository {}. Please switch to a branch with remote upstream.", branch, repository);
×
437
      return false;
×
438
    }
439

440
    return true;
×
441
  }
442

443
  private void runGitCommand(Path directory, String... args) {
444

445
    ProcessResult result = runGitCommand(directory, ProcessMode.DEFAULT, args);
6✔
446
    if (!result.isSuccessful()) {
3!
447
      String command = result.getCommand();
×
448
      this.context.requireOnline(command, false);
×
449
      result.failOnError();
×
450
    }
451
  }
1✔
452

453
  private void runGitCommand(Path directory, List<String> args) {
454

455
    runGitCommand(directory, args.toArray(String[]::new));
10✔
456
  }
1✔
457

458
  private String runGitCommandAndGetSingleOutput(String warningOnError, Path directory, String... args) {
459

460
    return runGitCommandAndGetSingleOutput(warningOnError, directory, ProcessMode.DEFAULT_CAPTURE, args);
7✔
461
  }
462

463
  private String runGitCommandAndGetSingleOutput(String warningOnError, Path directory, ProcessMode mode, String... args) {
464
    ProcessErrorHandling errorHandling = ProcessErrorHandling.NONE;
2✔
465
    if (LOG.isDebugEnabled()) {
3!
466
      errorHandling = ProcessErrorHandling.LOG_WARNING;
2✔
467
    }
468
    ProcessResult result = runGitCommand(directory, mode, errorHandling, args);
7✔
469
    if (result.isSuccessful()) {
3!
470
      List<String> out = result.getOut();
3✔
471
      int size = out.size();
3✔
472
      if (size == 1) {
3✔
473
        return out.getFirst();
4✔
474
      } else if (size == 0) {
2!
475
        warningOnError += " - No output received from " + result.getCommand();
6✔
476
      } else {
477
        warningOnError += " - Expected single line of output but received " + size + " lines from " + result.getCommand();
×
478
      }
479
    }
480
    LOG.warn(warningOnError);
3✔
481
    return null;
2✔
482
  }
483

484
  private ProcessResult runGitCommand(Path directory, ProcessMode mode, String... args) {
485

486
    return runGitCommand(directory, mode, ProcessErrorHandling.LOG_WARNING, args);
7✔
487
  }
488

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

491
    ProcessContext processContext;
492

493
    if (this.context.isBatchMode()) {
4!
494
      processContext = this.context.newProcess().executable(findGitRequired()).withEnvVar("GIT_TERMINAL_PROMPT", "0").withEnvVar("GCM_INTERACTIVE", "never")
×
495
          .withEnvVar("GIT_ASKPASS", "echo").withEnvVar("SSH_ASKPASS", "echo").errorHandling(errorHandling).directory(directory);
×
496
    } else {
497
      processContext = this.context.newProcess().executable(findGitRequired()).errorHandling(errorHandling).directory(directory);
11✔
498
    }
499

500
    processContext.addArgs(args);
4✔
501
    return processContext.run(mode);
4✔
502
  }
503

504
  @Override
505
  public void saveCurrentCommitId(Path repository, Path trackedCommitIdPath) {
506

507
    if ((repository == null) || (trackedCommitIdPath == null)) {
4!
508
      LOG.warn("Invalid usage of saveCurrentCommitId with null value");
×
509
      return;
×
510
    }
511
    LOG.trace("Saving commit Id of {} into {}", repository, trackedCommitIdPath);
5✔
512
    String currentCommitId = determineCurrentCommitId(repository);
4✔
513
    if (currentCommitId != null) {
2!
514
      try {
515
        Files.writeString(trackedCommitIdPath, currentCommitId);
6✔
516
      } catch (IOException e) {
×
517
        throw new IllegalStateException("Failed to save commit ID", e);
×
518
      }
1✔
519
    }
520
  }
1✔
521

522
  /**
523
   * @param repository the {@link Path} to the git repository.
524
   * @return the current commit ID of the given {@link Path repository}.
525
   */
526
  protected String determineCurrentCommitId(Path repository) {
527
    return runGitCommandAndGetSingleOutput("Failed to get current commit id.", repository, "rev-parse", FILE_HEAD);
×
528
  }
529
}
530

531

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