summaryrefslogtreecommitdiff
path: root/plugins/git4idea/remote-servers-git/src/com/intellij/remoteServer/util/CloudGitDeploymentRuntime.java
blob: 9846393b99a85018218bc5f889eb8492a8339f52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
package com.intellij.remoteServer.util;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vcs.changes.*;
import com.intellij.openapi.vcs.changes.ui.CommitChangeListDialog;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.remoteServer.agent.util.CloudGitApplication;
import com.intellij.remoteServer.agent.util.CloudLoggingHandler;
import com.intellij.remoteServer.configuration.deployment.DeploymentSource;
import com.intellij.remoteServer.runtime.deployment.DeploymentLogManager;
import com.intellij.remoteServer.runtime.deployment.DeploymentTask;
import com.intellij.util.ArrayUtil;
import com.intellij.util.concurrency.Semaphore;
import git4idea.GitPlatformFacade;
import git4idea.GitUtil;
import git4idea.actions.GitInit;
import git4idea.commands.*;
import git4idea.repo.GitRemote;
import git4idea.repo.GitRepository;
import git4idea.repo.GitRepositoryManager;
import git4idea.util.GitFileUtils;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * @author michael.golubev
 */
public class CloudGitDeploymentRuntime extends CloudDeploymentRuntime {

  private static final Logger LOG = Logger.getInstance("#" + CloudGitDeploymentRuntime.class.getName());

  private static final String COMMIT_MESSAGE = "Deploy";

  private static final CommitSession NO_COMMIT = new CommitSession() {

    @Nullable
    @Override
    public JComponent getAdditionalConfigurationUI() {
      return null;
    }

    @Nullable
    @Override
    public JComponent getAdditionalConfigurationUI(Collection<Change> changes, String commitMessage) {
      return null;
    }

    @Override
    public boolean canExecute(Collection<Change> changes, String commitMessage) {
      return true;
    }

    @Override
    public void execute(Collection<Change> changes, String commitMessage) {

    }

    @Override
    public void executionCanceled() {

    }

    @Override
    public String getHelpId() {
      return null;
    }
  };

  private static final List<CommitExecutor> ourCommitExecutors = Arrays.asList(
    new CommitExecutor() {

      @Nls
      @Override
      public String getActionText() {
        return "Commit and Push";
      }

      @NotNull
      @Override
      public CommitSession createCommitSession() {
        return CommitSession.VCS_COMMIT;
      }
    },
    new CommitExecutorBase() {

      @Nls
      @Override
      public String getActionText() {
        return "Push without Commit";
      }

      @NotNull
      @Override
      public CommitSession createCommitSession() {
        return NO_COMMIT;
      }

      @Override
      public boolean areChangesRequired() {
        return false;
      }
    }
  );

  private final GitRepositoryManager myGitRepositoryManager;
  private final Git myGit;

  private final VirtualFile myContentRoot;
  private final File myRepositoryRootFile;

  private final String myDefaultRemoteName;
  private final ChangeListManagerEx myChangeListManager;
  private String myRemoteName;
  private final String myCloudName;

  private GitRepository myRepository;

  public CloudGitDeploymentRuntime(CloudMultiSourceServerRuntimeInstance serverRuntime,
                                   DeploymentSource source,
                                   File repositoryRoot,
                                   DeploymentTask<? extends CloudDeploymentNameConfiguration> task,
                                   DeploymentLogManager logManager,
                                   String defaultRemoteName,
                                   String cloudName) throws ServerRuntimeException {
    super(serverRuntime, source, task, logManager);

    myDefaultRemoteName = defaultRemoteName;
    myCloudName = cloudName;

    myRepositoryRootFile = repositoryRoot;

    VirtualFile contentRoot = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(myRepositoryRootFile);
    LOG.assertTrue(contentRoot != null, "Repository root is not found");
    myContentRoot = contentRoot;

    Project project = getProject();
    myGitRepositoryManager = GitUtil.getRepositoryManager(project);
    myGit = ServiceManager.getService(Git.class);
    if (myGit == null) {
      throw new ServerRuntimeException("Can't initialize GIT");
    }
    GitPlatformFacade gitPlatformFacade = ServiceManager.getService(GitPlatformFacade.class);
    myChangeListManager = gitPlatformFacade.getChangeListManager(project);
  }

  @Override
  public CloudGitApplication deploy() throws ServerRuntimeException {
    CloudGitApplication application = findOrCreateApplication();
    deployApplication(application);
    return application;
  }

