Page MenuHomePhorge

No OneTemporary

diff --git a/src/lint/linter/__tests__/ArcanistLinterTestCase.php b/src/lint/linter/__tests__/ArcanistLinterTestCase.php
index c023efd1..d4007d2e 100644
--- a/src/lint/linter/__tests__/ArcanistLinterTestCase.php
+++ b/src/lint/linter/__tests__/ArcanistLinterTestCase.php
@@ -1,266 +1,266 @@
<?php
/**
* Facilitates implementation of test cases for @{class:ArcanistLinter}s.
*/
abstract class ArcanistLinterTestCase extends PhutilTestCase {
/**
* Returns an instance of the linter being tested.
*
* @return ArcanistLinter
*/
final protected function getLinter() {
$matches = null;
if (!preg_match('/^(\w+Linter)TestCase$/', get_class($this), $matches) ||
!is_subclass_of($matches[1], 'ArcanistLinter')) {
throw new Exception(pht('Unable to infer linter class name.'));
}
return newv($matches[1], array());
}
abstract public function testLinter();
/**
* Executes all tests from the specified subdirectory. If a linter is not
* explicitly specified, it will be inferred from the name of the test class.
*/
public function executeTestsInDirectory(
$root,
ArcanistLinter $linter = null) {
if (!$linter) {
$linter = $this->getLinter();
}
$files = id(new FileFinder($root))
->withType('f')
->withSuffix('lint-test')
->find();
$test_count = 0;
foreach ($files as $file) {
$this->lintFile($root.$file, $linter);
$test_count++;
}
$this->assertTrue(
($test_count > 0),
pht(
'Expected to find some %s tests in directory %s!',
'.lint-test',
$root));
}
private function lintFile($file, ArcanistLinter $linter) {
$linter = clone $linter;
$contents = Filesystem::readFile($file);
$contents = preg_split('/^~{4,}\n/m', $contents);
if (count($contents) < 2) {
throw new Exception(
pht(
"Expected '%s' separating test case and results.",
'~~~~~~~~~~'));
}
- list ($data, $expect, $xform, $config) = array_merge(
+ list($data, $expect, $xform, $config) = array_merge(
$contents,
array(null, null));
$basename = basename($file);
if ($config) {
$config = phutil_json_decode($config);
} else {
$config = array();
}
PhutilTypeSpec::checkMap(
$config,
array(
'config' => 'optional map<string, wild>',
'path' => 'optional string',
'mode' => 'optional string',
'stopped' => 'optional bool',
));
$exception = null;
$after_lint = null;
$messages = null;
$exception_message = false;
$caught_exception = false;
try {
$tmp = new TempFile($basename);
Filesystem::writeFile($tmp, $data);
$full_path = (string)$tmp;
$mode = idx($config, 'mode');
if ($mode) {
Filesystem::changePermissions($tmp, octdec($mode));
}
$dir = dirname($full_path);
$path = basename($full_path);
$working_copy = ArcanistWorkingCopyIdentity::newFromRootAndConfigFile(
$dir,
null,
pht('Unit Test'));
$configuration_manager = new ArcanistConfigurationManager();
$configuration_manager->setWorkingCopyIdentity($working_copy);
$engine = new ArcanistUnitTestableLintEngine();
$engine->setWorkingCopy($working_copy);
$engine->setConfigurationManager($configuration_manager);
$path_name = idx($config, 'path', $path);
$engine->setPaths(array($path_name));
$linter->addPath($path_name);
$linter->addData($path_name, $data);
foreach (idx($config, 'config', array()) as $key => $value) {
$linter->setLinterConfigurationValue($key, $value);
}
$engine->addLinter($linter);
$engine->addFileData($path_name, $data);
$results = $engine->run();
$this->assertEqual(
1,
count($results),
pht('Expect one result returned by linter.'));
$assert_stopped = idx($config, 'stopped');
if ($assert_stopped !== null) {
$this->assertEqual(
$assert_stopped,
$linter->didStopAllLinters(),
$assert_stopped
? pht('Expect linter to be stopped.')
: pht('Expect linter to not be stopped.'));
}
$result = reset($results);
$patcher = ArcanistLintPatcher::newFromArcanistLintResult($result);
$after_lint = $patcher->getModifiedFileContent();
} catch (PhutilTestTerminatedException $ex) {
throw $ex;
} catch (Exception $exception) {
$caught_exception = true;
if ($exception instanceof PhutilAggregateException) {
$caught_exception = false;
foreach ($exception->getExceptions() as $ex) {
if ($ex instanceof ArcanistUsageException ||
$ex instanceof ArcanistMissingLinterException) {
$this->assertSkipped($ex->getMessage());
} else {
$caught_exception = true;
}
}
} else if ($exception instanceof ArcanistUsageException ||
$exception instanceof ArcanistMissingLinterException) {
$this->assertSkipped($exception->getMessage());
}
$exception_message = $exception->getMessage()."\n\n".
$exception->getTraceAsString();
}
$this->assertEqual(false, $caught_exception, $exception_message);
$this->compareLint($basename, $expect, $result);
$this->compareTransform($xform, $after_lint);
}
private function compareLint($file, $expect, ArcanistLintResult $result) {
$seen = array();
$raised = array();
$message_map = array();
foreach ($result->getMessages() as $message) {
$sev = $message->getSeverity();
$line = $message->getLine();
$char = $message->getChar();
$code = $message->getCode();
$name = $message->getName();
$message_key = $sev.':'.$line.':'.$char;
$message_map[$message_key] = $message;
$seen[] = $message_key;
$raised[] = sprintf(
' %s: %s %s',
pht('%s at line %d, char %d', $sev, $line, $char),
$code,
$name);
}
$expect = trim($expect);
if ($expect) {
$expect = explode("\n", $expect);
} else {
$expect = array();
}
foreach ($expect as $key => $expected) {
$expect[$key] = head(explode(' ', $expected));
}
$expect = array_fill_keys($expect, true);
$seen = array_fill_keys($seen, true);
if (!$raised) {
$raised = array(pht('No messages.'));
}
$raised = sprintf(
"%s:\n%s",
pht('Actually raised'),
implode("\n", $raised));
foreach (array_diff_key($expect, $seen) as $missing => $ignored) {
$missing = explode(':', $missing);
$sev = array_shift($missing);
$pos = $missing;
$this->assertFailure(
pht(
"In '%s', expected lint to raise %s on line %d at char %d, ".
"but no %s was raised. %s",
$file,
$sev,
idx($pos, 0),
idx($pos, 1),
$sev,
$raised));
}
foreach (array_diff_key($seen, $expect) as $surprising => $ignored) {
$message = $message_map[$surprising];
$message_info = $message->getDescription();
list($sev, $line, $char) = explode(':', $surprising);
$this->assertFailure(
sprintf(
"%s:\n\n%s\n\n%s",
pht(
"In '%s', lint raised %s on line %d at char %d, ".
"but nothing was expected",
$file,
$sev,
$line,
$char),
$message_info,
$raised));
}
}
private function compareTransform($expected, $actual) {
if (!strlen($expected)) {
return;
}
$this->assertEqual(
$expected,
$actual,
pht('File as patched by lint did not match the expected patched file.'));
}
}
diff --git a/src/parser/ArcanistBundle.php b/src/parser/ArcanistBundle.php
index a4d85581..8586e281 100644
--- a/src/parser/ArcanistBundle.php
+++ b/src/parser/ArcanistBundle.php
@@ -1,860 +1,935 @@
<?php
/**
* Converts changesets between different formats.
*/
final class ArcanistBundle extends Phobject {
private $changes;
private $conduit;
private $blobs = array();
private $diskPath;
private $baseRevision;
private $revisionID;
private $encoding;
private $loadFileDataCallback;
private $authorName;
private $authorEmail;
public function setAuthorEmail($author_email) {
$this->authorEmail = $author_email;
return $this;
}
public function getAuthorEmail() {
return $this->authorEmail;
}
public function setAuthorName($author_name) {
$this->authorName = $author_name;
return $this;
}
public function getAuthorName() {
return $this->authorName;
}
public function getFullAuthor() {
$author_name = $this->getAuthorName();
if ($author_name === null) {
return null;
}
$author_email = $this->getAuthorEmail();
if ($author_email === null) {
return null;
}
$full_author = sprintf('%s <%s>', $author_name, $author_email);
// Because git is very picky about the author being in a valid format,
// verify that we can parse it.
$address = new PhutilEmailAddress($full_author);
if (!$address->getDisplayName() || !$address->getAddress()) {
return null;
}
return $full_author;
}
public function setConduit(ConduitClient $conduit) {
$this->conduit = $conduit;
return $this;
}
public function setBaseRevision($base_revision) {
$this->baseRevision = $base_revision;
return $this;
}
public function setEncoding($encoding) {
$this->encoding = $encoding;
return $this;
}
public function getEncoding() {
return $this->encoding;
}
public function getBaseRevision() {
return $this->baseRevision;
}
public function setRevisionID($revision_id) {
$this->revisionID = $revision_id;
return $this;
}
public function getRevisionID() {
return $this->revisionID;
}
public static function newFromChanges(array $changes) {
$obj = new ArcanistBundle();
$obj->changes = $changes;
return $obj;
}
private function getEOL($patch_type) {
// NOTE: Git always generates "\n" line endings, even under Windows, and
// can not parse certain patches with "\r\n" line endings. SVN generates
// patches with "\n" line endings on Mac or Linux and "\r\n" line endings
// on Windows. (This EOL style is used only for patch metadata lines, not
// for the actual patch content.)
// (On Windows, Mercurial generates \n newlines for `--git` diffs, as it
// must, but also \n newlines for unified diffs. We never need to deal with
// these as we use Git format for Mercurial, so this case is currently
// ignored.)
switch ($patch_type) {
case 'git':
return "\n";
case 'unified':
return phutil_is_windows() ? "\r\n" : "\n";
default:
throw new Exception(
pht("Unknown patch type '%s'!", $patch_type));
}
}
public static function newFromArcBundle($path) {
$path = Filesystem::resolvePath($path);
$future = new ExecFuture(
'tar tfO %s',
$path);
list($stdout, $file_list) = $future->resolvex();
$file_list = explode("\n", trim($file_list));
if (in_array('meta.json', $file_list)) {
$future = new ExecFuture(
'tar xfO %s meta.json',
$path);
$meta_info = $future->resolveJSON();
$version = idx($meta_info, 'version', 0);
$base_revision = idx($meta_info, 'baseRevision');
$revision_id = idx($meta_info, 'revisionID');
$encoding = idx($meta_info, 'encoding');
$author_name = idx($meta_info, 'authorName');
$author_email = idx($meta_info, 'authorEmail');
} else {
// this arc bundle was probably made before we started storing meta info
$version = 0;
$base_revision = null;
$revision_id = null;
$encoding = null;
$author = null;
}
$future = new ExecFuture(
'tar xfO %s changes.json',
$path);
$changes = $future->resolveJSON();
foreach ($changes as $change_key => $change) {
foreach ($change['hunks'] as $key => $hunk) {
list($hunk_data) = execx('tar xfO %s hunks/%s', $path, $hunk['corpus']);
$changes[$change_key]['hunks'][$key]['corpus'] = $hunk_data;
}
}
foreach ($changes as $change_key => $change) {
$changes[$change_key] = ArcanistDiffChange::newFromDictionary($change);
}
$obj = new ArcanistBundle();
$obj->changes = $changes;
$obj->diskPath = $path;
$obj->setBaseRevision($base_revision);
$obj->setRevisionID($revision_id);
$obj->setEncoding($encoding);
return $obj;
}
public static function newFromDiff($data) {
$obj = new ArcanistBundle();
$parser = new ArcanistDiffParser();
$obj->changes = $parser->parseDiff($data);
return $obj;
}
private function __construct() {}
public function writeToDisk($path) {
$changes = $this->getChanges();
$change_list = array();
foreach ($changes as $change) {
$change_list[] = $change->toDictionary();
}
$hunks = array();
foreach ($change_list as $change_key => $change) {
foreach ($change['hunks'] as $key => $hunk) {
$hunks[] = $hunk['corpus'];
$change_list[$change_key]['hunks'][$key]['corpus'] = count($hunks) - 1;
}
}
$blobs = array();
foreach ($change_list as $change) {
if (!empty($change['metadata']['old:binary-phid'])) {
$blobs[$change['metadata']['old:binary-phid']] = null;
}
if (!empty($change['metadata']['new:binary-phid'])) {
$blobs[$change['metadata']['new:binary-phid']] = null;
}
}
foreach ($blobs as $phid => $null) {
$blobs[$phid] = $this->getBlob($phid);
}
$meta_info = array(
'version' => 5,
'baseRevision' => $this->getBaseRevision(),
'revisionID' => $this->getRevisionID(),
'encoding' => $this->getEncoding(),
'authorName' => $this->getAuthorName(),
'authorEmail' => $this->getAuthorEmail(),
);
$dir = Filesystem::createTemporaryDirectory();
Filesystem::createDirectory($dir.'/hunks');
Filesystem::createDirectory($dir.'/blobs');
Filesystem::writeFile($dir.'/changes.json', json_encode($change_list));
Filesystem::writeFile($dir.'/meta.json', json_encode($meta_info));
foreach ($hunks as $key => $hunk) {
Filesystem::writeFile($dir.'/hunks/'.$key, $hunk);
}
foreach ($blobs as $key => $blob) {
Filesystem::writeFile($dir.'/blobs/'.$key, $blob);
}
execx(
'(cd %s; tar -czf %s *)',
$dir,
Filesystem::resolvePath($path));
Filesystem::remove($dir);
}
public function toUnifiedDiff() {
$eol = $this->getEOL('unified');
$result = array();
$changes = $this->getChanges();
foreach ($changes as $change) {
$hunk_changes = $this->buildHunkChanges($change->getHunks(), $eol);
if (!$hunk_changes) {
continue;
}
$old_path = $this->getOldPath($change);
$cur_path = $this->getCurrentPath($change);
$index_path = $cur_path;
if ($index_path === null) {
$index_path = $old_path;
}
$result[] = 'Index: '.$index_path;
$result[] = $eol;
$result[] = str_repeat('=', 67);
$result[] = $eol;
if ($old_path === null) {
$old_path = '/dev/null';
}
if ($cur_path === null) {
$cur_path = '/dev/null';
}
// When the diff is used by `patch`, `patch` ignores what is listed as the
// current path and just makes changes to the file at the old path (unless
// the current path is '/dev/null'.
// If the old path and the current path aren't the same (and neither is
// /dev/null), this indicates the file was moved or copied. By listing
// both paths as the new file, `patch` will apply the diff to the new
// file.
if ($cur_path !== '/dev/null' && $old_path !== '/dev/null') {
$old_path = $cur_path;
}
$result[] = '--- '.$old_path.$eol;
$result[] = '+++ '.$cur_path.$eol;
$result[] = $hunk_changes;
}
if (!$result) {
return '';
}
$diff = implode('', $result);
return $this->convertNonUTF8Diff($diff);
}
public function toGitPatch() {
$eol = $this->getEOL('git');
$result = array();
$changes = $this->getChanges();
$binary_sources = array();
foreach ($changes as $change) {
if (!$this->isGitBinaryChange($change)) {
continue;
}
$type = $change->getType();
if ($type == ArcanistDiffChangeType::TYPE_MOVE_AWAY ||
$type == ArcanistDiffChangeType::TYPE_COPY_AWAY ||
$type == ArcanistDiffChangeType::TYPE_MULTICOPY) {
foreach ($change->getAwayPaths() as $path) {
$binary_sources[$path] = $change;
}
}
}
foreach (array_keys($changes) as $multicopy_key) {
$multicopy_change = $changes[$multicopy_key];
$type = $multicopy_change->getType();
if ($type != ArcanistDiffChangeType::TYPE_MULTICOPY) {
continue;
}
// Decompose MULTICOPY into one MOVE_HERE and several COPY_HERE because
// we need more information than we have in order to build a delete patch
// and represent it as a bunch of COPY_HERE plus a delete. For details,
// see T419.
// Basically, MULTICOPY means there are 2 or more corresponding COPY_HERE
// changes, so find one of them arbitrarily and turn it into a MOVE_HERE.
// TODO: We might be able to do this more cleanly after T230 is resolved.
$decompose_okay = false;
foreach ($changes as $change_key => $change) {
if ($change->getType() != ArcanistDiffChangeType::TYPE_COPY_HERE) {
continue;
}
if ($change->getOldPath() != $multicopy_change->getCurrentPath()) {
continue;
}
$decompose_okay = true;
$change = clone $change;
$change->setType(ArcanistDiffChangeType::TYPE_MOVE_HERE);
$changes[$change_key] = $change;
// The multicopy is now fully represented by MOVE_HERE plus one or more
// COPY_HERE, so throw it away.
unset($changes[$multicopy_key]);
break;
}
if (!$decompose_okay) {
throw new Exception(
pht(
'Failed to decompose multicopy changeset in '.
'order to generate diff.'));
}
}
foreach ($changes as $change) {
$type = $change->getType();
$file_type = $change->getFileType();
if ($file_type == ArcanistDiffChangeType::FILE_DIRECTORY) {
// TODO: We should raise a FYI about this, so the user is aware
// that we omitted it, if the directory is empty or has permissions
// which git can't represent.
// Git doesn't support empty directories, so we simply ignore them. If
// the directory is nonempty, 'git apply' will create it when processing
// the changesets for files inside it.
continue;
}
if ($type == ArcanistDiffChangeType::TYPE_MOVE_AWAY) {
// Git will apply this in the corresponding MOVE_HERE.
continue;
}
$old_mode = idx($change->getOldProperties(), 'unix:filemode', '100644');
$new_mode = idx($change->getNewProperties(), 'unix:filemode', '100644');
$is_binary = $this->isGitBinaryChange($change);
if ($is_binary) {
$old_binary = idx($binary_sources, $this->getCurrentPath($change));
$change_body = $this->buildBinaryChange($change, $old_binary);
} else {
$change_body = $this->buildHunkChanges($change->getHunks(), $eol);
}
if ($type == ArcanistDiffChangeType::TYPE_COPY_AWAY) {
// TODO: This is only relevant when patching old Differential diffs
// which were created prior to arc pruning TYPE_COPY_AWAY for files
// with no modifications.
if (!strlen($change_body) && ($old_mode == $new_mode)) {
continue;
}
}
$old_path = $this->getOldPath($change);
$cur_path = $this->getCurrentPath($change);
if ($old_path === null) {
$old_index = 'a/'.$cur_path;
$old_target = '/dev/null';
} else {
$old_index = 'a/'.$old_path;
$old_target = 'a/'.$old_path;
}
if ($cur_path === null) {
$cur_index = 'b/'.$old_path;
$cur_target = '/dev/null';
} else {
$cur_index = 'b/'.$cur_path;
$cur_target = 'b/'.$cur_path;
}
$result[] = "diff --git {$old_index} {$cur_index}".$eol;
if ($type == ArcanistDiffChangeType::TYPE_ADD) {
$result[] = "new file mode {$new_mode}".$eol;
}
if ($type == ArcanistDiffChangeType::TYPE_COPY_HERE ||
$type == ArcanistDiffChangeType::TYPE_MOVE_HERE ||
$type == ArcanistDiffChangeType::TYPE_COPY_AWAY ||
$type == ArcanistDiffChangeType::TYPE_CHANGE) {
if ($old_mode !== $new_mode) {
$result[] = "old mode {$old_mode}".$eol;
$result[] = "new mode {$new_mode}".$eol;
}
}
if ($type == ArcanistDiffChangeType::TYPE_COPY_HERE) {
$result[] = "copy from {$old_path}".$eol;
$result[] = "copy to {$cur_path}".$eol;
} else if ($type == ArcanistDiffChangeType::TYPE_MOVE_HERE) {
$result[] = "rename from {$old_path}".$eol;
$result[] = "rename to {$cur_path}".$eol;
} else if ($type == ArcanistDiffChangeType::TYPE_DELETE ||
$type == ArcanistDiffChangeType::TYPE_MULTICOPY) {
$old_mode = idx($change->getOldProperties(), 'unix:filemode');
if ($old_mode) {
$result[] = "deleted file mode {$old_mode}".$eol;
}
}
if ($change_body) {
if (!$is_binary) {
$result[] = "--- {$old_target}".$eol;
$result[] = "+++ {$cur_target}".$eol;
}
$result[] = $change_body;
}
}
$diff = implode('', $result).$eol;
return $this->convertNonUTF8Diff($diff);
}
private function isGitBinaryChange(ArcanistDiffChange $change) {
$file_type = $change->getFileType();
return ($file_type == ArcanistDiffChangeType::FILE_BINARY ||
$file_type == ArcanistDiffChangeType::FILE_IMAGE);
}
private function convertNonUTF8Diff($diff) {
if ($this->encoding) {
$diff = phutil_utf8_convert($diff, $this->encoding, 'UTF-8');
}
return $diff;
}
public function getChanges() {
return $this->changes;
}
private function breakHunkIntoSmallHunks(ArcanistDiffHunk $base_hunk) {
$context = 3;
$results = array();
$lines = phutil_split_lines($base_hunk->getCorpus());
$n = count($lines);
$old_offset = $base_hunk->getOldOffset();
$new_offset = $base_hunk->getNewOffset();
$ii = 0;
$jj = 0;
while ($ii < $n) {
// Skip lines until we find the next line with changes. Note: this skips
// both ' ' (no changes) and '\' (no newline at end of file) lines. If we
// don't skip the latter, we may incorrectly generate a terminal hunk
// that has no actual change information when a file doesn't have a
// terminal newline and not changed near the end of the file. 'patch' will
// fail to apply the diff if we generate a hunk that does not actually
// contain changes.
for ($jj = $ii; $jj < $n; ++$jj) {
$char = $lines[$jj][0];
if ($char == '-' || $char == '+') {
break;
}
}
if ($jj >= $n) {
break;
}
$hunk_start = max($jj - $context, 0);
// NOTE: There are two tricky considerations here.
// We can not generate a patch with overlapping hunks, or 'git apply'
// rejects it after 1.7.3.4.
// We can not generate a patch with too much trailing context, or
// 'patch' rejects it.
// So we need to ensure that we generate disjoint hunks, but don't
// generate any hunks with too much context.
$old_lines = 0;
$new_lines = 0;
$hunk_adjust = 0;
$last_change = $jj;
$break_here = null;
for (; $jj < $n; ++$jj) {
if ($lines[$jj][0] == ' ') {
if ($jj - $last_change > $context) {
if ($break_here === null) {
// We haven't seen a change in $context lines, so this is a
// potential place to break the hunk. However, we need to keep
// looking in case there is another change fewer than $context
// lines away, in which case we have to merge the hunks.
$break_here = $jj;
}
}
if ($jj - $last_change > (($context + 1) * 2)) {
// We definitely aren't going to merge this with the next hunk, so
// break out of the loop. We'll end the hunk at $break_here.
break;
}
} else {
$break_here = null;
$last_change = $jj;
if ($lines[$jj][0] == '\\') {
// When we have a "\ No newline at end of file" line, it does not
// contribute to either hunk length.
++$hunk_adjust;
} else if ($lines[$jj][0] == '-') {
++$old_lines;
} else if ($lines[$jj][0] == '+') {
++$new_lines;
}
}
}
if ($break_here !== null) {
$jj = $break_here;
}
$hunk_length = min($jj, $n) - $hunk_start;
$count_length = ($hunk_length - $hunk_adjust);
$hunk = new ArcanistDiffHunk();
$hunk->setOldOffset($old_offset + $hunk_start - $ii);
$hunk->setNewOffset($new_offset + $hunk_start - $ii);
$hunk->setOldLength($count_length - $new_lines);
$hunk->setNewLength($count_length - $old_lines);
$corpus = array_slice($lines, $hunk_start, $hunk_length);
$corpus = implode('', $corpus);
$hunk->setCorpus($corpus);
$results[] = $hunk;
$old_offset += ($jj - $ii) - $new_lines;
$new_offset += ($jj - $ii) - $old_lines;
$ii = $jj;
}
return $results;
}
private function getOldPath(ArcanistDiffChange $change) {
$old_path = $change->getOldPath();
$type = $change->getType();
if (!strlen($old_path) ||
$type == ArcanistDiffChangeType::TYPE_ADD) {
$old_path = null;
}
return $old_path;
}
private function getCurrentPath(ArcanistDiffChange $change) {
$cur_path = $change->getCurrentPath();
$type = $change->getType();
if (!strlen($cur_path) ||
$type == ArcanistDiffChangeType::TYPE_DELETE ||
$type == ArcanistDiffChangeType::TYPE_MULTICOPY) {
$cur_path = null;
}
return $cur_path;
}
private function buildHunkChanges(array $hunks, $eol) {
assert_instances_of($hunks, 'ArcanistDiffHunk');
$result = array();
foreach ($hunks as $hunk) {
$small_hunks = $this->breakHunkIntoSmallHunks($hunk);
foreach ($small_hunks as $small_hunk) {
$o_off = $small_hunk->getOldOffset();
$o_len = $small_hunk->getOldLength();
$n_off = $small_hunk->getNewOffset();
$n_len = $small_hunk->getNewLength();
$corpus = $small_hunk->getCorpus();
// NOTE: If the length is 1 it can be omitted. Since git does this,
// we also do it so that "arc export --git" diffs are as similar to
// real git diffs as possible, which helps debug issues.
if ($o_len == 1) {
$o_head = "{$o_off}";
} else {
$o_head = "{$o_off},{$o_len}";
}
if ($n_len == 1) {
$n_head = "{$n_off}";
} else {
$n_head = "{$n_off},{$n_len}";
}
$result[] = "@@ -{$o_head} +{$n_head} @@".$eol;
$result[] = $corpus;
$last = substr($corpus, -1);
if ($last !== false && $last != "\r" && $last != "\n") {
$result[] = $eol;
}
}
}
return implode('', $result);
}
public function setLoadFileDataCallback($callback) {
$this->loadFileDataCallback = $callback;
return $this;
}
private function getBlob($phid, $name = null) {
if ($this->loadFileDataCallback) {
return call_user_func($this->loadFileDataCallback, $phid);
}
if ($this->diskPath) {
list($blob_data) = execx('tar xfO %s blobs/%s', $this->diskPath, $phid);
return $blob_data;
}
$console = PhutilConsole::getConsole();
if ($this->conduit) {
if ($name) {
$console->writeErr(
"%s\n",
pht("Downloading binary data for '%s'...", $name));
} else {
$console->writeErr("%s\n", pht('Downloading binary data...'));
}
$data_base64 = $this->conduit->callMethodSynchronous(
'file.download',
array(
'phid' => $phid,
));
return base64_decode($data_base64);
}
throw new Exception(pht("Nowhere to load blob '%s' from!", $phid));
}
private function buildBinaryChange(ArcanistDiffChange $change, $old_binary) {
$eol = $this->getEOL('git');
// In Git, when we write out a binary file move or copy, we need the
// original binary for the source and the current binary for the
// destination.
if ($old_binary) {
if ($old_binary->getOriginalFileData() !== null) {
$old_data = $old_binary->getOriginalFileData();
$old_phid = null;
} else {
$old_data = null;
$old_phid = $old_binary->getMetadata('old:binary-phid');
}
} else {
$old_data = $change->getOriginalFileData();
$old_phid = $change->getMetadata('old:binary-phid');
}
if ($old_data === null && $old_phid) {
$name = basename($change->getOldPath());
$old_data = $this->getBlob($old_phid, $name);
}
$old_length = strlen($old_data);
if ($old_data === null) {
$old_data = '';
$old_sha1 = str_repeat('0', 40);
} else {
$old_sha1 = sha1("blob {$old_length}\0{$old_data}");
}
$new_phid = $change->getMetadata('new:binary-phid');
$new_data = null;
if ($change->getCurrentFileData() !== null) {
$new_data = $change->getCurrentFileData();
} else if ($new_phid) {
$name = basename($change->getCurrentPath());
$new_data = $this->getBlob($new_phid, $name);
}
$new_length = strlen($new_data);
if ($new_data === null) {
$new_data = '';
$new_sha1 = str_repeat('0', 40);
} else {
$new_sha1 = sha1("blob {$new_length}\0{$new_data}");
}
$content = array();
$content[] = "index {$old_sha1}..{$new_sha1}".$eol;
$content[] = 'GIT binary patch'.$eol;
$content[] = "literal {$new_length}".$eol;
$content[] = $this->emitBinaryDiffBody($new_data).$eol;
$content[] = "literal {$old_length}".$eol;
$content[] = $this->emitBinaryDiffBody($old_data).$eol;
return implode('', $content);
}
private function emitBinaryDiffBody($data) {
$eol = $this->getEOL('git');
if (!function_exists('gzcompress')) {
throw new Exception(
pht(
'This patch has binary data. The PHP zlib extension is required to '.
'apply patches with binary data to git. Install the PHP zlib '.
'extension to continue.'));
}
// See emit_binary_diff_body() in diff.c for git's implementation.
$buf = '';
$deflated = gzcompress($data);
$lines = str_split($deflated, 52);
foreach ($lines as $line) {
$len = strlen($line);
// The first character encodes the line length.
if ($len <= 26) {
$buf .= chr($len + ord('A') - 1);
} else {
$buf .= chr($len - 26 + ord('a') - 1);
}
$buf .= self::encodeBase85($line);
$buf .= $eol;
}
return $buf;
}
public static function encodeBase85($data) {
// This is implemented awkwardly in order to closely mirror git's
// implementation in base85.c
// It is also implemented awkwardly to work correctly on 32-bit machines.
// Broadly, this algorithm converts the binary input to printable output
// by transforming each 4 binary bytes of input to 5 printable bytes of
// output, one piece at a time.
//
// To do this, we convert the 4 bytes into a 32-bit integer, then use
// modulus and division by 85 to pick out printable bytes (85^5 is slightly
// larger than 2^32). In C, this algorithm is fairly easy to implement
// because the accumulator can be made unsigned.
//
// In PHP, there are no unsigned integers, so values larger than 2^31 break
// on 32-bit systems under modulus:
//
// $ php -r 'print (1 << 31) % 13;' # On a 32-bit machine.
// -11
//
// However, PHP's float type is an IEEE 754 64-bit double precision float,
// so we can safely store integers up to around 2^53 without loss of
// precision. To work around the lack of an unsigned type, we just use a
// double and perform the modulus with fmod().
//
// (Since PHP overflows integer operations into floats, we don't need much
// additional casting.)
static $map = array(
- '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
- 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
- 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
- 'U', 'V', 'W', 'X', 'Y', 'Z',
- 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
- 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
- 'u', 'v', 'w', 'x', 'y', 'z',
- '!', '#', '$', '%', '&', '(', ')', '*', '+', '-',
- ';', '<', '=', '>', '?', '@', '^', '_', '`', '{',
- '|', '}', '~',
+ '0',
+ '1',
+ '2',
+ '3',
+ '4',
+ '5',
+ '6',
+ '7',
+ '8',
+ '9',
+ 'A',
+ 'B',
+ 'C',
+ 'D',
+ 'E',
+ 'F',
+ 'G',
+ 'H',
+ 'I',
+ 'J',
+ 'K',
+ 'L',
+ 'M',
+ 'N',
+ 'O',
+ 'P',
+ 'Q',
+ 'R',
+ 'S',
+ 'T',
+ 'U',
+ 'V',
+ 'W',
+ 'X',
+ 'Y',
+ 'Z',
+ 'a',
+ 'b',
+ 'c',
+ 'd',
+ 'e',
+ 'f',
+ 'g',
+ 'h',
+ 'i',
+ 'j',
+ 'k',
+ 'l',
+ 'm',
+ 'n',
+ 'o',
+ 'p',
+ 'q',
+ 'r',
+ 's',
+ 't',
+ 'u',
+ 'v',
+ 'w',
+ 'x',
+ 'y',
+ 'z',
+ '!',
+ '#',
+ '$',
+ '%',
+ '&',
+ '(',
+ ')',
+ '*',
+ '+',
+ '-',
+ ';',
+ '<',
+ '=',
+ '>',
+ '?',
+ '@',
+ '^',
+ '_',
+ '`',
+ '{',
+ '|',
+ '}',
+ '~',
);
$buf = '';
$pos = 0;
$bytes = strlen($data);
while ($bytes) {
$accum = 0;
for ($count = 24; $count >= 0; $count -= 8) {
$val = ord($data[$pos++]);
$val = $val * (1 << $count);
$accum = $accum + $val;
if (--$bytes == 0) {
break;
}
}
$slice = '';
for ($count = 4; $count >= 0; $count--) {
$val = (int)fmod($accum, 85.0);
$accum = floor($accum / 85.0);
$slice .= $map[$val];
}
$buf .= strrev($slice);
}
return $buf;
}
}
diff --git a/src/repository/api/ArcanistMercurialAPI.php b/src/repository/api/ArcanistMercurialAPI.php
index 4abcea04..33105c20 100644
--- a/src/repository/api/ArcanistMercurialAPI.php
+++ b/src/repository/api/ArcanistMercurialAPI.php
@@ -1,1128 +1,1128 @@
<?php
/**
* Interfaces with the Mercurial working copies.
*/
final class ArcanistMercurialAPI extends ArcanistRepositoryAPI {
private $branch;
private $localCommitInfo;
private $rawDiffCache = array();
private $supportsRebase;
private $supportsPhases;
protected function buildLocalFuture(array $argv) {
// Mercurial has a "defaults" feature which basically breaks automation by
// allowing the user to add random flags to any command. This feature is
// "deprecated" and "a bad idea" that you should "forget ... existed"
// according to project lead Matt Mackall:
//
// http://markmail.org/message/hl3d6eprubmkkqh5
//
// There is an HGPLAIN environmental variable which enables "plain mode"
// and hopefully disables this stuff.
if (phutil_is_windows()) {
$argv[0] = 'set HGPLAIN=1 & hg '.$argv[0];
} else {
$argv[0] = 'HGPLAIN=1 hg '.$argv[0];
}
$future = newv('ExecFuture', $argv);
$future->setCWD($this->getPath());
return $future;
}
public function execPassthru($pattern /* , ... */) {
$args = func_get_args();
if (phutil_is_windows()) {
$args[0] = 'hg '.$args[0];
} else {
$args[0] = 'HGPLAIN=1 hg '.$args[0];
}
return call_user_func_array('phutil_passthru', $args);
}
public function getSourceControlSystemName() {
return 'hg';
}
public function getMetadataPath() {
return $this->getPath('.hg');
}
public function getSourceControlBaseRevision() {
return $this->getCanonicalRevisionName($this->getBaseCommit());
}
public function getCanonicalRevisionName($string) {
$match = null;
if ($this->isHgSubversionRepo() &&
preg_match('/@([0-9]+)$/', $string, $match)) {
$string = hgsprintf('svnrev(%s)', $match[1]);
}
list($stdout) = $this->execxLocal(
'log -l 1 --template %s -r %s --',
'{node}',
$string);
return $stdout;
}
public function getHashFromFromSVNRevisionNumber($revision_id) {
$matches = array();
$string = hgsprintf('svnrev(%s)', $revision_id);
list($stdout) = $this->execxLocal(
'log -l 1 --template %s -r %s --',
'{node}',
$string);
if (!$stdout) {
throw new ArcanistUsageException(
pht('Cannot find the HG equivalent of %s given.', $revision_id));
}
return $stdout;
}
public function getSVNRevisionNumberFromHash($hash) {
$matches = array();
list($stdout) = $this->execxLocal(
'log -r %s --template {svnrev}', $hash);
if (!$stdout) {
throw new ArcanistUsageException(
pht('Cannot find the SVN equivalent of %s given.', $hash));
}
return $stdout;
}
public function getSourceControlPath() {
return '/';
}
public function getBranchName() {
if (!$this->branch) {
list($stdout) = $this->execxLocal('branch');
$this->branch = trim($stdout);
}
return $this->branch;
}
protected function didReloadCommitRange() {
$this->localCommitInfo = null;
}
protected function buildBaseCommit($symbolic_commit) {
if ($symbolic_commit !== null) {
try {
$commit = $this->getCanonicalRevisionName(
hgsprintf('ancestor(%s,.)', $symbolic_commit));
} catch (Exception $ex) {
// Try it as a revset instead of a commit id
try {
$commit = $this->getCanonicalRevisionName(
hgsprintf('ancestor(%R,.)', $symbolic_commit));
} catch (Exception $ex) {
throw new ArcanistUsageException(
pht(
"Commit '%s' is not a valid Mercurial commit identifier.",
$symbolic_commit));
}
}
$this->setBaseCommitExplanation(
pht(
'it is the greatest common ancestor of the working directory '.
'and the commit you specified explicitly.'));
return $commit;
}
if ($this->getBaseCommitArgumentRules() ||
$this->getConfigurationManager()->getConfigFromAnySource('base')) {
$base = $this->resolveBaseCommit();
if (!$base) {
throw new ArcanistUsageException(
pht(
"None of the rules in your 'base' configuration matched a valid ".
"commit. Adjust rules or specify which commit you want to use ".
"explicitly."));
}
return $base;
}
// Mercurial 2.1 and up have phases which indicate if something is
// published or not. To find which revs are outgoing, it's much
// faster to check the phase instead of actually checking the server.
if ($this->supportsPhases()) {
list($err, $stdout) = $this->execManualLocal(
'log --branch %s -r %s --style default',
$this->getBranchName(),
'draft()');
} else {
list($err, $stdout) = $this->execManualLocal(
'outgoing --branch %s --style default',
$this->getBranchName());
}
if (!$err) {
$logs = ArcanistMercurialParser::parseMercurialLog($stdout);
} else {
// Mercurial (in some versions?) raises an error when there's nothing
// outgoing.
$logs = array();
}
if (!$logs) {
$this->setBaseCommitExplanation(
pht(
'you have no outgoing commits, so arc assumes you intend to submit '.
'uncommitted changes in the working copy.'));
return $this->getWorkingCopyRevision();
}
$outgoing_revs = ipull($logs, 'rev');
// This is essentially an implementation of a theoretical `hg merge-base`
// command.
$against = $this->getWorkingCopyRevision();
while (true) {
// NOTE: The "^" and "~" syntaxes were only added in hg 1.9, which is
// new as of July 2011, so do this in a compatible way. Also, "hg log"
// and "hg outgoing" don't necessarily show parents (even if given an
// explicit template consisting of just the parents token) so we need
// to separately execute "hg parents".
list($stdout) = $this->execxLocal(
'parents --style default --rev %s',
$against);
$parents_logs = ArcanistMercurialParser::parseMercurialLog($stdout);
list($p1, $p2) = array_merge($parents_logs, array(null, null));
if ($p1 && !in_array($p1['rev'], $outgoing_revs)) {
$against = $p1['rev'];
break;
} else if ($p2 && !in_array($p2['rev'], $outgoing_revs)) {
$against = $p2['rev'];
break;
} else if ($p1) {
$against = $p1['rev'];
} else {
// This is the case where you have a new repository and the entire
// thing is outgoing; Mercurial literally accepts "--rev null" as
// meaning "diff against the empty state".
$against = 'null';
break;
}
}
if ($against == 'null') {
$this->setBaseCommitExplanation(
pht('this is a new repository (all changes are outgoing).'));
} else {
$this->setBaseCommitExplanation(
pht(
'it is the first commit reachable from the working copy state '.
'which is not outgoing.'));
}
return $against;
}
public function getLocalCommitInformation() {
if ($this->localCommitInfo === null) {
$base_commit = $this->getBaseCommit();
list($info) = $this->execxLocal(
'log --template %s --rev %s --branch %s --',
"{node}\1{rev}\1{author}\1".
"{date|rfc822date}\1{branch}\1{tag}\1{parents}\1{desc}\2",
hgsprintf('(%s::. - %s)', $base_commit, $base_commit),
$this->getBranchName());
$logs = array_filter(explode("\2", $info));
$last_node = null;
$futures = array();
$commits = array();
foreach ($logs as $log) {
list($node, $rev, $full_author, $date, $branch, $tag,
$parents, $desc) = explode("\1", $log, 9);
- list ($author, $author_email) = $this->parseFullAuthor($full_author);
+ list($author, $author_email) = $this->parseFullAuthor($full_author);
// NOTE: If a commit has only one parent, {parents} returns empty.
// If it has two parents, {parents} returns revs and short hashes, not
// full hashes. Try to avoid making calls to "hg parents" because it's
// relatively expensive.
$commit_parents = null;
if (!$parents) {
if ($last_node) {
$commit_parents = array($last_node);
}
}
if (!$commit_parents) {
// We didn't get a cheap hit on previous commit, so do the full-cost
// "hg parents" call. We can run these in parallel, at least.
$futures[$node] = $this->execFutureLocal(
'parents --template %s --rev %s',
'{node}\n',
$node);
}
$commits[$node] = array(
'author' => $author,
'time' => strtotime($date),
'branch' => $branch,
'tag' => $tag,
'commit' => $node,
'rev' => $node, // TODO: Remove eventually.
'local' => $rev,
'parents' => $commit_parents,
'summary' => head(explode("\n", $desc)),
'message' => $desc,
'authorEmail' => $author_email,
);
$last_node = $node;
}
$futures = id(new FutureIterator($futures))
->limit(4);
foreach ($futures as $node => $future) {
list($parents) = $future->resolvex();
$parents = array_filter(explode("\n", $parents));
$commits[$node]['parents'] = $parents;
}
// Put commits in newest-first order, to be consistent with Git and the
// expected order of "hg log" and "git log" under normal circumstances.
// The order of ancestors() is oldest-first.
$commits = array_reverse($commits);
$this->localCommitInfo = $commits;
}
return $this->localCommitInfo;
}
public function getAllFiles() {
// TODO: Handle paths with newlines.
$future = $this->buildLocalFuture(array('manifest'));
return new LinesOfALargeExecFuture($future);
}
public function getChangedFiles($since_commit) {
list($stdout) = $this->execxLocal(
'status --rev %s',
$since_commit);
return ArcanistMercurialParser::parseMercurialStatus($stdout);
}
public function getBlame($path) {
list($stdout) = $this->execxLocal(
'annotate -u -v -c --rev %s -- %s',
$this->getBaseCommit(),
$path);
$lines = phutil_split_lines($stdout, $retain_line_endings = true);
$blame = array();
foreach ($lines as $line) {
if (!strlen($line)) {
continue;
}
$matches = null;
$ok = preg_match('/^\s*([^:]+?) ([a-f0-9]{12}):/', $line, $matches);
if (!$ok) {
throw new Exception(
pht(
'Unable to parse Mercurial blame line: %s',
$line));
}
$revision = $matches[2];
$author = trim($matches[1]);
$blame[] = array($author, $revision);
}
return $blame;
}
protected function buildUncommittedStatus() {
list($stdout) = $this->execxLocal('status');
$results = new PhutilArrayWithDefaultValue();
$working_status = ArcanistMercurialParser::parseMercurialStatus($stdout);
foreach ($working_status as $path => $mask) {
if (!($mask & ArcanistRepositoryAPI::FLAG_UNTRACKED)) {
// Mark tracked files as uncommitted.
$mask |= self::FLAG_UNCOMMITTED;
}
$results[$path] |= $mask;
}
return $results->toArray();
}
protected function buildCommitRangeStatus() {
// TODO: Possibly we should use "hg status --rev X --rev ." for this
// instead, but we must run "hg diff" later anyway in most cases, so
// building and caching it shouldn't hurt us.
$diff = $this->getFullMercurialDiff();
if (!$diff) {
return array();
}
$parser = new ArcanistDiffParser();
$changes = $parser->parseDiff($diff);
$status_map = array();
foreach ($changes as $change) {
$flags = 0;
switch ($change->getType()) {
case ArcanistDiffChangeType::TYPE_ADD:
case ArcanistDiffChangeType::TYPE_MOVE_HERE:
case ArcanistDiffChangeType::TYPE_COPY_HERE:
$flags |= self::FLAG_ADDED;
break;
case ArcanistDiffChangeType::TYPE_CHANGE:
case ArcanistDiffChangeType::TYPE_COPY_AWAY: // Check for changes?
$flags |= self::FLAG_MODIFIED;
break;
case ArcanistDiffChangeType::TYPE_DELETE:
case ArcanistDiffChangeType::TYPE_MOVE_AWAY:
case ArcanistDiffChangeType::TYPE_MULTICOPY:
$flags |= self::FLAG_DELETED;
break;
}
$status_map[$change->getCurrentPath()] = $flags;
}
return $status_map;
}
protected function didReloadWorkingCopy() {
// Diffs are against ".", so we need to drop the cache if we change the
// working copy.
$this->rawDiffCache = array();
$this->branch = null;
}
private function getDiffOptions() {
$options = array(
'--git',
'-U'.$this->getDiffLinesOfContext(),
);
return implode(' ', $options);
}
public function getRawDiffText($path) {
$options = $this->getDiffOptions();
$range = $this->getBaseCommit();
$raw_diff_cache_key = $options.' '.$range.' '.$path;
if (idx($this->rawDiffCache, $raw_diff_cache_key)) {
return idx($this->rawDiffCache, $raw_diff_cache_key);
}
list($stdout) = $this->execxLocal(
'diff %C --rev %s -- %s',
$options,
$range,
$path);
$this->rawDiffCache[$raw_diff_cache_key] = $stdout;
return $stdout;
}
public function getFullMercurialDiff() {
return $this->getRawDiffText('');
}
public function getOriginalFileData($path) {
return $this->getFileDataAtRevision($path, $this->getBaseCommit());
}
public function getCurrentFileData($path) {
return $this->getFileDataAtRevision(
$path,
$this->getWorkingCopyRevision());
}
public function getBulkOriginalFileData($paths) {
return $this->getBulkFileDataAtRevision($paths, $this->getBaseCommit());
}
public function getBulkCurrentFileData($paths) {
return $this->getBulkFileDataAtRevision(
$paths,
$this->getWorkingCopyRevision());
}
private function getBulkFileDataAtRevision($paths, $revision) {
// Calling 'hg cat' on each file individually is slow (1 second per file
// on a large repo) because mercurial has to decompress and parse the
// entire manifest every time. Do it in one large batch instead.
// hg cat will write the file data to files in a temp directory
$tmpdir = Filesystem::createTemporaryDirectory();
// Mercurial doesn't create the directories for us :(
foreach ($paths as $path) {
$tmppath = $tmpdir.'/'.$path;
Filesystem::createDirectory(dirname($tmppath), 0755, true);
}
list($err, $stdout) = $this->execManualLocal(
'cat --rev %s --output %s -- %C',
$revision,
// %p is the formatter for the repo-relative filepath
$tmpdir.'/%p',
implode(' ', $paths));
$filedata = array();
foreach ($paths as $path) {
$tmppath = $tmpdir.'/'.$path;
if (Filesystem::pathExists($tmppath)) {
$filedata[$path] = Filesystem::readFile($tmppath);
}
}
Filesystem::remove($tmpdir);
return $filedata;
}
private function getFileDataAtRevision($path, $revision) {
list($err, $stdout) = $this->execManualLocal(
'cat --rev %s -- %s',
$revision,
$path);
if ($err) {
// Assume this is "no file at revision", i.e. a deleted or added file.
return null;
} else {
return $stdout;
}
}
public function getWorkingCopyRevision() {
return '.';
}
public function isHistoryDefaultImmutable() {
return true;
}
public function supportsAmend() {
list($err, $stdout) = $this->execManualLocal('help commit');
if ($err) {
return false;
} else {
return (strpos($stdout, 'amend') !== false);
}
}
public function supportsRebase() {
if ($this->supportsRebase === null) {
- list ($err) = $this->execManualLocal('help rebase');
+ list($err) = $this->execManualLocal('help rebase');
$this->supportsRebase = $err === 0;
}
return $this->supportsRebase;
}
public function supportsPhases() {
if ($this->supportsPhases === null) {
- list ($err) = $this->execManualLocal('help phase');
+ list($err) = $this->execManualLocal('help phase');
$this->supportsPhases = $err === 0;
}
return $this->supportsPhases;
}
public function supportsCommitRanges() {
return true;
}
public function supportsLocalCommits() {
return true;
}
public function getAllBranches() {
list($branch_info) = $this->execxLocal('bookmarks');
if (trim($branch_info) == 'no bookmarks set') {
return array();
}
$matches = null;
preg_match_all(
'/^\s*(\*?)\s*(.+)\s(\S+)$/m',
$branch_info,
$matches,
PREG_SET_ORDER);
$return = array();
foreach ($matches as $match) {
list(, $current, $name) = $match;
$return[] = array(
'current' => (bool)$current,
'name' => rtrim($name),
);
}
return $return;
}
public function hasLocalCommit($commit) {
try {
$this->getCanonicalRevisionName($commit);
return true;
} catch (Exception $ex) {
return false;
}
}
public function getCommitMessage($commit) {
list($message) = $this->execxLocal(
'log --template={desc} --rev %s',
$commit);
return $message;
}
public function getAllLocalChanges() {
$diff = $this->getFullMercurialDiff();
if (!strlen(trim($diff))) {
return array();
}
$parser = new ArcanistDiffParser();
return $parser->parseDiff($diff);
}
public function supportsLocalBranchMerge() {
return true;
}
public function performLocalBranchMerge($branch, $message) {
if ($branch) {
$err = phutil_passthru(
'(cd %s && HGPLAIN=1 hg merge --rev %s && hg commit -m %s)',
$this->getPath(),
$branch,
$message);
} else {
$err = phutil_passthru(
'(cd %s && HGPLAIN=1 hg merge && hg commit -m %s)',
$this->getPath(),
$message);
}
if ($err) {
throw new ArcanistUsageException(pht('Merge failed!'));
}
}
public function getFinalizedRevisionMessage() {
return pht(
"You may now push this commit upstream, as appropriate (e.g. with ".
"'%s' or by printing and faxing it).",
'hg push');
}
public function getCommitMessageLog() {
$base_commit = $this->getBaseCommit();
list($stdout) = $this->execxLocal(
'log --template %s --rev %s --branch %s --',
"{node}\1{desc}\2",
hgsprintf('(%s::. - %s)', $base_commit, $base_commit),
$this->getBranchName());
$map = array();
$logs = explode("\2", trim($stdout));
foreach (array_filter($logs) as $log) {
list($node, $desc) = explode("\1", $log);
$map[$node] = $desc;
}
return array_reverse($map);
}
public function loadWorkingCopyDifferentialRevisions(
ConduitClient $conduit,
array $query) {
$messages = $this->getCommitMessageLog();
$parser = new ArcanistDiffParser();
// First, try to find revisions by explicit revision IDs in commit messages.
$reason_map = array();
$revision_ids = array();
foreach ($messages as $node_id => $message) {
$object = ArcanistDifferentialCommitMessage::newFromRawCorpus($message);
if ($object->getRevisionID()) {
$revision_ids[] = $object->getRevisionID();
$reason_map[$object->getRevisionID()] = $node_id;
}
}
if ($revision_ids) {
$results = $conduit->callMethodSynchronous(
'differential.query',
$query + array(
'ids' => $revision_ids,
));
foreach ($results as $key => $result) {
$hash = substr($reason_map[$result['id']], 0, 16);
$results[$key]['why'] =
pht(
"Commit message for '%s' has explicit 'Differential Revision'.",
$hash);
}
return $results;
}
// Try to find revisions by hash.
$hashes = array();
foreach ($this->getLocalCommitInformation() as $commit) {
$hashes[] = array('hgcm', $commit['commit']);
}
if ($hashes) {
// NOTE: In the case of "arc diff . --uncommitted" in a Mercurial working
// copy with dirty changes, there may be no local commits.
$results = $conduit->callMethodSynchronous(
'differential.query',
$query + array(
'commitHashes' => $hashes,
));
foreach ($results as $key => $hash) {
$results[$key]['why'] = pht(
'A mercurial commit hash in the commit range is already attached '.
'to the Differential revision.');
}
return $results;
}
return array();
}
public function updateWorkingCopy() {
$this->execxLocal('up');
$this->reloadWorkingCopy();
}
private function getMercurialConfig($key, $default = null) {
list($stdout) = $this->execxLocal('showconfig %s', $key);
if ($stdout == '') {
return $default;
}
return rtrim($stdout);
}
public function getAuthor() {
$full_author = $this->getMercurialConfig('ui.username');
list($author, $author_email) = $this->parseFullAuthor($full_author);
return $author;
}
/**
* Parse the Mercurial author field.
*
* Not everyone enters their email address as a part of the username
* field. Try to make it work when it's obvious.
*
* @param string $full_author
* @return array
*/
protected function parseFullAuthor($full_author) {
if (strpos($full_author, '@') === false) {
$author = $full_author;
$author_email = null;
} else {
$email = new PhutilEmailAddress($full_author);
$author = $email->getDisplayName();
$author_email = $email->getAddress();
}
return array($author, $author_email);
}
public function addToCommit(array $paths) {
$this->execxLocal(
'addremove -- %Ls',
$paths);
$this->reloadWorkingCopy();
}
public function doCommit($message) {
$tmp_file = new TempFile();
Filesystem::writeFile($tmp_file, $message);
$this->execxLocal('commit -l %s', $tmp_file);
$this->reloadWorkingCopy();
}
public function amendCommit($message = null) {
if ($message === null) {
$message = $this->getCommitMessage('.');
}
$tmp_file = new TempFile();
Filesystem::writeFile($tmp_file, $message);
try {
$this->execxLocal(
'commit --amend -l %s',
$tmp_file);
} catch (CommandException $ex) {
if (preg_match('/nothing changed/', $ex->getStdOut())) {
// NOTE: Mercurial considers it an error to make a no-op amend. Although
// we generally defer to the underlying VCS to dictate behavior, this
// one seems a little goofy, and we use amend as part of various
// workflows under the assumption that no-op amends are fine. If this
// amend failed because it's a no-op, just continue.
} else {
throw $ex;
}
}
$this->reloadWorkingCopy();
}
public function getCommitSummary($commit) {
if ($commit == 'null') {
return pht('(The Empty Void)');
}
list($summary) = $this->execxLocal(
'log --template {desc} --limit 1 --rev %s',
$commit);
$summary = head(explode("\n", $summary));
return trim($summary);
}
public function backoutCommit($commit_hash) {
$this->execxLocal('backout -r %s', $commit_hash);
$this->reloadWorkingCopy();
if (!$this->getUncommittedStatus()) {
throw new ArcanistUsageException(
pht('%s has already been reverted.', $commit_hash));
}
}
public function getBackoutMessage($commit_hash) {
return pht('Backed out changeset %s,', $commit_hash);
}
public function resolveBaseCommitRule($rule, $source) {
list($type, $name) = explode(':', $rule, 2);
// NOTE: This function MUST return node hashes or symbolic commits (like
// branch names or the word "tip"), not revsets. This includes ".^" and
// similar, which a revset, not a symbolic commit identifier. If you return
// a revset it will be escaped later and looked up literally.
switch ($type) {
case 'hg':
$matches = null;
if (preg_match('/^gca\((.+)\)$/', $name, $matches)) {
list($err, $merge_base) = $this->execManualLocal(
'log --template={node} --rev %s',
sprintf('ancestor(., %s)', $matches[1]));
if (!$err) {
$this->setBaseCommitExplanation(
pht(
"it is the greatest common ancestor of '%s' and %s, as ".
"specified by '%s' in your %s 'base' configuration.",
$matches[1],
'.',
$rule,
$source));
return trim($merge_base);
}
} else {
list($err, $commit) = $this->execManualLocal(
'log --template {node} --rev %s',
hgsprintf('%s', $name));
if ($err) {
list($err, $commit) = $this->execManualLocal(
'log --template {node} --rev %s',
$name);
}
if (!$err) {
$this->setBaseCommitExplanation(
pht(
"it is specified by '%s' in your %s 'base' configuration.",
$rule,
$source));
return trim($commit);
}
}
break;
case 'arc':
switch ($name) {
case 'empty':
$this->setBaseCommitExplanation(
pht(
"you specified '%s' in your %s 'base' configuration.",
$rule,
$source));
return 'null';
case 'outgoing':
list($err, $outgoing_base) = $this->execManualLocal(
'log --template={node} --rev %s',
'limit(reverse(ancestors(.) - outgoing()), 1)');
if (!$err) {
$this->setBaseCommitExplanation(
pht(
"it is the first ancestor of the working copy that is not ".
"outgoing, and it matched the rule %s in your %s ".
"'base' configuration.",
$rule,
$source));
return trim($outgoing_base);
}
case 'amended':
$text = $this->getCommitMessage('.');
$message = ArcanistDifferentialCommitMessage::newFromRawCorpus(
$text);
if ($message->getRevisionID()) {
$this->setBaseCommitExplanation(
pht(
"'%s' has been amended with 'Differential Revision:', ".
"as specified by '%s' in your %s 'base' configuration.",
'.'.
$rule,
$source));
// NOTE: This should be safe because Mercurial doesn't support
// amend until 2.2.
return $this->getCanonicalRevisionName('.^');
}
break;
case 'bookmark':
$revset =
'limit('.
' sort('.
' (ancestors(.) and bookmark() - .) or'.
' (ancestors(.) - outgoing()), '.
' -rev),'.
'1)';
list($err, $bookmark_base) = $this->execManualLocal(
'log --template={node} --rev %s',
$revset);
if (!$err) {
$this->setBaseCommitExplanation(
pht(
"it is the first ancestor of %s that either has a bookmark, ".
"or is already in the remote and it matched the rule %s in ".
"your %s 'base' configuration",
'.',
$rule,
$source));
return trim($bookmark_base);
}
break;
case 'this':
$this->setBaseCommitExplanation(
pht(
"you specified '%s' in your %s 'base' configuration.",
$rule,
$source));
return $this->getCanonicalRevisionName('.^');
default:
if (preg_match('/^nodiff\((.+)\)$/', $name, $matches)) {
list($results) = $this->execxLocal(
'log --template %s --rev %s',
"{node}\1{desc}\2",
sprintf('ancestor(.,%s)::.^', $matches[1]));
$results = array_reverse(explode("\2", trim($results)));
foreach ($results as $result) {
if (empty($result)) {
continue;
}
list($node, $desc) = explode("\1", $result, 2);
$message = ArcanistDifferentialCommitMessage::newFromRawCorpus(
$desc);
if ($message->getRevisionID()) {
$this->setBaseCommitExplanation(
pht(
"it is the first ancestor of %s that has a diff and is ".
"the gca or a descendant of the gca with '%s', ".
"specified by '%s' in your %s 'base' configuration.",
'.',
$matches[1],
$rule,
$source));
return $node;
}
}
}
break;
}
break;
default:
return null;
}
return null;
}
public function isHgSubversionRepo() {
return file_exists($this->getPath('.hg/svn/rev_map'));
}
public function getSubversionInfo() {
$info = array();
$base_path = null;
$revision = null;
list($err, $raw_info) = $this->execManualLocal('svn info');
if (!$err) {
foreach (explode("\n", trim($raw_info)) as $line) {
list($key, $value) = explode(': ', $line, 2);
switch ($key) {
case 'URL':
$info['base_path'] = $value;
$base_path = $value;
break;
case 'Repository UUID':
$info['uuid'] = $value;
break;
case 'Revision':
$revision = $value;
break;
default:
break;
}
}
if ($base_path && $revision) {
$info['base_revision'] = $base_path.'@'.$revision;
}
}
return $info;
}
public function getActiveBookmark() {
$bookmarks = $this->getBookmarks();
foreach ($bookmarks as $bookmark) {
if ($bookmark['is_active']) {
return $bookmark['name'];
}
}
return null;
}
public function isBookmark($name) {
$bookmarks = $this->getBookmarks();
foreach ($bookmarks as $bookmark) {
if ($bookmark['name'] === $name) {
return true;
}
}
return false;
}
public function isBranch($name) {
$branches = $this->getBranches();
foreach ($branches as $branch) {
if ($branch['name'] === $name) {
return true;
}
}
return false;
}
public function getBranches() {
list($stdout) = $this->execxLocal('--debug branches');
$lines = ArcanistMercurialParser::parseMercurialBranches($stdout);
$branches = array();
foreach ($lines as $name => $spec) {
$branches[] = array(
'name' => $name,
'revision' => $spec['rev'],
);
}
return $branches;
}
public function getBookmarks() {
$bookmarks = array();
list($raw_output) = $this->execxLocal('bookmarks');
$raw_output = trim($raw_output);
if ($raw_output !== 'no bookmarks set') {
foreach (explode("\n", $raw_output) as $line) {
// example line: * mybook 2:6b274d49be97
list($name, $revision) = $this->splitBranchOrBookmarkLine($line);
$is_active = false;
if ('*' === $name[0]) {
$is_active = true;
$name = substr($name, 2);
}
$bookmarks[] = array(
'is_active' => $is_active,
'name' => $name,
'revision' => $revision,
);
}
}
return $bookmarks;
}
private function splitBranchOrBookmarkLine($line) {
// branches and bookmarks are printed in the format:
// default 0:a5ead76cdf85 (inactive)
// * mybook 2:6b274d49be97
// this code divides the name half from the revision half
// it does not parse the * and (inactive) bits
$colon_index = strrpos($line, ':');
$before_colon = substr($line, 0, $colon_index);
$start_rev_index = strrpos($before_colon, ' ');
$name = substr($line, 0, $start_rev_index);
$rev = substr($line, $start_rev_index);
return array(trim($name), trim($rev));
}
public function getRemoteURI() {
list($stdout) = $this->execxLocal('paths default');
$stdout = trim($stdout);
if (strlen($stdout)) {
return $stdout;
}
return null;
}
}

File Metadata

Mime Type
text/x-diff
Expires
Sun, Jan 19, 20:23 (1 w, 5 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1128416
Default Alt Text
(69 KB)

Event Timeline