|
| 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 | +} |
0 commit comments