  private void deployApplication(CloudGitApplication application) throws ServerRuntimeException {
    boolean firstDeploy = findRepository() == null;

    GitRepository repository = findOrCreateRepository();
    addOrResetGitRemote(application, repository);

    if (firstDeploy) {
      add();
      commit();
      return;
    }

    final LocalChangeList activeChangeList = myChangeListManager.getDefaultChangeList();
    if (activeChangeList == null) {
      add();
      commit();
      return;
    }

    Collection<Change> changes = activeChangeList.getChanges();
    final List<Change> relevantChanges = new ArrayList<Change>();
    for (Change change : changes) {
      if (isRelevant(change.getBeforeRevision()) || isRelevant(change.getAfterRevision())) {
        relevantChanges.add(change);
      }
    }

    final Semaphore commitSemaphore = new Semaphore();
    commitSemaphore.down();

    final Ref<Boolean> commitSucceeded = new Ref<Boolean>(false);
    Boolean commitStarted = runOnEdt(new Computable<Boolean>() {

      @Override
      public Boolean compute() {
        return CommitChangeListDialog.commitChanges(getProject(),
                                                    relevantChanges,
                                                    activeChangeList,
                                                    ourCommitExecutors,
                                                    false,
                                                    COMMIT_MESSAGE,
                                                    new CommitResultHandler() {

                                                      @Override
                                                      public void onSuccess(@NotNull String commitMessage) {
                                                        commitSucceeded.set(true);
                                                        commitSemaphore.up();
                                                      }

                                                      @Override
                                                      public void onFailure() {
                                                        commitSemaphore.up();
                                                      }
                                                    },
                                                    false);
      }
    });
    if (commitStarted != null && commitStarted) {
      commitSemaphore.waitFor();
      if (!commitSucceeded.get()) {
        repository.update();
        throw new ServerRuntimeException("Commit failed");
      }
    }
    else {
      throw new ServerRuntimeException("Deploy interrupted");
    }

    repository.update();
    pushApplication(application);
  }

  private boolean isRelevant(ContentRevision contentRevision) throws ServerRuntimeException {
    if (contentRevision == null) {
      return false;
    }
    GitRepository repository = getRepository();
    VirtualFile affectedFile = contentRevision.getFile().getVirtualFile();
    return affectedFile != null && VfsUtilCore.isAncestor(repository.getRoot(), affectedFile, false);
  }

  private static <T> T runOnEdt(final Computable<T> computable) {
    final Ref<T> result = new Ref<T>();
    ApplicationManager.getApplication().invokeAndWait(new Runnable() {

      @Override
      public void run() {
        result.set(computable.compute());
      }
    }, ModalityState.any());
    return result.get();
  }

  public boolean isDeployed() throws ServerRuntimeException {
    return findApplication() != null;
  }

  public CloudGitApplication findOrCreateApplication() throws ServerRuntimeException {
    CloudGitApplication application = findApplication();
    if (application == null) {
      application = createApplication();
    }
    return application;
  }

  public void addOrResetGitRemote(CloudGitApplication application, GitRepository repository) throws ServerRuntimeException {
    String gitUrl = application.getGitUrl();
    if (myRemoteName == null) {
      for (GitRemote gitRemote : repository.getRemotes()) {
        if (gitRemote.getUrls().contains(gitUrl)) {
          myRemoteName = gitRemote.getName();
          return;
        }
      }
    }
    GitRemote gitRemote = GitUtil.findRemoteByName(repository, getRemoteName());
    if (gitRemote == null) {
      addGitRemote(application);
    }
    else if (!gitRemote.getUrls().contains(gitUrl)) {
      resetGitRemote(application);
    }
  }

  public GitRepository findOrCreateRepository() throws ServerRuntimeException {
    GitRepository repository = findRepository();
    if (repository == null) {
      getLoggingHandler().println("Initializing git repository...");
      GitCommandResult gitInitResult = getGit().init(getProject(), getRepositoryRoot(), createGitLineHandlerListener());
      checkGitResult(gitInitResult);

      refreshApplicationRepository();

      repository = getRepository();
    }
    return repository;
  }

  public void downloadExistingApplication() throws ServerRuntimeException {
    new CloneJobWithRemote().cloneToModule(getApplication().getGitUrl());
    getRepository().update();
    refreshContentRoot();
  }

  protected Git getGit() {
    return myGit;
  }

  protected VirtualFile getRepositoryRoot() {
    return myContentRoot;
  }

  protected File getRepositoryRootFile() {
    return myRepositoryRootFile;
  }

