<commit_before>// Copyright 2008-2010 <NAME>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under
// the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions and
// limitations under the License.
package org.zmlx.hg4idea.command;

import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.messages.MessageBus;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.zmlx.hg4idea.HgFile;
import org.zmlx.hg4idea.HgVcs;
import org.zmlx.hg4idea.HgVcsMessages;
import org.zmlx.hg4idea.execution.HgCommandException;
import org.zmlx.hg4idea.execution.HgCommandExecutor;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import static org.zmlx.hg4idea.HgErrorHandler.ensureSuccess;

public class HgCommitCommand {

  private static final Logger LOG = Logger.getInstance(HgCommitCommand.class.getName());

  private static final String TEMP_FILE_NAME = ".hg4idea-commit.tmp";

  private final Project project;
  private final VirtualFile repo;
  private final String message;

  private Set<HgFile> files = Collections.emptySet();

  public HgCommitCommand(Project project, @NotNull VirtualFile repo, String message) {
    this.project = project;
    this.repo = repo;
    this.message = message;
  }

  public void setFiles(@NotNull Set<HgFile> files) {
    this.files = files;
  }

  public void execute() throws HgCommandException, VcsException {
    if (StringUtils.isBlank(message)) {
      throw new HgCommandException(HgVcsMessages.message("hg4idea.commit.error.messageEmpty"));
    }
    try {
      List<String> parameters = new LinkedList<String>();
      parameters.add("--logfile");
      parameters.add(saveCommitMessage().getAbsolutePath());
      for (HgFile hgFile : files) {
        parameters.add(hgFile.getRelativePath());
      }
      ensureSuccess(new HgCommandExecutor(project).executeInCurrentThread(repo, "commit", parameters));
      final MessageBus messageBus = project.getMessageBus();
      messageBus.syncPublisher(HgVcs.REMOTE_TOPIC).update(project);
      messageBus.syncPublisher(HgVcs.BRANCH_TOPIC).update(project);
    } catch (IOException e) {
      LOG.info(e);
    }
  }

  private File saveCommitMessage() throws IOException {
    File systemDir = new File(PathManager.getSystemPath());
    File tempFile = new File(systemDir, TEMP_FILE_NAME);
    Writer output = new BufferedWriter(new FileWriter(tempFile, false));
    try {
      output.write(message);
    } finally {
      output.close();
    }
    return tempFile;
  }

}<commit_msg>IDEA-72302 Mercurial: commit in UTF-8<commit_after>// Copyright 2008-2010 <NAME>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under
// the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions and
// limitations under the License.
package org.zmlx.hg4idea.command;

import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vfs.CharsetToolkit;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.messages.MessageBus;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.zmlx.hg4idea.HgFile;
import org.zmlx.hg4idea.HgVcs;
import org.zmlx.hg4idea.HgVcsMessages;
import org.zmlx.hg4idea.execution.HgCommandException;
import org.zmlx.hg4idea.execution.HgCommandExecutor;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import static org.zmlx.hg4idea.HgErrorHandler.ensureSuccess;

public class HgCommitCommand {

  private static final String TEMP_FILE_NAME = ".hg4idea-commit.tmp";

  private final Project myProject;
  private final VirtualFile myRoot;
  private final String myMessage;

  private Set<HgFile> myFiles = Collections.emptySet();

  public HgCommitCommand(Project project, @NotNull VirtualFile root, String message) {
    myProject = project;
    myRoot = root;
    myMessage = message;
  }

  public void setFiles(@NotNull Set<HgFile> files) {
    myFiles = files;
  }

  public void execute() throws HgCommandException, VcsException {
    if (StringUtils.isBlank(myMessage)) {
      throw new HgCommandException(HgVcsMessages.message("hg4idea.commit.error.messageEmpty"));
    }
    List<String> parameters = new LinkedList<String>();
    parameters.add("--logfile");
    parameters.add(saveCommitMessage().getAbsolutePath());
    for (HgFile hgFile : myFiles) {
      parameters.add(hgFile.getRelativePath());
    }
    parameters.add("--encoding");
    parameters.add(CharsetToolkit.UTF8);

    ensureSuccess(new HgCommandExecutor(myProject).executeInCurrentThread(myRoot, "commit", parameters));
    final MessageBus messageBus = myProject.getMessageBus();
    messageBus.syncPublisher(HgVcs.REMOTE_TOPIC).update(myProject);
    messageBus.syncPublisher(HgVcs.BRANCH_TOPIC).update(myProject);
  }

  private File saveCommitMessage() throws VcsException {
    File systemDir = new File(PathManager.getSystemPath());
    File tempFile = new File(systemDir, TEMP_FILE_NAME);
    
    try {
      FileUtil.writeToFile(tempFile, myMessage);
    } catch (IOException e) {
      throw new VcsException("Couldn't prepare commit message", e);
    }
    return tempFile;
  }

}
{'fraction_non_alphanumeric': 0.10389809699201964, 'fraction_numerical': 0.007519950890116636, 'mean_word_length': 4.3904052936311, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3109148', 'n_tokens_mistral': 2031, 'n_tokens_neox': 1930, 'n_words': 486}