Skip to content

Commit 9463d97

Browse files
author
bobgarner
committed
Enhanced caching to keep track of the commit sha1 for a tag or branch so it knows to invalidate if it is different. This way if someone moves a tag or commits to a branch the cache will know it invalidate and we won't use stale files.
1 parent 5a9cae3 commit 9463d97

File tree

7 files changed

+139
-20
lines changed

7 files changed

+139
-20
lines changed

src/main/java/org/entityc/compiler/cmdline/command/CLInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public CLInfo(CommandLine commandLine) {
3232

3333
@Override
3434
public void printUsage() {
35-
super.printUsageWithArguments("[-s,--sources][-c,--configs][-t,--templates][-g,--generated][-a,--all]");
35+
super.printUsageWithArguments("[-s,--sources] [-c,--configs] [-t,--templates] [-g,--generated] [-a,--all]");
3636
}
3737

3838
@Override

src/main/java/org/entityc/compiler/model/config/MTRepository.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class MTRepository extends MTNode {
3030
private String repoName;
3131
private boolean indexedTemplates = false;
3232
private String setupFilename;
33+
private String commitSHA1;
3334

3435
public MTRepository(ParserRuleContext ctx, String name) {
3536
super(ctx);
@@ -68,7 +69,7 @@ public MTRepository(String setupUri) {
6869
Integer lastPathDelimIndex = path.lastIndexOf("/");
6970
if (lastPathDelimIndex == -1) {
7071
setupFilename = path;
71-
path = "";
72+
path = "";
7273
} else {
7374
setupFilename = path.substring(lastPathDelimIndex + 1);
7475
path = path.substring(0, lastPathDelimIndex);
@@ -208,4 +209,12 @@ public String getUri() {
208209
}
209210
return builder.toString();
210211
}
212+
213+
public String getCommitSHA1() {
214+
return commitSHA1;
215+
}
216+
217+
public void setCommitSHA1(String commitSHA1) {
218+
this.commitSHA1 = commitSHA1;
219+
}
211220
}

src/main/java/org/entityc/compiler/repository/GitHubRepositoryImporter.java

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66

77
package org.entityc.compiler.repository;
88

9+
import org.apache.commons.io.IOUtils;
910
import org.entityc.compiler.EntityCompiler;
1011
import org.entityc.compiler.model.config.MTRepository;
1112
import org.entityc.compiler.model.config.MTRepositoryImport;
1213
import org.entityc.compiler.util.ECLog;
13-
import org.apache.commons.io.IOUtils;
14+
import org.kohsuke.github.GHBranch;
1415
import org.kohsuke.github.GHContent;
16+
import org.kohsuke.github.GHRef;
1517
import org.kohsuke.github.GHRepository;
1618
import org.kohsuke.github.GitHub;
1719

@@ -38,11 +40,11 @@ public RepositoryFile importFromRepository(MTRepository repository, MTRepository
3840
"Unable to connect to GitHub: " + repository.getOrganization() + "/" + repository.getRepoName()
3941
+ "\n" + e.getMessage());
4042
}
41-
String pathPart = alternatePath != null ?
42-
(alternatePath + "/") :
43-
repository.getPath() != null && !repository.getPath().isEmpty() ?
44-
repository.getPath() + "/" :
45-
"";
43+
String pathPart = alternatePath != null ?
44+
(alternatePath + "/") :
45+
repository.getPath() != null && !repository.getPath().isEmpty() ?
46+
repository.getPath() + "/" :
47+
"";
4648
String gitRepoPath = pathPart + repositoryImport.getFilename() + "." + extension;
4749
GHRepository repo = null;
4850
try {
@@ -61,6 +63,7 @@ public RepositoryFile importFromRepository(MTRepository repository, MTRepository
6163
"Could not find file in repository: " + cachedRepositoryFile.getFilepath() + " with tag "
6264
+ repository.getTag());
6365
}
66+
6467
String outputFilepath = cachedRepositoryFile.getFilepath();
6568
// in case the filename has sub directories, make sure they are created
6669
if (outputFilepath.contains(File.separator)) {
@@ -90,6 +93,50 @@ public RepositoryFile importFromRepository(MTRepository repository, MTRepository
9093
return cachedRepositoryFile;
9194
}
9295

96+
@Override
97+
public void updateRepositoryCommitSHA1(MTRepository repository) {
98+
99+
if (repository.getCommitSHA1() != null) {
100+
return; // we are good
101+
}
102+
103+
try {
104+
if (github == null) {
105+
github = GitHub.connect();
106+
}
107+
} catch (IOException e) {
108+
ECLog.logFatal(
109+
"Unable to connect to GitHub: " + repository.getOrganization() + "/" + repository.getRepoName()
110+
+ "\n" + e.getMessage());
111+
}
112+
113+
GHRepository repo = null;
114+
try {
115+
repo = github.getRepository(repository.getOrganization() + "/" + repository.getRepoName());
116+
} catch (IOException e) {
117+
ECLog.logFatal("Unable to connect to repository: " + repository.getOrganization() + "/"
118+
+ repository.getRepoName());
119+
}
120+
121+
String sha1 = null;
122+
try {
123+
GHBranch branch = repo.getBranch(repository.getTag());
124+
if (branch != null) {
125+
sha1 = branch.getSHA1();
126+
}
127+
} catch (IOException e) {
128+
// could not find the commit, maybe its not a branch name
129+
}
130+
try {
131+
GHRef ref = repo.getRef("tags/" + repository.getTag());
132+
sha1 = ref.getObject().getSha();
133+
} catch (IOException e) {
134+
// must not be a tag
135+
}
136+
137+
repository.setCommitSHA1(sha1);
138+
}
139+
93140
@Override
94141
public void close() {
95142
github = null;

src/main/java/org/entityc/compiler/repository/LocalRepositoryImporter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public RepositoryFile importFromRepository(MTRepository repository, MTRepository
5252
return cachedRepositoryFile;
5353
}
5454

55+
@Override
56+
public void updateRepositoryCommitSHA1(MTRepository repository) {
57+
// nothing to do for this type of repository
58+
}
59+
5560
@Override
5661
public void close() {
5762
}

src/main/java/org/entityc/compiler/repository/RepositoryCache.java

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66

77
package org.entityc.compiler.repository;
88

9+
import org.apache.commons.io.FileUtils;
910
import org.entityc.compiler.model.config.MTRepository;
1011
import org.entityc.compiler.model.config.MTRepositoryType;
12+
import org.entityc.compiler.util.ECLog;
1113

1214
import java.io.File;
15+
import java.io.IOException;
1316
import java.util.UUID;
1417

1518
public class RepositoryCache {
@@ -32,10 +35,6 @@ public RepositoryCache(CacheStructure structure) {
3235
baseCacheDirectory.mkdirs();
3336
}
3437

35-
public File getBaseCacheDirectory() {
36-
return baseCacheDirectory;
37-
}
38-
3938
public RepositoryFile getRepositoryFile(MTRepository repository, String filename, boolean asInclude) {
4039
String outputFilepath = baseCacheDirectory.getAbsolutePath() + File.separator;
4140

@@ -44,23 +43,71 @@ public RepositoryFile getRepositoryFile(MTRepository repository, String filename
4443
outputFilepath += filename;
4544
break;
4645
case UserCache: {
47-
String pathPart = repository.getPath() != null && !repository.getPath().isEmpty() ? repository.getPath() + File.separator : "";
46+
String pathPart = repository.getPath() != null && !repository.getPath().isEmpty() ?
47+
repository.getPath() + File.separator :
48+
"";
4849
String completeFilename = pathPart + filename;
4950
if (repository.getType() == MTRepositoryType.LOCAL) {
5051
outputFilepath = completeFilename;
5152
} else {
5253
outputFilepath += getRepositoryCachePath(repository) + File.separator + completeFilename;
5354
}
5455
}
55-
break;
56+
break;
5657
}
5758

5859
RepositoryFile repositoryFile = new RepositoryFile(repository, outputFilepath, asInclude);
5960
return repositoryFile;
6061
}
6162

6263
private String getRepositoryCachePath(MTRepository repository) {
63-
return repository.getOrganization() + File.separator + repository.getRepoName() + File.separator + repository.getTag();
64+
return repository.getOrganization() + File.separator + repository.getRepoName() + File.separator
65+
+ repository.getTag();
66+
}
67+
68+
public void validateRepositoryInCache(MTRepository repository) {
69+
if (structure != CacheStructure.UserCache || repository.getType() == MTRepositoryType.LOCAL || !repository.isValid()) {
70+
return; // not this one
71+
}
72+
String commitSha1Filename = getBaseCacheDirectory().getAbsolutePath() + File.separator + getRepositoryCachePath(
73+
repository) + File.separator + ".commitSha1";
74+
File file = new File(commitSha1Filename);
75+
String cacheSHA1 = null;
76+
if (file.exists()) {
77+
try {
78+
cacheSHA1 = FileUtils.readFileToString(file);
79+
} catch (IOException e) {
80+
// oh well, assume it is not there
81+
}
82+
}
83+
String repositoryCommitSHA1 = repository.getCommitSHA1();
84+
if (repositoryCommitSHA1 == null || cacheSHA1 == null || !repository.getCommitSHA1().equals(cacheSHA1)) {
85+
invalidateCacheDirectory(repository);
86+
if (repositoryCommitSHA1 != null) {
87+
try {
88+
FileUtils.write(file, repositoryCommitSHA1);
89+
} catch (IOException e) {
90+
ECLog.logWarning("Unable to record repository sha1 in cache directory: " + commitSha1Filename);
91+
}
92+
}
93+
}
94+
}
95+
96+
public void invalidateCacheDirectory(MTRepository repository) {
97+
if (structure != CacheStructure.UserCache) {
98+
return;
99+
}
100+
String fullPath = getBaseCacheDirectory().getAbsolutePath() + File.separator + getRepositoryCachePath(
101+
repository);
102+
try {
103+
FileUtils.cleanDirectory(new File(fullPath));
104+
} catch (IOException e) {
105+
ECLog.logWarning("Unable to remove cache directory: " + fullPath);
106+
}
107+
}
108+
109+
public File getBaseCacheDirectory() {
110+
return baseCacheDirectory;
64111
}
65112

66113
public enum CacheStructure {

src/main/java/org/entityc/compiler/repository/RepositoryImportManager.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public RepositoryFile getRepositoryFileByName(String identifier) {
4747
return repositoryFilesByIdentifier.get(identifier);
4848
}
4949

50+
public void updateRepositoryCommitSHA1(MTRepository repository) {
51+
RepositoryImporter repositoryImporter = importersByType.get(repository.getType());
52+
repositoryImporter.updateRepositoryCommitSHA1(repository);
53+
}
54+
5055
public RepositoryFile importFromRepository(MTSpace space, MTRepositoryImport repositoryImport,
5156
String extension, boolean asInclude) {
5257

@@ -67,15 +72,20 @@ public RepositoryFile importFromRepository(MTSpace space, MTRepositoryImport rep
6772
&& type == MTRepositoryType.LOCAL;
6873

6974
MTRepository sourceRepository = repository;
70-
String alternatePath = null;
75+
String alternatePath = null;
7176
if (shouldUseSpaceRepository) {
7277
sourceRepository = space.getRepositoryThatImportedThisSpace();
73-
alternatePath = repository.getPath();
78+
alternatePath = repository.getPath();
7479
}
7580

7681
RepositoryImporter repositoryImporter = importersByType.get(sourceRepository.getType());
77-
RepositoryFile cacheRepositoryFile = repositoryCache.getRepositoryFile(sourceRepository, filename, asInclude);
78-
if (sourceRepository.getType() != MTRepositoryType.LOCAL && cacheRepositoryFile.exists()) { // check if it exists in the cache already
82+
repositoryImporter.updateRepositoryCommitSHA1(
83+
sourceRepository); // if it has one, fetch it so the cache will know if it needs to invalidate itself
84+
repositoryCache.validateRepositoryInCache(sourceRepository);
85+
RepositoryFile cacheRepositoryFile = repositoryCache.getRepositoryFile(sourceRepository, filename,
86+
asInclude);
87+
if (sourceRepository.getType() != MTRepositoryType.LOCAL
88+
&& cacheRepositoryFile.exists()) { // check if it exists in the cache already
7989
if (EntityCompiler.isVerbose()) {
8090
ECLog.logInfo("Found file already in cache: " + cacheRepositoryFile.getFilepath());
8191
}
@@ -84,7 +94,7 @@ public RepositoryFile importFromRepository(MTSpace space, MTRepositoryImport rep
8494
}
8595

8696
RepositoryFile file = repositoryImporter.importFromRepository(sourceRepository, repositoryImport,
87-
cacheRepositoryFile, extension, alternatePath);
97+
cacheRepositoryFile, extension, alternatePath);
8898
if (file == null || !file.exists()) {
8999
String nameOrPath = repository.getType() == MTRepositoryType.LOCAL ?
90100
repository.getPath() :

src/main/java/org/entityc/compiler/repository/RepositoryImporter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
public interface RepositoryImporter {
1313

1414
RepositoryFile importFromRepository(MTRepository repository, MTRepositoryImport repositoryImport, RepositoryFile cachedRepositoryFile, String extension, String alternatePath);
15+
void updateRepositoryCommitSHA1(MTRepository repository);
1516

1617
void close();
1718
}

0 commit comments

Comments
 (0)