<?php

final class DiffusionGitRawDiffQuery extends DiffusionRawDiffQuery {

  protected function newQueryFuture() {
    $drequest = $this->getRequest();
    $repository = $drequest->getRepository();

    $commit = $this->getAnchorCommit();

    $options = array(
      '-M',
      '-C',
      '--no-ext-diff',
      '--no-color',
      '--src-prefix=a/',
      '--dst-prefix=b/',
      '-U'.(int)$this->getLinesOfContext(),
    );

    $against = $this->getAgainstCommit();
    if ($against === null) {
      // Check if this is the root commit by seeing if it has parents, since
      // `git diff X^ X` does not work if "X" is the initial commit.
      list($parents) = $repository->execxLocalCommand(
        'log --format=%s %s --',
        '%P',
        $commit);

      if (strlen(trim($parents))) {
        $against = $commit.'^';
      } else {
        $against = ArcanistGitAPI::GIT_MAGIC_ROOT_COMMIT;
      }
    }

    $path = $drequest->getPath();
    if (!strlen($path)) {
      $path = '.';
    }

    return $repository->getLocalCommandFuture(
      'diff %Ls %s %s -- %s',
      $options,
      $against,
      $commit,
      $path);
  }

}

<?php

final class DiffusionGitRawDiffQuery extends DiffusionRawDiffQuery {

  protected function newQueryFuture() {
    $drequest = $this->getRequest();
    $repository = $drequest->getRepository();

    $commit = $this->getAnchorCommit();

    $options = array(
      '-M',
      '-C',
      '--no-ext-diff',
      '--no-color',
      '--src-prefix=a/',
      '--dst-prefix=b/',
      '-U'.(int)$this->getLinesOfContext(),
    );

    $against = $this->getAgainstCommit();
    if ($against === null) {
      // Check if this is the root commit by seeing if it has parents, since
      // `git diff X^ X` does not work if "X" is the initial commit.
      list($parents) = $repository->execxLocalCommand(
        'log -n 1 --format=%s %s --',
        '%P',
        $commit);

      if (strlen(trim($parents))) {
        $against = $commit.'^';
      } else {
        $against = ArcanistGitAPI::GIT_MAGIC_ROOT_COMMIT;
      }
    }

    $path = $drequest->getPath();
    if (!strlen($path)) {
      $path = '.';
    }

    return $repository->getLocalCommandFuture(
      'diff %Ls %s %s -- %s',
      $options,
      $against,
      $commit,
      $path);
  }

}

Stop doing an excessive amount of work in `diffusion.rawdiffquery`
Stop doing an excessive amount of work in `diffusion.rawdiffquery`

Ref T11665.

Without `-n 1`, this logs the ENTIRE history of the repository. We
actually get the right result, but this is egregiously slow. Add `-n 1`
to return only one result.

It appears that I wrote this wrong way back in 2011, in D953. This
query is rarely used (until recently) which is likely why it has
escaped notice for so long.

Test Plan: Used Conduit console to execute `diffusion.rawdiffquery`.
Got the same results but spent 8ms instead of 200ms executing this
command, in a very small repository.
