Browse Source

open repo

home
esoe 2 years ago
parent
commit
7b02766581
  1. 164
      src/main/java/ru/molokoin/sourceListener/GitListener.java

164
src/main/java/ru/molokoin/sourceListener/GitListener.java

@ -0,0 +1,164 @@ @@ -0,0 +1,164 @@
package ru.molokoin.sourceListener;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.eclipse.jgit.api.FetchCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.InvalidRemoteException;
import org.eclipse.jgit.api.errors.TransportException;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.FetchResult;
import com.fasterxml.jackson.core.exc.StreamReadException;
import com.fasterxml.jackson.databind.DatabindException;
import com.fasterxml.jackson.databind.ObjectMapper;
import ru.molokoin.sourceListener.git.RepoCrafter;
import ru.molokoin.sourceListener.net.Service;
import ru.molokoin.sourceListener.opt.Options;
public class GitListener {
public Options opt;
public String optionsPath = "options-home.json";
private Repository repo;
public GitListener(){
try {
opt = readOptions();
} catch (StreamReadException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (DatabindException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
/**
* Метод запускает бесконечный цикл проверки обновлений в репозитарии.
*/
public void live(){
//TODO live()
}
/**
* Метод инициирует репозиторий / клонирует репозиторий
* по сути не нужный метод, реализующий одну команду.
* в коде надо использовать сразу клонирующую команду.
*/
public void initRepo(){
try {
setRepo(RepoCrafter.copy(opt.getGitLink(), opt.getGitLocalPath()));
//repo.close();
} catch (InvalidRemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransportException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (GitAPIException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Открываем репозитарий, ранее склонированный
* по хорошему делать статическим методом, возвращающим открытый репозитарий
* @throws IOException
*/
public void openRepo() throws IOException{
Path repoPath = Paths.get("C:\\Users\\Strannik\\Documents\\esoe\\code\\sourceListener\\out\\repos");
try (Git git = Git.open(repoPath.toFile())) {
this.repo = git.getRepository();
git.close();
}
}
/**
* Извлечение данных из options.json
* @return
* @throws StreamReadException
* @throws DatabindException
* @throws IOException
*/
public Options readOptions() throws StreamReadException, DatabindException, IOException{
Options opt;
ObjectMapper mapper = new ObjectMapper();
opt = mapper.readValue(new File(optionsPath), Options.class);
return opt;
}
public Options getOpt() {
return opt;
}
/**
* распаковка архива
*/
public void extract()throws IOException{
//считываем настройки извлечения
try (ZipFile file = new ZipFile(opt.getZipPath())){
Enumeration<? extends ZipEntry> zipEntries = file.entries();
while (zipEntries.hasMoreElements()){
ZipEntry zipEntry = zipEntries.nextElement();
File newFile = new File(opt.getUnzipPath(), zipEntry.getName());
newFile.getParentFile().mkdirs();
if (!zipEntry.isDirectory()){
try (FileOutputStream outputStream = new FileOutputStream(newFile)){
BufferedInputStream inputStream = new BufferedInputStream(file.getInputStream(zipEntry));
while (inputStream.available() > 0){
outputStream.write(inputStream.read());
}
inputStream.close();
}
}
}
}
}
public void setRepo(Repository repo) {
this.repo = repo;
}
public Repository getRepo() {
return repo;
}
public static void main(String[] args) throws InvalidRemoteException, TransportException, GitAPIException {
GitListener ear = new GitListener();
// try {
// Service.download(ear.opt.getZipLink(), ear.opt.getDownloadPath());;
// ear.extract();
// } catch (IOException e) {
// System.out.println(e.getMessage());
// }
//клонируем репозитарий
// ear.initRepo();
// Git git = new Git(ear.getRepo());
//открываем репозитарий из локального хранилища
try {
ear.openRepo();
} catch (IOException e) {
System.out.println("Epic fail with >>>>> openRepo() ... ");
e.printStackTrace();
}
Git git = new Git(ear.getRepo());
Collection<Ref> c= git.lsRemote().call();
for (Ref ref : c) {
System.out.println("ref >>>>>>>>>> " + ref);
}
git.close();
}
}
Loading…
Cancel
Save