Last active
July 4, 2019 17:32
-
-
Save gurkanakdeniz/e9d08af0b37d4ad843306b096efd967c to your computer and use it in GitHub Desktop.
eclipse jgit and git utility java and spring boot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#git.active = true | |
#git.user = user | |
#git.pass = tokenorpass | |
#git.remote = remote.git | |
#git.branch = refs/heads/java | |
#git.directory = codes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Optional; | |
import javax.annotation.PostConstruct; | |
import org.springframework.boot.context.properties.ConfigurationProperties; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.context.annotation.PropertySource; | |
@Configuration("application") | |
@PropertySource("application.properties") | |
@ConfigurationProperties(prefix = "git") | |
public class GitProperties { | |
private String user; | |
private String pass; | |
private String remote; | |
private String branch; | |
private String directory; | |
private String active; | |
public boolean active() { | |
return "true".equals(active); | |
} | |
@PostConstruct | |
private void init() { | |
try { | |
user = Optional.ofNullable(user).filter(s -> !s.isBlank()).orElseGet(() -> System.getenv("GIT_USER")); | |
pass = Optional.ofNullable(pass).filter(s -> !s.isBlank()).orElseGet(() -> System.getenv("GIT_PASS")); | |
remote = Optional.ofNullable(remote).filter(s -> !s.isBlank()).orElseGet(() -> System.getenv("GIT_REMOTE")); | |
branch = Optional.ofNullable(branch).filter(s -> !s.isBlank()).orElseGet(() -> System.getenv("GIT_BRANCH")); | |
directory = Optional.ofNullable(directory).filter(s -> !s.isBlank()).orElseGet(() -> System.getenv("GIT_DIRECTORY")); | |
active = Optional.ofNullable(active).filter(s -> !s.isBlank()).orElseGet(() -> System.getenv("GIT_ACTIVE")); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public String getUser() { | |
return user; | |
} | |
public void setUser(String user) { | |
this.user = user; | |
} | |
public String getPass() { | |
return pass; | |
} | |
public void setPass(String pass) { | |
this.pass = pass; | |
} | |
public String getBranch() { | |
return branch; | |
} | |
public void setBranch(String branch) { | |
this.branch = branch; | |
} | |
public String getDirectory() { | |
return directory; | |
} | |
public void setDirectory(String directory) { | |
this.directory = directory; | |
} | |
public String getRemote() { | |
return remote; | |
} | |
public void setRemote(String remote) { | |
this.remote = remote; | |
} | |
public String getActive() { | |
return active; | |
} | |
public void setActive(String active) { | |
this.active = active; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import org.eclipse.jgit.api.AddCommand; | |
import org.eclipse.jgit.api.CommitCommand; | |
import org.eclipse.jgit.api.Git; | |
import org.eclipse.jgit.api.PushCommand; | |
import org.eclipse.jgit.transport.CredentialsProvider; | |
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.util.Comparator; | |
public class GitUtility { | |
private String user; | |
private String pass; | |
private String remote; | |
private String branch; | |
private String directory; | |
private static final int timeout = 60; | |
private CredentialsProvider cp; | |
private Git git; | |
public GitUtility(GitProperties prop, boolean clone) throws Exception { | |
user = prop.getUser(); | |
pass = prop.getPass(); | |
remote = prop.getRemote(); | |
branch = prop.getBranch(); | |
directory = prop.getDirectory(); | |
cp = new UsernamePasswordCredentialsProvider(user, pass); | |
if (clone) { | |
remoteClone(); | |
} else { | |
git = Git.init().setDirectory(new File(directory)).call(); | |
} | |
} | |
private void remoteClone() throws Exception { | |
try { | |
try { | |
Path path = Paths.get(directory).resolve(""); | |
Files.walk(path).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
git = Git.cloneRepository().setURI(remote).setTimeout(timeout).setDirectory(new File(directory)) | |
.setBranch(branch).setCloneAllBranches(false).setCredentialsProvider(cp).call(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
throw e; | |
} | |
} | |
public void remoteAdd() { | |
try { | |
AddCommand add = git.add(); | |
add.addFilepattern("."); | |
add.call(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public void remoteAdd(String pattern) { | |
try { | |
AddCommand add = git.add(); | |
add.addFilepattern(pattern); | |
add.call(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public void remoteCommit(String message) { | |
try { | |
CommitCommand commit = git.commit(); | |
commit.setMessage(message); | |
commit.setCredentialsProvider(cp); | |
commit.call(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public void remotePush() { | |
try { | |
PushCommand push = git.push(); | |
push.setCredentialsProvider(cp); | |
push.call(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public void remotePush(String pattern, String message) { | |
try { | |
remoteAdd(pattern); | |
remoteCommit(message); | |
remotePush(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public void remoteAllPush(String commitMessage) { | |
try { | |
remoteAdd(); | |
remoteCommit(commitMessage); | |
remotePush(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public void remotePull() throws Exception { | |
try { | |
git.pull().setCredentialsProvider(cp).call(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- ... --> | |
<properties> | |
<java.version>1.8</java.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-web</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.eclipse.jgit</groupId> | |
<artifactId>org.eclipse.jgit</artifactId> | |
<version>5.4.0.201906121030-r</version> | |
</dependency> | |
</dependencies> | |
<!-- ... --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment