public void update(ProjectController controller) throws IllegalStateException { if (Utils.isNetworkOnline(context) == false) throw new IllegalStateException("Network is not available"); this.controller = controller; File sourceRepo = new File(project.getSourcePath(context)); GitCommand<?> command; if(sourceRepo.exists()) { command = git.pull(); } else { command = Git.cloneRepository().setURI(project.getUrl()) .setDirectory(new File(project.getSourcePath(context))); } new GitTask(project.getName()).execute(command); }
/** * Configures the transport of the command to deal with things like SSH */ public static <C extends GitCommand> void configureCommand(TransportCommand<C, ?> command, CredentialsProvider credentialsProvider, final File sshPrivateKey, final File sshPublicKey) { if (sshPrivateKey != null) { final CredentialsProvider provider = credentialsProvider; command.setTransportConfigCallback(new TransportConfigCallback() { @Override public void configure(Transport transport) { if (transport instanceof SshTransport) { SshTransport sshTransport = (SshTransport) transport; SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() { @Override protected void configure(OpenSshConfig.Host host, Session session) { session.setConfig("StrictHostKeyChecking", "no"); UserInfo userInfo = new CredentialsProviderUserInfo(session, provider); session.setUserInfo(userInfo); } @Override protected JSch createDefaultJSch(FS fs) throws JSchException { JSch jsch = super.createDefaultJSch(fs); jsch.removeAllIdentity(); String absolutePath = sshPrivateKey.getAbsolutePath(); if (LOG.isDebugEnabled()) { LOG.debug("Adding identity privateKey: " + sshPrivateKey + " publicKey: " + sshPublicKey); } if (sshPublicKey != null) { jsch.addIdentity(absolutePath, sshPublicKey.getAbsolutePath(), null); } else { jsch.addIdentity(absolutePath); } return jsch; } }; sshTransport.setSshSessionFactory(sshSessionFactory); } } }); } }
@Override public void handle(GitCommand<Git> command, final Caller caller, final String callerWorkspaceName, final Git result) { final VarScript plugin = caller.getService().getPlugin(); Bukkit.getScheduler().runTask(plugin, new Runnable() { @Override public void run() { executor.onCommandGitCloneDone(caller, callerWorkspaceName, result, folderName); } }); }
@Override protected Long doInBackground(GitCommand<?>... params) { GitCommand<?> command = params[0]; Utils.announceSyncStart(context, projectName); if(command instanceof CloneCommand) { ((CloneCommand)command).setProgressMonitor(monitor); status = "Cloning"; } else if(command instanceof PullCommand) { ((PullCommand)command).setProgressMonitor(monitor); status = "Pulling"; } else { throw new IllegalArgumentException( "Coudln't attach progressMonitor to git command"); } publishProgress(-1); try { command.call(); } catch (Exception e) { status = e.getLocalizedMessage(); publishProgress(100); } return 0L; }
/** * Setups the Git credentials if specified and needed * * @param command The git command to configure */ @SuppressWarnings("rawtypes") protected void setupCredentials(GitCommand<?> command) { GitSettings settings = lookupSettings(); if (settings != null && command instanceof TransportCommand) { TransportCommand cmd = (TransportCommand) command; cmd.setCredentialsProvider(settings.getCredentials()); } }
/** * Configures the transport of the command to deal with things like SSH */ public static <C extends GitCommand> void configureCommand(TransportCommand<C, ?> command, UserDetails userDetails) { configureCommand(command, userDetails.createCredentialsProvider(), userDetails.getSshPrivateKey(), userDetails.getSshPublicKey()); command.setCredentialsProvider(userDetails.createCredentialsProvider()); }
@Override protected String doInBackground(GitCommand... commands) { Integer nbChanges = null; for (GitCommand command : commands) { Log.d("doInBackground", "Executing the command <" + command.toString() + ">"); try { if (command instanceof StatusCommand) { // in case we have changes, we want to keep track of it org.eclipse.jgit.api.Status status = ((StatusCommand) command).call(); nbChanges = status.getChanged().size() + status.getMissing().size(); } else if (command instanceof CommitCommand) { // the previous status will eventually be used to avoid a commit if (nbChanges == null || nbChanges > 0) command.call(); }else if (command instanceof PushCommand) { for (final PushResult result : ((PushCommand) command).call()) { // Code imported (modified) from Gerrit PushOp, license Apache v2 for (final RemoteRefUpdate rru : result.getRemoteUpdates()) { switch (rru.getStatus()) { case REJECTED_NONFASTFORWARD: return activity.getString(R.string.git_push_nff_error); case REJECTED_NODELETE: case REJECTED_REMOTE_CHANGED: case NON_EXISTING: case NOT_ATTEMPTED: return activity.getString(R.string.git_push_generic_error) + rru.getStatus().name(); case REJECTED_OTHER_REASON: if ("non-fast-forward".equals(rru.getMessage())) { return activity.getString(R.string.git_push_other_error); } else { return activity.getString(R.string.git_push_generic_error) + rru.getMessage(); } default: break; } } } } else { command.call(); } } catch (Exception e) { e.printStackTrace(); return e.getMessage() + "\nCaused by:\n" + e.getCause(); } } return ""; }
public void setCommand(GitCommand<T> command) { this.command = command; }
@Override public void handle(GitCommand<FetchResult> command, Caller caller, String callerWorkspaceName, FetchResult result) { String message = "Fetch done. " + ChatColor.AQUA + result.getMessages(); VarScript plugin = caller.getService().getPlugin(); Bukkit.getScheduler().runTask(plugin, new MessageSender(caller, message, callerWorkspaceName,0)); }
void handle(GitCommand<T> command, Caller caller, String callerWorkspaceName, T result);