-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathExperiment.java
More file actions
195 lines (140 loc) · 6.54 KB
/
Experiment.java
File metadata and controls
195 lines (140 loc) · 6.54 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package ru.fusionsoft.dbgit;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.Status;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.diff.RawTextComparator;
import org.eclipse.jgit.dircache.DirCache;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.DepthWalk.RevWalk;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.PathFilter;
import org.eclipse.jgit.util.io.DisabledOutputStream;
import ru.fusionsoft.dbgit.core.DBGit;
//This class for experiment
public class Experiment {
public static void main(String[] args) throws Exception {
Experiment ex = new Experiment();
ex.test();
//main2(args);
}
public void test() throws Exception {
DBGit dbGit = DBGit.getInstance();
System.out.println(dbGit.getRepository().getDirectory().getAbsolutePath());
System.out.println(dbGit.getRepository().getBranch());
System.out.println(dbGit.getRootDirectory());
List<String> files = dbGit.getGitIndexFiles("");
for (int i = 0; i < files.size(); i++) {
System.out.println(files.get(i));
}
System.out.println("Add =======================================");
files = dbGit.getAddedObjects("");
for (int i = 0; i < files.size(); i++) {
System.out.println(files.get(i));
}
System.out.println("=======================================================");
Repository repository = dbGit.getRepository();
Git git = new Git(repository);
RevWalk walk = new RevWalk(repository, 10);
Status st = git.status().call();
for (String s : st.getAdded()) {
System.out.println("Added: "+s);
}
/*
ObjectId head = repository.resolve(Constants.HEAD);
RevCommit commit = walk.parseCommit(head);
*/
DirCache cache = repository.readDirCache();
//repository.di
System.out.println("QQQQQ "+cache.findEntry("dbgit/src/main/java/ru/fusionsoft/dbgit/command/"));
for (int i = 0; i < cache.getEntryCount(); i++) {
System.out.println(cache.getEntry(i).getPathString());
}
System.out.println("!!!!! " + repository.readDirCache().getEntryCount());
//getEntry("").getObjectId();
System.out.println("=======================================================");
Ref head = repository.findRef("HEAD");
RevCommit commit = walk.parseCommit(head.getObjectId());
RevTree tree = commit.getTree();
System.out.println("Having tree: " + tree);
TreeWalk treeWalk = new TreeWalk(repository);
treeWalk.addTree(tree);
//treeWalk.setFilter(PathFilter.create("dbgit/src/main/java/ru/fusionsoft/dbgit/command/"));
//TreeFilter newFilter = new TreeFilter
//treeWalk.setFilter(newFilter);
treeWalk.setRecursive(true);
while (treeWalk.next()) {
if (treeWalk.isSubtree()) {
System.out.println("dir: " + treeWalk.getPathString());
treeWalk.enterSubtree();
} else {
System.out.println("file: " + treeWalk.getPathString() +" "+treeWalk.getOperationType().name());
}
}
/*
RevWalk rw = new RevWalk(repository, 10);
ObjectId head = repository.resolve(Constants.HEAD);
RevCommit commit = rw.parseCommit(head);
RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
DiffFormatter df = new DiffFormatter(DisabledOutputStream.INSTANCE);
df.setRepository(repository);
df.setDiffComparator(RawTextComparator.DEFAULT);
df.setDetectRenames(true);
List<DiffEntry> diffs = df.scan(parent.getTree(), commit.getTree());
for (DiffEntry diff : diffs) {
System.out.println(MessageFormat.format("({0} {1} {2}", diff.getChangeType().name(), diff.getNewMode().getBits(), diff.getNewPath()));
}
*/
}
public static void main2(String[] args) throws IOException, GitAPIException {
Repository repo = new FileRepository("../.git");
System.out.println(repo.getDirectory().getAbsolutePath());
System.out.println(repo.getBranch());
Git git = new Git(repo);
RevWalk walk = new RevWalk(repo, 1);
List<Ref> branches = git.branchList().call();
for (Ref branch : branches) {
String branchName = branch.getName();
System.out.println("Commits of branch: " + branch.getName());
System.out.println("-------------------------------------");
Iterable<RevCommit> commits = git.log().all().call();
for (RevCommit commit : commits) {
boolean foundInThisBranch = false;
RevCommit targetCommit = walk.parseCommit(repo.resolve(
commit.getName()));
for (Map.Entry<String, Ref> e : repo.getAllRefs().entrySet()) {
if (e.getKey().startsWith(Constants.R_HEADS)) {
if (walk.isMergedInto(targetCommit, walk.parseCommit(
e.getValue().getObjectId()))) {
String foundInBranch = e.getValue().getName();
if (branchName.equals(foundInBranch)) {
foundInThisBranch = true;
break;
}
}
}
}
if (foundInThisBranch) {
System.out.println(commit.getName());
System.out.println(commit.getAuthorIdent().getName());
System.out.println(new Date(commit.getCommitTime() * 1000L));
System.out.println(commit.getFullMessage());
}
}
}
walk.close();
git.close();
}
}