Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0ee99b2

Browse files
test git compare local with remotes
1 parent 1dde581 commit 0ee99b2

File tree

3 files changed

+118
-22
lines changed

3 files changed

+118
-22
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package task;
2+
3+
/**
4+
* @author zacconding
5+
* @Date 2018年12月28日
6+
* @GitHub : https://github.com/zacscoding
7+
*/
8+
public class DoTask {
9+
10+
public static void step(String title, Task task) throws Exception {
11+
System.out.println("## ======================================================== ");
12+
System.out.println(title);
13+
task.run();
14+
System.out.println("## ======================================================== //\n\n");
15+
}
16+
17+
@FunctionalInterface
18+
public interface Task {
19+
20+
void run() throws Exception;
21+
}
22+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package git;
2+
3+
import java.io.File;
4+
import java.util.Collection;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.stream.Collectors;
8+
import org.eclipse.jgit.api.Git;
9+
import org.eclipse.jgit.lib.Ref;
10+
import org.eclipse.jgit.transport.RemoteConfig;
11+
import org.eclipse.jgit.transport.URIish;
12+
import org.junit.Test;
13+
import task.DoTask;
14+
import util.SimpleLogger;
15+
16+
/**
17+
* @author zacconding
18+
* @Date 2018年12月29日
19+
* @GitHub : https://github.com/zacscoding
20+
*/
21+
public class GitCompareTest {
22+
23+
File gitDir = new File("C:\\git\\zaccoding\\git-temp");
24+
25+
@Test
26+
public void compareLocalWithRemotes() throws Exception {
27+
DoTask.step("Compare local repository with remote repository", () -> {
28+
try (Git git = Git.open(gitDir)) {
29+
// local branchs map
30+
Map<String, String> localRefsMap = git.branchList().call().stream().collect(
31+
Collectors.toMap(ref -> ref.getName(), ref -> ref.getObjectId().name())
32+
);
33+
34+
// get remote configs
35+
List<RemoteConfig> remoteConfigs = git.remoteList().call();
36+
for (RemoteConfig remoteConfig : remoteConfigs) {
37+
SimpleLogger.println("Compare local with {}", remoteConfig.getName());
38+
39+
for (URIish url : remoteConfig.getURIs()) {
40+
SimpleLogger.println("> Check {}` url : {}", remoteConfig.getName(), url.toString());
41+
42+
Collection<Ref> refs = Git.lsRemoteRepository()
43+
.setHeads(true)
44+
.setTags(true)
45+
.setRemote(url.toString())
46+
.call();
47+
48+
for (Ref ref : refs) {
49+
String sha = localRefsMap.get(ref.getName());
50+
SimpleLogger.println(">> Check ref : {} | sha : {} | local sha : {}"
51+
, ref.getName(), ref.getObjectId().name(), sha);
52+
53+
if (sha == null) {
54+
System.out.println(">>> There is no local branch : " + ref.getName());
55+
continue;
56+
}
57+
58+
if (sha.equals(ref.getObjectId().name())) {
59+
SimpleLogger.println(">>> Same sha");
60+
continue;
61+
}
62+
63+
SimpleLogger.println(">>> Find different sha");
64+
}
65+
}
66+
}
67+
}
68+
});
69+
70+
/* Output
71+
72+
## ========================================================
73+
Compare local repository with remote repository
74+
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
75+
SLF4J: Defaulting to no-operation (NOP) logger implementation
76+
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
77+
Compare local with origin
78+
> Check origin` url : https://github.com/zacscoding/git-temp.git
79+
>> Check ref : refs/heads/branch1 | sha : 7be2369eea92cec0cf035bfd486aaf1729b6fea5 | local sha : 070e197a30efa7db6dd0ed1c7d20a843ab0e0bfc
80+
>>> Find different sha
81+
>> Check ref : refs/heads/master | sha : 6f4576480708f73ed2f5cfdd1729a56e1379f454 | local sha : 1b3ce2a22ccc969c541869e75fbab9bb01f1de5b
82+
>>> Find different sha
83+
## ======================================================== //
84+
*/
85+
}
86+
}

‎jgit-demo/src/test/java/git/GitTest.java

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.eclipse.jgit.treewalk.TreeWalk;
2020
import org.eclipse.jgit.treewalk.filter.PathFilter;
2121
import org.junit.Test;
22+
import task.DoTask;
2223
import util.SimpleLogger;
2324

2425
/**
@@ -39,19 +40,19 @@ public class GitTest {
3940
@Test
4041
public void stepByStep() throws Exception {
4142
// del dir & clone
42-
step("Start to clone", () -> cloneRepository());
43+
DoTask.step("Start to clone", () -> cloneRepository());
4344

4445
// pull
45-
step("Start to pull", () -> pull());
46+
DoTask.step("Start to pull", () -> pull());
4647

4748
// write 3 times & commit
48-
step("Start to write & commit 3", () -> writeAndCommitRepeat(3));
49+
DoTask.step("Start to write & commit 3", () -> writeAndCommitRepeat(3));
4950

5051
// read content at commits
51-
step("Start to read content at commits", () -> displayFiles());
52+
DoTask.step("Start to read content at commits", () -> displayFiles());
5253

5354
// push to upstream
54-
step("Start to push", () -> push());
55+
DoTask.step("Start to push", () -> push());
5556
}
5657

5758
// Step1 : clone repository
@@ -81,9 +82,10 @@ private void writeAndCommitRepeat(int count) throws Exception {
8182
// Step4 : Push
8283
private void push() throws Exception {
8384
FileRepositoryBuilder builder = new FileRepositoryBuilder();
84-
try (Repository repository = builder.setGitDir(new File(destDir + "\\.git")).readEnvironment() // scan environment GIT_* variables
85-
.findGitDir() // scan up the file system tree
86-
.build()) {
85+
try (Repository repository = builder.setGitDir(new File(destDir + "\\.git"))
86+
.readEnvironment() // scan environment GIT_* variables
87+
.findGitDir() // scan up the file system tree
88+
.build()) {
8789
try (Git git = new Git(repository)) {
8890
git.push().call();
8991
}
@@ -154,18 +156,4 @@ private void writeFileAndCommit(int version) throws Exception {
154156
git.commit().setMessage("commit v" + version).call();
155157
}
156158
}
157-
158-
private void step(String title, DoTask task) throws Exception {
159-
System.out.println("## ======================================================== ");
160-
System.out.println(title);
161-
task.run();
162-
System.out.println("## ======================================================== //\n\n");
163-
}
164-
165-
166-
@FunctionalInterface
167-
public interface DoTask {
168-
169-
void run() throws Exception;
170-
}
171159
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /