Ejemplo n.º 1
0
 /**
  * {@code git checkout &lt;reference&gt;} <br>
  * {@code git checkout -b &lt;newBranch&gt; &lt;reference&gt;}
  */
 @NotNull
 @Override
 public GitCommandResult checkout(
     @NotNull GitRepository repository,
     @NotNull String reference,
     @Nullable String newBranch,
     boolean force,
     @NotNull GitLineHandlerListener... listeners) {
   final GitLineHandler h =
       new GitLineHandler(repository.getProject(), repository.getRoot(), GitCommand.CHECKOUT);
   h.setSilent(false);
   h.setStdoutSuppressed(false);
   if (force) {
     h.addParameters("--force");
   }
   if (newBranch == null) { // simply checkout
     h.addParameters(reference);
   } else { // checkout reference as new branch
     h.addParameters("-b", newBranch, reference);
   }
   for (GitLineHandlerListener listener : listeners) {
     h.addLineListener(listener);
   }
   return run(h);
 }
Ejemplo n.º 2
0
 /** Create branch without checking it out. {@code git branch <branchName>} */
 @Override
 @NotNull
 public GitCommandResult branchCreate(
     @NotNull GitRepository repository, @NotNull String branchName) {
   final GitLineHandler h =
       new GitLineHandler(repository.getProject(), repository.getRoot(), GitCommand.BRANCH);
   h.setStdoutSuppressed(false);
   h.addParameters(branchName);
   return run(h);
 }
Ejemplo n.º 3
0
 @NotNull
 @Override
 public GitCommandResult diff(
     @NotNull GitRepository repository, @NotNull List<String> parameters, @NotNull String range) {
   final GitLineHandler diff =
       new GitLineHandler(repository.getProject(), repository.getRoot(), GitCommand.DIFF);
   diff.addParameters(parameters);
   diff.addParameters(range);
   diff.setStdoutSuppressed(true);
   diff.setStderrSuppressed(true);
   diff.setSilent(true);
   return run(diff);
 }
Ejemplo n.º 4
0
 /** Calls 'git init' on the specified directory. */
 @NotNull
 @Override
 public GitCommandResult init(
     @NotNull Project project,
     @NotNull VirtualFile root,
     @NotNull GitLineHandlerListener... listeners) {
   GitLineHandler h = new GitLineHandler(project, root, GitCommand.INIT);
   for (GitLineHandlerListener listener : listeners) {
     h.addLineListener(listener);
   }
   h.setSilent(false);
   h.setStdoutSuppressed(false);
   return run(h);
 }
 private static GitLineHandler getLogHandler(
     Project project,
     VirtualFile root,
     GitLogParser parser,
     FilePath path,
     String lastCommit,
     String... parameters) {
   final GitLineHandler h = new GitLineHandler(project, root, GitCommand.LOG);
   h.setNoSSH(true);
   h.setStdoutSuppressed(true);
   h.addParameters("--name-status", parser.getPretty(), "--encoding=UTF-8", lastCommit);
   if (parameters != null && parameters.length > 0) {
     h.addParameters(parameters);
   }
   h.endOptions();
   h.addRelativePaths(path);
   return h;
 }
Ejemplo n.º 6
0
 /** {@code git branch -d <reference>} or {@code git branch -D <reference>} */
 @NotNull
 @Override
 public GitCommandResult branchDelete(
     @NotNull GitRepository repository,
     @NotNull String branchName,
     boolean force,
     @NotNull GitLineHandlerListener... listeners) {
   final GitLineHandler h =
       new GitLineHandler(repository.getProject(), repository.getRoot(), GitCommand.BRANCH);
   h.setSilent(false);
   h.setStdoutSuppressed(false);
   h.addParameters(force ? "-D" : "-d");
   h.addParameters(branchName);
   for (GitLineHandlerListener listener : listeners) {
     h.addLineListener(listener);
   }
   return run(h);
 }
Ejemplo n.º 7
0
 @Override
 @NotNull
 public GitCommandResult cherryPick(
     @NotNull GitRepository repository,
     @NotNull String hash,
     boolean autoCommit,
     @NotNull GitLineHandlerListener... listeners) {
   final GitLineHandler handler =
       new GitLineHandler(repository.getProject(), repository.getRoot(), GitCommand.CHERRY_PICK);
   handler.addParameters("-x");
   if (!autoCommit) {
     handler.addParameters("-n");
   }
   handler.addParameters(hash);
   addListeners(handler, listeners);
   handler.setSilent(false);
   handler.setStdoutSuppressed(false);
   return run(handler);
 }
Ejemplo n.º 8
0
 /** {@code git checkout -b &lt;branchName&gt;} */
 @NotNull
 @Override
 public GitCommandResult checkoutNewBranch(
     @NotNull GitRepository repository,
     @NotNull String branchName,
     @Nullable GitLineHandlerListener listener) {
   final GitLineHandler h =
       new GitLineHandler(
           repository.getProject(),
           repository.getRoot(),
           GitCommand.CHECKOUT.readLockingCommand());
   h.setSilent(false);
   h.setStdoutSuppressed(false);
   h.addParameters("-b");
   h.addParameters(branchName);
   if (listener != null) {
     h.addLineListener(listener);
   }
   return run(h);
 }
  public static void historyWithLinks(
      final Project project,
      FilePath path,
      @Nullable final SymbolicRefsI refs,
      @NotNull final AsynchConsumer<GitCommit> gitCommitConsumer,
      @Nullable final Getter<Boolean> isCanceled,
      @Nullable Collection<VirtualFile> paths,
      final String... parameters)
      throws VcsException {
    // adjust path using change manager
    path = getLastCommitName(project, path);
    final VirtualFile root = GitUtil.getGitRoot(path);
    final GitLineHandler h = new GitLineHandler(project, root, GitCommand.LOG);
    final GitLogParser parser =
        new GitLogParser(
            project,
            GitLogParser.NameStatus.STATUS,
            SHORT_HASH,
            HASH,
            COMMIT_TIME,
            AUTHOR_NAME,
            AUTHOR_TIME,
            AUTHOR_EMAIL,
            COMMITTER_NAME,
            COMMITTER_EMAIL,
            SHORT_PARENTS,
            REF_NAMES,
            SUBJECT,
            BODY,
            RAW_BODY);
    h.setNoSSH(true);
    h.setStdoutSuppressed(true);
    h.addParameters(parameters);
    h.addParameters("--name-status", parser.getPretty(), "--encoding=UTF-8", "--full-history");
    if (paths != null && !paths.isEmpty()) {
      h.endOptions();
      h.addRelativeFiles(paths);
    } else {
      h.addParameters("--sparse");
      h.endOptions();
      h.addRelativePaths(path);
    }

    final VcsException[] exc = new VcsException[1];
    final Semaphore semaphore = new Semaphore();
    final StringBuilder sb = new StringBuilder();
    final Ref<Boolean> skipFirst = new Ref<Boolean>(true);
    h.addLineListener(
        new GitLineHandlerAdapter() {
          @Override
          public void onLineAvailable(final String line, final Key outputType) {
            try {
              if (ProcessOutputTypes.STDOUT.equals(outputType)) {
                if (isCanceled != null && isCanceled.get()) {
                  h.cancel();
                  return;
                }
                // if (line.charAt(line.length() - 1) != '\u0003') {
                if ((!line.startsWith("\u0001")) || skipFirst.get()) {
                  if (sb.length() > 0) {
                    sb.append("\n");
                  }
                  sb.append(line);
                  skipFirst.set(false);
                  return;
                }
                takeLine(project, line, sb, parser, refs, root, exc, h, gitCommitConsumer);
              }
            } catch (ProcessCanceledException e) {
              h.cancel();
              semaphore.up();
            }
          }

          @Override
          public void processTerminated(int exitCode) {
            semaphore.up();
          }

          @Override
          public void startFailed(Throwable exception) {
            semaphore.up();
          }
        });
    semaphore.down();
    h.start();
    semaphore.waitFor();
    takeLine(project, "", sb, parser, refs, root, exc, h, gitCommitConsumer);
    gitCommitConsumer.finished();
    if (exc[0] != null) {
      throw exc[0];
    }
  }
  public static void hashesWithParents(
      Project project,
      FilePath path,
      final AsynchConsumer<CommitHashPlusParents> consumer,
      final Getter<Boolean> isCanceled,
      Collection<VirtualFile> paths,
      final String... parameters)
      throws VcsException {
    // adjust path using change manager
    path = getLastCommitName(project, path);
    final VirtualFile root = GitUtil.getGitRoot(path);
    final GitLineHandler h = new GitLineHandler(project, root, GitCommand.LOG);
    final GitLogParser parser =
        new GitLogParser(
            project,
            GitLogParser.NameStatus.NAME,
            SHORT_HASH,
            COMMIT_TIME,
            SHORT_PARENTS,
            AUTHOR_NAME);
    h.setNoSSH(true);
    h.setStdoutSuppressed(true);
    h.addParameters(parameters);
    h.addParameters(parser.getPretty(), "--encoding=UTF-8", "--full-history");

    if (paths != null && !paths.isEmpty()) {
      h.endOptions();
      h.addRelativeFiles(paths);
    } else {
      h.addParameters("--sparse");
      h.endOptions();
      h.addRelativePaths(path);
    }

    final Semaphore semaphore = new Semaphore();
    h.addLineListener(
        new GitLineHandlerListener() {
          @Override
          public void onLineAvailable(final String line, final Key outputType) {
            try {
              if (ProcessOutputTypes.STDOUT.equals(outputType)) {
                if (isCanceled != null && isCanceled.get()) {
                  h.cancel();
                  return;
                }
                GitLogRecord record = parser.parseOneRecord(line);
                consumer.consume(
                    new CommitHashPlusParents(
                        record.getShortHash(),
                        record.getParentsShortHashes(),
                        record.getLongTimeStamp() * 1000,
                        record.getAuthorName()));
              }
            } catch (ProcessCanceledException e) {
              h.cancel();
              semaphore.up();
            }
          }

          @Override
          public void processTerminated(int exitCode) {
            semaphore.up();
          }

          @Override
          public void startFailed(Throwable exception) {
            semaphore.up();
          }
        });
    semaphore.down();
    h.start();
    semaphore.waitFor();
    consumer.finished();
  }