  protected static void checkGitResult(GitCommandResult commandResult) throws ServerRuntimeException {
    if (!commandResult.success()) {
      Throwable exception = commandResult.getException();
      if (exception != null) {
        LOG.info(exception);
        throw new ServerRuntimeException(exception);
      }
      else {
        throw new ServerRuntimeException(commandResult.getErrorOutputAsJoinedString());
      }
    }
  }

  protected GitLineHandlerListener createGitLineHandlerListener() {
    return new GitLineHandlerAdapter() {

      @Override
      public void onLineAvailable(String line, Key outputType) {
        getLoggingHandler().println(line);
      }
    };
  }

  @Override
  protected CloudLoggingHandler getLoggingHandler() {
    return super.getLoggingHandler();
  }

  protected void addGitRemote(CloudGitApplication application) throws ServerRuntimeException {
    doGitRemote(getRemoteName(), application, "add", CloudBundle.getText("failed.add.remote", getRemoteName()));
  }

  protected void resetGitRemote(CloudGitApplication application) throws ServerRuntimeException {
    doGitRemote(getRemoteName(), application, "set-url", CloudBundle.getText("failed.reset.remote", getRemoteName()));
  }

  protected void doGitRemote(String remoteName,
                             CloudGitApplication application,
                             String subCommand,
                             String failMessage)
    throws ServerRuntimeException {
    try {
      final GitSimpleHandler handler = new GitSimpleHandler(getProject(), myContentRoot, GitCommand.REMOTE);
      handler.setSilent(false);
      handler.addParameters(subCommand, remoteName, application.getGitUrl());
      handler.run();
      getRepository().update();
      if (handler.getExitCode() != 0) {
        throw new ServerRuntimeException(failMessage);
      }
    }
    catch (VcsException e) {
      throw new ServerRuntimeException(e);
    }
  }

  @Nullable
  protected GitRepository findRepository() {
    if (myRepository != null) {
      return myRepository;
    }
    myRepository = myGitRepositoryManager.getRepositoryForRoot(myContentRoot);
    return myRepository;
  }

  protected void refreshApplicationRepository() {
    GitInit.refreshAndConfigureVcsMappings(getProject(), getRepositoryRoot(), getRepositoryRootFile().getAbsolutePath());
  }

  protected void pushApplication(@NotNull CloudGitApplication application) throws ServerRuntimeException {
    push(application, getRepository(), getRemoteName());
  }

  protected void push(@NotNull CloudGitApplication application, @NotNull GitRepository repository, @NotNull String remote)
    throws ServerRuntimeException {
    GitCommandResult gitPushResult
      = getGit().push(repository, remote, application.getGitUrl(), "master:master", createGitLineHandlerListener());
    checkGitResult(gitPushResult);
  }

  @NotNull
  protected GitRepository getRepository() throws ServerRuntimeException {
    GitRepository repository = findRepository();
    if (repository == null) {
      throw new ServerRuntimeException("Unable to find GIT repository for module root: " + myContentRoot);
    }
    return repository;
  }

  protected void fetch() throws ServerRuntimeException {
    final VirtualFile contentRoot = getRepositoryRoot();
    GitRepository repository = getRepository();
    final GitLineHandler fetchHandler = new GitLineHandler(getProject(), contentRoot, GitCommand.FETCH);
    fetchHandler.setUrl(getApplication().getGitUrl());
    fetchHandler.setSilent(false);
    fetchHandler.addParameters(getRemoteName());
    fetchHandler.addLineListener(createGitLineHandlerListener());
    performRemoteGitTask(fetchHandler, CloudBundle.getText("fetching.application", getCloudName()));

    repository.update();
  }

  protected void add() throws ServerRuntimeException {
    try {
      GitFileUtils.addFiles(getProject(), myContentRoot, myContentRoot);
    }
    catch (VcsException e) {
      throw new ServerRuntimeException(e);
    }
  }

  protected void commit() throws ServerRuntimeException {
    try {
      if (GitUtil.hasLocalChanges(true, getProject(), myContentRoot)) {
        GitSimpleHandler handler = new GitSimpleHandler(getProject(), myContentRoot, GitCommand.COMMIT);
        handler.setSilent(false);
        handler.setStdoutSuppressed(false);
        handler.addParameters("-m", COMMIT_MESSAGE);
        handler.endOptions();
        handler.run();
      }
    }
    catch (VcsException e) {
      throw new ServerRuntimeException(e);
    }
  }

