Skip to content

Commit 8746243

Browse files
authored
#118: smart git pulls (#163)
1 parent 830c11f commit 8746243

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

cli/src/main/java/com/devonfw/tools/ide/context/AbstractIdeContext.java

+45-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
import java.nio.file.Files;
66
import java.nio.file.Path;
77
import java.nio.file.Paths;
8+
import java.time.Duration;
89
import java.util.HashMap;
910
import java.util.Iterator;
1011
import java.util.List;
1112
import java.util.Locale;
1213
import java.util.Map;
1314
import java.util.Objects;
1415
import java.util.function.Function;
16+
import java.nio.file.attribute.FileTime;
1517

1618
import com.devonfw.tools.ide.cli.CliArgument;
1719
import com.devonfw.tools.ide.cli.CliArguments;
@@ -119,6 +121,8 @@ public abstract class AbstractIdeContext implements IdeContext {
119121

120122
private UrlMetadata urlMetadata;
121123

124+
private static final Duration GIT_PULL_CACHE_DELAY_MILLIS = Duration.ofMillis(30 * 60 * 1000);
125+
122126
/**
123127
* The constructor.
124128
*
@@ -493,7 +497,7 @@ public UrlMetadata getUrls() {
493497

494498
if (this.urlMetadata == null) {
495499
if (!isTest()) {
496-
gitPullOrClone(this.urlsPath, "https://github.com/devonfw/ide-urls.git");
500+
gitPullOrCloneIfNeeded(this.urlsPath, "https://github.com/devonfw/ide-urls.git");
497501
}
498502
this.urlMetadata = new UrlMetadata(this);
499503
}
@@ -658,6 +662,46 @@ public void gitPullOrClone(Path target, String gitRepoUrl) {
658662
}
659663
}
660664

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+
661705
@Override
662706
public IdeSubLogger level(IdeLogLevel level) {
663707

0 commit comments

Comments
 (0)