@ -1,3 +1,8 @@
/ *
* Copyright ( C ) 2021 Anton Romanov - All Rights Reserved
* You may use , distribute and modify this code , please write to : romanov73 @gmail.com.
* /
package ru.ulstu.extractor.service ;
import org.apache.commons.io.FileUtils ;
@ -7,7 +12,6 @@ import org.eclipse.jgit.api.ListBranchCommand;
import org.eclipse.jgit.api.errors.GitAPIException ;
import org.eclipse.jgit.diff.DiffFormatter ;
import org.eclipse.jgit.internal.storage.file.FileRepository ;
import org.eclipse.jgit.lib.Ref ;
import org.eclipse.jgit.lib.Repository ;
import org.eclipse.jgit.revwalk.RevCommit ;
import org.springframework.beans.factory.annotation.Value ;
@ -21,6 +25,9 @@ import ru.ulstu.extractor.model.LineChange;
import java.io.ByteArrayOutputStream ;
import java.io.File ;
import java.io.IOException ;
import java.net.MalformedURLException ;
import java.net.URISyntaxException ;
import java.net.URL ;
import java.nio.file.Path ;
import java.time.Instant ;
import java.util.ArrayList ;
@ -37,39 +44,32 @@ public class GitRepositoryService {
@Value ( "${extractor.custom-projects-dir}" )
private String customProjectsDir ;
public void cloneOrUpdateRepo ( String url ) throws GitAPIException , IOException {
Git git ;
if ( projectDirExists ( getProjectDirectoryFile ( url ) ) ) {
Repository localRepo = new FileRepository ( getProjectGitDirectory ( url ) ) ;
git = new Git ( localRepo ) ;
git . pull ( ) . call ( ) ;
localRepo . close ( ) ;
} else {
git = Git . cloneRepository ( )
. setURI ( url )
. setDirectory ( getProjectDirectoryFile ( url ) )
. call ( ) ;
}
public List < Branch > getRemoteBranches ( String url ) throws GitAPIException , IOException {
cloneOrUpdateRepo ( url ) ;
Repository localRepo = new FileRepository ( getProjectGitDirectory ( url ) ) ;
Git git = new Git ( localRepo ) ;
List < Branch > branches = git . branchList ( ) . setListMode ( ListBranchCommand . ListMode . REMOTE )
. call ( )
. stream ( )
. map ( r - > new Branch ( r . getName ( ) . replace ( BRANCH_PREFIX , "" ) ) )
. collect ( Collectors . toList ( ) ) ;
git . close ( ) ;
localRepo . close ( ) ;
return branches ;
}
private String getProjectDirectory ( String url ) {
return ( isBlank ( customProjectsDir )
? System . getProperty ( "java.io.tmpdir" )
: customProjectsDir ) + url . substring ( url . lastIndexOf ( '/' ) ) ;
}
private String getProjectGitDirectory ( String url ) {
return getProjectDirectory ( url ) + "/.git" ;
}
private File getProjectDirectoryFile ( String url ) {
return Path . of ( getProjectDirectory ( url ) )
. toFile ( ) ;
}
private boolean projectDirExists ( File file ) {
return file . exists ( ) ;
public List < Branch > getLocalBranches ( String url ) throws GitAPIException , IOException {
cloneOrUpdateRepo ( url ) ;
Repository localRepo = new FileRepository ( getProjectGitDirectory ( url ) ) ;
Git git = new Git ( localRepo ) ;
List < Branch > branches = git . branchList ( )
. call ( )
. stream ( )
. map ( r - > new Branch ( r . getName ( ) . replace ( BRANCH_PREFIX , "" ) ) )
. collect ( Collectors . toList ( ) ) ;
git . close ( ) ;
localRepo . close ( ) ;
return branches ;
}
public List < Commit > getCommits ( String repositoryUrl , String branchName ) throws GitAPIException , IOException {
@ -78,12 +78,18 @@ public class GitRepositoryService {
Git git = new Git ( localRepo ) ;
git . pull ( ) . call ( ) ;
if ( ! localRepo . getBranch ( ) . equals ( branchName ) ) {
Ref ref = git . checkout ( ) .
setCreateBranch ( true ) .
setName ( branchName ) .
setUpstreamMode ( CreateBranchCommand . SetupUpstreamMode . TRACK ) .
setStartPoint ( "origin/" + branchName ) .
call ( ) ;
if ( getLocalBranches ( repositoryUrl ) . stream ( ) . anyMatch ( localBranch - > localBranch . getName ( ) . contains ( branchName ) ) ) {
git . checkout ( )
. setName ( branchName )
. call ( ) ;
} else {
git . checkout ( )
. setCreateBranch ( true )
. setName ( branchName )
. setUpstreamMode ( CreateBranchCommand . SetupUpstreamMode . TRACK )
. setStartPoint ( "origin/" + branchName )
. call ( ) ;
}
}
List < RevCommit > commits = new ArrayList < > ( ) ;
git . log ( ) . call ( ) . forEach ( commits : : add ) ;
@ -107,7 +113,58 @@ public class GitRepositoryService {
return list ;
}
public List < FileChange > findDiffBetweenTwoRevisions ( RevCommit laterCommit , RevCommit earlierCommit , Repository localRepo ) {
public void remove ( String repositoryUrl ) throws IOException {
FileUtils . deleteDirectory ( getProjectDirectoryFile ( repositoryUrl ) ) ;
}
private void cloneOrUpdateRepo ( String url ) throws GitAPIException , IOException {
Git git ;
if ( projectDirExists ( getProjectDirectoryFile ( url ) ) ) {
Repository localRepo = new FileRepository ( getProjectGitDirectory ( url ) ) ;
git = new Git ( localRepo ) ;
git . pull ( ) . call ( ) ;
localRepo . close ( ) ;
} else {
git = Git . cloneRepository ( )
. setURI ( url )
. setDirectory ( getProjectDirectoryFile ( url ) )
. call ( ) ;
}
git . close ( ) ;
}
private String getProjectDirectory ( String url ) {
return ( isBlank ( customProjectsDir )
? System . getProperty ( "java.io.tmpdir" )
: customProjectsDir ) + url . substring ( url . lastIndexOf ( '/' ) ) ;
}
private String getProjectGitDirectory ( String url ) {
return getProjectDirectory ( url ) + "/.git" ;
}
private File getProjectDirectoryFile ( String url ) {
validateUrl ( url ) ;
return Path . of ( getProjectDirectory ( url ) )
. toFile ( ) ;
}
private void validateUrl ( String url ) {
if ( url = = null | | url . isEmpty ( ) ) {
throw new RuntimeException ( "Repository url must not empty" ) ;
}
try {
new URL ( url ) . toURI ( ) ;
} catch ( MalformedURLException | URISyntaxException e ) {
throw new RuntimeException ( "Repository url not valid" ) ;
}
}
private boolean projectDirExists ( File file ) {
return file . exists ( ) ;
}
private List < FileChange > findDiffBetweenTwoRevisions ( RevCommit laterCommit , RevCommit earlierCommit , Repository localRepo ) {
if ( laterCommit = = null | | earlierCommit = = null ) {
return null ;
}
@ -205,22 +262,4 @@ public class GitRepositoryService {
}
return Optional . empty ( ) ;
}
public List < Branch > getBranches ( String url ) throws GitAPIException , IOException {
cloneOrUpdateRepo ( url ) ;
Repository localRepo = new FileRepository ( getProjectGitDirectory ( url ) ) ;
Git git = new Git ( localRepo ) ;
List < Branch > branches = git . branchList ( ) . setListMode ( ListBranchCommand . ListMode . REMOTE )
. call ( )
. stream ( )
. map ( r - > new Branch ( r . getName ( ) . replace ( BRANCH_PREFIX , "" ) ) )
. collect ( Collectors . toList ( ) ) ;
git . close ( ) ;
localRepo . close ( ) ;
return branches ;
}
public void remove ( String repositoryUrl ) throws IOException {
FileUtils . deleteDirectory ( getProjectDirectoryFile ( repositoryUrl ) ) ;
}
}