<commit_before><?php

/*
 * Copyright 2012 Facebook, Inc.
 *
 * 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.
 */

final class PhabricatorRepositoryGitCommitChangeParserWorker
  extends PhabricatorRepositoryCommitChangeParserWorker {

  protected function parseCommit(
    PhabricatorRepository $repository,
    PhabricatorRepositoryCommit $commit) {

    $full_name = 'r'.$repository->getCallsign().$commit->getCommitIdentifier();
    echo "Parsing {$full_name}...\n";
    if ($this->isBadCommit($full_name)) {
      echo "This commit is marked bad!\n";
      return;
    }

    // NOTE: "--pretty=format: " is to disable log output, we only want the
    // part we get from "--raw".
    list($raw) = $repository->execxLocalCommand(
      'log -n1 -M -C -B --find-copies-harder --raw -t '.
        '--abbrev=40 --pretty=format: %s',
      $commit->getCommitIdentifier());

    $changes = array();
    $move_away = array();
    $copy_away = array();
    $lines = explode("\n", $raw);
    foreach ($lines as $line) {
      if (!strlen(trim($line))) {
        continue;
      }
      list($old_mode, $new_mode,
           $old_hash, $new_hash,
           $more_stuff) = preg_split('/ +/', $line);

      // We may only have two pieces here.
      list($action, $src_path, $dst_path) = array_merge(
        explode("\t", $more_stuff),
        array(null));

      // Normalize the paths for consistency with the SVN workflow.
      $src_path = '/'.$src_path;
      if ($dst_path) {
        $dst_path = '/'.$dst_path;
      }

      $old_mode = intval($old_mode, 8);
      $new_mode = intval($new_mode, 8);

      switch ($new_mode & 0160000) {
        case 0160000:
          $file_type = DifferentialChangeType::FILE_SUBMODULE;<commit_msg>Detect changes in merge commits as the diff of the merge and the first parent<commit_after><?php

/*
 * Copyright 2012 Facebook, Inc.
 *
 * 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.
 */

final class PhabricatorRepositoryGitCommitChangeParserWorker
  extends PhabricatorRepositoryCommitChangeParserWorker {

  protected function parseCommit(
    PhabricatorRepository $repository,
    PhabricatorRepositoryCommit $commit) {

    $full_name = 'r'.$repository->getCallsign().$commit->getCommitIdentifier();
    echo "Parsing {$full_name}...\n";
    if ($this->isBadCommit($full_name)) {
      echo "This commit is marked bad!\n";
      return;
    }

    // Check if the commit has parents. We're testing to see whether it is the
    // first commit in history (in which case we must use "git log") or some
    // other commit (in which case we can use "git diff"). We'd rather use
    // "git diff" because it has the right behavior for merge commits, but
    // it requires the commit to have a parent that we can diff against. The
    // first commit doesn't, so "commit^" is not a valid ref.
    list($parents) = $repository->execxLocalCommand(
      'log -n1 --format=%s %s',
      '%P',
      $commit->getCommitIdentifier());

    $use_log = !strlen(trim($parents));
    if ($use_log) {
      // This is the first commit so we need to use "log". We know it's not a
      // merge commit because it couldn't be merging anything, so this is safe.

      // NOTE: "--pretty=format: " is to disable diff output, we only want the
      // part we get from "--raw".
      list($raw) = $repository->execxLocalCommand(
        'log -n1 -M -C -B --find-copies-harder --raw -t '.
          '--pretty=format: --abbrev=40 %s',
        $commit->getCommitIdentifier());
    } else {
      // Otherwise, we can use "diff", which will give us output for merges.
      // We diff against the first parent, as this is generally the expectation
      // and results in sensible behavior.
      list($raw) = $repository->execxLocalCommand(
        'diff -n1 -M -C -B --find-copies-harder --raw -t '.
          '--abbrev=40 %s^1 %s',
        $commit->getCommitIdentifier(),
        $commit->getCommitIdentifier());
    }

    $changes = array();
    $move_away = array();
    $copy_away = array();
    $lines = explode("\n", $raw);
    foreach ($lines as $line) {
      if (!strlen(trim($line))) {
        continue;
      }
      list($old_mode, $new_mode,
           $old_hash, $new_hash,
           $more_stuff) = preg_split('/ +/', $line);

      // We may only have two pieces here.
      list($action, $src_path, $dst_path) = array_merge(
        explode("\t", $more_stuff),
        array(null));

      // Normalize the paths for consistency with the SVN workflow.
      $src_path = '/'.$src_path;
      if ($dst_path) {
        $dst_path = '/'.$dst_path;
      }

      $old_mode = intval($old_mode, 8);
      $new_mode = intval($new_mode, 8);

      switch ($new_mode & 0160000) {
        case 0160000:
          $file_type = DifferentialChangeType::FILE_SUBMODULE;
{'fraction_non_alphanumeric': 0.12543073742246727, 'fraction_numerical': 0.01016540317022743, 'mean_word_length': 2.75, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '265561', 'n_tokens_mistral': 1757, 'n_tokens_neox': 1705, 'n_words': 583}