/** Adds {@code stash@{x}} parameter to the handler, quotes it if needed. */
 private void addStashParameter(@NotNull GitHandler handler, @NotNull String stash) {
   if (GitVersionSpecialty.NEEDS_QUOTES_IN_STASH_NAME.existsIn(myVcs.getVersion())) {
     handler.addParameters("\"" + stash + "\"");
     handler.dontEscapeQuotes();
   } else {
     handler.addParameters(stash);
   }
 }
예제 #2
0
  private GitFetchResult fetchNatively(
      @NotNull VirtualFile root, @NotNull GitRemote remote, @Nullable String branch) {
    final GitLineHandlerPasswordRequestAware h =
        new GitLineHandlerPasswordRequestAware(myProject, root, GitCommand.FETCH);
    h.addProgressParameter();
    if (GitVersionSpecialty.SUPPORTS_FETCH_PRUNE.existsIn(myVcs.getVersion())) {
      h.addParameters("--prune");
    }

    String remoteName = remote.getName();
    h.addParameters(remoteName);
    if (branch != null) {
      h.addParameters(getFetchSpecForBranch(branch, remoteName));
    }

    final GitTask fetchTask = new GitTask(myProject, h, "Fetching " + remote.getFirstUrl());
    fetchTask.setProgressIndicator(myProgressIndicator);
    fetchTask.setProgressAnalyzer(new GitStandardProgressAnalyzer());

    GitFetchPruneDetector pruneDetector = new GitFetchPruneDetector();
    h.addLineListener(pruneDetector);

    final AtomicReference<GitFetchResult> result = new AtomicReference<GitFetchResult>();
    fetchTask.execute(
        true,
        false,
        new GitTaskResultHandlerAdapter() {
          @Override
          protected void onSuccess() {
            result.set(GitFetchResult.success());
          }

          @Override
          protected void onCancel() {
            LOG.info("Cancelled fetch.");
            result.set(GitFetchResult.cancel());
          }

          @Override
          protected void onFailure() {
            LOG.info("Error fetching: " + h.errors());
            if (!h.hadAuthRequest()) {
              myErrors.addAll(h.errors());
            } else {
              myErrors.add(new VcsException("Authentication failed"));
            }
            result.set(GitFetchResult.error(myErrors));
          }
        });

    result.get().addPruneInfo(pruneDetector.getPrunedRefs());
    return result.get();
  }
 @NotNull
 @Override
 public List<? extends VcsFullCommitDetails> readFullDetails(
     @NotNull VirtualFile root, @NotNull List<String> hashes) throws VcsException {
   String noWalk =
       GitVersionSpecialty.NO_WALK_UNSORTED.existsIn(myVcs.getVersion())
           ? "--no-walk=unsorted"
           : "--no-walk";
   List<String> params = new ArrayList<String>();
   params.add(noWalk);
   params.addAll(hashes);
   return GitHistoryUtils.history(myProject, root, ArrayUtil.toStringArray(params));
 }
 @Nullable
 @Override
 protected String getLastCommitMessage(@NotNull VirtualFile root) throws VcsException {
   GitSimpleHandler h = new GitSimpleHandler(myProject, root, GitCommand.LOG);
   h.addParameters("--max-count=1");
   String formatPattern;
   if (GitVersionSpecialty.STARTED_USING_RAW_BODY_IN_FORMAT.existsIn(myVcs.getVersion())) {
     formatPattern = "%B";
   } else {
     // only message: subject + body; "%-b" means that preceding line-feeds will be deleted if
     // the body is empty
     // %s strips newlines from subject; there is no way to work around it before 1.7.2 with %B
     // (unless parsing some fixed format)
     formatPattern = "%s%n%n%-b";
   }
   h.addParameters("--pretty=format:" + formatPattern);
   return h.run();
 }
    private ReturnResult checkUserName() {
      Project project = myPanel.getProject();
      GitVcs vcs = GitVcs.getInstance(project);
      assert vcs != null;

      Collection<VirtualFile> notDefined = new ArrayList<VirtualFile>();
      Map<VirtualFile, Pair<String, String>> defined =
          new HashMap<VirtualFile, Pair<String, String>>();
      Collection<VirtualFile> allRoots =
          new ArrayList<VirtualFile>(
              Arrays.asList(ProjectLevelVcsManager.getInstance(project).getRootsUnderVcs(vcs)));

      Collection<VirtualFile> affectedRoots = getSelectedRoots();
      for (VirtualFile root : affectedRoots) {
        try {
          Pair<String, String> nameAndEmail = getUserNameAndEmailFromGitConfig(project, root);
          String name = nameAndEmail.getFirst();
          String email = nameAndEmail.getSecond();
          if (name == null || email == null) {
            notDefined.add(root);
          } else {
            defined.put(root, nameAndEmail);
          }
        } catch (VcsException e) {
          LOG.error("Couldn't get user.name and user.email for root " + root, e);
          // doing nothing - let commit with possibly empty user.name/email
        }
      }

      if (notDefined.isEmpty()) {
        return ReturnResult.COMMIT;
      }

      GitVersion version = vcs.getVersion();
      if (System.getenv("HOME") == null
          && GitVersionSpecialty.DOESNT_DEFINE_HOME_ENV_VAR.existsIn(version)) {
        Messages.showErrorDialog(
            project,
            "You are using Git "
                + version
                + " which doesn't define %HOME% environment variable properly.\n"
                + "Consider updating Git to a newer version "
                + "or define %HOME% to point to the place where the global .gitconfig is stored \n"
                + "(it is usually %USERPROFILE% or %HOMEDRIVE%%HOMEPATH%).",
            "HOME Variable Is Not Defined");
        return ReturnResult.CANCEL;
      }

      if (defined.isEmpty() && allRoots.size() > affectedRoots.size()) {
        allRoots.removeAll(affectedRoots);
        for (VirtualFile root : allRoots) {
          try {
            Pair<String, String> nameAndEmail = getUserNameAndEmailFromGitConfig(project, root);
            String name = nameAndEmail.getFirst();
            String email = nameAndEmail.getSecond();
            if (name != null && email != null) {
              defined.put(root, nameAndEmail);
              break;
            }
          } catch (VcsException e) {
            LOG.error("Couldn't get user.name and user.email for root " + root, e);
            // doing nothing - not critical not to find the values for other roots not affected by
            // commit
          }
        }
      }

      GitUserNameNotDefinedDialog dialog =
          new GitUserNameNotDefinedDialog(project, notDefined, affectedRoots, defined);
      dialog.show();
      if (dialog.isOK()) {
        try {
          if (dialog.isGlobal()) {
            GitConfigUtil.setValue(
                project,
                notDefined.iterator().next(),
                GitConfigUtil.USER_NAME,
                dialog.getUserName(),
                "--global");
            GitConfigUtil.setValue(
                project,
                notDefined.iterator().next(),
                GitConfigUtil.USER_EMAIL,
                dialog.getUserEmail(),
                "--global");
          } else {
            for (VirtualFile root : notDefined) {
              GitConfigUtil.setValue(project, root, GitConfigUtil.USER_NAME, dialog.getUserName());
              GitConfigUtil.setValue(
                  project, root, GitConfigUtil.USER_EMAIL, dialog.getUserEmail());
            }
          }
        } catch (VcsException e) {
          String message = "Couldn't set user.name and user.email";
          LOG.error(message, e);
          Messages.showErrorDialog(myPanel.getComponent(), message);
          return ReturnResult.CANCEL;
        }
        return ReturnResult.COMMIT;
      }
      return ReturnResult.CLOSE_WINDOW;
    }
예제 #6
0
 public static void assumeSupportedGitVersion(@NotNull GitVcs vcs) {
   GitVersion version = vcs.getVersion();
   assumeTrue("Unsupported Git version: " + version, version.isSupported());
 }