  protected void performRemoteGitTask(final GitLineHandler handler, String title) throws ServerRuntimeException {
    final GitTask task = new GitTask(getProject(), handler, title);
    task.setProgressAnalyzer(new GitStandardProgressAnalyzer());

    final Semaphore semaphore = new Semaphore();
    semaphore.down();

    final Ref<ServerRuntimeException> errorRef = new Ref<ServerRuntimeException>();

    ApplicationManager.getApplication().invokeLater(new Runnable() {

      @Override
      public void run() {
        task.execute(false, false, new GitTaskResultHandlerAdapter() {

          @Override
          protected void run(GitTaskResult result) {
            super.run(result);
            semaphore.up();
          }

          @Override
          protected void onFailure() {
            for (VcsException error : handler.errors()) {
              getLoggingHandler().println(error.toString());
              if (errorRef.isNull()) {
                errorRef.set(new ServerRuntimeException(error));
              }
            }
          }
        });
      }
    });

    semaphore.waitFor();
    if (!errorRef.isNull()) {
      throw errorRef.get();
    }
  }

  protected void refreshContentRoot() {
    ApplicationManager.getApplication().invokeLater(new Runnable() {

      @Override
      public void run() {
        getRepositoryRoot().refresh(false, true);
      }
    });
  }

  public void fetchAndRefresh() throws ServerRuntimeException {
    fetch();
    refreshContentRoot();
  }

  private String getRemoteName() {
    if (myRemoteName == null) {
      myRemoteName = myDefaultRemoteName;
    }
    return myRemoteName;
  }

  private String getCloudName() {
    return myCloudName;
  }

  protected CloudGitApplication findApplication() throws ServerRuntimeException {
    return getAgentTaskExecutor().execute(new Computable<CloudGitApplication>() {

      @Override
      public CloudGitApplication compute() {
        return getDeployment().findApplication();
      }
    });
  }

  protected CloudGitApplication getApplication() throws ServerRuntimeException {
    CloudGitApplication application = findApplication();
    if (application == null) {
      throw new ServerRuntimeException("Can't find the application: " + getApplicationName());
    }
    return application;
  }

  protected CloudGitApplication createApplication() throws ServerRuntimeException {
    return getAgentTaskExecutor().execute(new Computable<CloudGitApplication>() {

      @Override
      public CloudGitApplication compute() {
        return getDeployment().createApplication();
      }
    });
  }

  public CloudGitApplication findApplication4Repository() throws ServerRuntimeException {
    final List<String> repositoryUrls = new ArrayList<String>();
    for (GitRemote remote : getRepository().getRemotes()) {
      for (String url : remote.getUrls()) {
        repositoryUrls.add(url);
      }
    }

    return getAgentTaskExecutor().execute(new Computable<CloudGitApplication>() {

      @Override
      public CloudGitApplication compute() {
        return getDeployment().findApplication4Repository(ArrayUtil.toStringArray(repositoryUrls));
      }
    });
  }

  public class CloneJob {

    public File cloneToTemp(String gitUrl) throws ServerRuntimeException {
      File cloneDir;
      try {
        cloneDir = FileUtil.createTempDirectory("cloud", "clone");
      }
      catch (IOException e) {
        throw new ServerRuntimeException(e);
      }

      File cloneDirParent = cloneDir.getParentFile();
      String cloneDirName = cloneDir.getName();

      doClone(cloneDirParent, cloneDirName, gitUrl);
      return cloneDir;
    }

    public void cloneToModule(String gitUrl) throws ServerRuntimeException {
      File cloneDir = cloneToTemp(gitUrl);

      try {
        FileUtil.copyDir(cloneDir, getRepositoryRootFile());
      }
      catch (IOException e) {
        throw new ServerRuntimeException(e);
      }

      refreshApplicationRepository();
    }

    public void doClone(File cloneDirParent, String cloneDirName, String gitUrl) throws ServerRuntimeException {
      GitCommandResult gitCloneResult
        = getGit().clone(getProject(), cloneDirParent, gitUrl, cloneDirName, createGitLineHandlerListener());
      checkGitResult(gitCloneResult);
    }
  }

  public class CloneJobWithRemote extends CloneJob {

    public void doClone(File cloneDirParent, String cloneDirName, String gitUrl) throws ServerRuntimeException {
      final GitLineHandler handler = new GitLineHandler(getProject(), cloneDirParent, GitCommand.CLONE);
      handler.setSilent(false);
      handler.setStdoutSuppressed(false);
      handler.setUrl(gitUrl);
      handler.addParameters("--progress");
      handler.addParameters(gitUrl);
      handler.addParameters(cloneDirName);
      handler.addParameters("-o");
      handler.addParameters(getRemoteName());
      handler.addLineListener(createGitLineHandlerListener());
      performRemoteGitTask(handler, CloudBundle.getText("cloning.existing.application", getCloudName()));
    }
  }
}