From be64138ec5625fba0b866928a1973c740f4ff76b Mon Sep 17 00:00:00 2001 From: grog Date: Thu, 12 Oct 2023 08:44:26 -0700 Subject: [PATCH] removal of static methods from Git service --- src/main/java/org/myrobotlab/service/Git.java | 203 ++++++++++-------- .../java/org/myrobotlab/service/Updater.java | 98 ++++++++- 2 files changed, 210 insertions(+), 91 deletions(-) diff --git a/src/main/java/org/myrobotlab/service/Git.java b/src/main/java/org/myrobotlab/service/Git.java index 1d2d3766f5..8832326052 100644 --- a/src/main/java/org/myrobotlab/service/Git.java +++ b/src/main/java/org/myrobotlab/service/Git.java @@ -3,7 +3,6 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Properties; @@ -31,6 +30,7 @@ import org.eclipse.jgit.lib.TextProgressMonitor; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.storage.file.FileRepositoryBuilder; +import org.eclipse.jgit.submodule.SubmoduleWalk; import org.myrobotlab.framework.Platform; import org.myrobotlab.framework.Service; import org.myrobotlab.logging.Level; @@ -45,7 +45,7 @@ public class Git extends Service { public final static Logger log = LoggerFactory.getLogger(Git.class); - transient static TextProgressMonitor monitor = new TextProgressMonitor(); + transient ProgressMonitor monitor = new ProgressMonitor(); Map repos = new TreeMap<>(); @@ -56,76 +56,43 @@ public static class RepoData { String branch; String location; String url; - List branches; String checkout; transient org.eclipse.jgit.api.Git git; - public RepoData(String location, String url, List branches, String checkout, org.eclipse.jgit.api.Git git) { + public RepoData(String location, String url, String checkout, org.eclipse.jgit.api.Git git) { this.location = location; this.url = url; - this.branches = branches; this.checkout = checkout; this.git = git; } } + // TODO - overload updates to publish + public class ProgressMonitor extends TextProgressMonitor { + + } + public Git(String n, String id) { super(n, id); } - - // max complexity clone - public void clone(String location, String url, List inbranches, String incheckout) throws InvalidRemoteException, TransportException, GitAPIException, IOException { - - File repoLocation = new File(location); - org.eclipse.jgit.api.Git git = null; - Repository repo = null; - - List branches = new ArrayList<>(); - for (String b : inbranches) { - if (!b.contains("refs")) { - branches.add("refs/heads/" + b); - } - } - - String checkout = (incheckout.contains("refs")) ? incheckout : "refs/heads/" + incheckout; - - if (!repoLocation.exists()) { - // clone - log.info("cloning {} {} into {}", url, incheckout, location); - git = org.eclipse.jgit.api.Git.cloneRepository().setProgressMonitor(monitor).setURI(url).setDirectory(repoLocation).setBranchesToClone(branches).setBranch(checkout).call(); - - } else { - // Open an existing repository - String gitDir = repoLocation.getAbsolutePath() + File.separator + ".git"; - log.info("opening repo {} from {}", gitDir, url); - repo = new FileRepositoryBuilder().setGitDir(new File(gitDir)).build(); - git = new org.eclipse.jgit.api.Git(repo); - } - - repo = git.getRepository(); - - // checkout - log.info("checking out {}", incheckout); - // git.branchCreate().setForce(true).setName(incheckout).setStartPoint("origin/" - // + incheckout).call(); - git.branchCreate().setForce(true).setName(incheckout).setStartPoint(incheckout).call(); - git.checkout().setName(incheckout).call(); - - repos.put(location, new RepoData(location, url, inbranches, incheckout, git)); - + + public void clone(String location, String url, String branch, String checkout) throws InvalidRemoteException, TransportException, GitAPIException, IOException { + clone(location, url, branch, checkout, false); } + + // max complexity sync - public void sync(String location, String url, List branches, String checkout) throws InvalidRemoteException, TransportException, GitAPIException, IOException { + public void sync(String location, String url, String branch, String checkout) throws InvalidRemoteException, TransportException, GitAPIException, IOException { // initial clone - clone(location, url, branches, checkout); + clone(location, url, branch, checkout); addTask(checkStatusIntervalMs, "checkStatus"); } public void sync(String location, String url, String checkout) throws InvalidRemoteException, TransportException, GitAPIException, IOException { - sync(location, url, Arrays.asList(checkout), checkout); + sync(location, url, checkout, checkout); } public RevCommit checkStatus() throws WrongRepositoryStateException, InvalidConfigurationException, InvalidRemoteException, CanceledException, RefNotFoundException, @@ -167,7 +134,7 @@ public RevCommit publishPull(RevCommit commit) { return commit; } - private static List getLogs(org.eclipse.jgit.api.Git git, String ref, int maxCount) + private List getLogs(org.eclipse.jgit.api.Git git, String ref, int maxCount) throws RevisionSyntaxException, NoHeadException, MissingObjectException, IncorrectObjectTypeException, AmbiguousObjectException, GitAPIException, IOException { List ret = new ArrayList<>(); Repository repository = git.getRepository(); @@ -193,17 +160,17 @@ public void stopSync() { purgeTasks(); } - static public int pull() throws WrongRepositoryStateException, InvalidConfigurationException, DetachedHeadException, InvalidRemoteException, CanceledException, - RefNotFoundException, NoHeadException, TransportException, IOException, GitAPIException { + public int pull() throws WrongRepositoryStateException, InvalidConfigurationException, DetachedHeadException, InvalidRemoteException, CanceledException, RefNotFoundException, + NoHeadException, TransportException, IOException, GitAPIException { return pull(null, null); } - static public int pull(String branch) throws WrongRepositoryStateException, InvalidConfigurationException, DetachedHeadException, InvalidRemoteException, CanceledException, + public int pull(String branch) throws WrongRepositoryStateException, InvalidConfigurationException, DetachedHeadException, InvalidRemoteException, CanceledException, RefNotFoundException, NoHeadException, TransportException, IOException, GitAPIException { return pull(null, branch); } - static org.eclipse.jgit.api.Git getGit(String rootFolder) throws IOException { + org.eclipse.jgit.api.Git getGit(String rootFolder) throws IOException { if (rootFolder == null) { rootFolder = System.getProperty("user.dir"); } @@ -224,7 +191,7 @@ static org.eclipse.jgit.api.Git getGit(String rootFolder) throws IOException { return git; } - static public int pull(String src, String branch) throws IOException, WrongRepositoryStateException, InvalidConfigurationException, DetachedHeadException, InvalidRemoteException, + public int pull(String src, String branch) throws IOException, WrongRepositoryStateException, InvalidConfigurationException, DetachedHeadException, InvalidRemoteException, CanceledException, RefNotFoundException, NoHeadException, TransportException, GitAPIException { if (src == null) { @@ -278,11 +245,11 @@ static public int pull(String src, String branch) throws IOException, WrongRepos return 0; } - static public void init() throws IllegalStateException, GitAPIException { + public void init() throws IllegalStateException, GitAPIException { init(null); } - static public void init(String directory) throws IllegalStateException, GitAPIException { + public void init(String directory) throws IllegalStateException, GitAPIException { if (directory == null) { directory = System.getProperty("user.dir"); } @@ -291,55 +258,30 @@ static public void init(String directory) throws IllegalStateException, GitAPIEx org.eclipse.jgit.api.Git git = org.eclipse.jgit.api.Git.init().setDirectory(dir).call(); } - public static void main(String[] args) { - try { - - LoggingFactory.init(Level.INFO); - - Properties properties = Platform.gitProperties(); - Git.removeProps(); - log.info("{}", properties); - - /* - * // start the service Git git = (Git) Runtime.start("git", "Git"); - * - * // check out and sync every minute // git.sync("test", - * "https://github.com/MyRobotLab/WorkE.git", "master"); // - * git.sync("/lhome/grperry/github/mrl/myrobotlab", // - * "https://github.com/MyRobotLab/myrobotlab.git", "agent-removal"); - * git.gitPull("agent-removal"); // - * git.sync(System.getProperty("user.dir"), // - * "https://github.com/MyRobotLab/myrobotlab.git", "agent-removal"); - */ - } catch (Exception e) { - log.error("main threw", e); - } - } - - public static String getBranch() throws IOException { + public String getBranch() throws IOException { return getBranch(null); } - public static String getBranch(String src) throws IOException { + public String getBranch(String src) throws IOException { org.eclipse.jgit.api.Git git = getGit(src); return git.getRepository().getBranch(); } - public static Status status() throws NoWorkTreeException, IOException, GitAPIException { + public Status status() throws NoWorkTreeException, IOException, GitAPIException { return status(null); } - public static Status status(String src) throws IOException, NoWorkTreeException, GitAPIException { + public Status status(String src) throws IOException, NoWorkTreeException, GitAPIException { org.eclipse.jgit.api.Git git = getGit(src); Status status = git.status().call(); return status; } - public static void removeProps() { + public void removeProps() { removeProps(null); } - public static void removeProps(String rootFolder) { + public void removeProps(String rootFolder) { if (rootFolder == null) { rootFolder = System.getProperty("user.dir"); } @@ -349,4 +291,89 @@ public static void removeProps(String rootFolder) { } + public static void main(String[] args) { + try { + + LoggingFactory.init(Level.INFO); + + Properties properties = Platform.gitProperties(); + // Git.removeProps(); + log.info("{}", properties); + Git git = (Git) Runtime.start("git", "Git"); + git.clone("./depthai", "https://github.com/luxonis/depthai.git", "main", "refs/tags/v1.13.1-sdk", true); + log.info("here"); + + + } catch (Exception e) { + log.error("main threw", e); + } + } + + // max complexity clone and checkout + public void clone(String location, String url, String branch, String checkout, boolean recursive) throws InvalidRemoteException, TransportException, GitAPIException, IOException { + + File repoLocation = new File(location); + org.eclipse.jgit.api.Git git = null; + Repository repo = null; + + // git clone + + if (!repoLocation.exists()) { + // clone + log.info("cloning {} {} checking out {} into {}", url, branch, checkout, location); + git = org.eclipse.jgit.api.Git.cloneRepository().setProgressMonitor(monitor).setURI(url).setDirectory(repoLocation).setBranch(branch).call(); + + } else { + // Open an existing repository + String gitDir = repoLocation.getAbsolutePath() + File.separator + ".git"; + log.info("opening repo {} from {}", gitDir, url); + repo = new FileRepositoryBuilder().setGitDir(new File(gitDir)).build(); + git = new org.eclipse.jgit.api.Git(repo); + } + + repo = git.getRepository(); + + // git pull + + PullCommand pullCmd = git.pull() + // .setRemote(remoteName) + // .setCredentialsProvider(new + // UsernamePasswordCredentialsProvider(username, password)) + .setRemoteBranchName(branch); + + // Perform the pull operation + pullCmd.call(); + + + // recursive + if (recursive) { + + // Recursively fetch and checkout submodules if they exist + SubmoduleWalk submoduleWalk = SubmoduleWalk.forIndex(repo); + while (submoduleWalk.next()) { + String submodulePath = submoduleWalk.getPath(); + org.eclipse.jgit.api.Git submoduleGit = org.eclipse.jgit.api.Git.open(new File(location, submodulePath)); + submoduleGit.fetch() + .setRemote("origin") + .call(); + submoduleGit.checkout() + .setName(branch) // Replace with the desired branch name + .call(); + } + + } + + if (checkout != null) { + // checkout + log.info("checking out {}", checkout); + // git.branchCreate().setForce(true).setName(incheckout).setStartPoint("origin/" + // + incheckout).call(); + git.branchCreate().setForce(true).setName(branch).setStartPoint(checkout).call(); + git.checkout().setName(checkout).call(); + } + + repos.put(location, new RepoData(location, url, checkout, git)); + + } + } diff --git a/src/main/java/org/myrobotlab/service/Updater.java b/src/main/java/org/myrobotlab/service/Updater.java index 779dc1c81f..f97f3149e6 100644 --- a/src/main/java/org/myrobotlab/service/Updater.java +++ b/src/main/java/org/myrobotlab/service/Updater.java @@ -4,11 +4,32 @@ import java.io.FilenameFilter; import java.io.IOException; import java.io.UnsupportedEncodingException; +import java.util.ArrayList; import java.util.HashSet; +import java.util.List; import java.util.Properties; import java.util.Set; import java.util.TreeSet; +import org.eclipse.jgit.api.PullCommand; +import org.eclipse.jgit.api.errors.CanceledException; +import org.eclipse.jgit.api.errors.DetachedHeadException; +import org.eclipse.jgit.api.errors.GitAPIException; +import org.eclipse.jgit.api.errors.InvalidConfigurationException; +import org.eclipse.jgit.api.errors.InvalidRemoteException; +import org.eclipse.jgit.api.errors.NoHeadException; +import org.eclipse.jgit.api.errors.RefNotFoundException; +import org.eclipse.jgit.api.errors.TransportException; +import org.eclipse.jgit.api.errors.WrongRepositoryStateException; +import org.eclipse.jgit.errors.AmbiguousObjectException; +import org.eclipse.jgit.errors.IncorrectObjectTypeException; +import org.eclipse.jgit.errors.MissingObjectException; +import org.eclipse.jgit.errors.RevisionSyntaxException; +import org.eclipse.jgit.lib.BranchTrackingStatus; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.lib.TextProgressMonitor; +import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.storage.file.FileRepositoryBuilder; import org.myrobotlab.codec.CodecUtils; import org.myrobotlab.framework.CmdOptions; import org.myrobotlab.framework.MrlException; @@ -392,10 +413,13 @@ public void checkForUpdates() { if (isSrcMode) { String cwd = System.getProperty("user.dir"); boolean makeBuild = false; - String branch = Git.getBranch(); + + Repository repo = new FileRepositoryBuilder().setGitDir(new File(System.getProperty("user.dir"))).build(); + org.eclipse.jgit.api.Git git = new org.eclipse.jgit.api.Git(repo); + String branch = git.getRepository().getBranch(); log.info("current source branch is \"{}\"", branch); - int commitsBehind = Git.pull(branch); + int commitsBehind = pull(null, branch); if (gitProps == null) { log.info("target/classes/git.properties does not exist - will build"); @@ -413,7 +437,8 @@ public void checkForUpdates() { // FIXME - download mvn if it does not exist ?? // remove git properties before compile - Git.removeProps(); + File props = new File(System.getProperty("user.dir") + File.separator + "target" + File.separator + "classes" + File.separator + "git.properties"); + props.delete(); // FIXME - compile or package mode ! String ret = Maven.mvn(cwd, branch, "compile", System.currentTimeMillis() / 1000, offline); @@ -613,6 +638,73 @@ public boolean accept(File dir, String name) { return false; } + TextProgressMonitor monitor = new TextProgressMonitor(); + + public int pull(String src, String branch) throws IOException, WrongRepositoryStateException, InvalidConfigurationException, DetachedHeadException, InvalidRemoteException, + CanceledException, RefNotFoundException, NoHeadException, TransportException, GitAPIException { + + if (src == null) { + src = System.getProperty("user.dir"); + } + + if (branch == null) { + log.warn("branch is not set - setting to default develop"); + branch = "develop"; + } + + List branches = new ArrayList(); + branches.add("refs/heads/" + branch); + + File repoParentFolder = new File(src); + + org.eclipse.jgit.api.Git git = null; + Repository repo = null; + + // Open an existing repository FIXME Try Git.open(dir) + String gitDir = repoParentFolder.getAbsolutePath() + "/.git"; + repo = new FileRepositoryBuilder().setGitDir(new File(gitDir)).build(); + git = new org.eclipse.jgit.api.Git(repo); + + repo = git.getRepository(); + git.branchCreate().setForce(true).setName(branch).setStartPoint(branch).call(); + git.checkout().setName(branch).call(); + + git.fetch().setProgressMonitor(monitor).call(); + + List localLogs = getLogs(git, "origin/" + branch, 1); + List remoteLogs = getLogs(git, "remotes/origin/" + branch, 1); + + RevCommit localCommit = localLogs.get(0); + RevCommit remoteCommit = remoteLogs.get(0); + + BranchTrackingStatus status = BranchTrackingStatus.of(repo, branch); + + // FIXME - Git.close() file handles + + if (status.getBehindCount() > 0) { + log.info("local ts {}, remote {} - {} pulling", localCommit.getCommitTime(), remoteCommit.getCommitTime(), remoteCommit.getFullMessage()); + PullCommand pullCmd = git.pull(); + pullCmd.setProgressMonitor(monitor); + pullCmd.call(); + git.close(); + return status.getBehindCount(); + } + log.info("no new commits on branch {}", branch); + git.close(); + return 0; + } + + private List getLogs(org.eclipse.jgit.api.Git git, String ref, int maxCount) + throws RevisionSyntaxException, NoHeadException, MissingObjectException, IncorrectObjectTypeException, AmbiguousObjectException, GitAPIException, IOException { + List ret = new ArrayList<>(); + Repository repository = git.getRepository(); + Iterable logs = git.log().setMaxCount(maxCount).add(repository.resolve(ref)).call(); + for (RevCommit rev : logs) { + ret.add(rev); + } + return ret; + } + public static void main(String[] args) { LoggingFactory.init(Level.INFO);