This repository has been archived by the owner on Jul 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
GitWorker.java
99 lines (82 loc) · 2.95 KB
/
GitWorker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package Workers.Git;
import Singletons.SettingsManager;
import Utilities.OSValidator;
import Utilities.Utils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.PullResult;
import org.eclipse.jgit.api.errors.GitAPIException;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
/**
* Created by matthewyork on 4/2/14.
*/
public class GitWorker extends SwingWorker<Void, Void> {
private static final int DEFAULT_TIMEOUT = 5000; //10s
private String REMOTE_SPECS_URL = "https://github.com/AndroidGears/Specs.git";
public boolean successful = false;
@Override
protected Void doInBackground() throws Exception {
SettingsManager.getInstance().loadSettings();
syncAndroidGears();
return null;
}
private void syncAndroidGears(){
//Setup file
File androidGearsDirectory = Utils.androidGearsDirectory();
//Pull changes or clone repo
if(androidGearsDirectory.exists()){
successful = pullChanges(androidGearsDirectory);
}
else {
androidGearsDirectory.mkdir();
successful = cloneRepository(androidGearsDirectory);
}
}
private Boolean cloneRepository(File androidGearsDirectory){
try {
Git git = Git.cloneRepository()
.setURI(REMOTE_SPECS_URL)
.setBranch("master")
.setDirectory(androidGearsDirectory)
.setTimeout(DEFAULT_TIMEOUT)
.call();
//Get repos directory
File reposDirectory = git.getRepository().getDirectory().getParentFile();
//Close git connection!
git.close();
//If everything was created successfully, return true
if (reposDirectory != null){
if (reposDirectory.exists()){
if (reposDirectory.list().length > 1){
return true;
}
}
}
} catch (GitAPIException e) {
e.printStackTrace();
}
return false;
}
private Boolean pullChanges(File androidGearsDirectory){
try {
Git git = Git.open(new File(androidGearsDirectory.getAbsolutePath()+ Utils.pathSeparator()+".git"));
PullResult result = git.pull().setTimeout(DEFAULT_TIMEOUT).call();
git.close();
return result.isSuccessful();
/*
PullCommand pullCmd = git.pull();
PullResult result = pullCmd.call();
FetchResult fetchResult = result.getFetchResult();
MergeResult mergeResult = result.getMergeResult();
mergeResult.getMergeStatus(); // this should be interesting*/
}
catch (IOException exception){
//cloneRepository(androidGearsDirectory);
}
catch (GitAPIException exception){
exception.printStackTrace();
}
return false;
}
}