From e72c94584f8d94d139a7c490191ccd5aebaaea05 Mon Sep 17 00:00:00 2001 From: Ed Merks Date: Fri, 18 Oct 2013 15:10:38 +0200 Subject: Improve setup related models Change-Id: I08d568b1e13f20e7cd63027d4ab10eaffec84ae7 Signed-off-by: Ed Merks --- .../META-INF/MANIFEST.MF | 8 +- .../Setup Dialog.launch | 8 +- .../META-INF/MANIFEST.MF | 76 ++-- .../Setup IDE.launch | 8 +- .../setup/impl/BuckminsterImportTaskImpl.java | 93 +++-- .../cdo/releng/setup/impl/GitCloneTaskImpl.java | 387 ++++++++++++--------- 6 files changed, 331 insertions(+), 249 deletions(-) diff --git a/plugins/org.eclipse.emf.cdo.releng.predicates/META-INF/MANIFEST.MF b/plugins/org.eclipse.emf.cdo.releng.predicates/META-INF/MANIFEST.MF index 90f473b2ad..06175a96ee 100644 --- a/plugins/org.eclipse.emf.cdo.releng.predicates/META-INF/MANIFEST.MF +++ b/plugins/org.eclipse.emf.cdo.releng.predicates/META-INF/MANIFEST.MF @@ -7,10 +7,10 @@ Bundle-Name: %pluginName Bundle-Vendor: %providerName Bundle-Localization: plugin Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.5.0,4.0.0)", - org.eclipse.emf.ecore;visibility:=reexport;bundle-version="[2.9.0,3.0.0)", - org.eclipse.core.resources;visibility:=reexport;bundle-version="[3.5.0,4.0.0)", - org.eclipse.team.core;bundle-version="[3.7.0,4.0.0)", - org.eclipse.egit.core;bundle-version="[3.0.1,5.0.0)" + org.eclipse.emf.ecore;bundle-version="[2.9.0,3.0.0)";visibility:=reexport, + org.eclipse.core.resources;bundle-version="[3.5.0,4.0.0)";visibility:=reexport, + org.eclipse.team.core;bundle-version="[3.7.0,4.0.0)";resolution:=optional, + org.eclipse.egit.core;bundle-version="[3.0.1,5.0.0)";resolution:=optional Bundle-ActivationPolicy: lazy Bundle-RequiredExecutionEnvironment: J2SE-1.5 Export-Package: org.eclipse.emf.cdo.releng.predicates;version="1.0.200", diff --git a/plugins/org.eclipse.emf.cdo.releng.setup.product/Setup Dialog.launch b/plugins/org.eclipse.emf.cdo.releng.setup.product/Setup Dialog.launch index 83f377a58e..2a5a6b5979 100644 --- a/plugins/org.eclipse.emf.cdo.releng.setup.product/Setup Dialog.launch +++ b/plugins/org.eclipse.emf.cdo.releng.setup.product/Setup Dialog.launch @@ -12,7 +12,7 @@ - + @@ -25,9 +25,9 @@ - - - + + + diff --git a/plugins/org.eclipse.emf.cdo.releng.setup/META-INF/MANIFEST.MF b/plugins/org.eclipse.emf.cdo.releng.setup/META-INF/MANIFEST.MF index 200fb3f7ec..b1722d1fc2 100644 --- a/plugins/org.eclipse.emf.cdo.releng.setup/META-INF/MANIFEST.MF +++ b/plugins/org.eclipse.emf.cdo.releng.setup/META-INF/MANIFEST.MF @@ -8,43 +8,49 @@ Bundle-Vendor: %providerName Bundle-Localization: plugin Require-Bundle: org.eclipse.net4j.util;bundle-version="[3.0.0,4.0.0)", org.eclipse.core.runtime;bundle-version="[3.5.0,4.0.0)", - org.eclipse.emf.cdo.releng.workingsets;visibility:=reexport;bundle-version="[1.0.0,2.0.0)", - org.eclipse.emf.cdo.releng.predicates;visibility:=reexport;bundle-version="[1.0.0,2.0.0)", - org.eclipse.emf.ecore;visibility:=reexport;bundle-version="[2.5.0,3.0.0)", + org.eclipse.emf.cdo.releng.workingsets;bundle-version="[1.0.0,2.0.0)";visibility:=reexport, + org.eclipse.emf.cdo.releng.predicates;bundle-version="[1.0.0,2.0.0)";visibility:=reexport, + org.eclipse.emf.ecore;bundle-version="[2.5.0,3.0.0)";visibility:=reexport, org.eclipse.emf.ecore.xmi;bundle-version="[2.5.0,3.0.0)", - org.eclipse.core.resources;resolution:=optional;bundle-version="[3.5.0,4.0.0)", - org.eclipse.core.variables;resolution:=optional;bundle-version="[3.0.0,4.0.0)", - org.eclipse.pde.core;resolution:=optional;bundle-version="[3.5.0,4.0.0)", - org.eclipse.pde.api.tools;resolution:=optional;bundle-version="[1.0.0,2.0.0)", - org.eclipse.team.ui;resolution:=optional;bundle-version="[3.5.0,4.0.0)", - javaewah;resolution:=optional;bundle-version="[0.0.0,1.0.0)", - org.eclipse.jgit;resolution:=optional;bundle-version="[3.0.0,4.0.0)", - org.eclipse.egit;resolution:=optional;bundle-version="[3.0.0,4.0.0)", - org.eclipse.egit.core;resolution:=optional;bundle-version="[3.0.0,4.0.0)", - org.eclipse.egit.ui;resolution:=optional;bundle-version="[3.0.0,4.0.0)", - org.eclipse.emf.edit.ui;resolution:=optional;bundle-version="[2.5.0,3.0.0)", - org.eclipse.ecf.provider.filetransfer;resolution:=optional;bundle-version="[3.2.0,4.0.0)", - org.eclipse.ecf.filetransfer;resolution:=optional;bundle-version="[5.0.0,6.0.0)", - org.eclipse.ecf.identity;resolution:=optional;bundle-version="[3.2.0,4.0.0)", - org.eclipse.ecf;resolution:=optional;bundle-version="[3.2.0,4.0.0)", - org.eclipse.buckminster.sax;resolution:=optional;bundle-version="[1.0.0,2.0.0)", - org.eclipse.buckminster.cmdline;resolution:=optional;bundle-version="[1.0.0,2.0.0)", - org.eclipse.buckminster.download;resolution:=optional;bundle-version="[1.0.0,2.0.0)", - org.eclipse.buckminster.runtime;resolution:=optional;bundle-version="[1.0.0,2.0.0)", - org.eclipse.buckminster.core;resolution:=optional;bundle-version="[1.0.0,2.0.0)", - org.eclipse.buckminster.pde;resolution:=optional;bundle-version="[1.0.0,2.0.0)", - org.eclipse.equinox.p2.core;resolution:=optional;bundle-version="[2.0.0,3.0.0)", - org.eclipse.equinox.p2.ui;resolution:=optional;bundle-version="[2.0.0,3.0.0)", - org.eclipse.equinox.p2.metadata;resolution:=optional;bundle-version="[2.0.0,3.0.0)", - org.eclipse.equinox.p2.repository;resolution:=optional;bundle-version="[2.0.0,3.0.0)", - org.eclipse.equinox.p2.artifact.repository;resolution:=optional;bundle-version="[1.0.0,2.0.0)", - org.eclipse.equinox.p2.metadata.repository;resolution:=optional;bundle-version="[1.0.0,2.0.0)", - org.eclipse.equinox.p2.console;resolution:=optional;bundle-version="[1.0.0,2.0.0)", - org.eclipse.equinox.p2.publisher;resolution:=optional;bundle-version="[1.0.0,2.0.0)", - org.eclipse.equinox.p2.publisher.eclipse;resolution:=optional;bundle-version="[1.0.0,2.0.0)", - org.eclipse.equinox.p2.director.app;resolution:=optional;bundle-version="[1.0.0,2.0.0)", + org.eclipse.core.resources;bundle-version="[3.5.0,4.0.0)";resolution:=optional, + org.eclipse.core.variables;bundle-version="[3.0.0,4.0.0)";resolution:=optional, + org.eclipse.pde.core;bundle-version="[3.5.0,4.0.0)";resolution:=optional, + org.eclipse.pde.api.tools;bundle-version="[1.0.0,2.0.0)";resolution:=optional, + org.eclipse.team.ui;bundle-version="[3.5.0,4.0.0)";resolution:=optional, + javaewah;bundle-version="[0.0.0,1.0.0)";resolution:=optional, + org.eclipse.jgit;bundle-version="[3.0.0,4.0.0)";resolution:=optional, + org.eclipse.egit;bundle-version="[3.0.0,4.0.0)";resolution:=optional, + org.eclipse.egit.core;bundle-version="[3.0.0,4.0.0)";resolution:=optional, + org.eclipse.egit.ui;bundle-version="[3.0.0,4.0.0)";resolution:=optional, + org.eclipse.emf.edit.ui;bundle-version="[2.5.0,3.0.0)";resolution:=optional, + org.eclipse.ecf.provider.filetransfer;bundle-version="[3.2.0,4.0.0)";resolution:=optional, + org.eclipse.ecf.filetransfer;bundle-version="[5.0.0,6.0.0)";resolution:=optional, + org.eclipse.ecf.identity;bundle-version="[3.2.0,4.0.0)";resolution:=optional, + org.eclipse.ecf;bundle-version="[3.2.0,4.0.0)";resolution:=optional, + org.eclipse.buckminster.sax;bundle-version="[1.0.0,2.0.0)";resolution:=optional, + org.eclipse.buckminster.cmdline;bundle-version="[1.0.0,2.0.0)";resolution:=optional, + org.eclipse.buckminster.download;bundle-version="[1.0.0,2.0.0)";resolution:=optional, + org.eclipse.buckminster.runtime;bundle-version="[1.0.0,2.0.0)";resolution:=optional, + org.eclipse.buckminster.core;bundle-version="[1.0.0,2.0.0)";resolution:=optional, + org.eclipse.buckminster.pde;bundle-version="[1.0.0,2.0.0)";resolution:=optional, + org.eclipse.equinox.p2.core;bundle-version="[2.0.0,3.0.0)", + org.eclipse.equinox.p2.ui;bundle-version="[2.0.0,3.0.0)";resolution:=optional, + org.eclipse.equinox.p2.metadata;bundle-version="[2.0.0,3.0.0)", + org.eclipse.equinox.p2.repository;bundle-version="[2.0.0,3.0.0)", + org.eclipse.equinox.p2.artifact.repository;bundle-version="[1.0.0,2.0.0)", + org.eclipse.equinox.p2.metadata.repository;bundle-version="[1.0.0,2.0.0)", + org.eclipse.equinox.p2.console;bundle-version="[1.0.0,2.0.0)", + org.eclipse.equinox.p2.publisher;bundle-version="[1.0.0,2.0.0)", + org.eclipse.equinox.p2.publisher.eclipse;bundle-version="[1.0.0,2.0.0)", + org.eclipse.equinox.p2.director.app;bundle-version="[1.0.0,2.0.0)", org.eclipse.equinox.p2.operations;bundle-version="[2.0.0,3.0.0)", - org.eclipse.equinox.p2.engine;bundle-version="[2.0.0,3.0.0)" + org.eclipse.equinox.p2.engine;bundle-version="[2.0.0,3.0.0)", + org.eclipse.equinox.p2.transport.ecf;bundle-version="[1.1.0,2.0.0)", + org.eclipse.ecf.provider.filetransfer.httpclient4;bundle-version="[1.0.0,2.0.0)", + org.eclipse.core.net;bundle-version="[1.2.0,2.0.0)", + org.eclipse.equinox.p2.touchpoint.eclipse;bundle-version="[2.1.0,3.0.0)", + org.eclipse.equinox.p2.touchpoint.natives;bundle-version="[1.1.0,2.0.0)", + org.eclipse.equinox.p2.updatesite;bundle-version="[1.0.0,2.0.0)" Bundle-RequiredExecutionEnvironment: J2SE-1.5 Bundle-ActivationPolicy: lazy Export-Package: org.eclipse.emf.cdo.releng.internal.setup;x-friends:="org.eclipse.emf.cdo.releng.setup.product,org.eclipse.emf.cdo.releng.setup.editor";version="1.0.0", diff --git a/plugins/org.eclipse.emf.cdo.releng.setup/Setup IDE.launch b/plugins/org.eclipse.emf.cdo.releng.setup/Setup IDE.launch index 1efc5454b4..39c738d644 100644 --- a/plugins/org.eclipse.emf.cdo.releng.setup/Setup IDE.launch +++ b/plugins/org.eclipse.emf.cdo.releng.setup/Setup IDE.launch @@ -13,7 +13,7 @@ - + @@ -22,11 +22,11 @@ - + - - + + diff --git a/plugins/org.eclipse.emf.cdo.releng.setup/src/org/eclipse/emf/cdo/releng/setup/impl/BuckminsterImportTaskImpl.java b/plugins/org.eclipse.emf.cdo.releng.setup/src/org/eclipse/emf/cdo/releng/setup/impl/BuckminsterImportTaskImpl.java index 4755b8a631..9c0d25e5f3 100644 --- a/plugins/org.eclipse.emf.cdo.releng.setup/src/org/eclipse/emf/cdo/releng/setup/impl/BuckminsterImportTaskImpl.java +++ b/plugins/org.eclipse.emf.cdo.releng.setup/src/org/eclipse/emf/cdo/releng/setup/impl/BuckminsterImportTaskImpl.java @@ -39,6 +39,7 @@ import org.eclipse.equinox.p2.publisher.eclipse.FeaturesAndBundlesPublisherAppli import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; +import java.net.MalformedURLException; import java.net.URL; import java.util.Set; @@ -160,8 +161,10 @@ public class BuckminsterImportTaskImpl extends SetupTaskImpl implements Buckmins String oldMspec = mspec; mspec = newMspec; if (eNotificationRequired()) + { eNotify(new ENotificationImpl(this, Notification.SET, SetupPackage.BUCKMINSTER_IMPORT_TASK__MSPEC, oldMspec, mspec)); + } } /** @@ -184,8 +187,10 @@ public class BuckminsterImportTaskImpl extends SetupTaskImpl implements Buckmins String oldTargetPlatform = targetPlatform; targetPlatform = newTargetPlatform; if (eNotificationRequired()) + { eNotify(new ENotificationImpl(this, Notification.SET, SetupPackage.BUCKMINSTER_IMPORT_TASK__TARGET_PLATFORM, oldTargetPlatform, targetPlatform)); + } } /** @@ -208,8 +213,10 @@ public class BuckminsterImportTaskImpl extends SetupTaskImpl implements Buckmins String oldBundlePool = bundlePool; bundlePool = newBundlePool; if (eNotificationRequired()) + { eNotify(new ENotificationImpl(this, Notification.SET, SetupPackage.BUCKMINSTER_IMPORT_TASK__BUNDLE_POOL, oldBundlePool, bundlePool)); + } } /** @@ -308,7 +315,9 @@ public class BuckminsterImportTaskImpl extends SetupTaskImpl implements Buckmins public String toString() { if (eIsProxy()) + { return super.toString(); + } StringBuffer result = new StringBuffer(super.toString()); result.append(" (mspec: "); @@ -362,28 +371,7 @@ public class BuckminsterImportTaskImpl extends SetupTaskImpl implements Buckmins .getProject().getName() + " Target", true, context); - URL mSpecURL = new URL(context.expandString(getMspec())); - MaterializationSpec mspec = getMSpec(mSpecURL, monitor); // 20 ticks - ComponentQuery cquery = getCQuery(mspec.getResolvedURL(), monitor); // 20 ticks - - IResolver resolver = new MainResolver(new ResolutionContext(mspec, cquery)); - resolver.getContext().setContinueOnError(true); - - monitor.subTask("Resolving components"); - BillOfMaterials bom = resolver.resolve(MonitorUtils.subMonitor(monitor, 40)); - - MaterializationSpecBuilder mspecBuilder = new MaterializationSpecBuilder(); - mspecBuilder.initFrom(mspec); - mspecBuilder.setName(bom.getViewName()); - - bom.addMaterializationNodes(mspecBuilder); - - ResolutionContext resolutionContext = new ResolutionContext(bom.getQuery()); - MaterializationContext materializationContext = new MaterializationContext(bom, mspec, resolutionContext); - - monitor.subTask("Materializing components"); - MaterializationJob job = new MaterializationJob(materializationContext); - job.run(MonitorUtils.subMonitor(monitor, 80)); + BuckminsterHelper.materialize(context, getMspec(), monitor); if (tpPoolLock != null) { @@ -491,25 +479,56 @@ public class BuckminsterImportTaskImpl extends SetupTaskImpl implements Buckmins publisher.run(args); } - private MaterializationSpec getMSpec(URL mspecURL, IProgressMonitor monitor) throws Exception + private static class BuckminsterHelper { - monitor.subTask("Downloading MSpec"); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - DownloadManager.readInto(mspecURL, null, baos, MonitorUtils.subMonitor(monitor, 20)); + private static MaterializationSpec getMSpec(URL mspecURL, IProgressMonitor monitor) throws Exception + { + monitor.subTask("Downloading MSpec"); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DownloadManager.readInto(mspecURL, null, baos, MonitorUtils.subMonitor(monitor, 20)); + + monitor.subTask("Parsing MSpec"); + IParser parser = CorePlugin.getDefault().getParserFactory() + .getMaterializationSpecParser(true); + return parser.parse(mspecURL.toString(), new ByteArrayInputStream(baos.toByteArray())); + } - monitor.subTask("Parsing MSpec"); - IParser parser = CorePlugin.getDefault().getParserFactory().getMaterializationSpecParser(true); - return parser.parse(mspecURL.toString(), new ByteArrayInputStream(baos.toByteArray())); - } + private static ComponentQuery getCQuery(URL cqueryURL, IProgressMonitor monitor) throws Exception + { + monitor.subTask("Downloading CQuery"); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DownloadManager.readInto(cqueryURL, null, baos, MonitorUtils.subMonitor(monitor, 20)); - private ComponentQuery getCQuery(URL cqueryURL, IProgressMonitor monitor) throws Exception - { - monitor.subTask("Downloading CQuery"); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - DownloadManager.readInto(cqueryURL, null, baos, MonitorUtils.subMonitor(monitor, 20)); + monitor.subTask("Parsing CQuery"); + return ComponentQuery.fromStream(cqueryURL, null, new ByteArrayInputStream(baos.toByteArray()), true); + } + + private static void materialize(SetupTaskContext context, String mSpec, IProgressMonitor monitor) + throws MalformedURLException, Exception + { + URL mSpecURL = new URL(context.expandString(mSpec)); + MaterializationSpec mspec = BuckminsterHelper.getMSpec(mSpecURL, monitor); // 20 ticks + ComponentQuery cquery = BuckminsterHelper.getCQuery(mspec.getResolvedURL(), monitor); // 20 ticks + + IResolver resolver = new MainResolver(new ResolutionContext(mspec, cquery)); + resolver.getContext().setContinueOnError(true); + + monitor.subTask("Resolving components"); + BillOfMaterials bom = resolver.resolve(MonitorUtils.subMonitor(monitor, 40)); + + MaterializationSpecBuilder mspecBuilder = new MaterializationSpecBuilder(); + mspecBuilder.initFrom(mspec); + mspecBuilder.setName(bom.getViewName()); - monitor.subTask("Parsing CQuery"); - return ComponentQuery.fromStream(cqueryURL, null, new ByteArrayInputStream(baos.toByteArray()), true); + bom.addMaterializationNodes(mspecBuilder); + + ResolutionContext resolutionContext = new ResolutionContext(bom.getQuery()); + MaterializationContext materializationContext = new MaterializationContext(bom, mspec, resolutionContext); + + monitor.subTask("Materializing components"); + MaterializationJob job = new MaterializationJob(materializationContext); + job.run(MonitorUtils.subMonitor(monitor, 80)); + } } } // BuckminsterImportTaskImpl diff --git a/plugins/org.eclipse.emf.cdo.releng.setup/src/org/eclipse/emf/cdo/releng/setup/impl/GitCloneTaskImpl.java b/plugins/org.eclipse.emf.cdo.releng.setup/src/org/eclipse/emf/cdo/releng/setup/impl/GitCloneTaskImpl.java index 1dfbf1aa87..a5ec45b520 100644 --- a/plugins/org.eclipse.emf.cdo.releng.setup/src/org/eclipse/emf/cdo/releng/setup/impl/GitCloneTaskImpl.java +++ b/plugins/org.eclipse.emf.cdo.releng.setup/src/org/eclipse/emf/cdo/releng/setup/impl/GitCloneTaskImpl.java @@ -134,13 +134,7 @@ public class GitCloneTaskImpl extends SetupTaskImpl implements GitCloneTask */ protected String checkoutBranch = CHECKOUT_BRANCH_EDEFAULT; - private transient Object cachedGit; - - private transient Object cachedRepository; - - private transient File workDir; - - private transient boolean hasCheckout; + private transient GitDelegate gitDelegate; /** * @@ -183,7 +177,9 @@ public class GitCloneTaskImpl extends SetupTaskImpl implements GitCloneTask String oldName = name; name = newName; if (eNotificationRequired()) + { eNotify(new ENotificationImpl(this, Notification.SET, SetupPackage.GIT_CLONE_TASK__NAME, oldName, name)); + } } /** @@ -206,8 +202,10 @@ public class GitCloneTaskImpl extends SetupTaskImpl implements GitCloneTask String oldRemoteName = remoteName; remoteName = newRemoteName; if (eNotificationRequired()) + { eNotify(new ENotificationImpl(this, Notification.SET, SetupPackage.GIT_CLONE_TASK__REMOTE_NAME, oldRemoteName, remoteName)); + } } /** @@ -230,8 +228,10 @@ public class GitCloneTaskImpl extends SetupTaskImpl implements GitCloneTask String oldRemoteURI = remoteURI; remoteURI = newRemoteURI; if (eNotificationRequired()) + { eNotify(new ENotificationImpl(this, Notification.SET, SetupPackage.GIT_CLONE_TASK__REMOTE_URI, oldRemoteURI, remoteURI)); + } } /** @@ -254,8 +254,10 @@ public class GitCloneTaskImpl extends SetupTaskImpl implements GitCloneTask String oldCheckoutBranch = checkoutBranch; checkoutBranch = newCheckoutBranch; if (eNotificationRequired()) + { eNotify(new ENotificationImpl(this, Notification.SET, SetupPackage.GIT_CLONE_TASK__CHECKOUT_BRANCH, oldCheckoutBranch, checkoutBranch)); + } } /** @@ -364,7 +366,9 @@ public class GitCloneTaskImpl extends SetupTaskImpl implements GitCloneTask public String toString() { if (eIsProxy()) + { return super.toString(); + } StringBuffer result = new StringBuffer(super.toString()); result.append(" (name: "); @@ -387,228 +391,281 @@ public class GitCloneTaskImpl extends SetupTaskImpl implements GitCloneTask public boolean isNeeded(SetupTaskContext context) throws Exception { - workDir = getWorkDir(context); - if (!workDir.isDirectory()) - { - return true; - } - - context.log("Opening Git clone " + workDir); - - Git git = Git.open(workDir); - if (!hasWorkTree(git)) - { - FileUtil.rename(workDir); - return true; - } - - Repository repository = git.getRepository(); - configureRepository(context, repository); - - hasCheckout = repository.getAllRefs().containsKey("refs/heads/" + getCheckoutBranch()); - if (!hasCheckout) - { - cachedGit = git; - cachedRepository = repository; - return true; - } - - return false; + gitDelegate = GitUtil.create(); + return gitDelegate.isNeeded(context, getName(), getCheckoutBranch(), getRemoteName()); } public void perform(SetupTaskContext context) throws Exception { - Git git = (Git)cachedGit; - Repository repository = (Repository)cachedRepository; - - if (git == null) + if (gitDelegate != null) { - git = cloneRepository(context, workDir, getCheckoutBranch()); - repository = git.getRepository(); - configureRepository(context, repository); - } - - if (!hasCheckout) - { - createBranch(context, git, getCheckoutBranch()); - checkout(context, git, getCheckoutBranch()); - resetHard(context, git); + gitDelegate.perform(context, getCheckoutBranch(), getRemoteName(), getRemoteURI()); } } @Override public void dispose() { - if (cachedRepository != null) + if (gitDelegate != null) { - ((Repository)cachedRepository).close(); + gitDelegate.dispose(); + gitDelegate = null; } } - private File getWorkDir(SetupTaskContext context) + private interface GitDelegate { - File gitDir = new File(context.getBranchDir(), "git"); - return new File(gitDir, getName()); - } + public boolean isNeeded(SetupTaskContext context, String name, String checkoutBranch, String remoteName) + throws Exception; - private boolean hasWorkTree(Git git) throws Exception - { - try - { - StatusCommand statusCommand = git.status(); - statusCommand.call(); - return true; - } - catch (NoWorkTreeException ex) - { - return false; - } - } + public void perform(SetupTaskContext context, String checkoutBranch, String remoteName, String remoteURI) + throws Exception; - private Git cloneRepository(SetupTaskContext context, File workDir, String checkoutBranch) throws Exception - { - URI baseURI = URI.createURI(getRemoteURI()); - String remote = URI.createHierarchicalURI(baseURI.scheme(), - context.getSetup().getPreferences().getUserName() + "@" + baseURI.authority(), baseURI.device(), - baseURI.segments(), baseURI.query(), baseURI.fragment()).toString(); - - context.log("Cloning Git repo " + remote + " to " + workDir); - - CloneCommand command = Git.cloneRepository(); - command.setNoCheckout(true); - command.setURI(remote); - command.setRemote(getRemoteName()); - command.setBranchesToClone(Collections.singleton(checkoutBranch)); - command.setDirectory(workDir); - command.setTimeout(10); - command.setProgressMonitor(new ProgressLogWrapper(context)); - return command.call(); + public void dispose(); } - private void configureRepository(SetupTaskContext context, Repository repository) throws Exception, IOException + private static class GitUtil implements GitDelegate { - StoredConfig config = repository.getConfig(); + private File workDir; + + private boolean hasCheckout; - boolean changed = false; - changed |= configureLineEndingConversion(context, config); - changed |= addPushRefSpec(context, config); - if (changed) + private Git cachedGit; + + private Repository cachedRepository; + + private static GitDelegate create() { - config.save(); + return new GitUtil(); } - } - private boolean configureLineEndingConversion(SetupTaskContext context, StoredConfig config) throws Exception - { - if (context.getOS().isLineEndingConversionNeeded()) + public boolean isNeeded(SetupTaskContext context, String name, String checkoutBranch, String remoteName) + throws Exception { - context.log("Setting " + ConfigConstants.CONFIG_KEY_AUTOCRLF + " = true"); - config.setEnum(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_AUTOCRLF, AutoCRLF.TRUE); - return true; - } + workDir = getWorkDir(context, name); + if (!workDir.isDirectory()) + { + return true; + } - return false; - } + context.log("Opening Git clone " + workDir); - private boolean addPushRefSpec(SetupTaskContext context, StoredConfig config) throws Exception - { - String gerritQueue = "refs/for/" + getCheckoutBranch(); - for (RemoteConfig remoteConfig : RemoteConfig.getAllRemoteConfigs(config)) - { - if (getRemoteName().equals(remoteConfig.getName())) + Git git = Git.open(workDir); + if (!GitUtil.hasWorkTree(git)) { - List pushRefSpecs = remoteConfig.getPushRefSpecs(); - if (hasGerritPushRefSpec(pushRefSpecs, gerritQueue)) - { - return false; - } + FileUtil.rename(workDir); + return true; + } - RefSpec refSpec = new RefSpec("HEAD:" + gerritQueue); - context.log("Adding push ref spec: " + refSpec); + Repository repository = git.getRepository(); + GitUtil.configureRepository(context, repository, checkoutBranch, remoteName); - remoteConfig.addPushRefSpec(refSpec); - remoteConfig.update(config); + hasCheckout = repository.getAllRefs().containsKey("refs/heads/" + checkoutBranch); + if (!hasCheckout) + { + cachedGit = git; + cachedRepository = repository; return true; } - } - return false; - } + return false; + } - private boolean hasGerritPushRefSpec(List pushRefSpecs, String gerritQueue) - { - for (RefSpec refSpec : pushRefSpecs) + public void perform(SetupTaskContext context, String checkoutBranch, String remoteName, String removeURI) + throws Exception { - if (refSpec.getDestination().equals(gerritQueue)) + if (cachedGit == null) { - return true; + cachedGit = GitUtil.cloneRepository(context, workDir, checkoutBranch, remoteName, removeURI); + cachedRepository = cachedGit.getRepository(); + GitUtil.configureRepository(context, cachedRepository, checkoutBranch, remoteName); + } + + if (!hasCheckout) + { + GitUtil.createBranch(context, cachedGit, checkoutBranch); + GitUtil.checkout(context, cachedGit, checkoutBranch); + GitUtil.resetHard(context, cachedGit); } } - return false; - } + public void dispose() + { + if (cachedRepository != null) + { + cachedRepository.close(); + } + } - private void createBranch(SetupTaskContext context, Git git, String checkoutBranch) throws Exception - { - context.log("Creating local branch " + checkoutBranch); + private static File getWorkDir(SetupTaskContext context, String name) + { + File gitDir = new File(context.getBranchDir(), "git"); + return new File(gitDir, name); + } - CreateBranchCommand command = git.branchCreate(); - command.setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM); - command.setName(checkoutBranch); - command.setStartPoint("refs/remotes/origin/" + checkoutBranch); - command.call(); - } + private static boolean hasWorkTree(Git git) throws Exception + { + try + { + StatusCommand statusCommand = git.status(); + statusCommand.call(); + return true; + } + catch (NoWorkTreeException ex) + { + return false; + } + } - private void checkout(SetupTaskContext context, Git git, String checkoutBranch) throws Exception - { - context.log("Checking out local branch " + checkoutBranch); + private static Git cloneRepository(SetupTaskContext context, File workDir, String checkoutBranch, + String remoteName, String remoteURI) throws Exception + { + URI baseURI = URI.createURI(remoteURI); + String remote = URI.createHierarchicalURI(baseURI.scheme(), + context.getSetup().getPreferences().getUserName() + "@" + baseURI.authority(), baseURI.device(), + baseURI.segments(), baseURI.query(), baseURI.fragment()).toString(); + + context.log("Cloning Git repo " + remote + " to " + workDir); + + CloneCommand command = Git.cloneRepository(); + command.setNoCheckout(true); + command.setURI(remote); + command.setRemote(remoteName); + command.setBranchesToClone(Collections.singleton(checkoutBranch)); + command.setDirectory(workDir); + command.setTimeout(10); + command.setProgressMonitor(new ProgressLogWrapper(context)); + return command.call(); + } - CheckoutCommand command = git.checkout(); - command.setName(checkoutBranch); - command.call(); - } + private static void configureRepository(SetupTaskContext context, Repository repository, String checkoutBranch, + String remoteName) throws Exception, IOException + { + StoredConfig config = repository.getConfig(); - private void resetHard(SetupTaskContext context, Git git) throws Exception - { - context.log("Resetting hard"); + boolean changed = false; + changed |= configureLineEndingConversion(context, config); + changed |= addPushRefSpec(context, config, checkoutBranch, remoteName); + if (changed) + { + config.save(); + } + } - ResetCommand command = git.reset(); - command.setMode(ResetType.HARD); - command.call(); - } + private static boolean configureLineEndingConversion(SetupTaskContext context, StoredConfig config) + throws Exception + { + if (context.getOS().isLineEndingConversionNeeded()) + { + context.log("Setting " + ConfigConstants.CONFIG_KEY_AUTOCRLF + " = true"); + config.setEnum(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_AUTOCRLF, AutoCRLF.TRUE); + return true; + } - /** - * @author Eike Stepper - */ - private static final class ProgressLogWrapper implements ProgressMonitor - { - private SetupTaskContext context; + return false; + } - public ProgressLogWrapper(SetupTaskContext context) + private static boolean addPushRefSpec(SetupTaskContext context, StoredConfig config, String checkoutBranch, + String remoteName) throws Exception { - this.context = context; + String gerritQueue = "refs/for/" + checkoutBranch; + for (RemoteConfig remoteConfig : RemoteConfig.getAllRemoteConfigs(config)) + { + if (remoteName.equals(remoteConfig.getName())) + { + List pushRefSpecs = remoteConfig.getPushRefSpecs(); + if (hasGerritPushRefSpec(pushRefSpecs, gerritQueue)) + { + return false; + } + + RefSpec refSpec = new RefSpec("HEAD:" + gerritQueue); + context.log("Adding push ref spec: " + refSpec); + + remoteConfig.addPushRefSpec(refSpec); + remoteConfig.update(config); + return true; + } + } + + return false; } - public void update(int completed) + private static boolean hasGerritPushRefSpec(List pushRefSpecs, String gerritQueue) { + for (RefSpec refSpec : pushRefSpecs) + { + if (refSpec.getDestination().equals(gerritQueue)) + { + return true; + } + } + + return false; } - public void start(int totalTasks) + private static void createBranch(SetupTaskContext context, Git git, String checkoutBranch) throws Exception { + context.log("Creating local branch " + checkoutBranch); + + CreateBranchCommand command = git.branchCreate(); + command.setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM); + command.setName(checkoutBranch); + command.setStartPoint("refs/remotes/origin/" + checkoutBranch); + command.call(); } - public boolean isCancelled() + private static void checkout(SetupTaskContext context, Git git, String checkoutBranch) throws Exception { - return context.isCancelled(); + context.log("Checking out local branch " + checkoutBranch); + + CheckoutCommand command = git.checkout(); + command.setName(checkoutBranch); + command.call(); } - public void endTask() + private static void resetHard(SetupTaskContext context, Git git) throws Exception { + context.log("Resetting hard"); + + ResetCommand command = git.reset(); + command.setMode(ResetType.HARD); + command.call(); } - public void beginTask(String title, int totalWork) + /** + * @author Eike Stepper + */ + private static final class ProgressLogWrapper implements ProgressMonitor { - context.log(title); + private SetupTaskContext context; + + public ProgressLogWrapper(SetupTaskContext context) + { + this.context = context; + } + + public void update(int completed) + { + } + + public void start(int totalTasks) + { + } + + public boolean isCancelled() + { + return context.isCancelled(); + } + + public void endTask() + { + } + + public void beginTask(String title, int totalWork) + { + context.log(title); + } } } -- cgit v1.2.3