@ -48,7 +48,7 @@ public class DefaultGitSyncService implements GitSyncService {
private final Map < String , GitRepository > repositories = new ConcurrentHashMap < > ( ) ;
private final Map < String , GitRepository > repositories = new ConcurrentHashMap < > ( ) ;
private final Map < String , Runnable > updateListeners = new ConcurrentHashMap < > ( ) ;
private final Map < String , Runnable > updateListeners = new ConcurrentHashMap < > ( ) ;
private RevCommit lastCommit ;
private final Map < String , RevCommit > lastCommits = new ConcurrentHashMap < > ( ) ;
@Override
@Override
public void registerSync ( String key , String repoUri , String branch , long fetchFrequencyMs , Runnable onUpdate ) {
public void registerSync ( String key , String repoUri , String branch , long fetchFrequencyMs , Runnable onUpdate ) {
@ -87,7 +87,7 @@ public class DefaultGitSyncService implements GitSyncService {
@Override
@Override
public List < RepoFile > listFiles ( String key , String path , int depth , FileType type ) {
public List < RepoFile > listFiles ( String key , String path , int depth , FileType type ) {
GitRepository repository = getRepository ( key ) ;
GitRepository repository = getRepository ( key ) ;
return repository . listFilesAtCommit ( lastCommit , path , depth ) . stream ( )
return repository . listFilesAtCommit ( getLastCommit ( key ) , path , depth ) . stream ( )
. filter ( file - > type = = null | | file . type ( ) = = type )
. filter ( file - > type = = null | | file . type ( ) = = type )
. toList ( ) ;
. toList ( ) ;
}
}
@ -96,7 +96,7 @@ public class DefaultGitSyncService implements GitSyncService {
@Override
@Override
public byte [ ] getFileContent ( String key , String path ) {
public byte [ ] getFileContent ( String key , String path ) {
GitRepository repository = getRepository ( key ) ;
GitRepository repository = getRepository ( key ) ;
return repository . getFileContentAtCommit ( path , lastCommit ) ;
return repository . getFileContentAtCommit ( path , getLastCommit ( key ) ) ;
}
}
@Override
@Override
@ -143,7 +143,7 @@ public class DefaultGitSyncService implements GitSyncService {
GitRepository repository = getRepository ( key ) ;
GitRepository repository = getRepository ( key ) ;
String branchRef = getBranchRef ( repository ) ;
String branchRef = getBranchRef ( repository ) ;
try {
try {
lastCommit = repository . resolveCommit ( branchRef ) ;
lastCommits . put ( key , repository . resolveCommit ( branchRef ) ) ;
} catch ( Throwable e ) {
} catch ( Throwable e ) {
log . error ( "[{}] Failed to resolve commit for ref {}" , key , branchRef , e ) ;
log . error ( "[{}] Failed to resolve commit for ref {}" , key , branchRef , e ) ;
return ;
return ;
@ -166,6 +166,14 @@ public class DefaultGitSyncService implements GitSyncService {
return Path . of ( repositoriesFolder , name ) ;
return Path . of ( repositoriesFolder , name ) ;
}
}
private RevCommit getLastCommit ( String key ) {
RevCommit commit = lastCommits . get ( key ) ;
if ( commit = = null ) {
throw new IllegalStateException ( key + " repository has no resolved commit" ) ;
}
return commit ;
}
private String getBranchRef ( GitRepository repository ) {
private String getBranchRef ( GitRepository repository ) {
return "refs/remotes/origin/" + repository . getSettings ( ) . getDefaultBranch ( ) ;
return "refs/remotes/origin/" + repository . getSettings ( ) . getDefaultBranch ( ) ;
}
}