|
4 | 4 | import java.net.InetAddress;
|
5 | 5 | import java.nio.file.Files;
|
6 | 6 | import java.nio.file.Path;
|
7 |
| -import java.nio.file.attribute.FileTime; |
8 |
| -import java.time.Duration; |
9 | 7 | import java.util.HashMap;
|
10 | 8 | import java.util.Iterator;
|
11 | 9 | import java.util.List;
|
|
39 | 37 | import com.devonfw.tools.ide.os.SystemInfoImpl;
|
40 | 38 | import com.devonfw.tools.ide.process.ProcessContext;
|
41 | 39 | import com.devonfw.tools.ide.process.ProcessContextImpl;
|
42 |
| -import com.devonfw.tools.ide.process.ProcessErrorHandling; |
43 | 40 | import com.devonfw.tools.ide.process.ProcessResult;
|
44 | 41 | import com.devonfw.tools.ide.property.Property;
|
45 | 42 | import com.devonfw.tools.ide.repo.CustomToolRepository;
|
|
54 | 51 | */
|
55 | 52 | public abstract class AbstractIdeContext implements IdeContext {
|
56 | 53 |
|
| 54 | + private static final String IDE_URLS_GIT = "https://github.com/devonfw/ide-urls.git"; |
| 55 | + |
57 | 56 | private final Map<IdeLogLevel, IdeSubLogger> loggers;
|
58 | 57 |
|
59 | 58 | private final Path ideHome;
|
@@ -120,8 +119,6 @@ public abstract class AbstractIdeContext implements IdeContext {
|
120 | 119 |
|
121 | 120 | private UrlMetadata urlMetadata;
|
122 | 121 |
|
123 |
| - private static final Duration GIT_PULL_CACHE_DELAY_MILLIS = Duration.ofMillis(30 * 60 * 1000); |
124 |
| - |
125 | 122 | /**
|
126 | 123 | * The constructor.
|
127 | 124 | *
|
@@ -186,10 +183,11 @@ public AbstractIdeContext(IdeLogLevel minLogLevel, Function<IdeLogLevel, IdeSubL
|
186 | 183 | Path rootPath = Path.of(root);
|
187 | 184 | if (Files.isDirectory(rootPath)) {
|
188 | 185 | if (!ideRootPath.equals(rootPath)) {
|
189 |
| - warning("Variable IDE_ROOT is set to '{}' but for your project '{}' would have been expected."); |
190 |
| - ideRootPath = rootPath; |
| 186 | + warning( |
| 187 | + "Variable IDE_ROOT is set to '{}' but for your project '{}' the path '{}' would have been expected.", |
| 188 | + root, this.ideHome.getFileName(), ideRootPath); |
191 | 189 | }
|
192 |
| - ideRootPath = this.ideHome.getParent(); |
| 190 | + ideRootPath = rootPath; |
193 | 191 | } else {
|
194 | 192 | warning("Variable IDE_ROOT is not set to a valid directory '{}'." + root);
|
195 | 193 | ideRootPath = null;
|
@@ -266,10 +264,7 @@ public String getMessageIdeHome() {
|
266 | 264 | */
|
267 | 265 | public boolean isTest() {
|
268 | 266 |
|
269 |
| - if (isMock()) { |
270 |
| - return true; |
271 |
| - } |
272 |
| - return false; |
| 267 | + return isMock(); |
273 | 268 | }
|
274 | 269 |
|
275 | 270 | /**
|
@@ -496,7 +491,7 @@ public UrlMetadata getUrls() {
|
496 | 491 |
|
497 | 492 | if (this.urlMetadata == null) {
|
498 | 493 | if (!isTest()) {
|
499 |
| - gitPullOrCloneIfNeeded(this.urlsPath, "https://github.com/devonfw/ide-urls.git"); |
| 494 | + this.getGitContext().pullOrFetchAndResetIfNeeded(IDE_URLS_GIT, this.urlsPath, "origin", "master"); |
500 | 495 | }
|
501 | 496 | this.urlMetadata = new UrlMetadata(this);
|
502 | 497 | }
|
@@ -566,7 +561,7 @@ public boolean isOnline() {
|
566 | 561 | try {
|
567 | 562 | int timeout = 1000;
|
568 | 563 | online = InetAddress.getByName("github.com").isReachable(timeout);
|
569 |
| - } catch (Exception e) { |
| 564 | + } catch (Exception ignored) { |
570 | 565 |
|
571 | 566 | }
|
572 | 567 | return online;
|
@@ -596,108 +591,15 @@ public DirectoryMerger getWorkspaceMerger() {
|
596 | 591 | }
|
597 | 592 |
|
598 | 593 | @Override
|
599 |
| - public ProcessContext newProcess() { |
| 594 | + public GitContext getGitContext() { |
600 | 595 |
|
601 |
| - ProcessContext processContext = new ProcessContextImpl(this); |
602 |
| - return processContext; |
| 596 | + return new GitContextImpl(this); |
603 | 597 | }
|
604 | 598 |
|
605 | 599 | @Override
|
606 |
| - public void gitPullOrClone(Path target, String gitRepoUrl) { |
607 |
| - |
608 |
| - Objects.requireNonNull(target); |
609 |
| - Objects.requireNonNull(gitRepoUrl); |
610 |
| - if (!gitRepoUrl.startsWith("http")) { |
611 |
| - throw new IllegalArgumentException("Invalid git URL '" + gitRepoUrl + "'!"); |
612 |
| - } |
613 |
| - ProcessContext pc = newProcess().directory(target).executable("git").withEnvVar("GIT_TERMINAL_PROMPT", "0"); |
614 |
| - if (Files.isDirectory(target.resolve(".git"))) { |
615 |
| - ProcessResult result = pc.addArg("remote").run(true, false); |
616 |
| - List<String> remotes = result.getOut(); |
617 |
| - if (remotes.isEmpty()) { |
618 |
| - String message = "This is a local git repo with no remote - if you did this for testing, you may continue...\n" |
619 |
| - + "Do you want to ignore the problem and continue anyhow?"; |
620 |
| - askToContinue(message); |
621 |
| - } else { |
622 |
| - pc.errorHandling(ProcessErrorHandling.WARNING); |
623 |
| - result = pc.addArg("pull").run(false, false); |
624 |
| - if (!result.isSuccessful()) { |
625 |
| - String message = "Failed to update git repository at " + target; |
626 |
| - if (this.offlineMode) { |
627 |
| - warning(message); |
628 |
| - interaction("Continuing as we are in offline mode - results may be outdated!"); |
629 |
| - } else { |
630 |
| - error(message); |
631 |
| - if (isOnline()) { |
632 |
| - error("See above error for details. If you have local changes, please stash or revert and retry."); |
633 |
| - } else { |
634 |
| - error( |
635 |
| - "It seems you are offline - please ensure Internet connectivity and retry or activate offline mode (-o or --offline)."); |
636 |
| - } |
637 |
| - askToContinue("Typically you should abort and fix the problem. Do you want to continue anyways?"); |
638 |
| - } |
639 |
| - } |
640 |
| - } |
641 |
| - } else { |
642 |
| - String branch = null; |
643 |
| - int hashIndex = gitRepoUrl.indexOf("#"); |
644 |
| - if (hashIndex != -1) { |
645 |
| - branch = gitRepoUrl.substring(hashIndex + 1); |
646 |
| - gitRepoUrl = gitRepoUrl.substring(0, hashIndex); |
647 |
| - } |
648 |
| - this.fileAccess.mkdirs(target); |
649 |
| - requireOnline("git clone of " + gitRepoUrl); |
650 |
| - pc.addArg("clone"); |
651 |
| - if (isQuietMode()) { |
652 |
| - pc.addArg("-q"); |
653 |
| - } else { |
654 |
| - } |
655 |
| - pc.addArgs("--recursive", gitRepoUrl, "--config", "core.autocrlf=false", "."); |
656 |
| - pc.run(); |
657 |
| - if (branch != null) { |
658 |
| - pc.addArgs("checkout", branch); |
659 |
| - pc.run(); |
660 |
| - } |
661 |
| - } |
662 |
| - } |
663 |
| - |
664 |
| - /** |
665 |
| - * Checks if the Git repository in the specified target folder needs an update by inspecting the modification time of |
666 |
| - * a magic file. |
667 |
| - * |
668 |
| - * @param urlsPath The Path to the Urls repository. |
669 |
| - * @param repoUrl The git remote URL of the Urls repository. |
670 |
| - */ |
671 |
| - |
672 |
| - private void gitPullOrCloneIfNeeded(Path urlsPath, String repoUrl) { |
673 |
| - |
674 |
| - Path gitDirectory = urlsPath.resolve(".git"); |
675 |
| - |
676 |
| - // Check if the .git directory exists |
677 |
| - if (Files.isDirectory(gitDirectory)) { |
678 |
| - Path magicFilePath = gitDirectory.resolve("HEAD"); |
679 |
| - long currentTime = System.currentTimeMillis(); |
680 |
| - // Get the modification time of the magic file |
681 |
| - long fileMTime; |
682 |
| - try { |
683 |
| - fileMTime = Files.getLastModifiedTime(magicFilePath).toMillis(); |
684 |
| - } catch (IOException e) { |
685 |
| - throw new IllegalStateException("Could not read " + magicFilePath, e); |
686 |
| - } |
| 600 | + public ProcessContext newProcess() { |
687 | 601 |
|
688 |
| - // Check if the file modification time is older than the delta threshold |
689 |
| - if ((currentTime - fileMTime > GIT_PULL_CACHE_DELAY_MILLIS.toMillis()) || isForceMode()) { |
690 |
| - gitPullOrClone(urlsPath, repoUrl); |
691 |
| - try { |
692 |
| - Files.setLastModifiedTime(magicFilePath, FileTime.fromMillis(currentTime)); |
693 |
| - } catch (IOException e) { |
694 |
| - throw new IllegalStateException("Could not read or write in " + magicFilePath, e); |
695 |
| - } |
696 |
| - } |
697 |
| - } else { |
698 |
| - // If the .git directory does not exist, perform git clone |
699 |
| - gitPullOrClone(urlsPath, repoUrl); |
700 |
| - } |
| 602 | + return new ProcessContextImpl(this); |
701 | 603 | }
|
702 | 604 |
|
703 | 605 | @Override
|
|
0 commit comments