|
5 | 5 | import java.nio.file.Files;
|
6 | 6 | import java.nio.file.Path;
|
7 | 7 | import java.nio.file.Paths;
|
| 8 | +import java.time.Duration; |
8 | 9 | import java.util.HashMap;
|
9 | 10 | import java.util.Iterator;
|
10 | 11 | import java.util.List;
|
11 | 12 | import java.util.Locale;
|
12 | 13 | import java.util.Map;
|
13 | 14 | import java.util.Objects;
|
14 | 15 | import java.util.function.Function;
|
| 16 | +import java.nio.file.attribute.FileTime; |
15 | 17 |
|
16 | 18 | import com.devonfw.tools.ide.cli.CliArgument;
|
17 | 19 | import com.devonfw.tools.ide.cli.CliArguments;
|
@@ -119,6 +121,8 @@ public abstract class AbstractIdeContext implements IdeContext {
|
119 | 121 |
|
120 | 122 | private UrlMetadata urlMetadata;
|
121 | 123 |
|
| 124 | + private static final Duration GIT_PULL_CACHE_DELAY_MILLIS = Duration.ofMillis(30 * 60 * 1000); |
| 125 | + |
122 | 126 | /**
|
123 | 127 | * The constructor.
|
124 | 128 | *
|
@@ -493,7 +497,7 @@ public UrlMetadata getUrls() {
|
493 | 497 |
|
494 | 498 | if (this.urlMetadata == null) {
|
495 | 499 | if (!isTest()) {
|
496 |
| - gitPullOrClone(this.urlsPath, "https://github.com/devonfw/ide-urls.git"); |
| 500 | + gitPullOrCloneIfNeeded(this.urlsPath, "https://github.com/devonfw/ide-urls.git"); |
497 | 501 | }
|
498 | 502 | this.urlMetadata = new UrlMetadata(this);
|
499 | 503 | }
|
@@ -658,6 +662,46 @@ public void gitPullOrClone(Path target, String gitRepoUrl) {
|
658 | 662 | }
|
659 | 663 | }
|
660 | 664 |
|
| 665 | + /** |
| 666 | + * Checks if the Git repository in the specified target folder needs an update by |
| 667 | + * inspecting the modification time of a magic file. |
| 668 | + * |
| 669 | + * @param urlsPath The Path to the Urls repository. |
| 670 | + * @param repoUrl The git remote URL of the Urls repository. |
| 671 | + */ |
| 672 | + |
| 673 | + private void gitPullOrCloneIfNeeded(Path urlsPath, String repoUrl) { |
| 674 | + |
| 675 | + Path gitDirectory = urlsPath.resolve(".git"); |
| 676 | + |
| 677 | + // Check if the .git directory exists |
| 678 | + if (Files.isDirectory(gitDirectory)) { |
| 679 | + Path magicFilePath = gitDirectory.resolve("HEAD"); |
| 680 | + long currentTime = System.currentTimeMillis(); |
| 681 | + // Get the modification time of the magic file |
| 682 | + long fileMTime; |
| 683 | + try { |
| 684 | + fileMTime = Files.getLastModifiedTime(magicFilePath).toMillis(); |
| 685 | + } catch (IOException e) { |
| 686 | + throw new IllegalStateException("Could not read " + magicFilePath, e); |
| 687 | + } |
| 688 | + |
| 689 | + // Check if the file modification time is older than the delta threshold |
| 690 | + if ((currentTime - fileMTime > GIT_PULL_CACHE_DELAY_MILLIS.toMillis()) || isForceMode()) { |
| 691 | + gitPullOrClone(urlsPath, repoUrl); |
| 692 | + try { |
| 693 | + Files.setLastModifiedTime(magicFilePath, FileTime.fromMillis(currentTime)); |
| 694 | + } catch (IOException e) { |
| 695 | + throw new IllegalStateException("Could not read or write in " + magicFilePath, e); |
| 696 | + } |
| 697 | + } |
| 698 | + } else { |
| 699 | + // If the .git directory does not exist, perform git clone |
| 700 | + gitPullOrClone(urlsPath, repoUrl); |
| 701 | + } |
| 702 | + } |
| 703 | + |
| 704 | + |
661 | 705 | @Override
|
662 | 706 | public IdeSubLogger level(IdeLogLevel level) {
|
663 | 707 |
|
|
0 commit comments